Files
trekkerv2/app/services/llm.py
T

77 lines
2.7 KiB
Python

"""OpenAI-compatible LLM adapter for trail guide extraction.
Works with any OpenAI-compatible endpoint (Ollama, Groq, Gemini, OpenAI)
via configurable base_url / model / api_key.
"""
from __future__ import annotations
import json
import logging
from typing import Any
from openai import OpenAI
from app.config import LLMConfig
log = logging.getLogger("trekker.llm")
EXTRACTION_PROMPT = """You extract structured overland trail data from text.
Return ONLY a JSON object with these keys (use null/[] when not mentioned):
{
"name": string,
"difficulty": string|null, // e.g. "easy", "moderate", "difficult", "extreme", or a rating like "7/10"
"obstacle_types": [string], // e.g. "rock garden", "ledges", "mud", "off-camber"
"water_crossings": [{"name": string|null, "depth_ft": number|null, "notes": string|null}],
"gate_codes": [string],
"vehicle_recommendations": [string],// e.g. "33-inch tires", "lockers recommended", "winch"
"closures": [string], // seasonal or permanent closure notes
"warnings": [string],
"notes": string|null
}"""
class LLMService:
def __init__(self, config: LLMConfig):
self.config = config
@property
def configured(self) -> bool:
return bool(self.config.api_key or self.config.base_url)
def _client(self) -> OpenAI:
return OpenAI(
base_url=self.config.base_url or None,
api_key=self.config.api_key or "not-needed", # local providers ignore the key
)
def extract_trail_data(self, text: str) -> dict[str, Any]:
"""Trail guide text -> structured dict. Raises on provider errors."""
client = self._client()
# Keep prompts inside small local-model context windows.
snippet = text[:24000]
kwargs: dict[str, Any] = {
"model": self.config.model,
"messages": [
{"role": "system", "content": EXTRACTION_PROMPT},
{"role": "user", "content": snippet},
],
}
try:
resp = client.chat.completions.create(
**kwargs, response_format={"type": "json_object"}
)
except Exception:
# Some providers reject response_format; retry without it.
resp = client.chat.completions.create(**kwargs)
content = resp.choices[0].message.content or "{}"
# Tolerate code-fenced JSON from smaller models.
content = content.strip()
if content.startswith("```"):
content = content.strip("`")
content = content.split("\n", 1)[1] if "\n" in content else content
start, end = content.find("{"), content.rfind("}")
if start != -1 and end > start:
content = content[start : end + 1]
return json.loads(content)