Spaces:
Sleeping
Sleeping
Update helpers/ai_client.py
Browse files- helpers/ai_client.py +1 -28
helpers/ai_client.py
CHANGED
@@ -10,17 +10,8 @@ logger = logging.getLogger(__name__)
|
|
10 |
|
11 |
class AIClient:
|
12 |
def __init__(self):
|
13 |
-
# Load environment variables
|
14 |
-
self.base_url = os.getenv("LLM_API_URL", "https://openrouter.ai/api/v1")
|
15 |
-
self.api_key = os.getenv("OPENROUTER_API_KEY")
|
16 |
-
self.site_url = os.getenv("SITE_URL", "") # Your site URL
|
17 |
-
self.site_name = os.getenv("SITE_NAME", "") # Your site name
|
18 |
# Initialize OpenAI client
|
19 |
-
self.client = OpenAI(
|
20 |
-
base_url=self.base_url,
|
21 |
-
api_key=self.api_key
|
22 |
-
)
|
23 |
-
|
24 |
def chat(
|
25 |
self,
|
26 |
prompt: str,
|
@@ -29,20 +20,6 @@ class AIClient:
|
|
29 |
conversation_id: str = "",
|
30 |
user_id: str = "string"
|
31 |
) -> str:
|
32 |
-
"""
|
33 |
-
Sends a prompt to the OpenRouter API and returns the response as text.
|
34 |
-
Args:
|
35 |
-
prompt (str): The user's input prompt.
|
36 |
-
system_message (str): Optional system message for the LLM.
|
37 |
-
model_id (str): The model ID to use (default: "openai/gpt-4o-mini").
|
38 |
-
conversation_id (str): Unique ID for the conversation.
|
39 |
-
user_id (str): Unique ID for the user.
|
40 |
-
api_key (str): API key for authentication.
|
41 |
-
Returns:
|
42 |
-
str: The text response from the LLM API.
|
43 |
-
Raises:
|
44 |
-
Exception: If the API request fails.
|
45 |
-
"""
|
46 |
try:
|
47 |
messages = []
|
48 |
|
@@ -55,10 +32,6 @@ class AIClient:
|
|
55 |
|
56 |
# Create completion
|
57 |
completion = self.client.chat.completions.create(
|
58 |
-
extra_headers={
|
59 |
-
"HTTP-Referer": self.site_url, # Optional
|
60 |
-
"X-Title": self.site_name, # Optional
|
61 |
-
},
|
62 |
model=model_id,
|
63 |
messages=messages
|
64 |
)
|
|
|
10 |
|
11 |
class AIClient:
|
12 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
13 |
# Initialize OpenAI client
|
14 |
+
self.client = OpenAI(api_key=os.getenv("OPENROUTER_API_KEY"), base_url="https://openrouter.ai/api/v1")
|
|
|
|
|
|
|
|
|
15 |
def chat(
|
16 |
self,
|
17 |
prompt: str,
|
|
|
20 |
conversation_id: str = "",
|
21 |
user_id: str = "string"
|
22 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
try:
|
24 |
messages = []
|
25 |
|
|
|
32 |
|
33 |
# Create completion
|
34 |
completion = self.client.chat.completions.create(
|
|
|
|
|
|
|
|
|
35 |
model=model_id,
|
36 |
messages=messages
|
37 |
)
|