-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerator.py
More file actions
174 lines (148 loc) · 6.61 KB
/
Copy pathgenerator.py
File metadata and controls
174 lines (148 loc) · 6.61 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import json
import httpx
import asyncio
import logging
from typing import Any
from config import shuffle_keys
async def generate(MODEL: str, gemini_contents: Any, max_retries: int = 0):
shuffled_keys = await shuffle_keys()
url = f"https://generativelanguage.googleapis.com/v1/models/{MODEL}:streamGenerateContent"
headers = {
"Content-Type": "application/json",
}
payload = {
"contents": gemini_contents,
"generationConfig": {
"maxOutputTokens": 65000,
"temperature": 0.7,
},
}
for key_index, key in enumerate(shuffled_keys):
params = {"key": key, "alt": "sse"}
for attempt in range(max_retries + 1):
logging.info(
f"[KEY {key_index + 1}/{len(shuffled_keys)}] Attempt {attempt + 1}/{max_retries + 1} with key {key[:6]}... on model {MODEL}"
)
try:
async with httpx.AsyncClient(timeout=None, http2=True) as client:
async with client.stream(
"POST", url, json=payload, headers=headers, params=params
) as response:
logging.info(f"Response status: {response.status_code}")
if response.status_code != 200:
error_text = await response.aread()
error_str = error_text.decode("utf-8", errors="ignore")
logging.info(
f"API Error {response.status_code} - trying next key"
)
logging.error(error_str)
break
chunk_count = 0
async for line in response.aiter_lines():
line = line.strip()
if not line or not line.startswith("data:"):
continue
try:
data = json.loads(line[6:])
text = data["candidates"][0]["content"]["parts"][0][
"text"
]
if text:
chunk_count += 1
openai_chunk = {
"id": f"chatcmpl-{chunk_count}",
"object": "chat.completion.chunk",
"created": int(asyncio.get_event_loop().time()),
"model": MODEL,
"choices": [
{
"index": 0,
"delta": {"content": text},
"finish_reason": None,
}
],
}
yield f"data: {json.dumps(openai_chunk)}\n\n"
except Exception as e:
logging.exception(f"Error parsing chunk: {e}")
pass
if chunk_count > 0:
final_chunk = {
"id": f"chatcmpl-{chunk_count + 1}",
"object": "chat.completion.chunk",
"created": int(asyncio.get_event_loop().time()),
"model": MODEL,
"choices": [
{
"index": 0,
"delta": {},
"finish_reason": "stop",
}
],
}
yield f"data: {json.dumps(final_chunk)}\n\n"
yield "data: [DONE]\n\n"
return
else:
break
except Exception as e:
logging.exception(f"Error during streaming request to {MODEL}: {e}")
if attempt < max_retries:
await asyncio.sleep(2**attempt)
continue
else:
break
logging.info("All keys exhausted - returning error")
error_chunk = {
"id": "chatcmpl-error",
"object": "chat.completion.chunk",
"created": int(asyncio.get_event_loop().time()),
"model": MODEL,
"choices": [
{
"index": 0,
"delta": {"content": "[Error: All API keys failed]"},
"finish_reason": "stop",
}
],
}
yield f"data: {json.dumps(error_chunk)}\n\n"
yield "data: [DONE]\n\n"
async def generate_non_stream(MODEL: str, user_msg: str, max_retries: int = 0):
shuffled_keys = await shuffle_keys()
url = f"https://generativelanguage.googleapis.com/v1/models/{MODEL}:generateContent"
headers = {
"Content-Type": "application/json",
}
payload = {
"contents": [{"role": "user", "parts": [{"text": user_msg}]}],
}
for key_index, key in enumerate(shuffled_keys):
params = {"key": key}
for attempt in range(max_retries + 1):
logging.info(
f"[NON-STREAM KEY {key_index + 1}/{len(shuffled_keys)}] Attempt {attempt + 1}/{max_retries + 1}"
)
try:
async with httpx.AsyncClient(timeout=None) as client:
response = await client.post(
url, json=payload, headers=headers, params=params
)
if response.status_code == 200:
data = response.json()
text = data["candidates"][0]["content"]["parts"][0]["text"]
if text:
return text
else:
logging.info(
f"API Error {response.status_code} - trying next key"
)
break
except Exception as e:
logging.exception(f"Error during non-streaming request to {MODEL}: {e}")
if attempt < max_retries:
await asyncio.sleep(2**attempt)
continue
else:
break
return "[Error: All API keys failed]"