mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-24 03:52:10 +00:00
[Anthropic] Shallow Copy
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import copy
|
||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
@@ -116,7 +117,6 @@ def _merge_messages(
|
|||||||
"""Merge runs of human/tool messages into single human messages with content blocks.""" # noqa: E501
|
"""Merge runs of human/tool messages into single human messages with content blocks.""" # noqa: E501
|
||||||
merged: list = []
|
merged: list = []
|
||||||
for curr in messages:
|
for curr in messages:
|
||||||
curr = curr.model_copy(deep=True)
|
|
||||||
if isinstance(curr, ToolMessage):
|
if isinstance(curr, ToolMessage):
|
||||||
if isinstance(curr.content, list) and all(
|
if isinstance(curr.content, list) and all(
|
||||||
isinstance(block, dict) and block.get("type") == "tool_result"
|
isinstance(block, dict) and block.get("type") == "tool_result"
|
||||||
@@ -139,12 +139,12 @@ def _merge_messages(
|
|||||||
if isinstance(last.content, str):
|
if isinstance(last.content, str):
|
||||||
new_content: List = [{"type": "text", "text": last.content}]
|
new_content: List = [{"type": "text", "text": last.content}]
|
||||||
else:
|
else:
|
||||||
new_content = last.content
|
new_content = copy.copy(last.content)
|
||||||
if isinstance(curr.content, str):
|
if isinstance(curr.content, str):
|
||||||
new_content.append({"type": "text", "text": curr.content})
|
new_content.append({"type": "text", "text": curr.content})
|
||||||
else:
|
else:
|
||||||
new_content.extend(curr.content)
|
new_content.extend(curr.content)
|
||||||
last.content = new_content
|
merged[-1] = curr.model_copy(update={"content": new_content}, deep=False)
|
||||||
else:
|
else:
|
||||||
merged.append(curr)
|
merged.append(curr)
|
||||||
return merged
|
return merged
|
||||||
@@ -174,9 +174,11 @@ def _format_messages(
|
|||||||
raise ValueError("System message must be at beginning of message list.")
|
raise ValueError("System message must be at beginning of message list.")
|
||||||
if isinstance(message.content, list):
|
if isinstance(message.content, list):
|
||||||
system = [
|
system = [
|
||||||
|
(
|
||||||
block
|
block
|
||||||
if isinstance(block, dict)
|
if isinstance(block, dict)
|
||||||
else {"type": "text", "text": "block"}
|
else {"type": "text", "text": "block"}
|
||||||
|
)
|
||||||
for block in message.content
|
for block in message.content
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user