Spaces:
Sleeping
Sleeping
Update helpers/ai_client.py
Browse files- helpers/ai_client.py +10 -20
helpers/ai_client.py
CHANGED
@@ -22,9 +22,9 @@ class AIClient:
|
|
22 |
conversation_id: str = "string",
|
23 |
user_id: str = "string",
|
24 |
api_key: Optional[str] = None
|
25 |
-
) ->
|
26 |
"""
|
27 |
-
Sends a prompt to the LLM API and returns the response.
|
28 |
|
29 |
Args:
|
30 |
prompt (str): The user's input prompt.
|
@@ -35,10 +35,10 @@ class AIClient:
|
|
35 |
api_key (str): API key for authentication.
|
36 |
|
37 |
Returns:
|
38 |
-
|
39 |
|
40 |
Raises:
|
41 |
-
Exception: If the API request fails
|
42 |
"""
|
43 |
if api_key is None:
|
44 |
api_key = self.api_key
|
@@ -57,20 +57,10 @@ class AIClient:
|
|
57 |
"Content-Type": "application/json"
|
58 |
}
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
logger.info(f"API Response Content: {response.text}")
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
raise Exception(f"Error from LLM API: {response.status_code} - {response.text}")
|
69 |
-
|
70 |
-
return response.json()
|
71 |
-
except requests.exceptions.JSONDecodeError as e:
|
72 |
-
logger.error(f"Failed to decode JSON response: {e}")
|
73 |
-
raise Exception(f"Invalid response from LLM API: {response.text}")
|
74 |
-
except Exception as e:
|
75 |
-
logger.error(f"Error in send_prompt: {e}")
|
76 |
-
raise
|
|
|
22 |
conversation_id: str = "string",
|
23 |
user_id: str = "string",
|
24 |
api_key: Optional[str] = None
|
25 |
+
) -> str:
|
26 |
"""
|
27 |
+
Sends a prompt to the LLM API and returns the response as text.
|
28 |
|
29 |
Args:
|
30 |
prompt (str): The user's input prompt.
|
|
|
35 |
api_key (str): API key for authentication.
|
36 |
|
37 |
Returns:
|
38 |
+
str: The text response from the LLM API.
|
39 |
|
40 |
Raises:
|
41 |
+
Exception: If the API request fails.
|
42 |
"""
|
43 |
if api_key is None:
|
44 |
api_key = self.api_key
|
|
|
57 |
"Content-Type": "application/json"
|
58 |
}
|
59 |
|
60 |
+
# Use requests to call the external API
|
61 |
+
response = requests.post(self.llm_api_url, json=payload, headers=headers)
|
62 |
+
if response.status_code != 200:
|
63 |
+
raise Exception(f"Error from LLM API: {response.status_code} - {response.text}")
|
|
|
64 |
|
65 |
+
# Return the response as text
|
66 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|