Skip to content

Conversation

lusmoura
Copy link
Collaborator

@lusmoura lusmoura commented Jun 25, 2024

The tool calls were not used correctly, since the loop was not considering the tool calls generated between steps. This PR moves the multistep loop so that all tool calls are used.

AI Description

The PR makes changes to the chat functionality, specifically to the handling of tool calls and the chat stream.

src/backend/chat/custom/custom.py

  • The chat function has been modified to remove the should_break variable and associated logic. Now, the loop iterates until there are no new tool calls.
  • The call_chat function has been updated to include a loop that invokes the chat stream and checks for new tool calls. If there are no new tool calls, the loop exits.
  • The has_new_tool_calls function has been added to check if the last element in the chat_history list has tool calls.
  • The update_chat_history_with_tool_results function has been introduced to update the chat history with tool results.
  • The call_tools function has been modified to check for the presence of "tool_calls" in the last element of the chat_history list instead of checking for tool_results.

src/backend/schemas/chat.py

  • The ChatMessage class now includes a default=None value for the message field.
  • The to_dict method of the ChatMessage class has been updated to include "tool_results" and "tool_calls" in the returned dictionary.
  • The StreamEnd class now includes a chat_history field with a default value of None.
  • The finish_reason field of the StreamEnd class has been updated to include an extra set of parentheses.
  • The error field has been added to the StreamEnd class with a default value of None.

src/backend/services/chat.py

  • The to_dict function has been imported from backend.chat.collate.
  • The handle_stream_end function now includes the chat history in the stream_end_data dictionary by converting the event to a dictionary and accessing the "response" and "chat_history" fields.

@codecov-commenter
Copy link

codecov-commenter commented Jun 25, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 26 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@bf09008). Learn more about missing BASE report.

Current head 8c18fd6 differs from pull request most recent head 7596108

Please upload reports for the commit 7596108 to get more accurate results.

Files Patch % Lines
src/backend/chat/custom/custom.py 57.89% 16 Missing ⚠️
src/backend/services/chat.py 66.66% 5 Missing ⚠️
src/backend/alembic/versions/038075cd8530_.py 80.00% 3 Missing ⚠️
src/backend/alembic/versions/06687ce9ea3b_.py 90.90% 1 Missing ⚠️
src/backend/schemas/chat.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #276   +/-   ##
=======================================
  Coverage        ?   85.35%           
=======================================
  Files           ?      162           
  Lines           ?     5932           
  Branches        ?        0           
=======================================
  Hits            ?     5063           
  Misses          ?      869           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* Chat: persist events

* fix tests

* fix files

* fix tests

* add tool plan to mesasge

* add ''''feature flag''''
@lusmoura lusmoura merged commit ae7b178 into main Jun 25, 2024
@lusmoura lusmoura deleted the luisa/persist_events branch June 25, 2024 18:22
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.

4 participants