-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: pass slack thread context to llm as message_history #5475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: pass slack thread context to llm as message_history #5475
Conversation
@brijsiyag-meesho is attempting to deploy a commit to the Danswer Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR adds a message history parameter with token counting functionality for Slack integration. The main changes include:
- Enhanced
stream_chat_message_objects()
- Added optionalmessage_history
parameter that allows passing pre-calculatedPreviousMessage
objects with accurate token counts - Improved Slack token counting - Updated Slack handler to calculate token counts for each message in the thread history using the LLM's tokenizer before creating
PreviousMessage
objects - Better performance - By pre-calculating token counts in the Slack handler, this avoids duplicate tokenization work and provides more accurate token counting for Slack thread contexts
The implementation maintains backward compatibility through proper null checking and fallback logic. When message_history
is not provided, the system falls back to the existing behavior of creating message history from history_msgs
.
Confidence Score: 5/5
- This PR is safe to merge with minimal risk
- This is a straightforward enhancement that adds optional token counting functionality. The changes maintain backward compatibility through proper null checking, use appropriate type annotations, and follow the existing codebase patterns. No breaking changes or risky modifications.
- No files require special attention
Important Files Changed
File Analysis
Filename | Score | Overview |
---|---|---|
backend/onyx/chat/process_message.py | 5/5 | Added optional message_history parameter with proper type annotation and fallback logic |
backend/onyx/onyxbot/slack/handlers/handle_regular_answer.py | 5/5 | Added proper token counting for message history and pass calculated message_history to stream_chat_message_objects |
Sequence Diagram
sequenceDiagram
participant SlackHandler as Slack Handler
participant TokenCount as Token Counter
participant ProcessMsg as Process Message
participant PreviousMsg as PreviousMessage
SlackHandler->>+TokenCount: check_number_of_tokens(message, tokenizer)
TokenCount-->>-SlackHandler: token_count
SlackHandler->>+PreviousMsg: PreviousMessage(message, role, token_count, files=[], tool_call=None, ...)
PreviousMsg-->>-SlackHandler: message_history_item
SlackHandler->>+ProcessMsg: stream_chat_message_objects(message_history=message_history)
ProcessMsg->>ProcessMsg: Check if message_history is None
alt message_history is provided
ProcessMsg->>ProcessMsg: Use provided message_history with token counts
else message_history is None
ProcessMsg->>ProcessMsg: Create message_history from history_msgs using PreviousMessage.from_chat_message()
end
ProcessMsg-->>-SlackHandler: answer_stream
2 files reviewed, no comments
@Weves currently this PR contains some patchy solutions, if team thinks we can proceed with this than I will polish the code further. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 2 files
Prompt for AI agents (all 1 issues)
Understand the root cause of the following 1 issues and fix them.
<file name="backend/onyx/chat/process_message.py">
<violation number="1" location="backend/onyx/chat/process_message.py:256">
Comment above this line is now misleading (describes a string). Update/move it to document single_message_history and add a brief comment for message_history.</violation>
</file>
React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai
to give feedback, ask questions, or re-run the review.
# a string which represents the history of a conversation. Used in cases like | ||
# Slack threads where the conversation cannot be represented by a chain of User/Assistant | ||
# messages. | ||
message_history: List[PreviousMessage] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment above this line is now misleading (describes a string). Update/move it to document single_message_history and add a brief comment for message_history.
Prompt for AI agents
Address the following comment on backend/onyx/chat/process_message.py at line 256:
<comment>Comment above this line is now misleading (describes a string). Update/move it to document single_message_history and add a brief comment for message_history.</comment>
<file context>
@@ -253,6 +253,7 @@ def stream_chat_message_objects(
# a string which represents the history of a conversation. Used in cases like
# Slack threads where the conversation cannot be represented by a chain of User/Assistant
# messages.
+ message_history: List[PreviousMessage] | None = None,
# NOTE: is not stored in the database at all.
single_message_history: str | None = None,
</file context>
544f3ad
to
b4b96ac
Compare
b4b96ac
to
f6d38c2
Compare
Description
Previously, Slack integration was using single_message_history but wasn't passing this to LLM for previous context.
This solution just passes all thread messages as message_history so LLM can get context of the thread.
Also we are not storing anything at onyx side so it shouldn't be issue in terms of slack
How Has This Been Tested?
Manually
Backporting (check the box to trigger backport action)
Note: You have to check that the action passes, otherwise resolve the conflicts manually and tag the patches.
Summary by cubic
Added per-message history with token counting for Slack threads to improve context management and prevent token overflow in long conversations.