anhdt-dsai-02 commited on
Commit
b229e20
1 Parent(s): 6737b1f

Update gemini.py

Browse files
Files changed (1) hide show
  1. gemini.py +29 -18
gemini.py CHANGED
@@ -1,13 +1,17 @@
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(chat, key, params={}):
9
  data = {
10
- "contents": chat,
 
 
 
 
 
 
 
 
11
  "generationConfig": {
12
  "stopSequences": ["Title"],
13
  "temperature": 1.0 if "temp" not in params else params["temp"],
@@ -19,18 +23,25 @@ async def complete_gemini_async(chat, key, params={}):
19
  params = {'key': key}
20
  headers = {"Content-Type": "application/json"}
21
  result = None
 
 
 
 
 
22
  try:
23
  async with httpx.AsyncClient() as client:
24
- result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
25
- result.raise_for_status()
26
- result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
27
- return {"response":result}
 
28
  except requests.RequestException as e:
29
  if e.status_code == 429:
30
- await asyncio.sleep(60)
31
- #rint(f"Error making Gemini API request: {e}")
 
32
  async with httpx.AsyncClient() as client:
33
- result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
34
- result.raise_for_status()
35
- result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
36
- return {"response":result}
 
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 = {
6
+ "contents": [
7
+ {
8
+ "parts": [
9
+ {
10
+ "text":prompt
11
+ }
12
+ ]
13
+ }
14
+ ],
15
  "generationConfig": {
16
  "stopSequences": ["Title"],
17
  "temperature": 1.0 if "temp" not in params else params["temp"],
 
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()