Send prompt to Gemini StreamGenerate with retry.
(prompt: str, model_id: int, think_mode: int)
| 143 | # ─── Gemini Protocol ───────────────────────────────────────────────────────── |
| 144 | |
| 145 | def gemini_stream_generate(prompt: str, model_id: int, think_mode: int) -> str: |
| 146 | """Send prompt to Gemini StreamGenerate with retry.""" |
| 147 | inner = [None] * 80 |
| 148 | inner[0] = [prompt, 0, None, None, None, None, 0] |
| 149 | inner[1] = ["en"] |
| 150 | inner[2] = ["", "", "", None, None, None, None, None, None, ""] |
| 151 | inner[6] = [0] |
| 152 | inner[7] = 1 |
| 153 | inner[10] = 1 |
| 154 | inner[11] = 0 |
| 155 | inner[17] = [[think_mode]] |
| 156 | inner[18] = 0 |
| 157 | inner[27] = 1 |
| 158 | inner[30] = [4] |
| 159 | inner[41] = [2] |
| 160 | inner[53] = 0 |
| 161 | inner[59] = str(uuid.uuid4()) |
| 162 | inner[61] = [] |
| 163 | inner[68] = 1 |
| 164 | inner[79] = model_id |
| 165 | |
| 166 | outer = [None, json.dumps(inner)] |
| 167 | params = {"f.req": json.dumps(outer)} |
| 168 | if CONFIG.get("xsrf_token"): |
| 169 | params["at"] = CONFIG["xsrf_token"] |
| 170 | body = urllib.parse.urlencode(params).encode() |
| 171 | reqid = int(time.time()) % 1000000 |
| 172 | prefix = account_prefix() |
| 173 | url = ( |
| 174 | f"https://gemini.google.com{prefix}/_/BardChatUi/data/" |
| 175 | "assistant.lamda.BardFrontendService/StreamGenerate" |
| 176 | f"?bl={CONFIG['gemini_bl']}&hl=en&_reqid={reqid}&rt=c" |
| 177 | ) |
| 178 | headers = { |
| 179 | "Content-Type": "application/x-www-form-urlencoded", |
| 180 | "Origin": "https://gemini.google.com", |
| 181 | "Referer": f"https://gemini.google.com{prefix}/app", |
| 182 | "X-Same-Domain": "1", |
| 183 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", |
| 184 | } |
| 185 | if prefix: |
| 186 | headers["X-Goog-AuthUser"] = str(CONFIG["auth_user"]) |
| 187 | |
| 188 | cookie_str, sapisid = load_cookie() |
| 189 | if cookie_str: |
| 190 | headers["Cookie"] = cookie_str |
| 191 | if sapisid: |
| 192 | headers["Authorization"] = make_sapisidhash(sapisid) |
| 193 | |
| 194 | last_err = None |
| 195 | for attempt in range(CONFIG["retry_attempts"]): |
| 196 | try: |
| 197 | req = urllib.request.Request(url, data=body, headers=headers, method="POST") |
| 198 | ctx = ssl.create_default_context() |
| 199 | proxy = CONFIG.get("proxy") |
| 200 | if proxy: |
| 201 | opener = urllib.request.build_opener( |
| 202 | urllib.request.ProxyHandler({"http": proxy, "https": proxy}), |
no test coverage detected