Basic start

This commit is contained in:
shamoon
2025-04-19 19:51:30 -07:00
parent 3180ccf4cb
commit 55cb9cedc7
7 changed files with 316 additions and 26 deletions

View File

@@ -0,0 +1,43 @@
import httpx
from django.conf import settings
def run_llm_query(prompt: str) -> str:
if settings.LLM_BACKEND == "ollama":
return _run_ollama_query(prompt)
return _run_openai_query(prompt)
def _run_ollama_query(prompt: str) -> str:
with httpx.Client(timeout=30.0) as client:
response = client.post(
f"{settings.OLLAMA_URL}/api/chat",
json={
"model": settings.LLM_MODEL,
"messages": [{"role": "user", "content": prompt}],
"stream": False,
},
)
response.raise_for_status()
return response.json()["message"]["content"]
def _run_openai_query(prompt: str) -> str:
if not settings.LLM_API_KEY:
raise RuntimeError("PAPERLESS_LLM_API_KEY is not set")
with httpx.Client(timeout=30.0) as client:
response = client.post(
f"{settings.OPENAI_URL}/v1/chat/completions",
headers={
"Authorization": f"Bearer {settings.LLM_API_KEY}",
"Content-Type": "application/json",
},
json={
"model": settings.LLM_MODEL,
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.3,
},
)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]