Add metadata
field to agent messages
#1013
Merged
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.
Description
Adds a
metadata
field to the agent message model, which should stores message metadata returned by the LLM provider. In LangChain, all providers have the option to return ageneration_info
dictionary when yielding one or moreGeneration
objects while replying to a prompt. This PR makes that dictionary accessible via themetadata
attribute in both the frontend & backend models for agent messages.This dictionary may contain info about why an LLM finished generating a response (
stop_reason
) or append provider-specific data like asession_id
. Some advanced custom use-cases like implementing thumbs up/down feedback in a custom message footer require this data to include in telemetry events.In this implementation, this field is only set by
DefaultChatHandler
. For the time being, custom chat handlers have to mimic the implementation in this PR to include metadata in the returned message. In the future, themetadata
field will be set automatically byBaseChatHandler
so other chat handlers do not need to duplicate the implementation in their code. However, this requires refactoringDefaultChatHandler
to migrate the streaming logic toBaseChatHandler
.Demo
Screen.Recording.2024-09-23.at.3.37.51.PM.mov
Testing
To replicate the above demo:
packages/jupyter-ai/src/components/chat-messages.tsx
.ChatMessageHeader
's definition to log the message metadata object:jlpm build
and restart JupyterLab.test-provider-with-streaming:test
model provided byjupyter_ai_test
(installable viajlpm dev-uninstall && jlpm dev-install
if not already installed). It should always set the message metadata to{"test_metadata_field":"foobar"}
.