Add token limit error detection and formatting across all provider modules to surface clear diagnostics when context windows are exceeded, improving error messages and preventing silent failures. New Features - Introduce `is_token_limit_error` checks in Anthropic, Gemini, Hugging Face, Ollama, OpenAI, and OpenRouter providers. - Use `format_token_limit_error` to generate user‑friendly error messages for token limit overs. Enhancements - Centralize token limit error handling for consistent diagnostics across providers. - Explicitly re‑raise `RuntimeError` in Hugging Face to preserve original behavior. Bug Fixes - None
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from openai import OpenAI
|
|
|
|
from devgen.utils import format_token_limit_error, is_token_limit_error
|
|
|
|
|
|
class OpenrouterProvider:
|
|
"""Generates content using OpenRouter (OpenAI-compatible API)."""
|
|
|
|
BASE_URL = "https://openrouter.ai/api/v1"
|
|
|
|
def generate(
|
|
self, prompt: str, api_key: str, model: str = "openai/gpt-3.5-turbo", **kwargs
|
|
) -> str:
|
|
"""Generates a response using OpenRouter."""
|
|
if not api_key:
|
|
raise ValueError(
|
|
"OpenRouter API key is missing. "
|
|
"Set it via `devgen setup config` or pass --api-key."
|
|
)
|
|
|
|
try:
|
|
client = OpenAI(
|
|
base_url=self.BASE_URL,
|
|
api_key=api_key,
|
|
)
|
|
|
|
# Remove debug from kwargs if present
|
|
kwargs.pop("debug", None)
|
|
|
|
response = client.chat.completions.create(
|
|
model=model,
|
|
messages=[{"role": "user", "content": prompt}],
|
|
extra_headers={
|
|
"HTTP-Referer": "https://github.com/S4NKALP/devgen", # Optional
|
|
"X-Title": "devgen CLI", # Optional
|
|
},
|
|
**kwargs,
|
|
)
|
|
return response.choices[0].message.content.strip()
|
|
except Exception as e:
|
|
if is_token_limit_error(e):
|
|
raise RuntimeError(format_token_limit_error("OpenRouter", e)) from e
|
|
raise RuntimeError(
|
|
f"OpenRouter request failed: {e}. "
|
|
"Verify the model id (e.g. `openai/gpt-4o`) and your API key credits."
|
|
) from e
|