Spaces:
Sleeping
Sleeping
Upload llm_config.py
Browse files- llm_config.py +32 -0
llm_config.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# llm_config.py
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Get the API key from the environment variable
|
11 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
12 |
+
|
13 |
+
# Check if the API key is set
|
14 |
+
if not OPENAI_API_KEY:
|
15 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
16 |
+
|
17 |
+
# Initialize the OpenAI client
|
18 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
19 |
+
|
20 |
+
def generate_llm_response(prompt):
|
21 |
+
try:
|
22 |
+
response = client.chat.completions.create(
|
23 |
+
model="gpt-4o-mini",
|
24 |
+
messages=[{"role": "user", "content": prompt}],
|
25 |
+
temperature=0.01
|
26 |
+
)
|
27 |
+
content = response.choices[0].message.content.strip()
|
28 |
+
#print(f"LLM Response: {content}") # For debugging
|
29 |
+
return content
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error generating LLM response: {str(e)}")
|
32 |
+
return None
|