Skip to main content

基于 OpenAI Assistant API 的代理

GPTAssistantAgent 是 AutoGen 框架的一个强大组件,利用 OpenAI 的 Assistant API 来增强代理的高级功能。该代理可以集成多个工具,如代码解释器、文件搜索和函数调用,实现高度可定制和动态交互模型。

版本要求:

  • AutoGen:0.2.27 版本或更高。
  • OpenAI:1.21 版本或更高。

GPTAssistantAgent 的主要特点:

  • 多工具掌握:代理可以利用 OpenAI 的内置工具,如代码解释器文件搜索,以及通过函数调用创建或集成的自定义工具。

  • 流畅的对话管理:通过持久线程自动存储消息历史记录并根据模型的上下文长度进行调整,简化开发过程,使您可以专注于添加新消息,而不是管理对话流程。

  • 文件访问和集成:使代理能够访问和利用各种格式的文件。文件可以在代理创建过程中或通过线程在对话过程中被引入。此外,代理可以生成文件(如图像、电子表格)并在其响应中引用已引用的文件。

以下是一些实际示例:

在 Autogen 中创建 OpenAI Assistant

import os

from autogen import config_list_from_json
from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent

assistant_id = os.environ.get("ASSISTANT_ID", None)
config_list = config_list_from_json("OAI_CONFIG_LIST")
llm_config = {
"config_list": config_list,
}
assistant_config = {
# 根据需要定义 OpenAI Assistant 的行为
}
oai_agent = GPTAssistantAgent(
name="oai_agent",
instructions="我是在 Autogen 中运行的 OpenAI Assistant",
llm_config=llm_config,
assistant_config=assistant_config,
)

使用 OpenAI Assistant 的内置工具和函数调用

代码解释器

代码解释器使您的代理能够在OpenAI提供的安全环境中编写和执行Python代码。这将解锁多种功能,包括但不限于:

  • 处理数据:处理各种数据格式并即时操作数据。
  • 生成输出:创建新的数据文件甚至可视化图表等。

使用以下配置使用代码解释器。

assistant_config = {
"tools": [
{"type": "code_interpreter"},
],
"tool_resources": {
"code_interpreter": {
"file_ids": ["$file.id"] # 可选。在助手级别传递的文件可供所有具有此助手的运行访问。
}
}
}

要获取file.id,您可以使用两种方法:

  1. OpenAI Playground:利用OpenAI Playground,一个可在https://platform.openai.com/playground访问的交互式平台,上传您的文件并获取相应的文件ID。

  2. 基于代码的上传:或者,您可以使用以下代码片段以编程方式上传文件并检索其文件ID:

    from openai import OpenAI
    client = OpenAI(
    # 默认为os.environ.get("OPENAI_API_KEY")
    )
    # 使用"assistants"目的上传文件
    file = client.files.create(
    file=open("mydata.csv", "rb"),
    purpose='assistants'
    )

文件搜索

文件搜索工具使您的代理能够利用其预训练模型之外的知识。这使您可以将自己的文档和数据(例如产品信息或代码文件)纳入代理的功能中。

使用以下配置使用文件搜索。

assistant_config = {
"tools": [
{"type": "file_search"},
],
"tool_resources": {
"file_search": {
"vector_store_ids": ["$vector_store.id"]
}
}
}

以下是如何使用两种方法获取vector_store.id:

  1. OpenAI Playground:利用OpenAI Playground,一个可在https://platform.openai.com/playground访问的交互式平台,创建一个向量存储,上传您的文件并将其添加到向量存储中。完成后,您将能够检索相关的`vector_store.id`。

  2. 基于代码的上传:或者,您可以使用以下代码片段以编程方式上传文件并检索其文件ID:

    from openai import OpenAI
    client = OpenAI(
    # 默认为os.environ.get("OPENAI_API_KEY")
    )

    # 步骤1:创建向量存储
    vector_store = client.beta.vector_stores.create(name="财务报表")
    print("向量存储已创建:", vector_store.id) # 这是您的vector_store.id

    # 步骤2:准备上传文件
    file_paths = ["edgar/goog-10k.pdf", "edgar/brka-10k.txt"]
file_streams = [open(path, "rb") for path in file_paths]

# 步骤 3:上传文件并添加到向量存储库(带状态轮询)
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id, files=file_streams
)

# 步骤 4:验证完成(可选)
print("文件批处理状态:", file_batch.status)
print("已上传文件数量:", file_batch.file_counts.processed)

函数调用

函数调用使您能够通过预定义的功能扩展您的代理程序的功能,从而允许您描述自定义函数给助手,实现智能函数选择和参数生成。

使用以下配置进行函数调用。

# 了解更多信息:https://platform.openai.com/docs/guides/function-calling/function-calling
from autogen.function_utils import get_function_schema

def get_current_weather(location: str) -> dict:
"""
获取指定位置的当前天气。

Args:
location (str): 要获取天气的位置。

Returns:
Union[str, dict]: 包含天气详情的字典。
"""

# 模拟响应
return {
"location": location,
"temperature": 22.5,
"description": "局部多云"
}

api_schema = get_function_schema(
get_current_weather,
name=get_current_weather.__name__,
description="返回指定位置的当前天气数据。"
)

assistant_config = {
"tools": [
{
"type": "function",
"function": api_schema,
}
],
}