mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
feat: (core, standard-tests) support PDF inputs in ToolMessages (#33183)
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
"""Standard LangChain interface tests for Responses API"""
|
||||
|
||||
import base64
|
||||
from pathlib import Path
|
||||
from typing import cast
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
|
||||
|
||||
from langchain_openai import ChatOpenAI
|
||||
from tests.integration_tests.chat_models.test_base_standard import TestOpenAIStandard
|
||||
@@ -52,6 +54,55 @@ class TestOpenAIResponses(TestOpenAIStandard):
|
||||
input_ = "What was the 3rd highest building in 2000?"
|
||||
return _invoke(llm, input_, stream)
|
||||
|
||||
@property
|
||||
def supports_pdf_tool_message(self) -> bool:
|
||||
# OpenAI requires a filename for PDF inputs
|
||||
# For now, we test with filename in OpenAI-specific tests
|
||||
return False
|
||||
|
||||
def test_openai_pdf_tool_messages(self, model: BaseChatModel) -> None:
|
||||
"""Test that the model can process PDF inputs in ToolMessages."""
|
||||
url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
|
||||
pdf_data = base64.b64encode(httpx.get(url).content).decode("utf-8")
|
||||
|
||||
tool_message = ToolMessage(
|
||||
content=[
|
||||
{
|
||||
"type": "file",
|
||||
"source_type": "base64",
|
||||
"data": pdf_data,
|
||||
"mime_type": "application/pdf",
|
||||
"filename": "my-pdf", # specify filename
|
||||
},
|
||||
],
|
||||
tool_call_id="1",
|
||||
name="random_pdf",
|
||||
)
|
||||
|
||||
messages = [
|
||||
HumanMessage(
|
||||
"Get a random PDF using the tool and relay the title verbatim."
|
||||
),
|
||||
AIMessage(
|
||||
[],
|
||||
tool_calls=[
|
||||
{
|
||||
"type": "tool_call",
|
||||
"id": "1",
|
||||
"name": "random_pdf",
|
||||
"args": {},
|
||||
}
|
||||
],
|
||||
),
|
||||
tool_message,
|
||||
]
|
||||
|
||||
def random_pdf() -> str:
|
||||
"""Return a random PDF."""
|
||||
return ""
|
||||
|
||||
_ = model.bind_tools([random_pdf]).invoke(messages)
|
||||
|
||||
|
||||
def _invoke(llm: ChatOpenAI, input_: str, stream: bool) -> AIMessage:
|
||||
if stream:
|
||||
|
||||
Reference in New Issue
Block a user