File size: 2,022 Bytes
367cc0b
 
 
 
483491a
 
 
 
 
367cc0b
 
 
 
f3b81df
 
367cc0b
0e97127
367cc0b
 
 
 
 
 
 
a6361a2
367cc0b
a6361a2
367cc0b
 
 
 
 
 
 
 
 
 
a6361a2
367cc0b
 
a6361a2
367cc0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6361a2
 
 
 
483491a
a6361a2
 
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
# 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 chat(
        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
    ) -> str:
        """
        Sends a prompt to the LLM API and returns the response as text.

        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:
            str: The text response from the LLM API.

        Raises:
            Exception: If the API request fails.
        """
        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"
        }

        # Use requests to call the external API
        response = requests.post(self.llm_api_url, json=payload, headers=headers)
        if response.status_code != 200:
            raise Exception(f"Error from LLM API: {response.status_code} - {response.text}")

        # Return the response as text
        return response.text