Send prompt and yield incremental text deltas using httpx streaming.
(prompt: str, model_id: int, think_mode: int)
| 215 | |
| 216 | |
| 217 | def gemini_stream_generate_iter(prompt: str, model_id: int, think_mode: int): |
| 218 | """Send prompt and yield incremental text deltas using httpx streaming.""" |
| 219 | inner = [None] * 80 |
| 220 | inner[0] = [prompt, 0, None, None, None, None, 0] |
| 221 | inner[1] = ["en"] |
| 222 | inner[2] = ["", "", "", None, None, None, None, None, None, ""] |
| 223 | inner[6] = [0] |
| 224 | inner[7] = 1 |
| 225 | inner[10] = 1 |
| 226 | inner[11] = 0 |
| 227 | inner[17] = [[think_mode]] |
| 228 | inner[18] = 0 |
| 229 | inner[27] = 1 |
| 230 | inner[30] = [4] |
| 231 | inner[41] = [2] |
| 232 | inner[53] = 0 |
| 233 | inner[59] = str(uuid.uuid4()) |
| 234 | inner[61] = [] |
| 235 | inner[68] = 1 |
| 236 | inner[79] = model_id |
| 237 | |
| 238 | outer = [None, json.dumps(inner)] |
| 239 | params = {"f.req": json.dumps(outer)} |
| 240 | if CONFIG.get("xsrf_token"): |
| 241 | params["at"] = CONFIG["xsrf_token"] |
| 242 | body = urllib.parse.urlencode(params) |
| 243 | reqid = int(time.time()) % 1000000 |
| 244 | prefix = account_prefix() |
| 245 | url = ( |
| 246 | f"https://gemini.google.com{prefix}/_/BardChatUi/data/" |
| 247 | "assistant.lamda.BardFrontendService/StreamGenerate" |
| 248 | f"?bl={CONFIG['gemini_bl']}&hl=en&_reqid={reqid}&rt=c" |
| 249 | ) |
| 250 | headers = { |
| 251 | "Content-Type": "application/x-www-form-urlencoded", |
| 252 | "Origin": "https://gemini.google.com", |
| 253 | "Referer": f"https://gemini.google.com{prefix}/app", |
| 254 | "X-Same-Domain": "1", |
| 255 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", |
| 256 | } |
| 257 | if prefix: |
| 258 | headers["X-Goog-AuthUser"] = str(CONFIG["auth_user"]) |
| 259 | cookie_str, sapisid = load_cookie() |
| 260 | if cookie_str: |
| 261 | headers["Cookie"] = cookie_str |
| 262 | if sapisid: |
| 263 | headers["Authorization"] = make_sapisidhash(sapisid) |
| 264 | |
| 265 | proxy = CONFIG.get("proxy") |
| 266 | |
| 267 | if not HAS_HTTPX: |
| 268 | # Fallback: non-streaming with urllib |
| 269 | raw = gemini_stream_generate(prompt, model_id, think_mode) |
| 270 | text = extract_response_text(raw) |
| 271 | if text: |
| 272 | yield text |
| 273 | return |
| 274 |
no test coverage detected