125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
"""
|
|
app/services/llm_service.py — LLM Client (Ollama)
|
|
Version: 0.5.2 (Fix: Removed strict limits, increased Context)
|
|
"""
|
|
|
|
import httpx
|
|
import yaml
|
|
import logging
|
|
import os
|
|
import asyncio
|
|
from pathlib import Path
|
|
from typing import Optional, Dict, Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class Settings:
|
|
OLLAMA_URL = os.getenv("MINDNET_OLLAMA_URL", "http://127.0.0.1:11434")
|
|
# Timeout für die Generierung (lang)
|
|
LLM_TIMEOUT = float(os.getenv("MINDNET_LLM_TIMEOUT", 300.0))
|
|
LLM_MODEL = os.getenv("MINDNET_LLM_MODEL", "phi3:mini")
|
|
PROMPTS_PATH = os.getenv("MINDNET_PROMPTS_PATH", "./config/prompts.yaml")
|
|
|
|
def get_settings():
|
|
return Settings()
|
|
|
|
class LLMService:
|
|
def __init__(self):
|
|
self.settings = get_settings()
|
|
self.prompts = self._load_prompts()
|
|
|
|
# FIX 1: Keine künstlichen Limits mehr. httpx defaults (100) sind besser.
|
|
# Wir wollen nicht, dass der Chat wartet, nur weil im Hintergrund Embeddings laufen.
|
|
|
|
# Timeout-Konfiguration:
|
|
# connect=10.0: Wenn Ollama nicht da ist, failen wir schnell.
|
|
# read=LLM_TIMEOUT: Wenn Ollama denkt, geben wir ihm Zeit.
|
|
self.timeout = httpx.Timeout(self.settings.LLM_TIMEOUT, connect=10.0)
|
|
|
|
self.client = httpx.AsyncClient(
|
|
base_url=self.settings.OLLAMA_URL,
|
|
timeout=self.timeout
|
|
)
|
|
|
|
def _load_prompts(self) -> dict:
|
|
path = Path(self.settings.PROMPTS_PATH)
|
|
if not path.exists():
|
|
return {}
|
|
try:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return yaml.safe_load(f)
|
|
except Exception as e:
|
|
logger.error(f"Failed to load prompts: {e}")
|
|
return {}
|
|
|
|
async def generate_raw_response(
|
|
self,
|
|
prompt: str,
|
|
system: str = None,
|
|
force_json: bool = False,
|
|
max_retries: int = 0,
|
|
base_delay: float = 2.0
|
|
) -> str:
|
|
"""
|
|
Führt einen LLM Call aus.
|
|
"""
|
|
payload: Dict[str, Any] = {
|
|
"model": self.settings.LLM_MODEL,
|
|
"prompt": prompt,
|
|
"stream": False,
|
|
"options": {
|
|
"temperature": 0.1 if force_json else 0.7,
|
|
# FIX 2: Kontext auf 8192 erhöht.
|
|
# Wichtig für komplexe Schemas und JSON-Stabilität.
|
|
"num_ctx": 8192
|
|
}
|
|
}
|
|
|
|
if force_json:
|
|
payload["format"] = "json"
|
|
|
|
if system:
|
|
payload["system"] = system
|
|
|
|
attempt = 0
|
|
|
|
while True:
|
|
try:
|
|
response = await self.client.post("/api/generate", json=payload)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
return data.get("response", "").strip()
|
|
else:
|
|
response.raise_for_status()
|
|
|
|
except Exception as e:
|
|
attempt += 1
|
|
if attempt > max_retries:
|
|
logger.error(f"LLM Final Error (Versuch {attempt}): {e}")
|
|
# Wir werfen den Fehler weiter, damit der Router nicht "Interner Fehler" als Typ interpretiert
|
|
raise e
|
|
|
|
wait_time = base_delay * (2 ** (attempt - 1))
|
|
logger.warning(f"⚠️ LLM Retry ({attempt}/{max_retries}) in {wait_time}s: {e}")
|
|
await asyncio.sleep(wait_time)
|
|
|
|
async def generate_rag_response(self, query: str, context_str: str) -> str:
|
|
"""
|
|
WICHTIG FÜR CHAT:
|
|
Kein JSON, keine Retries (User-Latency).
|
|
"""
|
|
system_prompt = self.prompts.get("system_prompt", "")
|
|
rag_template = self.prompts.get("rag_template", "{context_str}\n\n{query}")
|
|
|
|
final_prompt = rag_template.format(context_str=context_str, query=query)
|
|
|
|
return await self.generate_raw_response(
|
|
final_prompt,
|
|
system=system_prompt,
|
|
max_retries=0
|
|
)
|
|
|
|
async def close(self):
|
|
if self.client:
|
|
await self.client.aclose() |