refactor: improve keyword extraction and re-ranking prompts

Apply llm-findings.md recommendations:

Keyword extraction (temp 0.0):
- Add negative constraints: "Do NOT invent terms"
- Simplify output format
- Remove verbose example

LLM re-ranking (temp 0.0):
- Add explicit rules section
- Negative constraints: "Do NOT consider document length"
- Clearer output format specification

Both prompts now use temperature=0.0 for deterministic,
consistent outputs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-23 17:21:41 +01:00
co-authored by Claude Opus 4.5
parent dfd1f19bf9
commit 8e003bb9e8
+22 -19
View File
@@ -195,25 +195,21 @@ class HybridRAGService:
Returns:
Dictionary with keywords, entities, synonyms, expansions
"""
prompt = f"""Extract search terms from this query. For each important word, provide synonyms and expansions.
prompt = f"""Extract search terms from this query.
Query: "{query}"
Return ONLY valid JSON:
{{
"core_keywords": ["key", "words", "from", "query"],
"synonyms": {{
"word": ["alternative", "terms"]
}}
}}
RULES:
- Extract ONLY keywords explicitly present or directly implied in the query
- Do NOT invent terms, concepts, or synonyms not clearly related
- Do NOT add general knowledge or associations
- Provide synonyms ONLY for technical terms with well-known alternatives
- Return valid JSON only, no commentary
Example for "Docker container hosting":
Return format:
{{
"core_keywords": ["docker", "container", "hosting"],
"synonyms": {{
"docker": ["containerization", "container runtime"],
"hosting": ["server", "infrastructure"]
}}
"core_keywords": ["words", "from", "query"],
"synonyms": {{"term": ["direct", "alternatives"]}}
}}
JSON:"""
@@ -221,7 +217,8 @@ JSON:"""
try:
response = await self.ollama.generate_text(
prompt=prompt,
model=self.reranker_model
model=self.reranker_model,
temperature=0.0 # Deterministic for consistent extraction
)
# Parse JSON response (handle potential extra text)
@@ -623,21 +620,27 @@ JSON:"""
for i, r in enumerate(results)
])
prompt = f"""Given this search query and documents, rank them by relevance.
prompt = f"""Rank these documents by relevance to the query.
Query: {query}
Documents:
{docs_text}
Return only the numbers in order of relevance (most relevant first).
Example: 3,1,5,2,4
RULES:
- Rank ONLY by how well content answers the query
- Do NOT consider document length, formatting, or style
- Do NOT add explanation or commentary
- Return ONLY comma-separated numbers, most relevant first
Example output: 3,1,5,2,4
Ranking:"""
response = await self.ollama.generate_text(
prompt=prompt,
model=self.reranker_model
model=self.reranker_model,
temperature=0.0 # Deterministic for consistent rankings
)
# Parse response: "3,1,5,2,4" → [2, 0, 4, 1, 3] (0-indexed)