backend: Filter out invalid message text when generating chat history #862
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thanks @EugeneLightsOn for pointing out fix area
Right now if a chat stream fails after creating the initial Chatbot response message, it creates an empty chat in chat_history, which the chat model does not accept. This filters them out.
Ideally we go back and delete those previous messages, but I didn't want to introduce potentially janky logic.
AI Description
This PR introduces several changes to the chat functionality, enhancing the user experience and improving the backend logic.
Summary
The
create_chat_history
function insrc/backend/services/chat.py
now filters out the user's most recent message and any empty messages when generating chat history. This ensures that only relevant messages are included in the history, providing a more accurate context for the chat SDK call.Additionally, a new function
is_final_event
is added tosrc/backend/chat/custom/custom.py
, which appears to be related to event handling. However, the purpose of this function is not entirely clear from the provided code snippet.Changes
create_chat_history
function now filters out the user's most recent message and any empty messages.is_final_event
is introduced, which seems to be related to event handling._call_tool_async
is added, but its purpose is not evident from the provided code.