Repository navigation
Expand file tree
/
Copy pathrouter.py
More file actions
60 lines (49 loc) 路 1.68 KB
/
Copy pathrouter.py
File metadata and controls
60 lines (49 loc) 路 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding=utf-8
"""Model routing for CodeForge AI.
This module handles dynamic model selection and invocation via OpenRouter.
"""
from typing import Any
from openai import AsyncOpenAI
from tenacity import retry, stop_after_attempt, wait_fixed
from .config import settings
openrouter: AsyncOpenAI = AsyncOpenAI(
base_url="https://openrouter.ai/api/v1", api_key=settings.openrouter_api_key
)
@retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
async def route_model(task: str, category: str) -> dict[str, Any]:
"""Route task to appropriate model based on complexity and category.
Args:
task: Task description string.
category: Task category (e.g., "reasoning", "coding").
Returns:
Dictionary with selected model and response.
"""
if "complex" in task or category == "reasoning":
model: str = "xai/grok-4"
elif "coding" in task:
model = "anthropic/claude-4-sonnet"
elif "research" in task:
model = "kimi/k2"
else:
model = "google/gemini-2.5-flash"
response_format: dict[str, Any] | None = (
{
"type": "json_object",
"json_schema": {
"name": "response",
"schema": {
"type": "object",
"properties": {"content": {"type": "string"}},
},
},
}
if settings.use_structured
else None
)
response = await openrouter.chat.completions.create(
model=model,
messages=[{"role": "user", "content": task}],
max_tokens=500,
response_format=response_format,
)
return {"model": model, "response": response.choices[0].message.content}