forked from jupyterlab/jupyter-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
metadata
field to agent messages (jupyterlab#1013)
* add `metadata` field to agent messages Currently only set when the default chat handler is used. * include message metadata in TestLLMWithStreaming * tweak TestLLMWithStreaming parameters * pre-commit * default to empty dict if no generation_info
- Loading branch information
Showing
9 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
Provides classes which extend `langchain_core.callbacks:BaseCallbackHandler`. | ||
Not to be confused with Jupyter AI chat handlers. | ||
""" | ||
|
||
from .metadata import MetadataCallbackHandler |
26 changes: 26 additions & 0 deletions
26
packages/jupyter-ai/jupyter_ai/callback_handlers/metadata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from langchain_core.callbacks import BaseCallbackHandler | ||
from langchain_core.outputs import LLMResult | ||
|
||
|
||
class MetadataCallbackHandler(BaseCallbackHandler): | ||
""" | ||
When passed as a callback handler, this stores the LLMResult's | ||
`generation_info` dictionary in the `self.jai_metadata` instance attribute | ||
after the provider fully processes an input. | ||
If used in a streaming chat handler: the `metadata` field of the final | ||
`AgentStreamChunkMessage` should be set to `self.jai_metadata`. | ||
If used in a non-streaming chat handler: the `metadata` field of the | ||
returned `AgentChatMessage` should be set to `self.jai_metadata`. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.jai_metadata = {} | ||
|
||
def on_llm_end(self, response: LLMResult, **kwargs) -> None: | ||
if not (len(response.generations) and len(response.generations[0])): | ||
return | ||
|
||
self.jai_metadata = response.generations[0][0].generation_info or {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters