Spaces:
Sleeping
Sleeping
Update openai_client.py
Browse files- openai_client.py +22 -8
openai_client.py
CHANGED
|
@@ -1,12 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def ask_gpt(messages):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
|
| 4 |
+
# 🔐 Load API key
|
| 5 |
+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 6 |
+
|
| 7 |
+
# 🌟 Use Gemini Pro
|
| 8 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 9 |
|
| 10 |
def ask_gpt(messages):
|
| 11 |
+
"""
|
| 12 |
+
messages: [
|
| 13 |
+
{"role": "user", "content": "hello"},
|
| 14 |
+
{"role": "assistant", "content": "hi there!"}
|
| 15 |
+
]
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
formatted = [
|
| 19 |
+
m["content"] for m in messages if m["role"] == "user"
|
| 20 |
+
]
|
| 21 |
+
prompt = "\n\n".join(formatted)
|
| 22 |
+
|
| 23 |
+
response = model.generate_content(prompt)
|
| 24 |
+
return response.text.strip()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"⚠️ Gemini Error: {str(e)}"
|