OpenAI在5月2日公布了一个名为openai-assistants-quickstart的开源项目,旨在演示如何在Next.js框架中实施OpenAI的Assistants API。以后,开发布署一个聊天机器人就真的不再是什么难事了。
通过Assistants API,开发者能够在自己的应用中集成AI助手功能。这些助手能够根据用户的请求,通过命令执行操作,利用模型、工具和文件生成响应。目前,Assistants API支持三类工具,包括代码解释器、文件搜索器和函数执行器。
openai-assistants-quickstart项目提供了一个使用Assistants API的参考模板,适用于Next.js环境。项目集成了流媒体传输、工具应用(涵盖代码解释器和文件搜索)以及函数执行的功能。虽然项目中包含多个页面以展示各项功能,但所有页面均基于同一个AI助手构建,并开放了全部的功能。
OpenAI Assistants API 快速开始[1] 使用 OpenAI Assistants API[2] 和 Next.js[3] 的快速开始模板。
git clone https://github.com/openai/openai-assistants-quickstart.git
cd openai-assistants-quickstart
export OPENAI_API_KEY="sk_..."
(或者在 .env.example
中设置并将其重命名为 .env
)。
npm install
npm run dev
您可以将此项目部署到 Vercel 或任何支持 Next.js 的其他平台。
此项目旨在作为在 Next.js 中使用 Assistants API 的模板,包括 流式处理[6]、工具使用(代码解释器[7] 和 文件搜索[8])以及 函数调用[9]。尽管有多个页面展示了这些能力,但它们都使用相同的底层助手,并且启用了所有能力。
用于聊天的主要逻辑将在 app/components/chat.tsx
的 Chat
组件中找到,并且从 api/assistants/threads
开始的处理程序(位于 api/assistants/threads/...
)。随意开始您自己的项目并复制一些逻辑进去!Chat
组件本身可以直接复制并使用,只要您也复制了 app/components/chat.module.css
的样式。
•基础聊天示例:http://localhost:3000/examples/basic-chat•函数调用示例:http://localhost:3000/examples/function-calling•文件搜索示例:http://localhost:3000/examples/file-search•全功能示例:http://localhost:3000/examples/all
•app/components/chat.tsx
- 处理聊天渲染、流式处理[10]和函数调用[11]转发•app/components/file-viewer.tsx
- 处理文件上传、获取和删除,以进行 文件搜索[12]
•api/assistants
- POST
: 创建助手(仅在启动时使用)•api/assistants/threads
- POST
: 创建新线程•api/assistants/threads/[threadId]/messages
- POST
: 向助手发送消息•api/assistants/threads/[threadId]/actions
- POST
: 通知助手函数调用结果•api/assistants/files
- GET
/POST
/DELETE
: 获取、上传和删除助手文件,用于文件搜索
如果您有任何想法、问题或反馈,请在此表单[13]中告知我们!
本文由山行翻译整理自:https://github.com/openai/openai-assistants-quickstart,如果对您有帮助,请帮忙点赞、关注、收藏,谢谢~
另外,对提示词感兴趣的可以关注一下视频号,上面有比较多的提示词干货分享哦~
[1]
OpenAI Assistants API 快速开始: https://github.com/openai/openai-assistants-quickstart#openai-assistants-api-quickstart
[2]
Assistants API: https://platform.openai.com/docs/assistants/overview
[3]
Next.js: https://nextjs.org/docs
[4]
OpenAI API 密钥: https://platform.openai.com/api-keys
[5]
http://localhost:3000: http://localhost:3000/
[6]
流式处理: https://platform.openai.com/docs/assistants/overview/step-4-create-a-run
[7]
代码解释器: https://platform.openai.com/docs/assistants/tools/code-interpreter
[8]
文件搜索: https://platform.openai.com/docs/assistants/tools/file-search
[9]
函数调用: https://platform.openai.com/docs/assistants/tools/function-calling
[10]
流式处理: https://platform.openai.com/docs/assistants/overview?context=with-streaming
[11]
函数调用: https://platform.openai.com/docs/assistants/tools/function-calling/quickstart?context=streaming&lang=node.js
[12]
文件搜索: https://platform.openai.com/docs/assistants/tools/file-search
[13]
此表单: https://docs.google.com/forms/d/e/1FAIpQLScn_RSBryMXCZjCyWV4_ebctksVvQYWkrq90iN21l1HLv3kPg/viewform?usp=sf_link