-
Notifications
You must be signed in to change notification settings - Fork 135
Added support to get usage for ChatGoogleGenerativeAI #893
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
base: main
Are you sure you want to change the base?
Conversation
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.
Disclaimer: Experimental PR review
PR Summary
Added support for tracking usage of ChatGoogleGenerativeAI's langchain calls in the Langfuse Python SDK.
- Introduced check for 'usage_metadata' attribute in message chunks in
langfuse/callback/langchain.py
- Implemented parsing of 'usage_metadata' using
_parse_usage_model
function - Enhanced usage tracking capabilities for Google's Generative AI in addition to existing LLM providers
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
@NeerajG03 did you test this locally? Thanks a lot for the contribution! |
@maxdeichmann I'm sorry i'm very new to contributing for projects. |
This also worked for me for ChatVertexAI. It appears to fix my issues which appear to be those mentioned in langfuse/langfuse#5468 |
@@ -1139,6 +1139,11 @@ def _parse_usage(response: LLMResult): | |||
break |
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.
I would suggest adding
break | |
if llm_usage: | |
break |
or even better modify the previous condition to:
if generation_chunk.generation_info and (
generation_chunk.generation_info.get("usage_metadata", None)
):
as in my testing with ChatVertexAI generation_info
contains empty usage_metadata
while message
contains valid one.
|
||
if hasattr(message_chunk, "usage_metadata") and message_chunk.usage_metadata is not None: #for ChatGoogleGenerativeAI | ||
llm_usage = _parse_usage_model(message_chunk.usage_metadata) | ||
break | ||
|
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.
Looking at it better, this should have no effect, as a few lines down, there is:
or getattr(message_chunk, "usage_metadata", None) # for Ollama
This PR has been raised to add support to track
ChatGoogleGenerativeAI
's langchain call usage.A simple check for usage_metadata attribute and then parsing using the
_parse_usage_model