identity:name:function_callingauthor:Difylabel:en_US:FunctionCallingzh_Hans:FunctionCallingpt_BR:FunctionCallingdescription:en_US:Function Calling is a basic strategy for agent, model will use the tools provided to perform the task.zh_Hans:Function Calling 是一个基本的 Agent 策略,模型将使用提供的工具来执行任务。pt_BR:Function Calling is a basic strategy for agent, model will use the tools provided to perform the task.parameters: - name:modeltype:model-selectorscope:tool-call&llmrequired:truelabel:en_US:Modelzh_Hans:模型pt_BR:Model - name:toolstype:array[tools]required:truelabel:en_US:Tools listzh_Hans:工具列表pt_BR:Tools list - name:querytype:stringrequired:truelabel:en_US:Queryzh_Hans:用户提问pt_BR:Query - name:max_iterationstype:numberrequired:falsedefault:5label:en_US:Max Iterationszh_Hans:最大迭代次数pt_BR:Max Iterationsmax:50min:1extra:python:source:strategies/function_calling.py
from dify_plugin.entities.tool import ToolProviderTypeclassFunctionCallingAgentStrategy(AgentStrategy):def_invoke(self,parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:""" Run FunctionCall agent application """ fc_params =FunctionCallingParams(**parameters)# tool_call_name and tool_call_args parameter is obtained from the output of LLM tool_instances ={tool.identity.name: tool for tool in fc_params.tools}if fc_params.tools else{} tool_instance = tool_instances[tool_call_name] tool_invoke_responses = self.session.tool.invoke( provider_type=ToolProviderType.BUILT_IN, provider=tool_instance.identity.provider, tool_name=tool_instance.identity.name,# add the default value parameters={**tool_instance.runtime_parameters, **tool_call_args}, )
import jsonfrom collections.abc import Generatorfrom typing import castfrom dify_plugin.entities.agent import AgentInvokeMessagefrom dify_plugin.entities.tool import ToolInvokeMessagedefparse_invoke_response(tool_invoke_responses: Generator[AgentInvokeMessage]) ->str: result =""for response in tool_invoke_responses:if response.type == ToolInvokeMessage.MessageType.TEXT: result +=cast(ToolInvokeMessage.TextMessage, response.message).textelif response.type == ToolInvokeMessage.MessageType.LINK: result += (f"result link: {cast(ToolInvokeMessage.TextMessage, response.message).text}."+" please tell user to check it." )elif response.type in{ ToolInvokeMessage.MessageType.IMAGE_LINK, ToolInvokeMessage.MessageType.IMAGE,}: result += ("image has been created and sent to user already, "+"you do not need to create it, just tell the user to check it now." )elif response.type == ToolInvokeMessage.MessageType.JSON: text = json.dumps(cast(ToolInvokeMessage.JsonMessage, response.message).json_object, ensure_ascii=False) result +=f"tool response: {text}."else: result +=f"tool response: {response.message!r}."return result