-
Notifications
You must be signed in to change notification settings - Fork 82
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
Bedrock token count callbacks #20
Bedrock token count callbacks #20
Conversation
on_llm_end, invoke will provide the usage and stop_reason information
…on_chunk Also allowed additional response_body information to be passed into the generation_info (such as token counts and stop reasons)
The on_llm_end call will include all GenerationChunks as well as info on usage and stop_reason (in the llm_output) Added a provider stop code mapping
…sages api and outputs
…from the correct dict
…s api Need the `message_delta` to return in order to get the stop reason and output token count https://docs.anthropic.com/claude/reference/messages-streaming
…ncluding the stop_reason rather than just usage_info Removing the on_llm_end that was added and moving to _call to match with the pattern used for the ChatModel (as this was causing on_llm_end to be called twice)
…nfo rather than the full chunks to make it compatible with some calls needed from ChatBedrock Updating the usage info to use prompt_tokens and completion_tokens to match those returned from the non streaming route
…being cleared from the llm_output This was returning llm_outputs with no usage information
…reaming and non-streaming generation and passing into the ChatResult
@NAPTlME |
@3coins Apologies, I'm on Windows, but manually running Ruff and the unit tests (rather than via make). |
These appear to be holdovers from the langchain repo's structure and default branch
@3coins The only item I was unable to run was the integration test (I assume due to what I have provisioned using my AWS credentials). Thanks. |
@3coins |
@3coins |
@3coins |
Hello, thanks for the suggestion. This could be useful to have in the package. |
@DanielWhite95 That said, if you wanted to increment, this is just in the callback example so you would modify the callback handler to do so
|
@3coins is your man! |
@NAPTlME Can you do one last update and convert your sample code into an integration/unit test? |
@NAPTlME tests/integration_tests/chat_models/test_bedrock.py ....FFF.......FF(base) ➜ aws git:(bedrock-token-count-callbacks) poetry run pytest tests/integration_tests/chat_models/test_bedrock.py tests/integration_tests/chat_models/test_bedrock.py ....FFF.......FF [100%] ============================================================================================== FAILURES ==============================================================================================
tests/integration_tests/chat_models/test_bedrock.py:97: /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:249: in stream left = {'input_tokens': 10, 'output_tokens': 1}, right = {'output_tokens': 6}
E TypeError: Additional kwargs key output_tokens already exists in left dict and value has unsupported type <class 'int'>. /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/utils/_merge.py:40: TypeError chat = ChatBedrock(client=<botocore.client.BedrockRuntime object at 0x126949430>, region_name='us-west-2', model_id='anthropic.claude-v2', model_kwargs={'temperature': 0})
tests/integration_tests/chat_models/test_bedrock.py:109: /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/messages/ai.py:145: in add left = {'input_tokens': 13, 'output_tokens': 1}, right = {'output_tokens': 46}
E TypeError: Additional kwargs key output_tokens already exists in left dict and value has unsupported type <class 'int'>. /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/utils/_merge.py:40: TypeError chat = ChatBedrock(client=<botocore.client.BedrockRuntime object at 0x1321a7160>, region_name='us-west-2', model_id='anthropic.claude-v2', model_kwargs={'temperature': 0})
tests/integration_tests/chat_models/test_bedrock.py:118: /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:319: in astream left = {'input_tokens': 13, 'output_tokens': 1}, right = {'output_tokens': 46}
E TypeError: Additional kwargs key output_tokens already exists in left dict and value has unsupported type <class 'int'>. /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/utils/_merge.py:40: TypeError chat = ChatBedrock(client=<botocore.client.BedrockRuntime object at 0x126c30c40>, region_name='us-west-2', model_id='anthropi...ing\nThe city and state\n\n\n</tool_description>\n")
tests/integration_tests/chat_models/test_bedrock.py:207: /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:249: in stream left = {'input_tokens': 205, 'output_tokens': 1}, right = {'output_tokens': 115}
E TypeError: Additional kwargs key output_tokens already exists in left dict and value has unsupported type <class 'int'>. /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/utils/_merge.py:40: TypeError chat = ChatBedrock(client=<botocore.client.BedrockRuntime object at 0x126f0d940>, region_name='us-west-2', model_id='anthropi...ing\nThe city and state\n\n\n</tool_description>\n")
tests/integration_tests/chat_models/test_bedrock.py:220: /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:249: in stream left = {'input_tokens': 197, 'output_tokens': 1}, right = {'output_tokens': 183}
E TypeError: Additional kwargs key output_tokens already exists in left dict and value has unsupported type <class 'int'>. /Users/pijain/Library/Caches/pypoetry/virtualenvs/langchain-aws-eH7P7gjZ-py3.9/lib/python3.9/site-packages/langchain_core/utils/_merge.py:40: TypeError -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ---------- coverage: platform darwin, python 3.9.13-final-0 ----------
|
…ptured on_llm_end.
…hese integer values to prevent errors when merge_dicts is called from langchain_core.utils.
…PTlME/langchain-aws into bedrock-token-count-callbacks
… lists to the message_start message type.
@3coins It looks like merge_dicts doesn't allow for int types. To get around this, I am now putting "usage" token counts into lists and summing them when combining. Feel free to kick off the workflow. I believe everything is good. Let me know if there are any further areas you feel need to be modified. |
…ion to reduce the code duplication.
…nfo` test would occasionally fail (returned "Hello! How are you doing?" one time)
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.
@NAPTlME
Looks good! Thanks for your patience.
Updated both the BedrockLLM and ChatBedrock classes to yield token counts and stop reasons upon generation/call. This works for streaming/non-streaming as well as messages vs raw text.
The goal behind this is to take the input/output tokens and stop reasons directly from the Bedrock call and use them in a CallbackHandler on_llm_end.
Example use