mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
multiple: multi-modal content blocks (#30746)
Introduces standard content block format for images, audio, and files.
## Examples
Image from url:
```
{
"type": "image",
"source_type": "url",
"url": "https://path.to.image.png",
}
```
Image, in-line data:
```
{
"type": "image",
"source_type": "base64",
"data": "<base64 string>",
"mime_type": "image/png",
}
```
PDF, in-line data:
```
{
"type": "file",
"source_type": "base64",
"data": "<base64 string>",
"mime_type": "application/pdf",
}
```
File from ID:
```
{
"type": "file",
"source_type": "id",
"id": "file-abc123",
}
```
Plain-text file:
```
{
"type": "file",
"source_type": "text",
"text": "foo bar",
}
```
This commit is contained in:
@@ -30,6 +30,10 @@ class TestAzureOpenAIStandard(ChatModelIntegrationTests):
|
||||
def supports_image_inputs(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def supports_image_urls(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def supports_json_mode(self) -> bool:
|
||||
return True
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
"""Standard LangChain interface tests"""
|
||||
|
||||
import base64
|
||||
from pathlib import Path
|
||||
from typing import Literal, cast
|
||||
|
||||
import httpx
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
from langchain_tests.integration_tests import ChatModelIntegrationTests
|
||||
|
||||
from langchain_openai import ChatOpenAI
|
||||
@@ -25,6 +27,10 @@ class TestOpenAIStandard(ChatModelIntegrationTests):
|
||||
def supports_image_inputs(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def supports_image_urls(self) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def supports_json_mode(self) -> bool:
|
||||
return True
|
||||
@@ -71,6 +77,31 @@ class TestOpenAIStandard(ChatModelIntegrationTests):
|
||||
)
|
||||
return _invoke(llm, input_, stream)
|
||||
|
||||
@property
|
||||
def supports_pdf_inputs(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_inputs(self, model: BaseChatModel) -> None:
|
||||
"""Test that the model can process PDF inputs."""
|
||||
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")
|
||||
|
||||
message = HumanMessage(
|
||||
[
|
||||
{"type": "text", "text": "Summarize this document:"},
|
||||
{
|
||||
"type": "file",
|
||||
"source_type": "base64",
|
||||
"mime_type": "application/pdf",
|
||||
"data": pdf_data,
|
||||
"metadata": {"filename": "my-pdf"}, # OpenAI requires a filename
|
||||
},
|
||||
]
|
||||
)
|
||||
_ = model.invoke([message])
|
||||
|
||||
|
||||
def _invoke(llm: ChatOpenAI, input_: str, stream: bool) -> AIMessage:
|
||||
if stream:
|
||||
|
||||
@@ -649,6 +649,51 @@ def test_format_message_content() -> None:
|
||||
]
|
||||
assert [{"type": "text", "text": "hello"}] == _format_message_content(content)
|
||||
|
||||
# Standard multi-modal inputs
|
||||
content = [{"type": "image", "source_type": "url", "url": "https://..."}]
|
||||
expected = [{"type": "image_url", "image_url": {"url": "https://..."}}]
|
||||
assert expected == _format_message_content(content)
|
||||
|
||||
content = [
|
||||
{
|
||||
"type": "image",
|
||||
"source_type": "base64",
|
||||
"data": "<base64 data>",
|
||||
"mime_type": "image/png",
|
||||
}
|
||||
]
|
||||
expected = [
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {"url": "data:image/png;base64,<base64 data>"},
|
||||
}
|
||||
]
|
||||
assert expected == _format_message_content(content)
|
||||
|
||||
content = [
|
||||
{
|
||||
"type": "file",
|
||||
"source_type": "base64",
|
||||
"data": "<base64 data>",
|
||||
"mime_type": "application/pdf",
|
||||
"metadata": {"filename": "my_file"},
|
||||
}
|
||||
]
|
||||
expected = [
|
||||
{
|
||||
"type": "file",
|
||||
"file": {
|
||||
"filename": "my_file",
|
||||
"file_data": "data:application/pdf;base64,<base64 data>",
|
||||
},
|
||||
}
|
||||
]
|
||||
assert expected == _format_message_content(content)
|
||||
|
||||
content = [{"type": "file", "source_type": "id", "id": "file-abc123"}]
|
||||
expected = [{"type": "file", "file": {"file_id": "file-abc123"}}]
|
||||
assert expected == _format_message_content(content)
|
||||
|
||||
|
||||
class GenerateUsername(BaseModel):
|
||||
"Get a username based on someone's name and hair color."
|
||||
|
||||
Reference in New Issue
Block a user