Improve translation reliability and progress feedback

This commit is contained in:
burnintuna 2026-07-30 17:07:02 +09:00
parent 9439858b11
commit 51a6e845d1
20 changed files with 1194 additions and 216 deletions

View file

@ -2,6 +2,7 @@
import os
from pathlib import Path
from collections.abc import Awaitable, Callable
from openai import AsyncOpenAI
@ -22,6 +23,7 @@ def _load_llm_configs() -> list[LLMConfig]:
_llm_configs: list[LLMConfig] = []
_clients: dict[str, AsyncOpenAI] = {} # alias -> client instance
LLM_TIMEOUT_SECONDS = float(os.getenv("LLM_TIMEOUT_SECONDS", "180"))
def get_llm_configs() -> list[LLMConfig]:
@ -46,7 +48,8 @@ def get_client_for_alias(alias: str) -> tuple[AsyncOpenAI, LLMConfig]:
client = AsyncOpenAI(
api_key=cfg.api_key,
base_url=cfg.base_url,
timeout=120.0,
timeout=LLM_TIMEOUT_SECONDS,
max_retries=0,
)
_clients[alias] = client
@ -58,21 +61,33 @@ async def chat_complete(
system_prompt: str,
user_content: str,
temperature: float = 0.3,
on_delta: Callable[[str], Awaitable[None]] | None = None,
) -> str:
"""Send a single-turn chat completion and return the assistant message."""
client, cfg = get_client_for_alias(alias)
response = await client.chat.completions.create(
model=cfg.model,
messages=[
request = {
"model": cfg.model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_content},
],
temperature=temperature,
)
"temperature": temperature,
}
if on_delta is None:
response = await client.chat.completions.create(**request)
content = response.choices[0].message.content or ""
return content.strip()
content = response.choices[0].message.content or ""
return content.strip()
stream = await client.chat.completions.create(**request, stream=True)
parts: list[str] = []
async for event in stream:
if not event.choices:
continue
delta = event.choices[0].delta.content or ""
if delta:
parts.append(delta)
await on_delta(delta)
return "".join(parts).strip()
def get_context_size(alias: str) -> int | None: