community[patch]: support convert FunctionMessage for Tongyi (#23569)

**Description:** For function call agent with Tongyi, cause the
AgentAction will be converted to FunctionMessage by

47f69fe0d8/libs/core/langchain_core/agents.py (L188)
But now Tongyi's *convert_message_to_dict* doesn't support
FunctionMessage

47f69fe0d8/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
This commit is contained in:
mackong 2024-06-28 03:49:26 +08:00 committed by GitHub
parent d45ece0e58
commit 70834cd741
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -32,6 +32,7 @@ from langchain_core.messages import (
BaseMessageChunk,
ChatMessage,
ChatMessageChunk,
FunctionMessage,
HumanMessage,
HumanMessageChunk,
SystemMessage,
@ -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

View File

@ -1,5 +1,6 @@
from langchain_core.messages import (
AIMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
)
@ -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