Use previous context in summarization
This commit is contained in:
parent
3214160fe7
commit
93737852a8
4 changed files with 141 additions and 2 deletions
|
|
@ -11,12 +11,15 @@ from haiku.rag.utils import get_model
|
||||||
async def summarize_session(
|
async def summarize_session(
|
||||||
qa_history: list[QAResponse],
|
qa_history: list[QAResponse],
|
||||||
config: AppConfig,
|
config: AppConfig,
|
||||||
|
current_context: str | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Summarize qa_history into compact context.
|
"""Summarize qa_history into compact context.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
qa_history: List of Q&A pairs from the conversation.
|
qa_history: List of Q&A pairs from the conversation.
|
||||||
config: AppConfig for model selection.
|
config: AppConfig for model selection.
|
||||||
|
current_context: Previous context to incorporate (background_context or
|
||||||
|
previous session_context.summary). The summarizer will build upon this.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Markdown summary of the conversation history.
|
Markdown summary of the conversation history.
|
||||||
|
|
@ -33,6 +36,8 @@ async def summarize_session(
|
||||||
)
|
)
|
||||||
|
|
||||||
history_text = _format_qa_history(qa_history)
|
history_text = _format_qa_history(qa_history)
|
||||||
|
if current_context:
|
||||||
|
history_text = f"## Current Context\n{current_context}\n\n{history_text}"
|
||||||
result = await agent.run(history_text)
|
result = await agent.run(history_text)
|
||||||
return result.output
|
return result.output
|
||||||
|
|
||||||
|
|
@ -49,7 +54,18 @@ async def update_session_context(
|
||||||
config: AppConfig for model selection.
|
config: AppConfig for model selection.
|
||||||
session_state: The session state to update.
|
session_state: The session state to update.
|
||||||
"""
|
"""
|
||||||
summary = await summarize_session(qa_history, config)
|
# Determine current context to incorporate:
|
||||||
|
# 1. If session_context already exists, use its summary
|
||||||
|
# 2. Otherwise, use background_context (if available)
|
||||||
|
current_context: str | None = None
|
||||||
|
if session_state.session_context and session_state.session_context.summary:
|
||||||
|
current_context = session_state.session_context.summary
|
||||||
|
elif session_state.background_context:
|
||||||
|
current_context = session_state.background_context
|
||||||
|
|
||||||
|
summary = await summarize_session(
|
||||||
|
qa_history, config, current_context=current_context
|
||||||
|
)
|
||||||
session_state.session_context = SessionContext(
|
session_state.session_context = SessionContext(
|
||||||
summary=summary,
|
summary=summary,
|
||||||
last_updated=datetime.now(),
|
last_updated=datetime.now(),
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,9 @@ You can optionally specify a limit parameter (default 5).
|
||||||
|
|
||||||
IMPORTANT: You must make actual tool calls. Do not output "run_search(...)" as text."""
|
IMPORTANT: You must make actual tool calls. Do not output "run_search(...)" as text."""
|
||||||
|
|
||||||
SESSION_SUMMARY_PROMPT = """You are a session summarizer. Given a conversation history of Q&A pairs, produce a structured summary that captures key information for future context.
|
SESSION_SUMMARY_PROMPT = """You are a session summarizer. Given a conversation history of Q&A pairs (and optionally existing context), produce a structured summary that captures key information for future context.
|
||||||
|
|
||||||
|
If a "Current Context" section is provided at the start of the input, incorporate that context into your summary. This might be initial background context from the user or a previous summary - build upon it rather than discard it.
|
||||||
|
|
||||||
Your summary should be concise (aim for 500-1500 tokens) and include:
|
Your summary should be concise (aim for 500-1500 tokens) and include:
|
||||||
|
|
||||||
|
|
@ -49,6 +51,7 @@ Your summary should be concise (aim for 500-1500 tokens) and include:
|
||||||
|
|
||||||
Rules:
|
Rules:
|
||||||
- Extract only high-signal information that would help answer follow-up questions
|
- Extract only high-signal information that would help answer follow-up questions
|
||||||
|
- When building on existing context, merge new information with prior context
|
||||||
- Omit small talk, greetings, or low-confidence answers
|
- Omit small talk, greetings, or low-confidence answers
|
||||||
- Use bullet points for clarity
|
- Use bullet points for clarity
|
||||||
- Keep technical details but compress verbose explanations
|
- Keep technical details but compress verbose explanations
|
||||||
|
|
|
||||||
|
|
@ -172,6 +172,37 @@ class TestSummarizeSession:
|
||||||
assert "key facts" in result_lower or "established" in result_lower
|
assert "key facts" in result_lower or "established" in result_lower
|
||||||
assert "documents" in result_lower or "sources" in result_lower
|
assert "documents" in result_lower or "sources" in result_lower
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.vcr()
|
||||||
|
async def test_summarize_session_with_current_context(self, allow_model_requests):
|
||||||
|
"""Test summarize_session incorporates current_context into the summary."""
|
||||||
|
from haiku.rag.agents.chat.context import summarize_session
|
||||||
|
|
||||||
|
qa_history = [
|
||||||
|
QAResponse(
|
||||||
|
question="What's the rate limit?",
|
||||||
|
answer="100 requests per minute.",
|
||||||
|
confidence=0.9,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
# Provide current_context (e.g., background_context or previous summary)
|
||||||
|
current_context = "Focus on Python APIs. User is building a web application."
|
||||||
|
|
||||||
|
result = await summarize_session(
|
||||||
|
qa_history=qa_history,
|
||||||
|
config=Config,
|
||||||
|
current_context=current_context,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Summary should be non-empty and ideally incorporate context about Python/web
|
||||||
|
assert len(result) > 0
|
||||||
|
# The context about "Python" or "web application" should influence the summary
|
||||||
|
result_lower = result.lower()
|
||||||
|
assert (
|
||||||
|
"rate" in result_lower or "limit" in result_lower or "100" in result_lower
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestUpdateSessionContext:
|
class TestUpdateSessionContext:
|
||||||
"""Tests for update_session_context function."""
|
"""Tests for update_session_context function."""
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
interactions:
|
||||||
|
- request:
|
||||||
|
headers:
|
||||||
|
accept:
|
||||||
|
- application/json
|
||||||
|
accept-encoding:
|
||||||
|
- gzip, deflate, zstd
|
||||||
|
connection:
|
||||||
|
- keep-alive
|
||||||
|
content-length:
|
||||||
|
- '1581'
|
||||||
|
content-type:
|
||||||
|
- application/json
|
||||||
|
host:
|
||||||
|
- localhost:11434
|
||||||
|
method: POST
|
||||||
|
parsed_body:
|
||||||
|
messages:
|
||||||
|
- content: |-
|
||||||
|
You are a session summarizer. Given a conversation history of Q&A pairs (and optionally existing context), produce a structured summary that captures key information for future context.
|
||||||
|
|
||||||
|
If a "Current Context" section is provided at the start of the input, incorporate that context into your summary. This might be initial background context from the user or a previous summary - build upon it rather than discard it.
|
||||||
|
|
||||||
|
Your summary should be concise (aim for 500-1500 tokens) and include:
|
||||||
|
|
||||||
|
1. **Key Facts Established** - Specific facts, data, or conclusions learned during the conversation
|
||||||
|
2. **Documents Referenced** - Documents or sources that were cited, with brief notes on what they contain
|
||||||
|
3. **Current Focus** - What topic or question thread the user is currently exploring
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
- Extract only high-signal information that would help answer follow-up questions
|
||||||
|
- When building on existing context, merge new information with prior context
|
||||||
|
- Omit small talk, greetings, or low-confidence answers
|
||||||
|
- Use bullet points for clarity
|
||||||
|
- Keep technical details but compress verbose explanations
|
||||||
|
- Preserve document names/titles when mentioned in sources
|
||||||
|
|
||||||
|
Output the summary directly in markdown format. Do not include meta-commentary about the summary itself.
|
||||||
|
role: system
|
||||||
|
- content: |
|
||||||
|
## Current Context
|
||||||
|
Focus on Python APIs. User is building a web application.
|
||||||
|
|
||||||
|
## Q1: What's the rate limit?
|
||||||
|
**Answer** (confidence: 90%):
|
||||||
|
100 requests per minute.
|
||||||
|
role: user
|
||||||
|
model: gpt-oss
|
||||||
|
reasoning_effort: low
|
||||||
|
stream: false
|
||||||
|
uri: http://localhost:11434/v1/chat/completions
|
||||||
|
response:
|
||||||
|
headers:
|
||||||
|
content-length:
|
||||||
|
- '682'
|
||||||
|
content-type:
|
||||||
|
- application/json
|
||||||
|
parsed_body:
|
||||||
|
choices:
|
||||||
|
- finish_reason: stop
|
||||||
|
index: 0
|
||||||
|
message:
|
||||||
|
content: |-
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
- **Key Facts Established**
|
||||||
|
- The user is building a web application and is focused on Python APIs.
|
||||||
|
- The relevant rate limit is **100 requests per minute** (confidence 90%).
|
||||||
|
|
||||||
|
- **Documents Referenced**
|
||||||
|
- None cited in this exchange.
|
||||||
|
|
||||||
|
- **Current Focus**
|
||||||
|
- Understanding and managing API rate limits for the Python-based web application.
|
||||||
|
reasoning: We need summary.
|
||||||
|
role: assistant
|
||||||
|
created: 1769164539
|
||||||
|
id: chatcmpl-369
|
||||||
|
model: gpt-oss
|
||||||
|
object: chat.completion
|
||||||
|
system_fingerprint: fp_ollama
|
||||||
|
usage:
|
||||||
|
completion_tokens: 91
|
||||||
|
prompt_tokens: 362
|
||||||
|
total_tokens: 453
|
||||||
|
status:
|
||||||
|
code: 200
|
||||||
|
message: OK
|
||||||
|
version: 1
|
||||||
Loading…
Reference in a new issue