anthropic[patch]: use anthropic 0.23 (#20022)

This commit is contained in:
Bagatur 2024-04-04 14:23:53 -07:00 committed by GitHub
parent 4c969286fe
commit 6860450e48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 19 deletions

View File

@ -338,7 +338,6 @@
"\n",
"llm = ChatAnthropic(\n",
" model=\"claude-3-opus-20240229\",\n",
" default_headers={\"anthropic-beta\": \"tools-2024-04-04\"},\n",
")\n",
"\n",
"\n",
@ -697,9 +696,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "poetry-venv-2",
"language": "python",
"name": "python3"
"name": "poetry-venv-2"
},
"language_info": {
"codemirror_mode": {

View File

@ -325,7 +325,7 @@ class ChatAnthropic(BaseChatModel):
**kwargs: Any,
) -> Iterator[ChatGenerationChunk]:
params = self._format_params(messages=messages, stop=stop, **kwargs)
if "extra_body" in params and params["extra_body"].get("tools"):
if _tools_in_params(params):
warnings.warn("stream: Tool use is not yet supported in streaming mode.")
result = self._generate(
messages, stop=stop, run_manager=run_manager, **kwargs
@ -347,7 +347,7 @@ class ChatAnthropic(BaseChatModel):
**kwargs: Any,
) -> AsyncIterator[ChatGenerationChunk]:
params = self._format_params(messages=messages, stop=stop, **kwargs)
if "extra_body" in params and params["extra_body"].get("tools"):
if _tools_in_params(params):
warnings.warn("stream: Tool use is not yet supported in streaming mode.")
result = await self._agenerate(
messages, stop=stop, run_manager=run_manager, **kwargs
@ -385,7 +385,7 @@ class ChatAnthropic(BaseChatModel):
) -> ChatResult:
params = self._format_params(messages=messages, stop=stop, **kwargs)
if self.streaming:
if "extra_body" in params and params["extra_body"].get("tools"):
if _tools_in_params(params):
warnings.warn(
"stream: Tool use is not yet supported in streaming mode."
)
@ -394,7 +394,10 @@ class ChatAnthropic(BaseChatModel):
messages, stop=stop, run_manager=run_manager, **kwargs
)
return generate_from_stream(stream_iter)
data = self._client.messages.create(**params)
if _tools_in_params(params):
data = self._client.beta.tools.messages.create(**params)
else:
data = self._client.messages.create(**params)
return self._format_output(data, **kwargs)
async def _agenerate(
@ -406,7 +409,7 @@ class ChatAnthropic(BaseChatModel):
) -> ChatResult:
params = self._format_params(messages=messages, stop=stop, **kwargs)
if self.streaming:
if "extra_body" in params and params["extra_body"].get("tools"):
if _tools_in_params(params):
warnings.warn(
"stream: Tool use is not yet supported in streaming mode."
)
@ -415,7 +418,10 @@ class ChatAnthropic(BaseChatModel):
messages, stop=stop, run_manager=run_manager, **kwargs
)
return await agenerate_from_stream(stream_iter)
data = await self._async_client.messages.create(**params)
if _tools_in_params(params):
data = await self._async_client.beta.tools.messages.create(**params)
else:
data = await self._async_client.messages.create(**params)
return self._format_output(data, **kwargs)
@beta()
@ -434,9 +440,7 @@ class ChatAnthropic(BaseChatModel):
**kwargs: Any additional parameters to bind.
"""
formatted_tools = [convert_to_anthropic_tool(tool) for tool in tools]
extra_body = kwargs.pop("extra_body", {})
extra_body["tools"] = formatted_tools
return self.bind(extra_body=extra_body, **kwargs)
return self.bind(tools=formatted_tools, **kwargs)
@beta()
def with_structured_output(
@ -490,6 +494,12 @@ def convert_to_anthropic_tool(
)
def _tools_in_params(params: dict) -> bool:
return "tools" in params or (
"extra_body" in params and params["extra_body"].get("tools")
)
@deprecated(since="0.1.0", removal="0.2.0", alternative="ChatAnthropic")
class ChatAnthropicMessages(ChatAnthropic):
pass

View File

@ -16,13 +16,13 @@ typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.9\""}
[[package]]
name = "anthropic"
version = "0.17.0"
version = "0.23.1"
description = "The official Python library for the anthropic API"
optional = false
python-versions = ">=3.7"
files = [
{file = "anthropic-0.17.0-py3-none-any.whl", hash = "sha256:e582635d65d940cd0d957631d548aa2d8add4f261556bf53f8c1f6e5fa6082fd"},
{file = "anthropic-0.17.0.tar.gz", hash = "sha256:6f1958e7ffd706a19741260d6dfef3fd9af07d31a57b837792331b4ba0aa067c"},
{file = "anthropic-0.23.1-py3-none-any.whl", hash = "sha256:6dc5779dae83a5834864f4a4af0166c972b70f4cb8fd2765e1558282cc6d6242"},
{file = "anthropic-0.23.1.tar.gz", hash = "sha256:9325103702cbc96bb09d1b58c36bde75c726f6a01029fb4d85f41ebba07e9066"},
]
[package.dependencies]
@ -1204,4 +1204,4 @@ watchmedo = ["PyYAML (>=3.10)"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.8.1,<4.0"
content-hash = "b2372f2f83ea5a4bb48c34cec1f4c054e331aa63e0b8dfa3b8493f4045b8a1c7"
content-hash = "60a40f50a762f49026cfeeb822de78cef08d2a60e585cc994c0f5dcb9498f6ab"

View File

@ -13,7 +13,7 @@ license = "MIT"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain-core = "^0.1.33"
anthropic = ">=0.17.0,<1"
anthropic = ">=0.23.0,<1"
defusedxml = { version = "^0.7.1", optional = true }
[tool.poetry.group.test]

View File

@ -217,7 +217,6 @@ async def test_astreaming() -> None:
def test_tool_use() -> None:
llm = ChatAnthropic(
model="claude-3-opus-20240229",
default_headers={"anthropic-beta": "tools-2024-04-04"},
)
llm_with_tools = llm.bind_tools(
@ -240,7 +239,6 @@ def test_tool_use() -> None:
def test_with_structured_output() -> None:
llm = ChatAnthropic(
model="claude-3-opus-20240229",
default_headers={"anthropic-beta": "tools-2024-04-04"},
)
structured_llm = llm.with_structured_output(