forked from langchain-ai/langchain
-
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.
feat: add bedrock anthropic for token usage counting
feat: update fix: fix spell docs: remove unnecessary args from the pip install (langchain-ai#19823) **Description:** An additional `U` argument was added for the instructions to install the pip packages for the MediaWiki Dump Document loader which was leading to error in installing the package. Removing the argument fixed the command to install. **Issue:** langchain-ai#19820 **Dependencies:** No dependency change requierd **Twitter handle:** [@vardhaman722](https://twitter.com/vardhaman722) Update cross_encoder_reranker.ipynb (langchain-ai#19846) HuggingFace -> Hugging Face core: generate mermaid syntax and render visual graph (langchain-ai#19599) - **Description:** Add functionality to generate Mermaid syntax and render flowcharts from graph data. This includes support for custom node colors and edge curve styles, as well as the ability to export the generated graphs to PNG images using either the Mermaid.INK API or Pyppeteer for local rendering. - **Dependencies:** Optional dependencies are `pyppeteer` if rendering wants to be done using Pypeteer and Javascript code. --------- Co-authored-by: Angel Igareta <angel.igareta@klarna.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> feat: fix feat: update fix: fix lint fix: fix lint fix: fix lint ai21[patch]: release 0.1.3 (langchain-ai#19867) 👥 Update LangChain people data (langchain-ai#19858) 👥 Update LangChain people data Co-authored-by: github-actions <github-actions@github.com> community[patch]: Revert " Fix the bug that Chroma does not specify `e… (langchain-ai#19866) …mbedding_function` (langchain-ai#19277)" This reverts commit 7042934. Fixes langchain-ai#19848 fix: fix lint
- Loading branch information
Showing
13 changed files
with
1,530 additions
and
558 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
108 changes: 108 additions & 0 deletions
108
libs/community/langchain_community/callbacks/bedrock_anthropic_callback.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,108 @@ | ||
import threading | ||
from typing import Any, Dict, List, Union | ||
|
||
from langchain_core.callbacks import BaseCallbackHandler | ||
from langchain_core.outputs import LLMResult | ||
|
||
MODEL_COST_PER_1K_INPUT_TOKENS = { | ||
"anthropic.claude-instant-v1": 0.0008, | ||
"anthropic.claude-v2": 0.008, | ||
"anthropic.claude-v2:1": 0.008, | ||
"anthropic.claude-3-sonnet-20240229-v1:0": 0.003, | ||
"anthropic.claude-3-haiku-20240307-v1:0": 0.00025, | ||
} | ||
|
||
MODEL_COST_PER_1K_OUTPUT_TOKENS = { | ||
"anthropic.claude-instant-v1": 0.0024, | ||
"anthropic.claude-v2": 0.024, | ||
"anthropic.claude-v2:1": 0.024, | ||
"anthropic.claude-3-sonnet-20240229-v1:0": 0.015, | ||
"anthropic.claude-3-haiku-20240307-v1:0": 0.00125, | ||
} | ||
|
||
|
||
def _get_anthropic_claude_token_cost( | ||
prompt_tokens: int, completion_tokens: int, model_id: Union[str, None] | ||
) -> float: | ||
"""Get the cost of tokens for the Claude model.""" | ||
if not model_id: | ||
raise ValueError("Model name is required to calculate cost.") | ||
return (prompt_tokens / 1000) * MODEL_COST_PER_1K_INPUT_TOKENS[model_id] + ( | ||
completion_tokens / 1000 | ||
) * MODEL_COST_PER_1K_OUTPUT_TOKENS[model_id] | ||
|
||
|
||
class BedrockAnthropicTokenUsageCallbackHandler(BaseCallbackHandler): | ||
"""Callback Handler that tracks bedrock anthropic info.""" | ||
|
||
total_tokens: int = 0 | ||
prompt_tokens: int = 0 | ||
completion_tokens: int = 0 | ||
successful_requests: int = 0 | ||
total_cost: float = 0.0 | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self._lock = threading.Lock() | ||
|
||
def __repr__(self) -> str: | ||
return ( | ||
f"Tokens Used: {self.total_tokens}\n" | ||
f"\tPrompt Tokens: {self.prompt_tokens}\n" | ||
f"\tCompletion Tokens: {self.completion_tokens}\n" | ||
f"Successful Requests: {self.successful_requests}\n" | ||
f"Total Cost (USD): ${self.total_cost}" | ||
) | ||
|
||
@property | ||
def always_verbose(self) -> bool: | ||
"""Whether to call verbose callbacks even if verbose is False.""" | ||
return True | ||
|
||
def on_llm_start( | ||
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any | ||
) -> None: | ||
"""Print out the prompts.""" | ||
pass | ||
|
||
def on_llm_new_token(self, token: str, **kwargs: Any) -> None: | ||
"""Print out the token.""" | ||
pass | ||
|
||
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None: | ||
"""Collect token usage.""" | ||
if response.llm_output is None: | ||
return None | ||
|
||
if "usage" not in response.llm_output: | ||
with self._lock: | ||
self.successful_requests += 1 | ||
return None | ||
|
||
# compute tokens and cost for this request | ||
token_usage = response.llm_output["usage"] | ||
completion_tokens = token_usage.get("completion_tokens", 0) | ||
prompt_tokens = token_usage.get("prompt_tokens", 0) | ||
total_tokens = token_usage.get("total_tokens", 0) | ||
model_id = response.llm_output.get("model_id", None) | ||
total_cost = _get_anthropic_claude_token_cost( | ||
prompt_tokens=prompt_tokens, | ||
completion_tokens=completion_tokens, | ||
model_id=model_id, | ||
) | ||
|
||
# update shared state behind lock | ||
with self._lock: | ||
self.total_cost += total_cost | ||
self.total_tokens += total_tokens | ||
self.prompt_tokens += prompt_tokens | ||
self.completion_tokens += completion_tokens | ||
self.successful_requests += 1 | ||
|
||
def __copy__(self) -> "BedrockAnthropicTokenUsageCallbackHandler": | ||
"""Return a copy of the callback handler.""" | ||
return self | ||
|
||
def __deepcopy__(self, memo: Any) -> "BedrockAnthropicTokenUsageCallbackHandler": | ||
"""Return a deep copy of the callback handler.""" | ||
return self |
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
Oops, something went wrong.