Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\Fix tool_calls message merge #14613

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions libs/core/langchain_core/messages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,30 @@ def _merge_kwargs_dict(
merged[k] = v
elif merged[k] is None and v:
merged[k] = v
elif v is None:
continue
elif merged[k] == v:
continue
elif type(merged[k]) != type(v):
raise ValueError(
raise TypeError(
f'additional_kwargs["{k}"] already exists in this message,'
" but with a different type."
)
elif isinstance(merged[k], str):
merged[k] += v
elif isinstance(merged[k], dict):
merged[k] = self._merge_kwargs_dict(merged[k], v)
elif isinstance(merged[k], list):
merged[k] = merged[k].copy()
for i, e in enumerate(v):
if isinstance(e, dict) and isinstance(e.get("index"), int):
i = e["index"]
if i < len(merged[k]):
merged[k][i] = self._merge_kwargs_dict(merged[k][i], e)
else:
merged[k] = merged[k] + [e]
else:
raise ValueError(
raise TypeError(
f"Additional kwargs key {k} already exists in this message."
)
return merged
Expand Down
224 changes: 224 additions & 0 deletions libs/core/tests/unit_tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,227 @@ def test_message_chunk_to_message() -> None:
assert message_chunk_to_message(
FunctionMessageChunk(name="hello", content="I am")
) == FunctionMessage(name="hello", content="I am")


def test_tool_calls_merge() -> None:
chunks: list[dict] = [
dict(content=""),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": "call_CwGAsESnXehQEjiAIWzinlva",
"function": {"arguments": "", "name": "person"},
"type": "function",
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": None,
"function": {"arguments": '{"na', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": None,
"function": {"arguments": 'me": ', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": None,
"function": {"arguments": '"jane"', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": None,
"function": {"arguments": ', "a', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": None,
"function": {"arguments": 'ge": ', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": None,
"function": {"arguments": "2}", "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": "call_zXSIylHvc5x3JUAPcHZR5GZI",
"function": {"arguments": "", "name": "person"},
"type": "function",
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": None,
"function": {"arguments": '{"na', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": None,
"function": {"arguments": 'me": ', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": None,
"function": {"arguments": '"bob",', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": None,
"function": {"arguments": ' "ag', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": None,
"function": {"arguments": 'e": 3', "name": None},
"type": None,
}
]
},
),
dict(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 1,
"id": None,
"function": {"arguments": "}", "name": None},
"type": None,
}
]
},
),
dict(content=""),
]

final = None

for chunk in chunks:
msg = AIMessageChunk(**chunk)
if final is None:
final = msg
else:
final = final + msg

assert final == AIMessageChunk(
content="",
additional_kwargs={
"tool_calls": [
{
"index": 0,
"id": "call_CwGAsESnXehQEjiAIWzinlva",
"function": {
"arguments": '{"name": "jane", "age": 2}',
"name": "person",
},
"type": "function",
},
{
"index": 1,
"id": "call_zXSIylHvc5x3JUAPcHZR5GZI",
"function": {
"arguments": '{"name": "bob", "age": 3}',
"name": "person",
},
"type": "function",
},
]
},
)
Loading