File size: 1,623 Bytes
b229e20
cf1ac3f
b229e20
 
cf1ac3f
b229e20
 
 
 
 
 
 
 
 
cf1ac3f
 
 
 
 
 
 
 
 
 
 
b229e20
 
 
 
 
cf1ac3f
 
b229e20
 
 
 
 
cf1ac3f
 
b229e20
 
 
cf1ac3f
b229e20
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
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()