Commit ·
53ca86f
1
Parent(s): 745c606
Update chat to use google-genai SDK instead of REST API
Browse files
app.py
CHANGED
|
@@ -112,39 +112,18 @@ async def chat_with_gemini(request: ChatRequest):
|
|
| 112 |
Test Gemini API connectivity.
|
| 113 |
Uses GEMINI_API_KEY from environment.
|
| 114 |
"""
|
| 115 |
-
import requests
|
| 116 |
-
|
| 117 |
-
api_key = config.gemini_api_key or os.getenv("GEMINI_API_KEY")
|
| 118 |
-
if not api_key:
|
| 119 |
-
return {"error": "GEMINI_API_KEY not configured"}
|
| 120 |
-
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}"
|
| 124 |
-
|
| 125 |
-
payload = {
|
| 126 |
-
"contents": [{
|
| 127 |
-
"parts": [{"text": request.message}]
|
| 128 |
-
}]
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
response = requests.post(url, json=payload, timeout=30)
|
| 132 |
-
|
| 133 |
-
if response.status_code != 200:
|
| 134 |
-
return {"error": f"Gemini API error: {response.status_code} - {response.text[:200]}"}
|
| 135 |
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
parts = content.get("parts", [])
|
| 143 |
-
if parts:
|
| 144 |
-
reply = parts[0].get("text", "No response")
|
| 145 |
-
return {"reply": reply}
|
| 146 |
|
| 147 |
-
return {"
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
logger.error(f"Gemini chat error: {e}")
|
|
|
|
| 112 |
Test Gemini API connectivity.
|
| 113 |
Uses GEMINI_API_KEY from environment.
|
| 114 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
try:
|
| 116 |
+
from google import genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# Client gets API key from GEMINI_API_KEY environment variable
|
| 119 |
+
client = genai.Client()
|
| 120 |
|
| 121 |
+
response = client.models.generate_content(
|
| 122 |
+
model="gemini-2.5-flash",
|
| 123 |
+
contents=request.message
|
| 124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
return {"reply": response.text}
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
logger.error(f"Gemini chat error: {e}")
|