pvanand commited on
Commit
483491a
·
verified ·
1 Parent(s): 94b68df

Update helpers/ai_client.py

Browse files
Files changed (1) hide show
  1. helpers/ai_client.py +22 -6
helpers/ai_client.py CHANGED
@@ -2,6 +2,11 @@
2
  import requests
3
  import os
4
  from typing import Optional, Dict, Any
 
 
 
 
 
5
 
6
  class AIClient:
7
  def __init__(self):
@@ -33,7 +38,7 @@ class AIClient:
33
  Dict[str, Any]: The JSON response from the LLM API.
34
 
35
  Raises:
36
- HTTPException: If the API request fails.
37
  """
38
  if api_key is None:
39
  api_key = self.api_key
@@ -52,9 +57,20 @@ class AIClient:
52
  "Content-Type": "application/json"
53
  }
54
 
55
- # Use requests to call the external API
56
- response = requests.post(self.llm_api_url, json=payload, headers=headers)
57
- if response.status_code != 200:
58
- raise Exception(f"Error from LLM API: {response.status_code} - {response.text}")
 
 
 
 
 
59
 
60
- return response.json()
 
 
 
 
 
 
 
2
  import requests
3
  import os
4
  from typing import Optional, Dict, Any
5
+ import logging
6
+
7
+ # Set up logging
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
10
 
11
  class AIClient:
12
  def __init__(self):
 
38
  Dict[str, Any]: The JSON response from the LLM API.
39
 
40
  Raises:
41
+ Exception: If the API request fails or the response is invalid.
42
  """
43
  if api_key is None:
44
  api_key = self.api_key
 
57
  "Content-Type": "application/json"
58
  }
59
 
60
+ try:
61
+ # Use requests to call the external API
62
+ response = requests.post(self.llm_api_url, json=payload, headers=headers)
63
+ logger.info(f"API Response Status Code: {response.status_code}")
64
+ logger.info(f"API Response Content: {response.text}")
65
+
66
+ # Check if the response is valid JSON
67
+ if response.status_code != 200:
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