From 0a54aedb85aa2bac66910f9eefa03375f68145e4 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Fri, 10 Jan 2025 13:56:31 -0800 Subject: [PATCH] anthropic: pdf integration test (#29142) --- libs/partners/anthropic/poetry.lock | 16 ++++++++++- libs/partners/anthropic/pyproject.toml | 3 ++ .../integration_tests/test_chat_models.py | 28 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/libs/partners/anthropic/poetry.lock b/libs/partners/anthropic/poetry.lock index 2729e80cf9c2f..8dc1383dda9d3 100644 --- a/libs/partners/anthropic/poetry.lock +++ b/libs/partners/anthropic/poetry.lock @@ -1270,6 +1270,20 @@ files = [ {file = "tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff"}, ] +[[package]] +name = "types-requests" +version = "2.32.0.20241016" +description = "Typing stubs for requests" +optional = false +python-versions = ">=3.8" +files = [ + {file = "types-requests-2.32.0.20241016.tar.gz", hash = "sha256:0d9cad2f27515d0e3e3da7134a1b6f28fb97129d86b867f24d9c726452634d95"}, + {file = "types_requests-2.32.0.20241016-py3-none-any.whl", hash = "sha256:4195d62d6d3e043a4eaaf08ff8a62184584d2e8684e9d2aa178c7915a7da3747"}, +] + +[package.dependencies] +urllib3 = ">=2" + [[package]] name = "typing-extensions" version = "4.12.2" @@ -1343,4 +1357,4 @@ watchmedo = ["PyYAML (>=3.10)"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "c22d86d391e97dc8befa33fbd7ca674410eb318e2bef08e28284449b2a57aedf" +content-hash = "743255e1fe91bc0e2185a1bfe1b4c6bb13f6522affe5bb35bdfda419e93736ae" diff --git a/libs/partners/anthropic/pyproject.toml b/libs/partners/anthropic/pyproject.toml index 8224f59585a67..abe04e73dae8d 100644 --- a/libs/partners/anthropic/pyproject.toml +++ b/libs/partners/anthropic/pyproject.toml @@ -76,6 +76,7 @@ ruff = "^0.5" [tool.poetry.group.typing.dependencies] mypy = "^1.10" +types-requests = "^2.32.0.20241016" [tool.poetry.group.test.dependencies.langchain-core] path = "../../core" @@ -89,6 +90,8 @@ develop = true path = "../../core" develop = true +[tool.poetry.group.test_integration.dependencies] +requests = "^2.32.3" [tool.poetry.group.test_integration.dependencies.langchain-core] path = "../../core" develop = true diff --git a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py index 8ff2396c9d1c9..2fdf71845636f 100644 --- a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py +++ b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py @@ -1,9 +1,11 @@ """Test ChatAnthropic chat model.""" import json +from base64 import b64encode from typing import List, Optional import pytest +import requests from langchain_core.callbacks import CallbackManager from langchain_core.messages import ( AIMessage, @@ -575,3 +577,29 @@ def test_anthropic_bind_tools_tool_choice(tool_choice: str) -> None: chat_model_with_tools = chat_model.bind_tools([GetWeather], tool_choice=tool_choice) response = chat_model_with_tools.invoke("what's the weather in ny and la") assert isinstance(response, AIMessage) + + +def test_pdf_document_input() -> None: + url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" + data = b64encode(requests.get(url).content).decode() + + result = ChatAnthropic(model_name=MODEL_NAME).invoke( # type: ignore[call-arg] + [ + HumanMessage( + [ + "summarize this document", + { + "type": "document", + "source": { + "type": "base64", + "data": data, + "media_type": "application/pdf", + }, + }, + ] + ) + ] + ) + assert isinstance(result, AIMessage) + assert isinstance(result.content, str) + assert len(result.content) > 0