跳到主要内容

由OpenAI助手API支持的代理

GPTAssistantAgent 是 AutoGen 框架中的一个强大组件,它利用 OpenAI 的 Assistant API 来增强代理的高级功能。该代理允许集成多种工具,例如 Code Interpreter、File Search 和 Function Calling,从而实现高度可定制和动态的交互模型。

版本要求:

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

GPTAssistantAgent的主要特点:

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

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

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

为了实际说明,这里有一些例子:

在Autogen中创建一个OpenAI助手

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 = {
# define the openai assistant behavior as you need
}
oai_agent = GPTAssistantAgent(
name="oai_agent",
instructions="I'm an openai assistant running in autogen",
llm_config=llm_config,
assistant_config=assistant_config,
)

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

代码解释器

Code Interpreter 赋予您的代理在 OpenAI 提供的安全环境中编写和执行 Python 代码的能力。这解锁了多种功能,包括但不限于:

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

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

assistant_config = {
"tools": [
{"type": "code_interpreter"},
],
"tool_resources": {
"code_interpreter": {
"file_ids": ["$file.id"] # optional. Files that are passed at the Assistant level are accessible by all Runs with this Assistant.
}
}
}

要获取 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")
    )
    # 上传一个用于“助手”目的的文件
    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. Code-Based Uploading:Alternatively, you can upload files and retrieve their file IDs programmatically using the following code snippet:

    from openai import OpenAI
    client = OpenAI(
    # Defaults to os.environ.get("OPENAI_API_KEY")
    )

    # Step 1: Create a Vector Store
    vector_store = client.beta.vector_stores.create(name="Financial Statements")
    print("Vector Store created:", vector_store.id) # This is your vector_store.id

    # Step 2: Prepare Files for Upload
    file_paths = ["edgar/goog-10k.pdf", "edgar/brka-10k.txt"]
    file_streams = [open(path, "rb") for path in file_paths]

    # Step 3: Upload Files and Add to Vector Store (with status polling)
    file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
    vector_store_id=vector_store.id, files=file_streams
    )

    # Step 4: Verify Completion (Optional)
    print("File batch status:", file_batch.status)
    print("Uploaded file count:", file_batch.file_counts.processed)

函数调用

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

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

# learn more from 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:
"""
Retrieves the current weather for a specified location.

Args:
location (str): The location to get the weather for.

Returns:
Union[str, dict]: A dictionary with weather details..
"""

# Simulated response
return {
"location": location,
"temperature": 22.5,
"description": "Partly cloudy"
}

api_schema = get_function_schema(
get_current_weather,
name=get_current_weather.__name__,
description="Returns the current weather data for a specified location."
)

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