Spaces:
Sleeping
Sleeping
Update openai_client.py
Browse files- openai_client.py +15 -13
openai_client.py
CHANGED
|
@@ -1,24 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
| 4 |
-
# 🔐
|
| 5 |
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 6 |
|
| 7 |
-
#
|
| 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 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
response = model.generate_content(prompt)
|
| 24 |
return response.text.strip()
|
|
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
| 4 |
+
# 🔐 Authenticate
|
| 5 |
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 6 |
|
| 7 |
+
# ✅ Recommended way to use Gemini
|
| 8 |
+
model = genai.GenerativeModel(model_name="models/gemini-pro")
|
| 9 |
|
| 10 |
def ask_gpt(messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
prompt_parts = []
|
| 13 |
+
|
| 14 |
+
for m in messages:
|
| 15 |
+
role = m["role"]
|
| 16 |
+
content = m["content"]
|
| 17 |
+
|
| 18 |
+
if role == "user":
|
| 19 |
+
prompt_parts.append(f"User: {content}")
|
| 20 |
+
elif role == "assistant":
|
| 21 |
+
prompt_parts.append(f"Assistant: {content}")
|
| 22 |
+
|
| 23 |
+
prompt = "\n".join(prompt_parts)
|
| 24 |
|
| 25 |
response = model.generate_content(prompt)
|
| 26 |
return response.text.strip()
|