Assistant实际上是 OpenAI 版本的自治代理。OpenAI 与 Assistant API 的愿景是帮助开发人员构建强大的 AI 助手,这些助手能够通过一系列工具执行各种任务。
一些背景
在尝试 OpenAI 的 Assistant API 时,我感觉到 OpenAI 将 Assistant 视为基于 LLM 的代理,或使用工具的自主代理。
要创建 Assistant,您需要指定 model Assistant 必须使用的。
instructions 和 content 参数可用于用自然语言描述助手。可以 instructions 定义助手的目标,类似于用于聊天的系统消息。
OpenAI 助手可以访问多达 128 个工具,这些工具可以由 OpenAI 托管,也可以是第三方工具,通过function call 访问。
目前还有两种基于 OpenAI 的工具,分别 code_interpreter 和 retrieval 。
OpenAI 的下表定义了Assistant的构建块。
![](http://www.openaimy.com/wp-content/uploads/2023/11/d3d6ecdfee0487620a10f3bc10855034.png)
(Assistant)助手可以做什么?
助手可以访问 OpenAI 模型,其中包含描述助手个性和功能的说明。对于其他框架(如LangChain)上下文中的代理,某种元提示是代理的核心。
因此,OpenAI 的这种方法在很大程度上是一种无提示的方法。
助手可以并行访问多个工具,包括 OpenAI 托管工具和通过函数调用访问的自定义工具。
对话和对话管理是通过线程执行的。线程是持久的,用于存储消息历史记录。当对话对于模型上下文长度来说太长时,也会执行对话的截断。
线程创建一次,并在对话进行时将消息附加到消息列表中。
检索工具
助手的功能通过检索得到扩展,整合了其固有模型之外的外部知识。这可能包括用户提供的专有产品数据或文档。
当文档上传并传输到助手时,OpenAI 会自动将文档分成更小的段,创建索引,存储嵌入,并利用矢量搜索获取相关信息以响应用户查询。
允许的最大文件大小为 512MB。检索支持多种文件格式,例如 .pdf、.md、.docx 等。
您可以在下面的“支持的文件”部分中找到有关支持的文件扩展名及其相应的MIME类型的更多信息。
![](http://www.openaimy.com/wp-content/uploads/2023/11/ef3d2e80ecd6c1449b6531ec917e0517.png)
我可以看到一个场景,其中助手是通过 Python 创建和开发的,并通过 OpenAI GUI 进行监控和测试。这种方法在某种程度上让人想起了LangSmith。
![](http://www.openaimy.com/wp-content/uploads/2023/11/f3dd7a0f55fd5b310793d461e5c2018e.png)
在 GUI 上可以看到线程下方,这些对话都是通过 Python notebook进行的,并且可以通过 GUI 检查会话线程。
![](http://www.openaimy.com/wp-content/uploads/2023/11/3a45e361b46b4e0f7510c5e50c6fe6b3.png)
以下是在 Python Notebook中运行 OpenAI 助手并使用 Retriever 工具的完整代码。您只需要一个 OpenAI API 密钥。
pip install openai
import os
import openai
import requests
import json
from openai import OpenAI
#Pass the retrieval in the tools parameter of the Assistant to enable Retrieval
api_key = “sk-rxolBSN8pMbAaQ7DUsxlT3BlbkFsfsfsfsfswrewrgwwrAfKazZs”
client = OpenAI(api_key=api_key)
assistant = client.beta.assistants.create(
name=”General Knowledge Bot”,
instructions=”You are a customer support chatbot. Use your knowledge base to best respond to customer queries.”,
model=”gpt-4-1106-preview”,
tools=[{“type”: “retrieval”}]
)
#File upload via Colab Notebook
from google.colab import files
uploaded = files.upload()
for name, data in uploaded.items():
with open(name, ‘wb’) as file:
file.write(data)
print (‘saved file’, name)
#Pass the retrieval in the tools parameter of the Assistant to enable Retrieval
#Accessing the uploaded
file = client.files.create(
file=open(“/content/Rugby_World_Cup.txt”, “rb”),
purpose=’assistants’
)
api_key = “sk-rxolBSN8pMbAaQ7DUsxlT3BlbkFsfsfsfsfswrewrgwwrAfKazZs”
client = OpenAI(api_key=api_key)
assistant = client.beta.assistants.create(
name=”General Knowledge Bot”,
instructions=”You answer general knowledge questions as accureately as possible.”,
model=”gpt-4-1106-preview”,
tools=[{“type”: “retrieval”}]
)
##Files can also be added to a Message in a Thread. These files are only accessible within this specific thread.
##After having uploaded a file, you can pass the ID of this File when creating the Message.
message = client.beta.threads.messages.create(
thread_id=thread.id,
role=”user”,
content=”Who won the 2023 rugby world cup?”,
file_ids=[file.id]
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions=”You answer general knowledge questions as accureately as possible.”
)
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print (messages)
在线程下方,提出了一个关于最近发生的事件的问题,该事件超出了 LLM 的知识范围,以及助手通过检索器工具检索数据。