AutoGen Function Call 函数调用解析(一)

2024-09-08 15:12

本文主要是介绍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 函数调用解析(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1148460

相关文章

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

利用Python和C++解析gltf文件的示例详解

《利用Python和C++解析gltf文件的示例详解》gltf,全称是GLTransmissionFormat,是一种开放的3D文件格式,Python和C++是两个非常强大的工具,下面我们就来看看如何... 目录什么是gltf文件选择语言的原因安装必要的库解析gltf文件的步骤1. 读取gltf文件2. 提

Java中的runnable 和 callable 区别解析

《Java中的runnable和callable区别解析》Runnable接口用于定义不需要返回结果的任务,而Callable接口可以返回结果并抛出异常,通常与Future结合使用,Runnab... 目录1. Runnable接口1.1 Runnable的定义1.2 Runnable的特点1.3 使用Ru

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Java的volatile和sychronized底层实现原理解析

《Java的volatile和sychronized底层实现原理解析》文章详细介绍了Java中的synchronized和volatile关键字的底层实现原理,包括字节码层面、JVM层面的实现细节,以... 目录1. 概览2. Synchronized2.1 字节码层面2.2 JVM层面2.2.1 ente

Redis 内存淘汰策略深度解析(最新推荐)

《Redis内存淘汰策略深度解析(最新推荐)》本文详细探讨了Redis的内存淘汰策略、实现原理、适用场景及最佳实践,介绍了八种内存淘汰策略,包括noeviction、LRU、LFU、TTL、Rand... 目录一、 内存淘汰策略概述二、内存淘汰策略详解2.1 ​noeviction(不淘汰)​2.2 ​LR

IDEA与JDK、Maven安装配置完整步骤解析

《IDEA与JDK、Maven安装配置完整步骤解析》:本文主要介绍如何安装和配置IDE(IntelliJIDEA),包括IDE的安装步骤、JDK的下载与配置、Maven的安装与配置,以及如何在I... 目录1. IDE安装步骤2.配置操作步骤3. JDK配置下载JDK配置JDK环境变量4. Maven配置下