Use base URL for chat completions

This commit is contained in:
Thales Maciel 2026-02-10 11:00:52 -03:00
parent a0c3b02ab1
commit ad66a0d3cb
4 changed files with 320 additions and 493 deletions

View file

@ -49,7 +49,8 @@ class GenericAPIProcessor:
"temperature": self.cfg.temperature,
}
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(self.cfg.base_url, data=data, method="POST")
url = _chat_completions_url(self.cfg.base_url)
req = urllib.request.Request(url, data=data, method="POST")
req.add_header("Content-Type", "application/json")
if self.cfg.api_key:
req.add_header("Authorization", f"Bearer {self.cfg.api_key}")
@ -101,10 +102,28 @@ def list_models(base_url: str, api_key: str = "", timeout_sec: int = 10) -> list
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"
root = _root_url(base_url)
return root.rstrip("/") + "/v1/models"
def _chat_completions_url(base_url: str) -> str:
if not base_url:
return ""
trimmed = base_url.rstrip("/")
if "/v1/" in trimmed:
return trimmed
if trimmed.endswith("/v1"):
return trimmed + "/chat/completions"
return trimmed + "/v1/chat/completions"
def _root_url(base_url: str) -> str:
trimmed = base_url.rstrip("/")
if "/v1/" in trimmed:
return trimmed.split("/v1/")[0]
if trimmed.endswith("/v1"):
return trimmed[: -len("/v1")]
return trimmed
def _read_text(arg_text: str) -> str:
@ -130,20 +149,20 @@ def main() -> int:
json.dumps(redacted_dict(cfg), indent=2),
)
if not cfg.ai_enabled:
if not cfg.ai_cleanup.get("enabled", False):
logging.warning("ai_enabled is false; proceeding anyway")
prompt = load_system_prompt(cfg.ai_system_prompt_file)
prompt = load_system_prompt("")
logging.info("system prompt:\n%s", prompt)
processor = build_processor(
AIConfig(
model=cfg.ai_model,
temperature=cfg.ai_temperature,
system_prompt_file=cfg.ai_system_prompt_file,
base_url=cfg.ai_base_url,
api_key=cfg.ai_api_key,
timeout_sec=cfg.ai_timeout_sec,
model=cfg.ai_cleanup.get("model", ""),
temperature=cfg.ai_cleanup.get("temperature", 0.0),
system_prompt_file="",
base_url=cfg.ai_cleanup.get("base_url", ""),
api_key=cfg.ai_cleanup.get("api_key", ""),
timeout_sec=25,
)
)