autogen_ext.agents.openai#
- class OpenAIAssistantAgent(name: str, description: str, client: AsyncOpenAI | AsyncAzureOpenAI, model: str, instructions: str, tools: Iterable[字面量['code_interpreter', 'file_search'] | 工具 | Callable[[...], 任何] | Callable[[...], Awaitable[任何]]] | 无 = None, assistant_id: str | 无 = None, thread_id: str | 无 = None, metadata: object | 无 = None, response_format: 字面量['auto'] | ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | 无 = None, temperature: float | 无 = None, tool_resources: ToolResources | 无 = None, top_p: float | 无 = None)[源代码]#
-
一个使用Assistant API生成响应的代理实现。
安装:
pip install "autogen-ext[openai]" # pip install "autogen-ext[openai,azure]" # For Azure OpenAI Assistant
该代理利用Assistant API创建具备以下功能的AI助手:
代码解释与执行
文件处理和搜索
自定义函数调用
多轮对话
代理维护一个对话线程,并可以使用各种工具包括
代码解释器:用于执行代码和处理文件
文件搜索:用于搜索上传的文档
自定义函数:用于通过用户定义的工具扩展功能
主要特点:
支持多种文件格式,包括代码、文档、图像
每个助手最多可以处理128个工具
在对话线程中维护上下文
支持文件上传用于代码解释器和搜索
为高效文件搜索集成向量存储
自动文件解析和嵌入
你可以通过提供thread_id或assistant_id参数来使用现有的线程或助手。
示例
使用助手来分析CSV文件中的数据:
from openai import AsyncOpenAI from autogen_core import CancellationToken import asyncio from autogen_ext.agents.openai import OpenAIAssistantAgent from autogen_agentchat.messages import TextMessage async def example(): cancellation_token = CancellationToken() # Create an OpenAI client client = AsyncOpenAI(api_key="your-api-key", base_url="your-base-url") # Create an assistant with code interpreter assistant = OpenAIAssistantAgent( name="Python Helper", description="Helps with Python programming", client=client, model="gpt-4", instructions="You are a helpful Python programming assistant.", tools=["code_interpreter"], ) # Upload files for the assistant to use await assistant.on_upload_for_code_interpreter("data.csv", cancellation_token) # Get response from the assistant response = await assistant.on_messages( [TextMessage(source="user", content="Analyze the data in data.csv")], cancellation_token ) print(response) # Clean up resources await assistant.delete_uploaded_files(cancellation_token) await assistant.delete_assistant(cancellation_token) asyncio.run(example())
使用 Azure OpenAI 助手进行 AAD 身份验证:
from openai import AsyncAzureOpenAI import asyncio from azure.identity import DefaultAzureCredential, get_bearer_token_provider from autogen_core import CancellationToken from autogen_ext.agents.openai import OpenAIAssistantAgent from autogen_agentchat.messages import TextMessage async def example(): cancellation_token = CancellationToken() # Create an Azure OpenAI client token_provider = get_bearer_token_provider(DefaultAzureCredential()) client = AsyncAzureOpenAI( azure_deployment="YOUR_AZURE_DEPLOYMENT", api_version="YOUR_API_VERSION", azure_endpoint="YOUR_AZURE_ENDPOINT", azure_ad_token_provider=token_provider, ) # Create an assistant with code interpreter assistant = OpenAIAssistantAgent( name="Python Helper", description="Helps with Python programming", client=client, model="gpt-4o", instructions="You are a helpful Python programming assistant.", tools=["code_interpreter"], ) # Get response from the assistant response = await assistant.on_messages([TextMessage(source="user", content="Hello.")], cancellation_token) print(response) # Clean up resources await assistant.delete_assistant(cancellation_token) asyncio.run(example())
- Parameters:
name (str) – 助手的名称
description (str) – 描述助手的用途
client (AsyncOpenAI | AsyncAzureOpenAI) – OpenAI客户端或Azure OpenAI客户端实例
model (str) – 使用的模型(例如“gpt-4”)
instructions (str) – 助手的系统指令
工具 (可选[可迭代[联合[字面量["code_interpreter", "file_search"], 工具 | 可调用[..., 任意] | 可调用[..., 可等待[任意]]]]]) – 助手可以使用的工具
assistant_id (可选[str]) – 要使用的现有助手的ID
thread_id (可选[str]) – 要使用的现有线程的ID
metadata (可选[对象]) – 助手的额外元数据
response_format (可选[AssistantResponseFormatOptionParam]) – 响应格式设置
temperature (可选[浮点数]) – 用于生成响应的温度
tool_resources (可选[ToolResources]) – 额外的工具配置
top_p (Optional[float]) – Top p 采样参数
- async delete_assistant(cancellation_token: CancellationToken) 无 [源代码]#
如果是由此实例创建的助手,请删除它。
- async delete_uploaded_files(cancellation_token: CancellationToken) 无 [源代码]#
删除由该代理实例上传的所有文件。
- async delete_vector_store(cancellation_token: CancellationToken) 无 [源代码]#
如果向量存储是由此实例创建的,请删除它。
- async handle_text_message(content: str, cancellation_token: CancellationToken) 无 [源代码]#
通过将常规文本消息添加到线程中来处理它们。
- property messages: AsyncMessages#
- async on_messages(messages: Sequence[Annotated[TextMessage | MultiModalMessage | StopMessage | 工具调用摘要信息 | HandoffMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], cancellation_token: CancellationToken) 响应 [源代码]#
处理传入的消息并返回响应。
- async on_messages_stream(messages: Sequence[Annotated[TextMessage | MultiModalMessage | StopMessage | 工具调用摘要信息 | HandoffMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], cancellation_token: CancellationToken) AsyncGenerator[Annotated[ToolCallRequestEvent | ToolCallExecutionEvent | MemoryQueryEvent | UserInputRequestedEvent | ModelClientStreamingChunkEvent | ThoughtEvent, FieldInfo(annotation=NoneType, required=True, discriminator='type')] | Annotated[TextMessage | MultiModalMessage | StopMessage | 工具调用摘要信息 | HandoffMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')] | 响应, 无] [源代码]#
处理传入的消息并返回一个响应。
- async on_reset(cancellation_token: CancellationToken) 无 [源代码]#
通过删除自初始化以来的新消息和运行来处理重置命令。
- async on_upload_for_code_interpreter(file_paths: str | Iterable[str], cancellation_token: CancellationToken) 无 [源代码]#
处理代码解释器的文件上传。
- async on_upload_for_file_search(file_paths: str | Iterable[str], cancellation_token: CancellationToken) 无 [源代码]#
处理文件上传以进行文件搜索。
- property produced_message_types: Sequence[类型[Annotated[TextMessage | MultiModalMessage | StopMessage | 工具调用摘要信息 | HandoffMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]]]#
助手代理生成的消息类型。
- property runs: AsyncRuns#
- property threads: AsyncThreads#