Add settings history and quick AI chain
This commit is contained in:
parent
328dcec458
commit
a0c3b02ab1
13 changed files with 1627 additions and 23 deletions
|
|
@ -23,6 +23,8 @@ class AIConfig:
|
|||
base_url: str
|
||||
api_key: str
|
||||
timeout_sec: int
|
||||
language_hint: str | None = None
|
||||
wrap_transcript: bool = True
|
||||
|
||||
|
||||
class GenericAPIProcessor:
|
||||
|
|
@ -31,11 +33,18 @@ class GenericAPIProcessor:
|
|||
self.system = load_system_prompt(cfg.system_prompt_file)
|
||||
|
||||
def process(self, text: str) -> str:
|
||||
language = self.cfg.language_hint or ""
|
||||
if self.cfg.wrap_transcript:
|
||||
user_content = f"<transcript>{text}</transcript>"
|
||||
else:
|
||||
user_content = text
|
||||
if language:
|
||||
user_content = f"<language>{language}</language>\n{user_content}"
|
||||
payload = {
|
||||
"model": self.cfg.model,
|
||||
"messages": [
|
||||
{"role": "system", "content": self.system},
|
||||
{"role": "user", "content": f"<transcript>{text}</transcript>"},
|
||||
{"role": "user", "content": user_content},
|
||||
],
|
||||
"temperature": self.cfg.temperature,
|
||||
}
|
||||
|
|
@ -70,6 +79,34 @@ def build_processor(cfg: AIConfig) -> GenericAPIProcessor:
|
|||
return GenericAPIProcessor(cfg)
|
||||
|
||||
|
||||
def list_models(base_url: str, api_key: str = "", timeout_sec: int = 10) -> list[str]:
|
||||
if not base_url:
|
||||
return []
|
||||
url = _models_url(base_url)
|
||||
req = urllib.request.Request(url, method="GET")
|
||||
if api_key:
|
||||
req.add_header("Authorization", f"Bearer {api_key}")
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=timeout_sec) as resp:
|
||||
body = resp.read()
|
||||
data = json.loads(body.decode("utf-8"))
|
||||
models = []
|
||||
for item in data.get("data", []):
|
||||
model_id = item.get("id")
|
||||
if model_id:
|
||||
models.append(model_id)
|
||||
return models
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
|
||||
def _models_url(base_url: str) -> str:
|
||||
if "/v1/" in base_url:
|
||||
root = base_url.split("/v1/")[0]
|
||||
return root.rstrip("/") + "/v1/models"
|
||||
return base_url.rstrip("/") + "/v1/models"
|
||||
|
||||
|
||||
def _read_text(arg_text: str) -> str:
|
||||
if arg_text:
|
||||
return arg_text
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue