Skip to content

Add conversation memory for multi-turn questions#14

Merged
eugen-goebel merged 3 commits into
mainfrom
feature/conversation-memory
May 24, 2026
Merged

Add conversation memory for multi-turn questions#14
eugen-goebel merged 3 commits into
mainfrom
feature/conversation-memory

Conversation

@eugen-goebel
Copy link
Copy Markdown
Owner

@eugen-goebel eugen-goebel commented May 24, 2026

Summary

  • QAAgent.ask now takes an optional conversation_history of {"role": "user"|"assistant", "content": str} dicts
  • History is filtered (drops invalid roles / missing content), trimmed to the last max_history_turns exchanges (default 10), and prepended to the Anthropic messages array
  • Streamlit forwards st.session_state.messages (minus the new turn) so follow-ups like "and the year before?" resolve against prior context
  • Demo mode is unchanged, accepts the parameter but ignores it
  • 5 new tests bring the suite from 65 to 70

Closes #13

Test plan

  • All 70 backend tests pass (pytest -q)
  • Existing single-turn flow unchanged (one user message in API call when no history)
  • Manual: ask "What was revenue?" then "and the year before?": the second answer references the first

QAAgent.ask now takes an optional conversation_history list of
{"role": "user"|"assistant", "content": str} dicts. When provided, the
past turns are filtered, trimmed to the last max_history_turns
exchanges (default 10), and prepended to the messages array sent to
the Anthropic API. The retrieval and the new question keep their
context-injected formatting on the latest user turn.

Demo mode is unchanged: history only matters when an LLM is actually
called.

Refs #13
st.session_state.messages already kept the full conversation for
rendering. Now it is also passed to QAAgent.ask via the new
conversation_history parameter, so the assistant can resolve
follow-up questions like "and the year before?" against the prior
turns. The last message (the user's just-asked question) is excluded
to avoid duplication.

Refs #13
Five new tests cover:
- No history → still sends exactly one user message (no regression)
- History is forwarded in the right order with the new question last
- max_history_turns trims the oldest exchanges (last N pairs kept)
- Unexpected roles, missing content, and non-dict items are filtered
- Demo mode accepts conversation_history but ignores it (no API call)

Closes #13
@eugen-goebel eugen-goebel merged commit 78a474b into main May 24, 2026
3 checks passed
@eugen-goebel eugen-goebel deleted the feature/conversation-memory branch May 24, 2026 02:40
eugen-goebel added a commit that referenced this pull request Jun 2, 2026
Add conversation memory for multi-turn questions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add conversation memory for multi-turn questions

1 participant