feat(ai): add OpenRouter provider order and fallback controls
This commit is contained in:
@@ -69,6 +69,14 @@ class Settings(BaseSettings):
|
|||||||
description="Premium Llama model identifier",
|
description="Premium Llama model identifier",
|
||||||
)
|
)
|
||||||
OPENROUTER_TIMEOUT: int = Field(default=30, description="OpenRouter API timeout in seconds")
|
OPENROUTER_TIMEOUT: int = Field(default=30, description="OpenRouter API timeout in seconds")
|
||||||
|
OPENROUTER_PROVIDER_ORDER: List[str] = Field(
|
||||||
|
default=["NovitaAI", "AkashML", "Inception"],
|
||||||
|
description="Preferred OpenRouter providers in priority order",
|
||||||
|
)
|
||||||
|
OPENROUTER_ALLOW_PROVIDER_FALLBACKS: bool = Field(
|
||||||
|
default=True,
|
||||||
|
description="Allow OpenRouter to fallback outside preferred providers",
|
||||||
|
)
|
||||||
|
|
||||||
# WordPress Integration
|
# WordPress Integration
|
||||||
WORDPRESS_API_URL: str = Field(
|
WORDPRESS_API_URL: str = Field(
|
||||||
@@ -103,6 +111,14 @@ class Settings(BaseSettings):
|
|||||||
return [origin.strip() for origin in v.split(",") if origin.strip()]
|
return [origin.strip() for origin in v.split(",") if origin.strip()]
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
@field_validator("OPENROUTER_PROVIDER_ORDER", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def parse_provider_order(cls, v: Union[str, List[str]]) -> List[str]:
|
||||||
|
"""Parse comma-separated OpenRouter provider list into array."""
|
||||||
|
if isinstance(v, str):
|
||||||
|
return [provider.strip() for provider in v.split(",") if provider.strip()]
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
# Global settings instance
|
# Global settings instance
|
||||||
_settings: Union[Settings, None] = None
|
_settings: Union[Settings, None] = None
|
||||||
|
|||||||
@@ -325,7 +325,7 @@ async def call_openrouter_api(
|
|||||||
"X-Title": "IRT Bank Soal",
|
"X-Title": "IRT Bank Soal",
|
||||||
}
|
}
|
||||||
|
|
||||||
payload = {
|
payload: dict[str, Any] = {
|
||||||
"model": model,
|
"model": model,
|
||||||
"messages": [
|
"messages": [
|
||||||
{
|
{
|
||||||
@@ -336,6 +336,14 @@ async def call_openrouter_api(
|
|||||||
"max_tokens": 2000,
|
"max_tokens": 2000,
|
||||||
"temperature": 0.7,
|
"temperature": 0.7,
|
||||||
}
|
}
|
||||||
|
provider_order = [
|
||||||
|
provider for provider in settings.OPENROUTER_PROVIDER_ORDER if provider.strip()
|
||||||
|
]
|
||||||
|
if provider_order:
|
||||||
|
payload["provider"] = {
|
||||||
|
"order": provider_order,
|
||||||
|
"allow_fallbacks": settings.OPENROUTER_ALLOW_PROVIDER_FALLBACKS,
|
||||||
|
}
|
||||||
|
|
||||||
timeout = httpx.Timeout(settings.OPENROUTER_TIMEOUT)
|
timeout = httpx.Timeout(settings.OPENROUTER_TIMEOUT)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user