mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-07 22:11:51 +00:00
ollama[patch]: support standard image format (#30864)
Following https://github.com/langchain-ai/langchain/pull/30746
This commit is contained in:
@@ -30,6 +30,7 @@ from langchain_core.messages import (
|
||||
SystemMessage,
|
||||
ToolCall,
|
||||
ToolMessage,
|
||||
is_data_content_block,
|
||||
)
|
||||
from langchain_core.messages.ai import UsageMetadata
|
||||
from langchain_core.messages.tool import tool_call
|
||||
@@ -173,6 +174,20 @@ def _lc_tool_call_to_openai_tool_call(tool_call: ToolCall) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _get_image_from_data_content_block(block: dict) -> str:
|
||||
"""Format standard data content block to format expected by Ollama."""
|
||||
if block["type"] == "image":
|
||||
if block["source_type"] == "base64":
|
||||
return block["data"]
|
||||
else:
|
||||
error_message = "Image data only supported through in-line base64 format."
|
||||
raise ValueError(error_message)
|
||||
|
||||
else:
|
||||
error_message = f"Blocks of type {block['type']} not supported."
|
||||
raise ValueError(error_message)
|
||||
|
||||
|
||||
def _is_pydantic_class(obj: Any) -> bool:
|
||||
return isinstance(obj, type) and is_basemodel_subclass(obj)
|
||||
|
||||
@@ -553,7 +568,9 @@ class ChatOllama(BaseChatModel):
|
||||
images.append(image_url_components[1])
|
||||
else:
|
||||
images.append(image_url_components[0])
|
||||
|
||||
elif is_data_content_block(content_part):
|
||||
image = _get_image_from_data_content_block(content_part)
|
||||
images.append(image)
|
||||
else:
|
||||
raise ValueError(
|
||||
"Unsupported message content type. "
|
||||
|
Reference in New Issue
Block a user