Filter low-confidence answers

This commit is contained in:
Yiorgis Gozadinos 2026-01-12 15:01:32 +02:00
parent e4a7d86348
commit d43821659c
No known key found for this signature in database
2 changed files with 30 additions and 8 deletions

View file

@ -134,13 +134,19 @@ def create_chat_agent(config: AppConfig) -> Agent[ChatDeps, str]:
# Build filter from document_name
doc_filter = build_document_filter(document_name) if document_name else None
# Rank qa_history by similarity to current question
# Filter and rank qa_history
ranked_history: list[QAResponse] = []
if ctx.deps.session_state and ctx.deps.session_state.qa_history:
# Step 1: Filter out low-confidence responses
filtered_history = [
qa for qa in ctx.deps.session_state.qa_history if qa.confidence >= 0.3
]
# Step 2: Rank filtered history by similarity to current question
embedder = ctx.deps.client.chunk_repository.embedder
ranked_history = await rank_qa_history_by_similarity(
current_question=question,
qa_history=ctx.deps.session_state.qa_history,
qa_history=filtered_history,
embedder=embedder,
top_k=5,
)

View file

@ -55,6 +55,26 @@ def format_context_for_prompt(context: ResearchContext) -> str:
return format_as_xml(context_data, root_tag="research_context")
def format_conversational_context_for_prompt(context: ResearchContext) -> str:
"""Format context for conversational mode - excludes unanswered_questions."""
context_data: dict[str, object] = {
"question": context.original_question,
}
# Only include conversation_history if there are qa_responses
if context.qa_responses:
context_data["conversation_history"] = [
{
"question": qa.query,
"answer": qa.answer,
"sources": [c.document_title or c.document_uri for c in qa.citations],
}
for qa in context.qa_responses
]
return format_as_xml(context_data, root_tag="context")
# =============================================================================
# Shared step logic helpers
# =============================================================================
@ -460,12 +480,8 @@ def build_conversational_graph(
deps_type=ResearchDependencies,
)
context_xml = format_context_for_prompt(state.context)
prompt = (
f"Answer the following question based on the gathered evidence.\n\n"
f"{context_xml}\n\n"
f"Question: {state.context.original_question}"
)
context_xml = format_conversational_context_for_prompt(state.context)
prompt = f"Answer the question based on the gathered evidence.\n\n{context_xml}"
agent_deps = ResearchDependencies(
client=deps.client,
context=state.context,