mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-29 01:48:57 +00:00
update openai
This commit is contained in:
parent
b1fc20cbcd
commit
4c23ceb9ef
@ -68,6 +68,7 @@ from langchain_core.messages import (
|
|||||||
ToolCall,
|
ToolCall,
|
||||||
ToolMessage,
|
ToolMessage,
|
||||||
ToolMessageChunk,
|
ToolMessageChunk,
|
||||||
|
is_data_content_block,
|
||||||
)
|
)
|
||||||
from langchain_core.messages.ai import (
|
from langchain_core.messages.ai import (
|
||||||
InputTokenDetails,
|
InputTokenDetails,
|
||||||
@ -191,6 +192,45 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
|
|||||||
return ChatMessage(content=_dict.get("content", ""), role=role, id=id_) # type: ignore[arg-type]
|
return ChatMessage(content=_dict.get("content", ""), role=role, id=id_) # type: ignore[arg-type]
|
||||||
|
|
||||||
|
|
||||||
|
def _format_data_content_block(block: dict) -> dict:
|
||||||
|
"""Format standard data content block to format expected by OpenAI."""
|
||||||
|
if block["type"] == "image":
|
||||||
|
if block["source_type"] == "url":
|
||||||
|
formatted_block = {
|
||||||
|
"type": "image_url",
|
||||||
|
"image_url": {"url": block["source"]},
|
||||||
|
}
|
||||||
|
elif block["source_type"] == "base64":
|
||||||
|
formatted_block = {
|
||||||
|
"type": "image_url",
|
||||||
|
"image_url": {
|
||||||
|
"url": f"data:{block['mime_type']};base64,{block['source']}"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
"OpenAI only supports 'url' and 'base64' source_type for image "
|
||||||
|
"content blocks."
|
||||||
|
)
|
||||||
|
|
||||||
|
elif block["type"] == "file":
|
||||||
|
if block["source_type"] == "base64":
|
||||||
|
file = {"file_data": f"data:{block['mime_type']};base64,{block['source']}"}
|
||||||
|
if metadata := block.get("metadata"):
|
||||||
|
file = {**file, **metadata}
|
||||||
|
# Hack to support cross-compatibility with providers that do not require
|
||||||
|
# filename (OpenAI requires one).
|
||||||
|
if "filename" not in file:
|
||||||
|
file["filename"] = ""
|
||||||
|
formatted_block = {"type": "file", "file": file}
|
||||||
|
elif block["source_type"] == "id":
|
||||||
|
formatted_block = {"type": "file", "file": {"file_id": block["source"]}}
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Block of type {block['type']} is not supported.")
|
||||||
|
|
||||||
|
return formatted_block
|
||||||
|
|
||||||
|
|
||||||
def _format_message_content(content: Any) -> Any:
|
def _format_message_content(content: Any) -> Any:
|
||||||
"""Format message content."""
|
"""Format message content."""
|
||||||
if content and isinstance(content, list):
|
if content and isinstance(content, list):
|
||||||
@ -203,6 +243,8 @@ def _format_message_content(content: Any) -> Any:
|
|||||||
and block["type"] in ("tool_use", "thinking")
|
and block["type"] in ("tool_use", "thinking")
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
elif is_data_content_block(block):
|
||||||
|
formatted_content.append(_format_data_content_block(block))
|
||||||
# Anthropic image blocks
|
# Anthropic image blocks
|
||||||
elif (
|
elif (
|
||||||
isinstance(block, dict)
|
isinstance(block, dict)
|
||||||
|
Loading…
Reference in New Issue
Block a user