Upload 35 files
Browse files- app/agents/llm_client.py +17 -42
app/agents/llm_client.py
CHANGED
|
@@ -4,7 +4,9 @@ Supports Groq and OpenRouter for LLM inference.
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import httpx
|
|
|
|
| 7 |
from typing import Optional
|
|
|
|
| 8 |
|
| 9 |
from app.config import get_settings
|
| 10 |
|
|
@@ -15,18 +17,7 @@ async def generate_completion(
|
|
| 15 |
temperature: float = 0.3,
|
| 16 |
max_tokens: int = 2048,
|
| 17 |
) -> str:
|
| 18 |
-
"""
|
| 19 |
-
Generate a completion using the configured LLM provider.
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
messages: List of message dicts with 'role' and 'content'
|
| 23 |
-
model: Model override (uses settings default if None)
|
| 24 |
-
temperature: Sampling temperature
|
| 25 |
-
max_tokens: Maximum tokens to generate
|
| 26 |
-
|
| 27 |
-
Returns:
|
| 28 |
-
Generated text content
|
| 29 |
-
"""
|
| 30 |
settings = get_settings()
|
| 31 |
provider = settings.llm_provider
|
| 32 |
model = model or settings.llm_model
|
|
@@ -77,13 +68,12 @@ async def _call_openrouter(
|
|
| 77 |
temperature: float,
|
| 78 |
max_tokens: int,
|
| 79 |
) -> str:
|
| 80 |
-
"""Call OpenRouter API."""
|
| 81 |
settings = get_settings()
|
| 82 |
|
| 83 |
if not settings.openrouter_api_key:
|
| 84 |
raise ValueError("OPENROUTER_API_KEY not configured")
|
| 85 |
|
| 86 |
-
# OpenRouter requires specific headers
|
| 87 |
headers = {
|
| 88 |
"Authorization": f"Bearer {settings.openrouter_api_key}",
|
| 89 |
"Content-Type": "application/json",
|
|
@@ -91,37 +81,22 @@ async def _call_openrouter(
|
|
| 91 |
"X-Title": "Lancer Search API",
|
| 92 |
}
|
| 93 |
|
|
|
|
| 94 |
payload = {
|
| 95 |
"model": model,
|
| 96 |
"messages": messages,
|
| 97 |
-
"temperature": temperature,
|
| 98 |
-
"max_tokens": max_tokens,
|
| 99 |
}
|
| 100 |
|
| 101 |
-
print(f"[OpenRouter] Calling model: {model}")
|
| 102 |
-
print(f"[OpenRouter] URL: https://openrouter.ai/api/v1/chat/completions")
|
| 103 |
-
|
| 104 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
raise ValueError(f"OpenRouter error {response.status_code}: {error_text}")
|
| 118 |
-
|
| 119 |
-
data = response.json()
|
| 120 |
-
return data["choices"][0]["message"]["content"]
|
| 121 |
-
|
| 122 |
-
except httpx.HTTPStatusError as e:
|
| 123 |
-
print(f"[OpenRouter] HTTPStatusError: {e}")
|
| 124 |
-
raise
|
| 125 |
-
except Exception as e:
|
| 126 |
-
print(f"[OpenRouter] Exception: {e}")
|
| 127 |
-
raise
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import httpx
|
| 7 |
+
import json
|
| 8 |
from typing import Optional
|
| 9 |
+
import asyncio
|
| 10 |
|
| 11 |
from app.config import get_settings
|
| 12 |
|
|
|
|
| 17 |
temperature: float = 0.3,
|
| 18 |
max_tokens: int = 2048,
|
| 19 |
) -> str:
|
| 20 |
+
"""Generate a completion using the configured LLM provider."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
settings = get_settings()
|
| 22 |
provider = settings.llm_provider
|
| 23 |
model = model or settings.llm_model
|
|
|
|
| 68 |
temperature: float,
|
| 69 |
max_tokens: int,
|
| 70 |
) -> str:
|
| 71 |
+
"""Call OpenRouter API - following official docs exactly."""
|
| 72 |
settings = get_settings()
|
| 73 |
|
| 74 |
if not settings.openrouter_api_key:
|
| 75 |
raise ValueError("OPENROUTER_API_KEY not configured")
|
| 76 |
|
|
|
|
| 77 |
headers = {
|
| 78 |
"Authorization": f"Bearer {settings.openrouter_api_key}",
|
| 79 |
"Content-Type": "application/json",
|
|
|
|
| 81 |
"X-Title": "Lancer Search API",
|
| 82 |
}
|
| 83 |
|
| 84 |
+
# Payload exactly like official docs
|
| 85 |
payload = {
|
| 86 |
"model": model,
|
| 87 |
"messages": messages,
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 91 |
+
response = await client.post(
|
| 92 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 93 |
+
headers=headers,
|
| 94 |
+
content=json.dumps(payload), # Using content= with json.dumps like official docs
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if response.status_code != 200:
|
| 98 |
+
error_text = response.text
|
| 99 |
+
raise ValueError(f"OpenRouter error {response.status_code}: {error_text}")
|
| 100 |
+
|
| 101 |
+
data = response.json()
|
| 102 |
+
return data["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|