Spaces:
Sleeping
Sleeping
Update app/model_router.py
Browse files- app/model_router.py +66 -18
app/model_router.py
CHANGED
|
@@ -1,24 +1,72 @@
|
|
| 1 |
import random
|
| 2 |
import httpx
|
|
|
|
| 3 |
from .config import OPENROUTER_API_KEY, FREE_MODELS
|
| 4 |
|
| 5 |
-
async def query_model(prompt: str, model: str = None):
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
import httpx
|
| 3 |
+
from typing import Optional
|
| 4 |
from .config import OPENROUTER_API_KEY, FREE_MODELS
|
| 5 |
|
| 6 |
+
async def query_model(prompt: str, model: Optional[str] = None) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Query the OpenRouter API with a prompt and optional model selection.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
prompt: The user's input message
|
| 12 |
+
model: Optional model name to use (random free model selected if None)
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
The generated response content
|
| 16 |
+
|
| 17 |
+
Raises:
|
| 18 |
+
httpx.HTTPStatusError: If the API request fails
|
| 19 |
+
ValueError: If the response format is invalid
|
| 20 |
+
Exception: For other unexpected errors
|
| 21 |
+
"""
|
| 22 |
+
try:
|
| 23 |
+
# Select a random free model if none specified
|
| 24 |
+
model = model or random.choice(FREE_MODELS)
|
| 25 |
+
|
| 26 |
+
headers = {
|
| 27 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 28 |
+
"Content-Type": "application/json",
|
| 29 |
+
"HTTP-Referer": "https://github.com/your-repo", # Recommended by OpenRouter
|
| 30 |
+
"X-Title": "Your App Name" # Recommended by OpenRouter
|
| 31 |
+
}
|
| 32 |
|
| 33 |
+
payload = {
|
| 34 |
+
"model": model,
|
| 35 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 39 |
+
response = await client.post(
|
| 40 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 41 |
+
json=payload,
|
| 42 |
+
headers=headers
|
| 43 |
+
)
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
|
| 46 |
+
# Safely extract the response content
|
| 47 |
+
response_data = response.json()
|
| 48 |
+
if not response_data.get("choices"):
|
| 49 |
+
raise ValueError("No choices in response")
|
| 50 |
+
|
| 51 |
+
first_choice = response_data["choices"][0]
|
| 52 |
+
if "message" not in first_choice or "content" not in first_choice["message"]:
|
| 53 |
+
raise ValueError("Invalid message format in response")
|
| 54 |
+
|
| 55 |
+
return first_choice["message"]["content"]
|
| 56 |
+
|
| 57 |
+
except httpx.HTTPStatusError as e:
|
| 58 |
+
# Include more details in the error message
|
| 59 |
+
error_info = f"HTTP error {e.response.status_code}"
|
| 60 |
+
if e.response.text:
|
| 61 |
+
try:
|
| 62 |
+
error_detail = e.response.json().get("error", {}).get("message", e.response.text)
|
| 63 |
+
error_info += f": {error_detail}"
|
| 64 |
+
except ValueError:
|
| 65 |
+
error_info += f": {e.response.text[:200]}" # Truncate long text
|
| 66 |
+
raise httpx.HTTPStatusError(error_info, request=e.request, response=e.response)
|
| 67 |
+
|
| 68 |
+
except (KeyError, IndexError, ValueError) as e:
|
| 69 |
+
raise ValueError(f"Failed to parse API response: {str(e)}") from e
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
raise Exception(f"Unexpected error while querying model: {str(e)}") from e
|