Skip to content

Commit

Permalink
community[patch]: support convert FunctionMessage for Tongyi (#23569)
Browse files Browse the repository at this point in the history
**Description:** For function call agent with Tongyi, cause the
AgentAction will be converted to FunctionMessage by

https://github.com/langchain-ai/langchain/blob/47f69fe0d85056a378d7de7e7210247f5b9a8704/libs/core/langchain_core/agents.py#L188
But now Tongyi's *convert_message_to_dict* doesn't support
FunctionMessage

https://github.com/langchain-ai/langchain/blob/47f69fe0d85056a378d7de7e7210247f5b9a8704/libs/community/langchain_community/chat_models/tongyi.py#L184-L207
Then next round conversation will be failed by the *TypeError*
exception.

This patch adds the support to convert FunctionMessage for Tongyi.

**Issue:** N/A
**Dependencies:** N/A
  • Loading branch information
mackong committed Jun 27, 2024
1 parent d45ece0 commit 70834cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/community/langchain_community/chat_models/tongyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,7 @@
BaseMessageChunk,
ChatMessage,
ChatMessageChunk,
FunctionMessage,
HumanMessage,
HumanMessageChunk,
SystemMessage,
Expand Down Expand Up @@ -202,6 203,13 @@ def convert_message_to_dict(message: BaseMessage) -> dict:
"content": message.content,
"name": message.name,
}
elif isinstance(message, FunctionMessage):
message_dict = {
"role": "tool",
"tool_call_id": "",
"content": message.content,
"name": message.name,
}
else:
raise TypeError(f"Got unknown type {message}")
return message_dict
Expand Down
13 changes: 13 additions & 0 deletions libs/community/tests/unit_tests/chat_models/test_tongyi.py
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
from langchain_core.messages import (
AIMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
)
Expand Down Expand Up @@ -83,3 84,15 @@ def test__convert_message_to_dict_system() -> None:
result = convert_message_to_dict(message)
expected_output = {"role": "system", "content": "foo"}
assert result == expected_output


def test__convert_message_to_dict_tool() -> None:
message = FunctionMessage(name="foo", content="bar")
result = convert_message_to_dict(message)
expected_output = {
"role": "tool",
"tool_call_id": "",
"content": "bar",
"name": "foo",
}
assert result == expected_output

0 comments on commit 70834cd

Please sign in to comment.