Spaces:
Sleeping
Sleeping
File size: 1,526 Bytes
cf1ac3f |
1 2 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 |
import requests
import os
import httpx
import asyncio
GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
async def complete_gemini_async(chat, key, params={}):
data = {
"contents": chat,
"generationConfig": {
"stopSequences": ["Title"],
"temperature": 1.0 if "temp" not in params else params["temp"],
"maxOutputTokens": 2048 if "max_length" not in params else params["max_length"],
"topP": 0.8 if "top_p" not in params else params["top_p"],
"topK": 10 if "top_k" not in params else params["top_k"]
}
}
params = {'key': key}
headers = {"Content-Type": "application/json"}
result = None
try:
async with httpx.AsyncClient() as client:
result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
result.raise_for_status()
result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
return {"response":result}
except requests.RequestException as e:
if e.status_code == 429:
await asyncio.sleep(60)
#rint(f"Error making Gemini API request: {e}")
async with httpx.AsyncClient() as client:
result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
result.raise_for_status()
result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
return {"response":result}
|