feat: add category-based knowledge search and deduplication in inject_knowledge_context
Implement category-specific search for preferences and general entries alongside existing hybrid search, appending results with a fixed score of 0.6. Remove duplicate knowledge results by filtering out entries with identical content before building the final context list.
This commit is contained in:
@@ -16,7 +16,22 @@ def inject_knowledge_context(assistant, user_message):
|
||||
logger.debug(f"Removed existing knowledge base message at index {i}")
|
||||
break
|
||||
try:
|
||||
knowledge_results = assistant.enhanced.knowledge_store.search_entries(user_message, top_k=5)
|
||||
# Run all search methods
|
||||
knowledge_results = assistant.enhanced.knowledge_store.search_entries(user_message, top_k=5) # Hybrid semantic + keyword + category
|
||||
# Additional keyword search if needed (but already in hybrid)
|
||||
# Category-specific: preferences and general
|
||||
pref_results = assistant.enhanced.knowledge_store.get_by_category("preferences", limit=5)
|
||||
general_results = assistant.enhanced.knowledge_store.get_by_category("general", limit=5)
|
||||
category_results = []
|
||||
for entry in pref_results + general_results:
|
||||
if any(word in entry.content.lower() for word in user_message.lower().split()):
|
||||
category_results.append({
|
||||
"content": entry.content,
|
||||
"score": 0.6,
|
||||
"source": f"Knowledge Base ({entry.category})",
|
||||
"type": "knowledge_category",
|
||||
})
|
||||
|
||||
conversation_results = []
|
||||
if hasattr(assistant.enhanced, "conversation_memory"):
|
||||
history_results = assistant.enhanced.conversation_memory.search_conversations(
|
||||
@@ -48,6 +63,8 @@ def inject_knowledge_context(assistant, user_message):
|
||||
"type": "knowledge",
|
||||
}
|
||||
)
|
||||
for res in category_results:
|
||||
all_results.append(res)
|
||||
for conv in conversation_results:
|
||||
all_results.append(
|
||||
{
|
||||
@@ -57,8 +74,15 @@ def inject_knowledge_context(assistant, user_message):
|
||||
"type": "conversation",
|
||||
}
|
||||
)
|
||||
all_results.sort(key=lambda x: x["score"], reverse=True)
|
||||
top_results = all_results[:5]
|
||||
# Remove duplicates by content
|
||||
seen = set()
|
||||
unique_results = []
|
||||
for res in all_results:
|
||||
if res["content"] not in seen:
|
||||
seen.add(res["content"])
|
||||
unique_results.append(res)
|
||||
unique_results.sort(key=lambda x: x["score"], reverse=True)
|
||||
top_results = unique_results[:5]
|
||||
if not top_results:
|
||||
logger.debug("No relevant knowledge or conversation matches found")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user