Files
dagda/core/llm_client.py
T
2026-11-20 17:31:22 +00:00

41 lines
1.3 KiB
Python

# core/llm_client.py
import requests
import json
from django.conf import settings
from core.registry import TaskRegistry
SYSTEM_PROMPT = f"""{TaskRegistry.build_system_prompt()}
Rules:
- ALWAYS use tools when appropriate. Never respond with text unless explicitly asked for a summary.
- If unsure which module handles a request, choose the most relevant one.
- Include a "final_response" field in every tool_call result for user display."""
def process_user_input(user_text: str):
tools = TaskRegistry.get_all_tools()
payload = {
"model": "llama3.1",
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_text}
],
"tools": tools,
"tool_choice": "auto",
"temperature": 0.2
}
try:
resp = requests.post(settings.LLAMA_CPP_URL, json=payload)
data = resp.json()
tool_calls = data["choices"][0]["message"].get("tool_calls", [])
if not tool_calls:
return {"status": "text", "message": data["choices"][0]["message"]["content"]}
result = TaskRegistry.execute(tool_calls[0]["function"]["name"],
json.loads(tool_calls[0]["function"]["arguments"]))
return result
except Exception as e:
return {"error": f"LLM processing failed: {str(e)}"}