Non-streaming generation with retry.
(prompt: str, model_id: int, think_mode: int, file_refs: list = None, extra_fields: dict = None)
| 190 | |
| 191 | |
| 192 | def 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 | |
| 222 | def generate_stream(prompt: str, model_id: int, think_mode: int, file_refs: list = None, extra_fields: dict = None): |
no test coverage detected