Spaces:
Sleeping
Sleeping
File size: 2,566 Bytes
367cc0b 483491a 367cc0b f3b81df 367cc0b 483491a 367cc0b 483491a 367cc0b 483491a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# helpers/ai_client.py
import requests
import os
from typing import Optional, Dict, Any
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class AIClient:
def __init__(self):
# Load environment variables
self.llm_api_url = os.getenv("LLM_API_URL")
self.api_key = os.getenv("X_API_KEY")
def send_prompt(
self,
prompt: str,
system_message: str = "",
model_id: str = "openai/gpt-4o-mini",
conversation_id: str = "string",
user_id: str = "string",
api_key: Optional[str] = None
) -> Dict[str, Any]:
"""
Sends a prompt to the LLM API and returns the response.
Args:
prompt (str): The user's input prompt.
system_message (str): Optional system message for the LLM.
model_id (str): The model ID to use (default: "openai/gpt-4o-mini").
conversation_id (str): Unique ID for the conversation.
user_id (str): Unique ID for the user.
api_key (str): API key for authentication.
Returns:
Dict[str, Any]: The JSON response from the LLM API.
Raises:
Exception: If the API request fails or the response is invalid.
"""
if api_key is None:
api_key = self.api_key
payload = {
"prompt": prompt,
"system_message": system_message,
"model_id": model_id,
"conversation_id": conversation_id,
"user_id": user_id
}
headers = {
"accept": "application/json",
"X-API-Key": api_key,
"Content-Type": "application/json"
}
try:
# Use requests to call the external API
response = requests.post(self.llm_api_url, json=payload, headers=headers)
logger.info(f"API Response Status Code: {response.status_code}")
logger.info(f"API Response Content: {response.text}")
# Check if the response is valid JSON
if response.status_code != 200:
raise Exception(f"Error from LLM API: {response.status_code} - {response.text}")
return response.json()
except requests.exceptions.JSONDecodeError as e:
logger.error(f"Failed to decode JSON response: {e}")
raise Exception(f"Invalid response from LLM API: {response.text}")
except Exception as e:
logger.error(f"Error in send_prompt: {e}")
raise |