MCPcopy
hub / github.com/Sophomoresty/gemini-web2api / generate

Function generate

gemini_web2api/gemini.py:192–219  ·  view source on GitHub ↗

Non-streaming generation with retry.

(prompt: str, model_id: int, think_mode: int, file_refs: list = None, extra_fields: dict = None)

Source from the content-addressed store, hash-verified

190
191
192def generate(prompt: str, model_id: int, think_mode: int, file_refs: list = None, extra_fields: dict = None) -> str:
193 """Non-streaming generation with retry."""
194 body = _build_payload(prompt, model_id, think_mode, file_refs, extra_fields).encode()
195 url = _get_url()
196 headers = _build_headers()
197 ctx = _get_ssl_ctx()
198 proxy = CONFIG.get("proxy")
199
200 last_err = None
201 for attempt in range(CONFIG["retry_attempts"]):
202 try:
203 req = urllib.request.Request(url, data=body, headers=headers, method="POST")
204 if proxy:
205 opener = urllib.request.build_opener(
206 urllib.request.ProxyHandler({"http": proxy, "https": proxy}),
207 urllib.request.HTTPSHandler(context=ctx)
208 )
209 resp = opener.open(req, timeout=CONFIG["request_timeout_sec"])
210 else:
211 resp = urllib.request.urlopen(req, context=ctx, timeout=CONFIG["request_timeout_sec"])
212 raw = resp.read().decode("utf-8", errors="replace")
213 return extract_response_text(raw)
214 except Exception as e:
215 last_err = e
216 if attempt < CONFIG["retry_attempts"] - 1:
217 log(f"Retry {attempt+1}/{CONFIG['retry_attempts']}: {e}")
218 time.sleep(CONFIG["retry_delay_sec"])
219 raise last_err
220
221
222def generate_stream(prompt: str, model_id: int, think_mode: int, file_refs: list = None, extra_fields: dict = None):

Callers 4

generate_streamFunction · 0.85
_handle_chatMethod · 0.85
_handle_responsesMethod · 0.85

Calls 6

_build_payloadFunction · 0.85
_get_urlFunction · 0.85
_build_headersFunction · 0.85
_get_ssl_ctxFunction · 0.85
extract_response_textFunction · 0.70
logFunction · 0.70

Tested by

no test coverage detected