mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-03 12:07:36 +00:00
community[patch]: Add function call support in Tongyi chat model. (#20119)
- [ ] **PR message**: - **Description:** This pr adds function calling support in Tongyi chat model. - **Issue:** None - **Dependencies:** None - **Twitter handle:** None Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
This commit is contained in:
@@ -33,6 +33,10 @@ from langchain_core.messages import (
|
||||
SystemMessage,
|
||||
SystemMessageChunk,
|
||||
)
|
||||
from langchain_core.output_parsers.openai_tools import (
|
||||
make_invalid_tool_call,
|
||||
parse_tool_call,
|
||||
)
|
||||
from langchain_core.outputs import (
|
||||
ChatGeneration,
|
||||
ChatGenerationChunk,
|
||||
@@ -71,8 +75,28 @@ def convert_dict_to_message(
|
||||
else HumanMessage(content=content)
|
||||
)
|
||||
elif role == "assistant":
|
||||
tool_calls = []
|
||||
invalid_tool_calls = []
|
||||
if "tool_calls" in _dict:
|
||||
additional_kwargs = {"tool_calls": _dict["tool_calls"]}
|
||||
for raw_tool_call in _dict["tool_calls"]:
|
||||
try:
|
||||
tool_calls.append(parse_tool_call(raw_tool_call, return_id=True))
|
||||
except Exception as e:
|
||||
invalid_tool_calls.append(
|
||||
make_invalid_tool_call(raw_tool_call, str(e))
|
||||
)
|
||||
else:
|
||||
additional_kwargs = {}
|
||||
return (
|
||||
AIMessageChunk(content=content) if is_chunk else AIMessage(content=content)
|
||||
AIMessageChunk(content=content)
|
||||
if is_chunk
|
||||
else AIMessage(
|
||||
content=content,
|
||||
additional_kwargs=additional_kwargs,
|
||||
tool_calls=tool_calls,
|
||||
invalid_tool_calls=invalid_tool_calls,
|
||||
)
|
||||
)
|
||||
elif role == "system":
|
||||
return (
|
||||
|
Reference in New Issue
Block a user