client.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. from __future__ import annotations
  2. import os
  3. from dotenv import load_dotenv
  4. from openai import OpenAI
  5. load_dotenv()
  6. _DEFAULT_MODEL = "google/gemini-flash-1.5"
  7. def complete(prompt: str, max_tokens: int = 200) -> str:
  8. """Call OpenRouter with prompt, return response text. Raises on failure."""
  9. client = OpenAI(
  10. base_url="https://openrouter.ai/api/v1",
  11. api_key=os.environ["OPENROUTER_API_KEY"],
  12. )
  13. model = os.environ.get("OPENROUTER_MODEL", _DEFAULT_MODEL)
  14. resp = client.chat.completions.create(
  15. model=model,
  16. max_tokens=max_tokens,
  17. messages=[{"role": "user", "content": prompt}],
  18. extra_body={"reasoning": {"enabled": False}},
  19. )
  20. content = resp.choices[0].message.content
  21. if content is None:
  22. finish_reason = resp.choices[0].finish_reason
  23. raise RuntimeError(
  24. f"OpenRouter returned empty content for model {model!r} "
  25. f"(finish_reason={finish_reason!r}, raw={resp.model_dump_json()})"
  26. )
  27. return content.strip()