-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(filename_suggestion_ai/main.py): streamline POST request log…
…ic using APIClient class feat(filename_suggestion_ai/utils): add APIClient class to handle API interactions refactor(filename_suggestion_ai/utils/__init__.py): update imports to include api_client module
- Loading branch information
Showing
3 changed files
with
55 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .api_client import * | ||
from .file_util import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
|
||
import requests | ||
|
||
from filename_suggestion_ai.config import ( | ||
MODEL, | ||
SYSTEM_CONTENT, | ||
) | ||
from filename_suggestion_ai.models import LMStudioChatResponse | ||
|
||
|
||
class APIClient: | ||
def __init__(self, url: str, headers: dict) -> None: | ||
self.url = url | ||
self.headers = headers | ||
|
||
def send_post_request(self, payload): | ||
try: | ||
response = requests.post(self.url, json=payload, headers=self.headers, timeout=60) | ||
response.raise_for_status() | ||
return LMStudioChatResponse(**response.json()) | ||
except requests.exceptions.HTTPError: | ||
logging.exception("HTTP error occurred") | ||
except requests.exceptions.RequestException: | ||
logging.exception("Error during requests to %s", self.url) | ||
except Exception: | ||
logging.exception("An unexpected error occurred") | ||
raise | ||
return None | ||
|
||
def create_payload(self, user_content: str) -> dict: | ||
"""Create the payload for the POST request using the user content. | ||
Args: | ||
user_content (str): The content provided by the user. | ||
Returns: | ||
dict: The payload dictionary configured for the request. | ||
""" | ||
return { | ||
"model": MODEL, | ||
"messages": [ | ||
{"role": "system", "content": SYSTEM_CONTENT}, | ||
{"role": "user", "content": user_content}, | ||
], | ||
"temperature": 0.8, | ||
"max_tokens": -1, | ||
"seed": -1, | ||
"stream": False, | ||
} |