generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 224
Add: Google Gemini Callback #314
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
Open
tykimseoul
wants to merge
6
commits into
langchain-ai:main
Choose a base branch
from
tykimseoul:gemini-callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+479
−0
Conversation
This file contains hidden or 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
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a
GeminiCallbackHandler
, a new callback handler for monitoring token usage and costs associated with Google's Gemini models. It provides a straightforward way to track token consumption and estimate expenses when using the Gemini API, helping you stay on top of your spending without any extra hassle.Motivation
I wanted a way to track my token usage and costs when working with Gemini models. I saw that a callback already existed for OpenAI, which was super helpful, and I thought it would be great to have a similar one for Gemini. My goal was to create a simple tool to help developers get visibility into their API usage and manage costs effectively.
Key Changes
langchain_community/callbacks/gemini_info.py
: This new file contains theGeminiCallbackHandler
class, which is responsible for calculating token usage (prompt, completion, and total) and estimating costs based on the specific Gemini model being used. The file also includes a comprehensive dictionary of Gemini model costs.langchain_community/callbacks/__init__.py
: TheGeminiCallbackHandler
has been added to the__all__
list, making it easily accessible for import within the LangChain ecosystem.langchain_community/callbacks/manager.py
: A new context manager,get_gemini_callback
, has been introduced to provide a convenient and user-friendly way to utilize theGeminiCallbackHandler
.libs/community/tests/unit_tests/callbacks/test_gemini_info.py
: A suite of unit tests has been added to ensure the proper functioning of theGeminiCallbackHandler
. These tests cover token counting, cost calculation for various models, and the handling of different response formats.