anhdt-dsai-02 commited on
Commit
57545f8
1 Parent(s): 883a744

Update gemini.py

Browse files
Files changed (1) hide show
  1. gemini.py +17 -18
gemini.py CHANGED
@@ -1,5 +1,11 @@
1
- async def complete_gemini_async(infor, key, params={}):
 
 
 
 
 
2
 
 
3
  question, sample = infor
4
  prompt = f"Based on language and the answer of the following sample : {sample} \n Answer this the question : {question}"
5
  data = {
@@ -23,25 +29,18 @@ async def complete_gemini_async(infor, key, params={}):
23
  params = {'key': key}
24
  headers = {"Content-Type": "application/json"}
25
  result = None
26
- pass_data = {
27
- "chat": prompt,
28
- "key": key,
29
- "params": params
30
- }
31
  try:
32
  async with httpx.AsyncClient() as client:
33
- url = rd.choice(gate_geminis)
34
- result = await client.post(url, json=pass_data, timeout=120)
35
- result.raise_for_status()
36
- return result.json()
37
-
38
  except requests.RequestException as e:
39
  if e.status_code == 429:
40
- await asyncio.sleep(10)
41
-
42
- print(f"Error making Gemini API request: {e}")
43
  async with httpx.AsyncClient() as client:
44
- url = rd.choice(gate_geminis)
45
- result = await client.post(url, json=data, timeout=120)
46
- result.raise_for_status()
47
- return result.json()
 
1
+ import requests
2
+ import os
3
+ import httpx
4
+ import asyncio
5
+
6
+ GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
7
 
8
+ async def complete_gemini_async(infor, key, params={}):
9
  question, sample = infor
10
  prompt = f"Based on language and the answer of the following sample : {sample} \n Answer this the question : {question}"
11
  data = {
 
29
  params = {'key': key}
30
  headers = {"Content-Type": "application/json"}
31
  result = None
 
 
 
 
 
32
  try:
33
  async with httpx.AsyncClient() as client:
34
+ result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
35
+ result.raise_for_status()
36
+ result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
37
+ return {"response":result}
 
38
  except requests.RequestException as e:
39
  if e.status_code == 429:
40
+ await asyncio.sleep(60)
41
+ #rint(f"Error making Gemini API request: {e}")
 
42
  async with httpx.AsyncClient() as client:
43
+ result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
44
+ result.raise_for_status()
45
+ result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
46
+ return {"response":result}