gemini-gate / gemini.py
anhdt-dsai-02's picture
Update gemini.py
b229e20 verified
raw
history blame
1.62 kB
async def complete_gemini_async(infor, key, params={}):
question, sample = infor
prompt = f"Based on language and the answer of the following sample : {sample} \n Answer this the question : {question}"
data = {
"contents": [
{
"parts": [
{
"text":prompt
}
]
}
],
"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
pass_data = {
"chat": prompt,
"key": key,
"params": params
}
try:
async with httpx.AsyncClient() as client:
url = rd.choice(gate_geminis)
result = await client.post(url, json=pass_data, timeout=120)
result.raise_for_status()
return result.json()
except requests.RequestException as e:
if e.status_code == 429:
await asyncio.sleep(10)
print(f"Error making Gemini API request: {e}")
async with httpx.AsyncClient() as client:
url = rd.choice(gate_geminis)
result = await client.post(url, json=data, timeout=120)
result.raise_for_status()
return result.json()