mirror of
https://github.com/hwchase17/langchain.git
synced 2026-01-23 13:19:22 +00:00
Compare commits
25 Commits
cc/thropic
...
cc/tool_at
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36f5bfb552 | ||
|
|
29ba5fd1f1 | ||
|
|
62a5f993f2 | ||
|
|
db7b518308 | ||
|
|
e4bfc84d6e | ||
|
|
a4857bf09b | ||
|
|
7de02c5997 | ||
|
|
76971094ab | ||
|
|
42042abd82 | ||
|
|
668e4c68ec | ||
|
|
077199c5de | ||
|
|
826040f8b8 | ||
|
|
1bf1ab7986 | ||
|
|
c8d96ad346 | ||
|
|
8662fd8c7d | ||
|
|
a72e9d14f0 | ||
|
|
7f5c21dc0c | ||
|
|
ffb26b3298 | ||
|
|
84b4ea2198 | ||
|
|
c54b676a04 | ||
|
|
a3350f4174 | ||
|
|
5fc4ec6bc9 | ||
|
|
caed4e4ce8 | ||
|
|
ff2ef48b35 | ||
|
|
b2e8df4cea |
1
.github/scripts/check_diff.py
vendored
1
.github/scripts/check_diff.py
vendored
@@ -37,7 +37,6 @@ IGNORED_PARTNERS = [
|
||||
PY_312_MAX_PACKAGES = [
|
||||
f"libs/partners/{integration}"
|
||||
for integration in [
|
||||
"anthropic",
|
||||
"chroma",
|
||||
"couchbase",
|
||||
"huggingface",
|
||||
|
||||
@@ -5,10 +5,22 @@ from __future__ import annotations
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional, Set
|
||||
import warnings
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Callable,
|
||||
Dict,
|
||||
Optional,
|
||||
Sequence,
|
||||
Set,
|
||||
Type,
|
||||
Union,
|
||||
)
|
||||
|
||||
import requests
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_core.tools import BaseTool
|
||||
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
|
||||
from pydantic import Field, SecretStr, model_validator
|
||||
|
||||
@@ -197,10 +209,18 @@ class ChatAnyscale(ChatOpenAI):
|
||||
encoding = tiktoken_.get_encoding(model)
|
||||
return model, encoding
|
||||
|
||||
def get_num_tokens_from_messages(self, messages: list[BaseMessage]) -> int:
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: list[BaseMessage],
|
||||
tools: Optional[
|
||||
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
|
||||
] = None,
|
||||
) -> int:
|
||||
"""Calculate num tokens with tiktoken package.
|
||||
Official documentation: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
|
||||
"""
|
||||
if tools is not None:
|
||||
warnings.warn("Counting tokens in tool schemas is not yet supported.")
|
||||
if sys.version_info[1] <= 7:
|
||||
return super().get_num_tokens_from_messages(messages)
|
||||
model, encoding = self._get_encoding_model()
|
||||
|
||||
@@ -4,9 +4,21 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional, Set
|
||||
import warnings
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Callable,
|
||||
Dict,
|
||||
Optional,
|
||||
Sequence,
|
||||
Set,
|
||||
Type,
|
||||
Union,
|
||||
)
|
||||
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_core.tools import BaseTool
|
||||
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
|
||||
from pydantic import Field, model_validator
|
||||
|
||||
@@ -138,11 +150,19 @@ class ChatEverlyAI(ChatOpenAI):
|
||||
encoding = tiktoken_.get_encoding(model)
|
||||
return model, encoding
|
||||
|
||||
def get_num_tokens_from_messages(self, messages: list[BaseMessage]) -> int:
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: list[BaseMessage],
|
||||
tools: Optional[
|
||||
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
|
||||
] = None,
|
||||
) -> int:
|
||||
"""Calculate num tokens with tiktoken package.
|
||||
|
||||
Official documentation: https://github.com/openai/openai-cookbook/blob/
|
||||
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
|
||||
if tools is not None:
|
||||
warnings.warn("Counting tokens in tool schemas is not yet supported.")
|
||||
if sys.version_info[1] <= 7:
|
||||
return super().get_num_tokens_from_messages(messages)
|
||||
model, encoding = self._get_encoding_model()
|
||||
|
||||
@@ -46,6 +46,7 @@ from langchain_core.messages import (
|
||||
)
|
||||
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
||||
from langchain_core.runnables import Runnable
|
||||
from langchain_core.tools import BaseTool
|
||||
from langchain_core.utils import (
|
||||
get_from_dict_or_env,
|
||||
get_pydantic_field_names,
|
||||
@@ -644,11 +645,19 @@ class ChatOpenAI(BaseChatModel):
|
||||
_, encoding_model = self._get_encoding_model()
|
||||
return encoding_model.encode(text)
|
||||
|
||||
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: List[BaseMessage],
|
||||
tools: Optional[
|
||||
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
|
||||
] = None,
|
||||
) -> int:
|
||||
"""Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
|
||||
|
||||
Official documentation: https://github.com/openai/openai-cookbook/blob/
|
||||
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
|
||||
if tools is not None:
|
||||
warnings.warn("Counting tokens in tool schemas is not yet supported.")
|
||||
if sys.version_info[1] <= 7:
|
||||
return super().get_num_tokens_from_messages(messages)
|
||||
model, encoding = self._get_encoding_model()
|
||||
|
||||
@@ -364,13 +364,22 @@ class BaseLanguageModel(
|
||||
"""
|
||||
return len(self.get_token_ids(text))
|
||||
|
||||
def get_num_tokens_from_messages(self, messages: list[BaseMessage]) -> int:
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: list[BaseMessage],
|
||||
tools: Optional[Sequence] = None,
|
||||
) -> int:
|
||||
"""Get the number of tokens in the messages.
|
||||
|
||||
Useful for checking if an input fits in a model's context window.
|
||||
|
||||
**Note**: the base implementation of get_num_tokens_from_messages ignores
|
||||
tool schemas.
|
||||
|
||||
Args:
|
||||
messages: The message inputs to tokenize.
|
||||
tools: If provided, sequence of dict, BaseModel, function, or BaseTools
|
||||
to be converted to tool schemas.
|
||||
|
||||
Returns:
|
||||
The sum of the number of tokens across the messages.
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import base64
|
||||
import json
|
||||
import typing
|
||||
from collections.abc import Sequence
|
||||
from typing import Any, Callable, Optional, Union
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -19,6 +22,7 @@ from langchain_core.messages.utils import (
|
||||
merge_message_runs,
|
||||
trim_messages,
|
||||
)
|
||||
from langchain_core.tools import BaseTool
|
||||
|
||||
|
||||
@pytest.mark.parametrize("msg_cls", [HumanMessage, AIMessage, SystemMessage])
|
||||
@@ -431,7 +435,15 @@ def dummy_token_counter(messages: list[BaseMessage]) -> int:
|
||||
|
||||
|
||||
class FakeTokenCountingModel(FakeChatModel):
|
||||
def get_num_tokens_from_messages(self, messages: list[BaseMessage]) -> int:
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: list[BaseMessage],
|
||||
tools: Optional[
|
||||
Sequence[
|
||||
Union[typing.Dict[str, Any], type, Callable, BaseTool] # noqa: UP006
|
||||
]
|
||||
] = None,
|
||||
) -> int:
|
||||
return dummy_token_counter(messages)
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ from unittest import mock
|
||||
|
||||
import pytest
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
from langchain_core.messages import HumanMessage
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain_core.runnables import RunnableConfig, RunnableSequence
|
||||
from pydantic import SecretStr
|
||||
@@ -180,9 +179,6 @@ def test_configurable_with_default() -> None:
|
||||
)
|
||||
|
||||
assert model_with_config.model == "claude-3-sonnet-20240229" # type: ignore[attr-defined]
|
||||
# Anthropic defaults to using `transformers` for token counting.
|
||||
with pytest.raises(ImportError):
|
||||
model_with_config.get_num_tokens_from_messages([(HumanMessage("foo"))]) # type: ignore[attr-defined]
|
||||
|
||||
assert model_with_config.model_dump() == { # type: ignore[attr-defined]
|
||||
"name": None,
|
||||
|
||||
@@ -21,7 +21,7 @@ from typing import (
|
||||
)
|
||||
|
||||
import anthropic
|
||||
from langchain_core._api import deprecated
|
||||
from langchain_core._api import beta, deprecated
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForLLMRun,
|
||||
CallbackManagerForLLMRun,
|
||||
@@ -1113,6 +1113,41 @@ class ChatAnthropic(BaseChatModel):
|
||||
else:
|
||||
return llm | output_parser
|
||||
|
||||
@beta()
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: List[BaseMessage],
|
||||
tools: Optional[
|
||||
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
|
||||
] = None,
|
||||
) -> int:
|
||||
"""Count tokens in a sequence of input messages.
|
||||
|
||||
Args:
|
||||
messages: The message inputs to tokenize.
|
||||
tools: If provided, sequence of dict, BaseModel, function, or BaseTools
|
||||
to be converted to tool schemas.
|
||||
|
||||
.. versionchanged:: 0.2.5
|
||||
|
||||
Uses Anthropic's token counting API to count tokens in messages. See:
|
||||
https://docs.anthropic.com/en/docs/build-with-claude/token-counting
|
||||
"""
|
||||
formatted_system, formatted_messages = _format_messages(messages)
|
||||
kwargs: Dict[str, Any] = {}
|
||||
if isinstance(formatted_system, str):
|
||||
kwargs["system"] = formatted_system
|
||||
if tools:
|
||||
kwargs["tools"] = [convert_to_anthropic_tool(tool) for tool in tools]
|
||||
|
||||
response = self._client.beta.messages.count_tokens(
|
||||
betas=["token-counting-2024-11-01"],
|
||||
model=self.model,
|
||||
messages=formatted_messages, # type: ignore[arg-type]
|
||||
**kwargs,
|
||||
)
|
||||
return response.input_tokens
|
||||
|
||||
|
||||
class AnthropicTool(TypedDict):
|
||||
"""Anthropic tool definition."""
|
||||
|
||||
@@ -109,7 +109,6 @@ class _AnthropicCommon(BaseLanguageModel):
|
||||
)
|
||||
self.HUMAN_PROMPT = anthropic.HUMAN_PROMPT
|
||||
self.AI_PROMPT = anthropic.AI_PROMPT
|
||||
self.count_tokens = self.client.count_tokens
|
||||
return self
|
||||
|
||||
@property
|
||||
@@ -375,9 +374,11 @@ class AnthropicLLM(LLM, _AnthropicCommon):
|
||||
|
||||
def get_num_tokens(self, text: str) -> int:
|
||||
"""Calculate number of tokens."""
|
||||
if not self.count_tokens:
|
||||
raise NameError("Please ensure the anthropic package is loaded")
|
||||
return self.count_tokens(text)
|
||||
raise NotImplementedError(
|
||||
"Anthropic's legacy count_tokens method was removed in anthropic 0.39.0 "
|
||||
"and langchain-anthropic 0.2.5. Please use "
|
||||
"ChatAnthropic.get_num_tokens_from_messages instead."
|
||||
)
|
||||
|
||||
|
||||
@deprecated(since="0.1.0", removal="0.3.0", alternative="AnthropicLLM")
|
||||
|
||||
843
libs/partners/anthropic/poetry.lock
generated
843
libs/partners/anthropic/poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry]
|
||||
name = "langchain-anthropic"
|
||||
version = "0.2.4"
|
||||
version = "0.3.0"
|
||||
description = "An integration package connecting AnthropicMessages and LangChain"
|
||||
authors = []
|
||||
readme = "README.md"
|
||||
@@ -20,7 +20,7 @@ disallow_untyped_defs = "True"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.9,<4.0"
|
||||
anthropic = ">=0.30.0,<1"
|
||||
anthropic = ">=0.39.0,<1"
|
||||
langchain-core = "^0.3.15"
|
||||
pydantic = "^2.7.4"
|
||||
|
||||
|
||||
@@ -317,7 +317,7 @@ async def test_anthropic_async_streaming_callback() -> None:
|
||||
def test_anthropic_multimodal() -> None:
|
||||
"""Test that multimodal inputs are handled correctly."""
|
||||
chat = ChatAnthropic(model=MODEL_NAME) # type: ignore[call-arg]
|
||||
messages = [
|
||||
messages: list[BaseMessage] = [
|
||||
HumanMessage(
|
||||
content=[
|
||||
{
|
||||
@@ -334,6 +334,9 @@ def test_anthropic_multimodal() -> None:
|
||||
response = chat.invoke(messages)
|
||||
assert isinstance(response, AIMessage)
|
||||
assert isinstance(response.content, str)
|
||||
num_tokens = chat.get_num_tokens_from_messages(messages)
|
||||
assert num_tokens > 0
|
||||
import pdb; pdb.set_trace()
|
||||
|
||||
|
||||
def test_streaming() -> None:
|
||||
@@ -505,6 +508,60 @@ def test_with_structured_output() -> None:
|
||||
assert response["location"]
|
||||
|
||||
|
||||
def test_get_num_tokens_from_messages() -> None:
|
||||
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022") # type: ignore[call-arg]
|
||||
|
||||
# Test simple case
|
||||
messages = [
|
||||
SystemMessage(content="You are a scientist"),
|
||||
HumanMessage(content="Hello, Claude"),
|
||||
]
|
||||
num_tokens = llm.get_num_tokens_from_messages(messages)
|
||||
assert num_tokens > 0
|
||||
|
||||
# Test tool use
|
||||
@tool(parse_docstring=True)
|
||||
def get_weather(location: str) -> str:
|
||||
"""Get the current weather in a given location
|
||||
|
||||
Args:
|
||||
location: The city and state, e.g. San Francisco, CA
|
||||
"""
|
||||
return "Sunny"
|
||||
|
||||
messages = [
|
||||
HumanMessage(content="What's the weather like in San Francisco?"),
|
||||
]
|
||||
num_tokens = llm.get_num_tokens_from_messages(messages, tools=[get_weather])
|
||||
assert num_tokens > 0
|
||||
|
||||
messages = [
|
||||
HumanMessage(content="What's the weather like in San Francisco?"),
|
||||
AIMessage(
|
||||
content=[
|
||||
{"text": "Let's see.", "type": "text"},
|
||||
{
|
||||
"id": "toolu_01V6d6W32QGGSmQm4BT98EKk",
|
||||
"input": {"location": "SF"},
|
||||
"name": "get_weather",
|
||||
"type": "tool_use",
|
||||
},
|
||||
],
|
||||
tool_calls=[
|
||||
{
|
||||
"name": "get_weather",
|
||||
"args": {"location": "SF"},
|
||||
"id": "toolu_01V6d6W32QGGSmQm4BT98EKk",
|
||||
"type": "tool_call",
|
||||
},
|
||||
],
|
||||
),
|
||||
ToolMessage(content="Sunny", tool_call_id="toolu_01V6d6W32QGGSmQm4BT98EKk"),
|
||||
]
|
||||
num_tokens = llm.get_num_tokens_from_messages(messages, tools=[get_weather])
|
||||
assert num_tokens > 0
|
||||
|
||||
|
||||
class GetWeather(BaseModel):
|
||||
"""Get the current weather in a given location"""
|
||||
|
||||
|
||||
@@ -331,7 +331,7 @@ def dummy_tool() -> BaseTool:
|
||||
arg1: int = Field(..., description="foo")
|
||||
arg2: Literal["bar", "baz"] = Field(..., description="one of 'bar', 'baz'")
|
||||
|
||||
class DummyFunction(BaseTool):
|
||||
class DummyFunction(BaseTool): # type: ignore[override]
|
||||
args_schema: Type[BaseModel] = Schema
|
||||
name: str = "dummy_function"
|
||||
description: str = "dummy function"
|
||||
|
||||
@@ -886,8 +886,13 @@ class BaseChatOpenAI(BaseChatModel):
|
||||
_, encoding_model = self._get_encoding_model()
|
||||
return encoding_model.encode(text)
|
||||
|
||||
# TODO: Count bound tools as part of input.
|
||||
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
|
||||
def get_num_tokens_from_messages(
|
||||
self,
|
||||
messages: List[BaseMessage],
|
||||
tools: Optional[
|
||||
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
|
||||
] = None,
|
||||
) -> int:
|
||||
"""Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
|
||||
|
||||
**Requirements**: You must have the ``pillow`` installed if you want to count
|
||||
@@ -897,7 +902,16 @@ class BaseChatOpenAI(BaseChatModel):
|
||||
counting.
|
||||
|
||||
OpenAI reference: https://github.com/openai/openai-cookbook/blob/
|
||||
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
|
||||
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
|
||||
|
||||
Args:
|
||||
messages: The message inputs to tokenize.
|
||||
tools: If provided, sequence of dict, BaseModel, function, or BaseTools
|
||||
to be converted to tool schemas.
|
||||
"""
|
||||
# TODO: Count bound tools as part of input.
|
||||
if tools is not None:
|
||||
warnings.warn("Counting tokens in tool schemas is not yet supported.")
|
||||
if sys.version_info[1] <= 7:
|
||||
return super().get_num_tokens_from_messages(messages)
|
||||
model, encoding = self._get_encoding_model()
|
||||
|
||||
Reference in New Issue
Block a user