Filter low-confidence answers
This commit is contained in:
parent
e4a7d86348
commit
d43821659c
2 changed files with 30 additions and 8 deletions
|
|
@ -134,13 +134,19 @@ def create_chat_agent(config: AppConfig) -> Agent[ChatDeps, str]:
|
||||||
# Build filter from document_name
|
# Build filter from document_name
|
||||||
doc_filter = build_document_filter(document_name) if document_name else None
|
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] = []
|
ranked_history: list[QAResponse] = []
|
||||||
if ctx.deps.session_state and ctx.deps.session_state.qa_history:
|
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
|
embedder = ctx.deps.client.chunk_repository.embedder
|
||||||
ranked_history = await rank_qa_history_by_similarity(
|
ranked_history = await rank_qa_history_by_similarity(
|
||||||
current_question=question,
|
current_question=question,
|
||||||
qa_history=ctx.deps.session_state.qa_history,
|
qa_history=filtered_history,
|
||||||
embedder=embedder,
|
embedder=embedder,
|
||||||
top_k=5,
|
top_k=5,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,26 @@ def format_context_for_prompt(context: ResearchContext) -> str:
|
||||||
return format_as_xml(context_data, root_tag="research_context")
|
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
|
# Shared step logic helpers
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
@ -460,12 +480,8 @@ def build_conversational_graph(
|
||||||
deps_type=ResearchDependencies,
|
deps_type=ResearchDependencies,
|
||||||
)
|
)
|
||||||
|
|
||||||
context_xml = format_context_for_prompt(state.context)
|
context_xml = format_conversational_context_for_prompt(state.context)
|
||||||
prompt = (
|
prompt = f"Answer the question based on the gathered evidence.\n\n{context_xml}"
|
||||||
f"Answer the following question based on the gathered evidence.\n\n"
|
|
||||||
f"{context_xml}\n\n"
|
|
||||||
f"Question: {state.context.original_question}"
|
|
||||||
)
|
|
||||||
agent_deps = ResearchDependencies(
|
agent_deps = ResearchDependencies(
|
||||||
client=deps.client,
|
client=deps.client,
|
||||||
context=state.context,
|
context=state.context,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue