本文主要是介绍AutoGen Function Call 函数调用解析(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、AutoGen Function Call
1.1 register_for_llm 注册调用
1.2 register_for_execution 注册执行
1.3 三种注册方法
1.3.1 函数定义和注册分开
1.3.2 定义函数时注册
1.3.3 register_function 函数注册
二、实例
本文主要对 AutoGen Function Call 进行解析,并通过实例进行介绍。
一、AutoGen Function Call
AutoGen 支持 Function Call 功能,代理会根据 system_message 和函数描述进行调用。一个函数(工具)必须向至少两个代理注册,才能在对话中使用,一个负责调用,一个负责执行
1.1 register_for_llm 注册调用
负责调用的代理通过 register_for_llm 注册函数。
def register_for_llm(*,name: Optional[str] = None,description: Optional[str] = None,api_style: Literal["function", "tool"] = "tool") -> Callable[[F], F]
常用的参数是 name(函数名称) 和 description(函数描述)。
1.2 register_for_execution 注册执行
负责执行的代理通过 register_for_execution 注册函数。
def register_for_execution(name: Optional[str] = None) -> Callable[[F], F]
1.3 三种注册方法
AutoGen 支持代理三种方法注册调用和执行函数。
1.3.1 函数定义和注册分开
在定义函数后,代理分别注册函数。
from typing import Annotated, LiteralOperator = Literal["+", "-", "*", "/"]# 执行计算的函数
def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:if operator == "+":return a + belif operator == "-":return a - belif operator == "*":return a * belif operator == "/":return int(a / b)else:raise ValueError("Invalid operator")# 注册方法一
# assistant 注册函数调用
assistant.register_for_llm(name="calculator", description="A simple calculator")(calculator)# user_proxy 注册执行
user_proxy.register_for_execution(name="calculator")(calculator)
1.3.2 定义函数时注册
在定义函数时,代理注册函数。
from typing import Annotated, LiteralOperator = Literal["+", "-", "*", "/"]# 注册方法二
@user_proxy.register_for_execution()
@assistant.register_for_llm(name="calculator", description="A simple calculator")
def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:if operator == "+":return a + belif operator == "-":return a - belif operator == "*":return a * belif operator == "/":return int(a / b)else:raise ValueError("Invalid operator")
1.3.3 register_function 函数注册
通过 register_function 函数一起注册。
from typing import Annotated, LiteralOperator = Literal["+", "-", "*", "/"]def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:if operator == "+":return a + belif operator == "-":return a - belif operator == "*":return a * belif operator == "/":return int(a / b)else:raise ValueError("Invalid operator")register_function(calculator,caller=assistant, # The assistant agent can suggest calls to the calculator.executor=user_proxy, # The user proxy agent can execute the calculator calls.name="calculator", # By default, the function name is used as the tool name.description="A simple calculator", # A description of the tool.
)
二、实例
下面通过一个算数运算的实例进行说明。
from typing import Annotated, LiteralOperator = Literal["+", "-", "*", "/"]# 执行计算的函数
def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:if operator == "+":return a + belif operator == "-":return a - belif operator == "*":return a * belif operator == "/":return int(a / b)else:raise ValueError("Invalid operator")import osfrom autogen import ConversableAgent, config_list_from_json# 配置LLM
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST",
)# 负责调用的代理
assistant = ConversableAgent(name="Assistant",system_message="You are a helpful AI assistant. ""You can help with simple calculations. ""Return 'TERMINATE' when the task is done.",llm_config={"config_list": config_list},
)# 负责执行的代理
user_proxy = ConversableAgent(name="User",llm_config=False,is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],human_input_mode="NEVER",
)# 注册方法一
# assistant 注册函数调用
assistant.register_for_llm(name="calculator", description="A simple calculator")(calculator)# user_proxy 注册执行
user_proxy.register_for_execution(name="calculator")(calculator)'''
# 注册方法二
# register_function 支持两个代理同时注册
from autogen import register_function# Register the calculator function to the two agents.
register_function(calculator,caller=assistant, # The assistant agent can suggest calls to the calculator.executor=user_proxy, # The user proxy agent can execute the calculator calls.name="calculator", # By default, the function name is used as the tool name.description="A simple calculator", # A description of the tool.
)# 注册方法三
# 定义函数的时候注册
```
@user_proxy.register_for_execution()
@assistant.register_for_llm(name="my_function", description="This is a very useful function")
def my_function(a: Annotated[str, "description of a parameter"] = "a", b: int, c=3.14) -> str:return a + str(b * c)
```'''chat_result = user_proxy.initiate_chat(assistant, message="What is (44232 + 13312 / (232 - 32)) * 5?")
输出如下所示。
(base) D:\code\autogenstudio_images\example>python function_call.py
D:\Software\anaconda3\Lib\site-packages\paramiko\transport.py:219: CryptographyDeprecationWarning: Blowfish has been deprecated"class": algorithms.Blowfish,
User (to Assistant):What is (44232 + 13312 / (232 - 32)) * 5?-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...
[autogen.oai.client: 09-03 21:02:49] {329} WARNING - Model meta/llama-3.1-405b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Assistant (to User):***** Suggested tool call (chatcmpl-tool-affd8cb937d74e0585e71a80f8b36082): calculator *****
Arguments:
{"a": 232, "b": 32, "operator": "-"}
********************************************************************************************-------------------------------------------------------------------------------->>>>>>>> EXECUTING FUNCTION calculator...
User (to Assistant):User (to Assistant):***** Response from calling tool (chatcmpl-tool-affd8cb937d74e0585e71a80f8b36082) *****
200
***************************************************************************************-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...
[autogen.oai.client: 09-03 21:03:11] {329} WARNING - Model meta/llama-3.1-405b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Assistant (to User):***** Suggested tool call (chatcmpl-tool-b79bd1065ee94d228176dbc06c2a3981): calculator *****
Arguments:
{"a": 13312, "b": 200, "operator": "/"}
********************************************************************************************-------------------------------------------------------------------------------->>>>>>>> EXECUTING FUNCTION calculator...
User (to Assistant):User (to Assistant):***** Response from calling tool (chatcmpl-tool-b79bd1065ee94d228176dbc06c2a3981) *****
66
***************************************************************************************-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...
[autogen.oai.client: 09-03 21:06:13] {329} WARNING - Model moonshot-v1-8k is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Assistant (to User):***** Suggested tool call (calculator:0): calculator *****
Arguments:
{"a": 44232,"b": 66,"operator": "+"
}
**********************************************************-------------------------------------------------------------------------------->>>>>>>> EXECUTING FUNCTION calculator...
User (to Assistant):User (to Assistant):***** Response from calling tool (calculator:0) *****
44298
*****************************************************-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...
[autogen.oai.client: 09-03 21:06:17] {329} WARNING - Model meta/llama-3.1-405b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Assistant (to User):***** Suggested tool call (chatcmpl-tool-a13b42b8e844488793527ab64b55d0ea): calculator *****
Arguments:
{"a": 44298, "b": 5, "operator": "*"}
********************************************************************************************-------------------------------------------------------------------------------->>>>>>>> EXECUTING FUNCTION calculator...
User (to Assistant):User (to Assistant):***** Response from calling tool (chatcmpl-tool-a13b42b8e844488793527ab64b55d0ea) *****
221490
***************************************************************************************-------------------------------------------------------------------------------->>>>>>>> USING AUTO REPLY...
[autogen.oai.client: 09-03 21:06:21] {329} WARNING - Model meta/llama-3.1-405b-instruct is not found. The cost will be 0. In your config_list, add field {"price" : [prompt_price_per_1k, completion_token_price_per_1k]} for customized pricing.
Assistant (to User):The answer is 221490. TERMINATE.--------------------------------------------------------------------------------
参考链接:
[1] Tool Use | AutoGen
[2] agentchat.conversable_agent | AutoGen
这篇关于AutoGen Function Call 函数调用解析(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!