29 lines
901 B
Python
29 lines
901 B
Python
import httpx
|
|
|
|
from app.core.config import Settings
|
|
|
|
|
|
class LLMClient:
|
|
def __init__(self, settings: Settings):
|
|
self.base_url = settings.llm_base_url.rstrip("/")
|
|
self.api_key = settings.llm_api_key
|
|
self.model = settings.llm_model_name
|
|
|
|
def complete(self, prompt: str, temperature: float = 0.7) -> str:
|
|
response = httpx.post(
|
|
f"{self.base_url}/chat/completions",
|
|
headers={
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"Content-Type": "application/json",
|
|
},
|
|
json={
|
|
"model": self.model,
|
|
"messages": [{"role": "user", "content": prompt}],
|
|
"temperature": temperature,
|
|
},
|
|
timeout=60.0,
|
|
)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
return data["choices"][0]["message"]["content"]
|