mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-23 07:09:31 +00:00
fix(core): Permit OpenAI style blocks to be passed into convert_to_openai_messages (#31140)
Should effectively be a noop, just shouldn't throw CC @madams0013 --------- Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
parent
a15034d8d1
commit
66d1ed6099
@ -1139,6 +1139,24 @@ def convert_to_openai_messages(
|
||||
f"content block:\n\n{block}"
|
||||
)
|
||||
raise ValueError(err)
|
||||
# OpenAI file format
|
||||
elif (
|
||||
block.get("type") == "file"
|
||||
and isinstance(block.get("file"), dict)
|
||||
and isinstance(block.get("file", {}).get("file_data"), str)
|
||||
):
|
||||
if block.get("file", {}).get("filename") is None:
|
||||
logger.info("Generating a fallback filename.")
|
||||
block["file"]["filename"] = "LC_AUTOGENERATED"
|
||||
content.append(block)
|
||||
# OpenAI audio format
|
||||
elif (
|
||||
block.get("type") == "input_audio"
|
||||
and isinstance(block.get("input_audio"), dict)
|
||||
and isinstance(block.get("input_audio", {}).get("data"), str)
|
||||
and isinstance(block.get("input_audio", {}).get("format"), str)
|
||||
):
|
||||
content.append(block)
|
||||
elif block.get("type") == "tool_use":
|
||||
if missing := [
|
||||
k for k in ("id", "name", "input") if k not in block
|
||||
|
@ -1209,6 +1209,13 @@ def test_convert_to_openai_messages_multimodal() -> None:
|
||||
"mime_type": "application/pdf",
|
||||
"filename": "test.pdf",
|
||||
},
|
||||
{
|
||||
"type": "file",
|
||||
"file": {
|
||||
"filename": "draconomicon.pdf",
|
||||
"file_data": "data:application/pdf;base64,<base64 string>",
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "file",
|
||||
"source_type": "id",
|
||||
@ -1220,13 +1227,20 @@ def test_convert_to_openai_messages_multimodal() -> None:
|
||||
"data": "<base64 string>",
|
||||
"mime_type": "audio/wav",
|
||||
},
|
||||
{
|
||||
"type": "input_audio",
|
||||
"input_audio": {
|
||||
"data": "<base64 string>",
|
||||
"format": "wav",
|
||||
},
|
||||
},
|
||||
]
|
||||
)
|
||||
]
|
||||
result = convert_to_openai_messages(messages, text_format="block")
|
||||
assert len(result) == 1
|
||||
message = result[0]
|
||||
assert len(message["content"]) == 6
|
||||
assert len(message["content"]) == 8
|
||||
|
||||
# Test adding filename
|
||||
messages = [
|
||||
|
Loading…
Reference in New Issue
Block a user