Update models.py
Browse files
models.py
CHANGED
|
@@ -1,48 +1,56 @@
|
|
| 1 |
-
import openai
|
| 2 |
-
import google.generativeai as genai
|
| 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 |
-
return "Error
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
|
| 4 |
+
from config import (
|
| 5 |
+
OPENAI_API_KEY,
|
| 6 |
+
GEMINI_API_KEY,
|
| 7 |
+
OPENAI_DEFAULT_MODEL,
|
| 8 |
+
GEMINI_DEFAULT_MODEL
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def configure_llms():
|
| 13 |
+
"""
|
| 14 |
+
Configure OpenAI and Gemini if keys are provided.
|
| 15 |
+
"""
|
| 16 |
+
if OPENAI_API_KEY:
|
| 17 |
+
openai.api_key = OPENAI_API_KEY
|
| 18 |
+
if GEMINI_API_KEY:
|
| 19 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def openai_chat(system_prompt, user_prompt, model=None, temperature=0.3):
|
| 23 |
+
"""
|
| 24 |
+
Call OpenAI ChatCompletion with a system + user message.
|
| 25 |
+
"""
|
| 26 |
+
if not OPENAI_API_KEY:
|
| 27 |
+
return "Error: OpenAI API key not provided."
|
| 28 |
+
chat_model = model or OPENAI_DEFAULT_MODEL
|
| 29 |
+
try:
|
| 30 |
+
response = openai.ChatCompletion.create(
|
| 31 |
+
model=chat_model,
|
| 32 |
+
messages=[
|
| 33 |
+
{"role": "system", "content": system_prompt},
|
| 34 |
+
{"role": "user", "content": user_prompt}
|
| 35 |
+
],
|
| 36 |
+
temperature=temperature
|
| 37 |
+
)
|
| 38 |
+
return response.choices[0].message["content"].strip()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error calling OpenAI: {str(e)}"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def gemini_chat(system_prompt, user_prompt, model_name=None, temperature=0.3):
|
| 44 |
+
"""
|
| 45 |
+
Call Google's PaLM2/Gemini via google.generativeai.
|
| 46 |
+
"""
|
| 47 |
+
if not GEMINI_API_KEY:
|
| 48 |
+
return "Error: Gemini API key not provided."
|
| 49 |
+
final_model = model_name or GEMINI_DEFAULT_MODEL
|
| 50 |
+
try:
|
| 51 |
+
model = genai.GenerativeModel(model_name=final_model)
|
| 52 |
+
chat_session = model.start_chat(history=[("system", system_prompt)])
|
| 53 |
+
reply = chat_session.send_message(user_prompt, temperature=temperature)
|
| 54 |
+
return reply.text
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error calling Gemini: {str(e)}"
|