docs(step-8): aggiungi regole per parametri ottimali

fix(step-9): passa SYSTEM_PROMPT come campo system nell'API Ollama
anziche concatenato nel prompt — risolve risposte di fallback errate
con modelli piccoli
This commit is contained in:
2026-04-14 19:10:34 +02:00
parent 6594033673
commit 1a0ebafda5
2 changed files with 62 additions and 8 deletions
+7 -8
View File
@@ -60,10 +60,11 @@ def embed(text: str) -> list[float]:
# ─── Generazione ──────────────────────────────────────────────────────────────
def call_ollama(prompt: str) -> str:
def call_ollama(prompt: str, system: str = "") -> str:
"""Chiama Ollama /api/generate e ritorna la risposta."""
payload = json.dumps({
"model": LLM_MODEL,
"system": system,
"prompt": prompt,
"stream": False,
"think": not NO_THINK,
@@ -110,6 +111,7 @@ def retrieve(collection: chromadb.Collection, question: str) -> list[dict]:
# ─── Prompt ───────────────────────────────────────────────────────────────────
def build_prompt(question: str, chunks: list[dict]) -> str:
"""Ritorna (system, user_prompt) separati per l'API Ollama."""
context_parts = []
for i, c in enumerate(chunks, start=1):
header = f"[Contesto {i}"
@@ -121,11 +123,8 @@ def build_prompt(question: str, chunks: list[dict]) -> str:
context_parts.append(f"{header}\n{c['text']}")
context = "\n\n".join(context_parts)
return (
f"{SYSTEM_PROMPT}\n\n"
f"{context}\n\n"
f"Domanda: {question}"
)
user_prompt = f"{context}\n\nDomanda: {question}"
return SYSTEM_PROMPT, user_prompt
# ─── Loop interattivo ─────────────────────────────────────────────────────────
@@ -148,10 +147,10 @@ def answer(question: str, collection: chromadb.Collection, verbose: bool) -> Non
print(f" {c['text'][:120].replace(chr(10), ' ')}...")
print("──────────────────────────────────────────────────────────────\n")
prompt = build_prompt(question, chunks)
system, prompt = build_prompt(question, chunks)
try:
response = call_ollama(prompt)
response = call_ollama(prompt, system=system)
except (urllib.error.URLError, OSError) as e:
print(f"❌ Errore generazione: {e}")
return