mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-19 09:30:15 +00:00
fix mutability
This commit is contained in:
parent
071bc7ba59
commit
88d0101394
@ -935,6 +935,7 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
payload = {**self._default_params, **kwargs}
|
payload = {**self._default_params, **kwargs}
|
||||||
if _use_response_api(payload):
|
if _use_response_api(payload):
|
||||||
payload["input"] = _construct_response_api_input(messages)
|
payload["input"] = _construct_response_api_input(messages)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
payload["messages"] = [_convert_message_to_dict(m) for m in messages]
|
payload["messages"] = [_convert_message_to_dict(m) for m in messages]
|
||||||
return payload
|
return payload
|
||||||
@ -2803,37 +2804,51 @@ def _construct_response_api_input(messages: Sequence[BaseMessage]) -> list:
|
|||||||
"annotations": [],
|
"annotations": [],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
msg["content"].append(
|
msg["content"] = msg["content"] + [
|
||||||
{"type": "refusal", "refusal": lc_msg.additional_kwargs["refusal"]}
|
{"type": "refusal", "refusal": lc_msg.additional_kwargs["refusal"]}
|
||||||
)
|
]
|
||||||
if isinstance(msg["content"], list):
|
if isinstance(msg["content"], list):
|
||||||
|
new_blocks = []
|
||||||
for block in msg["content"]:
|
for block in msg["content"]:
|
||||||
# chat api: {"type": "text", "text": "..."}
|
# chat api: {"type": "text", "text": "..."}
|
||||||
# response api: {"type": "output_text", "text": "...", "annotations": [...]} # noqa: E501
|
# response api: {"type": "output_text", "text": "...", "annotations": [...]} # noqa: E501
|
||||||
if block["type"] == "text":
|
if block["type"] == "text":
|
||||||
block["type"] = "output_text"
|
new_blocks.append(
|
||||||
block["annotations"] = block.get("annotations") or []
|
{
|
||||||
|
"type": "output_text",
|
||||||
|
"text": block["text"],
|
||||||
|
"annotations": block.get("annotations") or [],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
new_blocks.append(block)
|
||||||
|
msg["content"] = new_blocks
|
||||||
if msg["content"]:
|
if msg["content"]:
|
||||||
input_.append(msg)
|
input_.append(msg)
|
||||||
input_.extend(function_calls)
|
input_.extend(function_calls)
|
||||||
elif msg["role"] == "user":
|
elif msg["role"] == "user":
|
||||||
if isinstance(msg["content"], list):
|
if isinstance(msg["content"], list):
|
||||||
|
new_blocks = []
|
||||||
for block in msg["content"]:
|
for block in msg["content"]:
|
||||||
# chat api: {"type": "text", "text": "..."}
|
# chat api: {"type": "text", "text": "..."}
|
||||||
# response api: {"type": "input_text", "text": "..."}
|
# response api: {"type": "input_text", "text": "..."}
|
||||||
if block["type"] == "text":
|
if block["type"] == "text":
|
||||||
block["type"] = "input_text"
|
new_blocks.append({"type": "input_text", "text": block["text"]})
|
||||||
# chat api: {"type": "image_url", "image_url": {"url": "...", "detail": "..."}} # noqa: E501
|
# chat api: {"type": "image_url", "image_url": {"url": "...", "detail": "..."}} # noqa: E501
|
||||||
# response api: {"type": "image_url", "image_url": "...", "detail": "...", "file_id": "..."} # noqa: E501
|
# response api: {"type": "image_url", "image_url": "...", "detail": "...", "file_id": "..."} # noqa: E501
|
||||||
elif block["type"] == "image_url":
|
elif block["type"] == "image_url":
|
||||||
block["type"] = "input_image"
|
new_block = {
|
||||||
if isinstance(block.get("image_url"), dict):
|
"type": "input_image",
|
||||||
image_url = block.pop("image_url")
|
"image_url": block["image_url"]["url"],
|
||||||
block["image_url"] = image_url["url"]
|
}
|
||||||
if image_url.get("detail"):
|
if block["image_url"].get("detail"):
|
||||||
block["detail"] = image_url["detail"]
|
new_block["detail"] = block["image_url"]["detail"]
|
||||||
|
new_blocks.append(new_block)
|
||||||
|
elif block["type"] in ("input_text", "input_image", "input_file"):
|
||||||
|
new_blocks.append(block)
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
msg["content"] = new_blocks
|
||||||
input_.append(msg)
|
input_.append(msg)
|
||||||
else:
|
else:
|
||||||
input_.append(msg)
|
input_.append(msg)
|
||||||
|
@ -1635,6 +1635,7 @@ def test__construct_response_api_input_multiple_message_types() -> None:
|
|||||||
]
|
]
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
messages_copy = [m.copy(deep=True) for m in messages]
|
||||||
|
|
||||||
result = _construct_response_api_input(messages)
|
result = _construct_response_api_input(messages)
|
||||||
|
|
||||||
@ -1675,3 +1676,6 @@ def test__construct_response_api_input_multiple_message_types() -> None:
|
|||||||
"annotations": [],
|
"annotations": [],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# assert no mutation has occurred
|
||||||
|
assert messages_copy == messages
|
||||||
|
Loading…
Reference in New Issue
Block a user