OpenAI Assistants API:如何使用代码或无需代码创建您自己的AI助手

It's now easier than ever to create your own AI Assistant that can handle a lot of computing tasks for you. See how you can get started with the OpenAI AI Assistant API.

现在比以往任何时候都更容易创建您自己的AI助手,它可以为您处理许多计算任务。了解如何使用OpenAI AI助手API开始操作。

New updates on 17th of April 2024 from OpenAI

来自OpenAI的2024年4月17日最新更新

OpenAI announced significant updates to the Assistants API. The update includes enhancements aimed at improving the capabilities and performance of the assistant.

OpenAI宣布了对Assistants API的重大更新。此次更新包括旨在提高助手功能和性能的改进。

  • Knowledge retrieval now supports up to 10,000 files.  知识检索支持高达10,000个文件
  • More control over token and model configuration.  对令牌和模型配置拥有更多的控制权。
  • Streaming support.   流式传输支持。

It's been a few days since OpenAI's DevDay and lots of interesting features and updates have been announced. Today, we're specifically interested in the new AI Assistant feature. So without further ado, let's get started!

OpenAI的开发者日已经过去几天了,宣布了许多有趣的功能和更新。今天,我们特别关注新的AI助手功能。那么,让我们开始吧!

"You just ask the computer for what you need, and it'll do all of these tasks for you." - Sam Altman - CEO, OpenAI

“你只需要向电脑提出你需要什么,它就会为你完成所有这些任务。”——萨姆·阿尔特曼(Sam Altman),OpenAI 首席执行官

AI Assistants        AI助手

An AI Assistant performs actions based on a given set of instructions and tools. You can think of an AI Assistant as a digital assistant (or agent) that you hire to solve specific problems for your use case.

AI助手根据给定的一组指令和工具来执行操作。你可以将AI助手视为你雇用的数字助手(或代理),用于解决特定用例中的特定问题。

AI Assistants are hosted within the OpenAI platform and use a chosen large language model to think, communicate, and take action.

AI助手托管在OpenAI平台上,并使用选定的大型语言模型进行思考、交流和采取行动。

We're going to see how to create an AI Assistant that reads a PDF file and converts it to a Twitter thread.

我们将学习如何创建一个AI助手,该助手可以读取PDF文件并将其转换为Twitter帖子。

We'll call it X Threads Converter.        我们将它称为“X线程转换器”。

How do I create an AI Assistant?        我如何创建一个AI助手?

There are two ways to do this:        有2种方式

    • Using the OpenAI Web Interface   使用OpenAI的Web界面 
    • Using the OpenAI Assistants API   使用OpenAI Assistants API

Method 1: Using the OpenAI Web Interface        方法1:使用OpenAI的Web界面

For this method, we do not need to write any code since everything will be done using the Web Interface provided by the OpenAI platform.

对于这种方法,我们不需要编写任何代码,因为所有操作都将通过OpenAI平台提供的Web界面来完成。

Let's go ahead and create our X Threads Converter AI Assistant using the Web Interface. Follow the steps below:

让我们继续使用Web界面来创建我们的X线程转换器AI助手。请按照以下步骤操作:

Step 1: Log into your OpenAI account        登录openai 账号
Step 2: On the left side navigation bar, you'll see the new Assistants tab:

步骤2:在左侧的导航栏中,您将看到新的“助手”选项卡:

Step 3: Click on the Assistants tab:        点击 助手 页签

As you can see, I don't have any assistants yet. We're going to create a new one.

如您所见,我还没有任何助手。我们将创建一个新的。

Step 4: Click on Create. You'll see the following options:

步骤4:点击“创建”。您将看到以下选项:

To create a new assistant, we'll need to enter the following:

要创建一个新的助手,我们需要输入以下内容:

  • Name: Let's call it X Threads Converter.    名称:让我们将其命名为“X线程转换器”。
  • Instructions: Use the following instructions: "You are a helpful assistant. Given a set of files, you extract the most interesting information and restructure it into Threads format for Twitter."

指令:使用以下指令:“你是一个有用的助手。给定一组文件,你提取最有趣的信息,并将其重新组织成Twitter帖子串的格式。”

  • Model: Select one of the supported OpenAI models. I will choose the new gpt-4-1106-preview model.

模型:选择支持的OpenAI模型之一。我将选择新的gpt-4-1106-preview模型。

  • Tools: We're just going to enable Retrieval for this example. This is because our assistant will need to retrieve the contents from the uploaded files.

工具:对于这个示例,我们只需要启用检索功能。这是因为我们的助手需要从上传的文件中检索内容。

  • Files: You can upload any supported file here. I uploaded a PDF version of this post. If you want to do the same, just open the link in your browser and save it as PDF.

文件:您可以在此处上传任何支持的文件。我上传了这篇帖子的PDF版本。如果您也想这样做,只需在浏览器中打开链接并将其保存为PDF即可。

Step 5: Click on Save. Your configuration should look like this:

步骤5:点击“保存”。您的配置应该如下所示:

That's it! Go ahead and click on the Save button. Our X Threads Converter will be ready in a few seconds.

就是这样!继续并点击“保存”按钮。我们的X线程转换器将在几秒钟内准备就绪。

Step 6: Try it out in the Playground:        步骤6:在测试区尝试一下:

Click on the "Test in Playground" button. The Playground is a chat interface where you can test and optimize your Assistant.

点击“在测试区测试”按钮。测试区是一个聊天界面,您可以在其中测试和优化您的助手。

Let's ask our shiny new assistant to "Create the X Threads Post" click "Add and run" and see what happens.

让我们向这个闪亮的新助手发出“创建X Threads帖子”的请求,点击“添加并运行”,看看会发生什么。

The AI Assistant will take a few seconds to think and process your request.

AI助手会花几秒钟的时间来思考和处理您的请求。

Behind the scenes, the following is happening:

在幕后,正在发生以下事情:

  1. The contents of the PDF are converted to Vector Embeddings

PDF的内容被转换为向量嵌入(Vector Embeddings)

  • Our Vector Embeddings are stored in a Vector Database

我们的向量嵌入(Vector Embeddings)被存储在向量数据库中。

  • The Query is used to find relevant and similar embeddings

查询用于查找相关和相似的嵌入

  • The relevant parts are sent to the chosen large language model

相关的部分被发送到所选的大型语言模型

  • The LLM will process and return the final output

大型语言模型(LLM)将处理并返回最终输出

You should end up with something like this:

您应该会得到类似这样的结果:

X Threads Converter        X 线程转换器

Based on the text extracted from the uploaded PDF, I will create a Twitter thread format for you. However, due to the length limitations of Twitter posts (280 characters each), the thread will summarize the content in concise tweets.

基于从上传的PDF中提取的文本,我将为您创建Twitter线程格式。但是,由于Twitter帖子的长度限制(每条280个字符),线程将以简洁的推文形式总结内容。

That's impressive! In no time we were able to build an assistant that can create a Twitter Thread given any PDF file. More importantly, we wrote exactly zero lines of code.

太令人印象深刻了!我们很快就构建了一个助手,可以根据任何PDF文件创建Twitter线程。更重要的是,我们一行代码都没有写。

Method 2: Using the OpenAI Assistants API        方法2:使用OpenAI助手API

Let's go ahead and create our X Threads Converter AI Assistant using the OpenAI Assistants API:

让我们继续使用OpenAI助手API来创建我们的X Threads Converter AI助手:

Step 1: Install the Official OpenAI Python SDK        步骤1:安装官方OpenAI Python SDK

In your terminal, type the below and hit return:        在您的终端中,输入以下命令并按下回车键:

pip install openai

To verify that the OpenAI SDK has been successfully installed, run the following command:

为了验证OpenAI SDK已成功安装,请运行以下命令:

pip show openai
Step 2: Import Required Packages        步骤2:导入所需的包

Great, now we can create a new directory for our project. In your terminal type the following commands:

很好,现在我们可以为我们的项目创建一个新目录。在您的终端中输入以下命令:

  • Create a new directory: mkdir openai-ai-assistant-demo

创建一个新目录:mkdir openai-ai-assistant-demo

  • cd into the new directory and create assistant.py file: cd openai-ai-assistant-demo && touch assistant.py

进入新目录并创建assistant.py文件:cd openai-ai-assistant-demo && touch assistant.py

  • Open the assistant.py file and import the OpenAI SDK

打开assistant.py文件并导入OpenAI SDK

from openai import OpenAI
Step 3: Create the OpenAI Client        步骤3:创建OpenAI客户端

We'll now create the OpenAI client. Make sure to replace with your own OpenAI API Key.

现在我们将创建OpenAI客户端。请确保使用您自己的OpenAI API密钥进行替换。

client = OpenAI(
    api_key="YOUR_API_KEY",
)
Step 4: Prepare the PDF File for Upload        步骤4:准备PDF文件以进行上传

Next, place the PDF file in the openai-ai-assistant-demo directory. In my case, the file name is document.pdf.

接下来,将PDF文件放在openai-ai-assistant-demo目录中。在我的情况下,文件名是document.pdf。

Let's create the file using the OpenAI SDK using client.files.create:

让我们使用client.files.create通过OpenAI SDK创建文件:

file = client.files.create(
  file=open("document.pdf", "rb"),
  purpose='assistants'
)

file_id = file.id

Here, we specify the purpose property as 'assistants' so that the file becomes accessible by the Assistants API.

在这里,我们将purpose属性指定为'assistants',以便文件可通过助手API进行访问。

Step 5: Define the Required AI Assistant Tools        步骤5:定义所需的AI助手工具

We create our tools variable which is a list of required tools that the AI Assistant will have access to. In this case, we only need the retrieval tool.

我们创建一个名为“tools”的变量,它是一个列表,包含AI助手可以访问的所需工具。在这种情况下,我们只需要检索工具。

tools = [
    {
        "type": "retrieval"
    }
]

Step 6: Create our AI Assistant using client.beta.assistants.create:

步骤6:使用client.beta.assistants.create创建我们的AI助手:

Finally, we'll use the client.beta.assistants.create and pass the following parameters:

最后,我们将使用client.beta.assistants.create并传递以下参数:

assistant = client.beta.assistants.create(
    name="X Threads Converter",
    instructions="You are a helpful assistant. Given a set of files, you extract the most interesting information and restructure it into Threads format for Twitter.",
    model="gpt-4-1106-preview",
    tools=tools,
    file_ids=[file.id]
)

assistant_id = assistant.id

The values used in the above code are exactly the same as the ones used to create the AI Assistant using the web interface in the previous example (Method 1).

上述代码中使用的值与在前面的示例(方法1)中使用网页界面创建AI助手时使用的值完全相同。

Step 7: Run the Code        步骤7:运行代码

To create our X Threads Converter AI Assistant, execute the assistant.py file:

创建我们的X Threads Converter AI助手,执行assistant.py文件:

python assistant.py

Once done, refresh the Assistants page​ and we can see our new X Threads Converter AI Assistant in the Assistants list.

一旦完成,刷新助手页面,我们就可以在助手列表中看到我们新的X Threads Converter AI助手了。

Once AI Assistant is up and running. We can take it for a spin in the Playground just like we did in Method 1.

一旦AI助手启动并运行。我们可以像在方法1中一样,在测试场上试用它。

How do I Use an AI Assistant?        我如何使用AI助手?

The Playground provided by OpenAI is a great tool to test our AI Assistant quickly. But, how do you actually use the AI Assistant using the API?

OpenAI提供的测试场是一个快速测试我们的AI助手的绝佳工具。但是,你如何实际使用API来使用AI助手呢?

Let's explore how AI Assistants work and go over two important concepts: Threads and Runs.

让我们探索AI助手是如何工作的,并介绍两个重要的概念:线程和运行。

What are Threads?        什么是线程?

Each thread represents a conversation. A thread contains a list of messages each belonging to the user and the large language model.

每个线程都代表一次对话。一个线程包含一系列消息,这些消息分别属于用户和大型语言模型。

That conversation with your spouse on WhatsApp? That's a thread.

你在WhatsApp上与配偶的对话?那就是一个线程。

The task we want to send to our assistant: "Create the X Threads Post." is represented as a message within a thread.

我们想要发送给助手的任务:“创建X Threads帖子。”在线程中表示为一条消息。

What are Runs?        什么是运行?

A Run represents the execution of a thread. A Run also needs to know which AI Assistant to use for that execution. Once initialized, the Run will determine which steps are needed to complete the task.

一个“运行”表示一个线程的执行。一个“运行”还需要知道应该使用哪个AI助手来执行这个任务。一旦初始化,“运行”将确定完成该任务需要哪些步骤。

To create a run we'll need to supply the thread_id and assistant_id parameters.

要创建一个运行,我们需要提供thread_id和assistant_id参数。

Creating a Thread        创建一个线程

To create a Thread, we'll need to initialize it with a list of messages. For our example, there's only one message, which is: "Create the X Threads Post.".

要创建一个线程,我们需要用消息列表来初始化它。对于我们的示例,只有一条消息,即:“Create the X Threads Post.”。

Since our AI Assistant needs to process data from a PDF file, we'll also need to specify the file that it needs to use. We can find the file id on this page. Alternatively, the id property is accessible after you initialize the file object in Step 4 by calling file.id.

由于我们的AI助手需要处理PDF文件中的数据,我们还需要指定它需要使用哪个文件。我们可以在这个页面上找到文件ID。或者,在步骤4中初始化文件对象后,通过调用file.id也可以访问id属性。

Let's create our thread using client.beta.threads.create:

让我们使用client.beta.threads.create来创建我们的线程:

thread = client.beta.threads.create(
  messages=[
    {
      "role": "user",
      "content": "Create the X Thread Post.",
      "file_ids": [file_id]
    }
  ]
)

thread_id = thread.id

We'll need the thread_id property below when creating the Run.

在创建“运行”时,我们需要使用下面的thread_id属性。

Creating a Run        创建一个运行

Let's create our Run using the client.beta.threads.runs.create method:

让我们使用client.beta.threads.runs.create方法来创建我们的“运行”:

run = client.beta.threads.runs.create(
  thread_id=thread_id,
  assistant_id=assistant_id
)

run_id = run.id

The assistant_id could be accessed using the id property after the assistant variable is created in Step 6.

 在步骤6中创建助手(assistant)变量后,可以通过访问其id属性来获取assistant_id。

Note: Using the API, you can retrieve all required IDs. For instance, the assistant_id could be retrieved by listing all the assistants and choosing the correct one using the client.beta.assistants.list method. 

注意:通过API,您可以检索所有必需的ID。例如,可以通过列出所有助手并选择正确的一个,使用client.beta.assistants.list方法来检索assistant_id。

Checking Run Status        检查运行状态

Our run will take a few seconds to complete. This is because the AI Assistant needs to determine the steps needed to complete the task. It also needs to process, store, and retrieve information from the PDF file and talk to the LLM for further processing.

我们的运行将需要几秒钟来完成。这是因为AI助手需要确定完成任务所需的步骤。它还需要处理、存储和从PDF文件中检索信息,并与LLM(大型语言模型)进行通信以进行进一步处理。

When using other tools such as the code_interpreter the Run may take even more time as it generates and runs Python code iteratively.

当使用其他工具(如code_interpreter)时,运行可能需要更长时间,因为它会迭代地生成和运行Python代码。

Therefore, we'll need to periodically check if the Run is completed. We can do this by checking its status property by calling the client.beta.threads.runs.retrieve method, as shown below:

因此,我们需要定期检查运行是否已完成。我们可以通过调用client.beta.threads.runs.retrieve方法来检查其状态属性,如下所示:

run = client.beta.threads.runs.retrieve(
  thread_id=thread_id,
  run_id=run_id
)

Once the status property changes to completed, we can see the model's response by retrieving the messages within our thread:

一旦状态属性更改为“已完成”,我们就可以通过检索线程中的消息来查看模型的响应:

thread_messages = client.beta.threads.messages.list(thread_id)
print(thread_messages.data)

Closing thoughts        总结

As we've seen, this should get you up and running with the new OpenAI AI Assistants in no time. It is now easier than ever to create your AI Assistant that can help you perform a variety of tasks useful to your business.

正如我们所见,这将使您很快上手新的OpenAI AI助手。现在比以往任何时候都更容易创建AI助手,帮助您执行各种对业务有用的任务。

You may also want to think about the fact that your application will be locked into the OpenAI platform. This means that if you decide to migrate to another LLM, storage, and vector database at a later time it may not be a simple task.

您可能还需要考虑到,您的应用程序将被锁定在OpenAI平台上。这意味着如果您决定在将来迁移到另一个大型语言模型(LLM)、存储和向量数据库,这可能不是一项简单的任务。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/710534.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【MySQL】事务一

事务一 1.什么是事务2.为什么会存在事务3.事务的版本支持4.事务的提交方式5.事务常见操作方式6.事务隔离级别6.1读未提交【Read Uncommitted】6.2读提交【Read Committed】6.3可重复读【Repeatable Read】6.4串行化【serializable】 点赞👍👍收藏&#x…

线性DP,状态优化,CF 958C2 - Encryption (medium)

一、题目 1、题目描述 2、输入输出 2.1输入 2.2输出 3、原题链接 Problem - 958C2 - Codeforces 二、解题报告 1、思路分析 明显的dp 我们可以写一个会超时的朴素dp 状态定义 f[i][j]为前i个数字,划分为j组的最大收益 那么f[i][j] max{ f[x][j - 1] sum(…

这世上又多了一只爬虫(spiderflow)

让我们一起默念: 爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫爬虫 接着大声喊出来: 一!只!爬!虫!呀!爬!呀!爬&#xf…

iOS ReactiveCocoa MVVM

学习了在MVVM中如何使用RactiveCocoa,简单的写上一个demo。重点在于如何在MVVM各层之间使用RAC的信号来更方便的在各个层之间进行响应式数据交互。 demo需求:一个登录界面(登录界面只有账号和密码都有输入,登录按钮才可以点击操作)&#xff0…

【linux】应用程序访问百度时,操作系统内核网络接口日志

代码合入: 登录 - Gitee.comhttps://gitee.com/r77683962/linux-6.9.0/commit/c639573cc7c4984913d4a89884347e5a30a51eac 启动操作系统运行dmesg的日志像这样: dmesg_log/2024_06_14_00_40_54.txt r77683962/linux-6.9.0 - Gitee.com 注意&#xf…

C语言小例程20/100

题目&#xff1a;一个数如果恰好等于它的因子之和&#xff0c;这个数就称为"完数"。例如61&#xff0b;2&#xff0b;3.编程找出1000以内的所有完数。 #include<stdio.h> #define N 1000 int main() {int i,j,k,n,sum;int a[256];for(i2;i<N;i){suma[0]1;k…

Nintex流程平台引入生成式人工智能,实现自动化革新

工作流自动化提供商Nintex宣布在其Nintex流程平台上推出一系列新的人工智能驱动改进。这些增强显著减少了文档化、管理和自动化业务流程所需的时间。这些新特性为Nintex流程平台不断扩展的人工智能能力增添了新的亮点。 Nintex首席产品官Niranjan Vijayaragavan表示&#xff1a…

阿里新发布的UniAnimate现高效人像动画生成;在ComfyUI中使用Stable 3模型;音频版的gpt2o;将 PDF 文档转换为音频播客

✨ 1: UniAnimate 阿里新发布的UniAnimate通过统一的视频扩散模型&#xff0c;实现高效人像动画生成&#xff0c;支持长视频生成 UniAnimate 是一种专注于一致性人像动画生成的统一视频扩散模型。该模型通过映射参考图像、姿势指导和噪声视频到一个共同特征空间&#xff0c;实…

SSM家乡旅游网-计算机毕业设计源码04802

摘 要 随着科学技术的飞速发展&#xff0c;社会的方方面面、各行各业都在努力与现代的先进技术接轨&#xff0c;通过科技手段来提高自身的优势&#xff0c;SSM家乡旅游网当然也不能排除在外。SSM家乡旅游网是以实际运用为开发背景&#xff0c;运用软件工程开发方法&#xff0c…

Django配置连接池:使用django-db-connection-pool配置连接池

一、该三方库文档使用 github地址&#xff1a; https://github.com/altairbow/django-db-connection-pool/blob/1.2.5/README_CN.mdhttps://github.com/altairbow/django-db-connection-pool/blob/1.2.5/README_CN.md1、选择指定版本&#xff0c;查看指定版本的文档和配置&am…

Xcode无法使用设备:Failed to prepare the device for development

问题&#xff1a; Xcode无法使用设备开发&#xff0c;失败报错如下&#xff1a; Failed to prepare the device for development. This operation can fail if the version of the OS on the device is incompatible with the installed version of Xcode. You may also need…

容性负载箱在电子元器件制造中的应用有哪些?

容性负载箱是一种能够模拟实际负载的电子设备&#xff0c;主要用于测试电源、变频器、逆变器等电力电子设备的性能。在电子元器件制造中&#xff0c;容性负载箱的应用非常广泛&#xff0c;主要体现在以下几个方面&#xff1a; 1. 电源测试&#xff1a;电源是电子元器件正常工作…

【触想智能】壁挂式工业一体机在智能制造行业上的应用分析

随着智能制造的兴起&#xff0c;壁挂式工业一体机成为了越来越多工厂的首选设备。壁挂式工业一体机是一种高性能的计算机&#xff0c;内置多种工业级传感器和执行器&#xff0c;可以实时获取工厂生产过程中的各种数据&#xff0c;并与其他设备进行无缝连接。 为了大家更深入的了…

自动生成企业培训视频:创新与效率的完美结合

前言 随着人工智能技术的飞速发展&#xff0c;大模型技术在各个领域的应用日益广泛。在企业培训领域&#xff0c;大模型技术的应用为培训视频的生成带来了革命性的变革。本文将探讨如何利用大模型技术自动生成企业培训视频&#xff0c;以及这一技术为企业培训带来的创新和效率…

停止游戏中的循环扣血显示

停止游戏中循环扣血并显示的具体实现方式会依赖于你的代码结构和游戏的逻辑。通常情况下&#xff0c;你可以通过以下方式来实现停止循环扣血和显示&#xff1a; 1、问题背景 在使用 Python 代码为游戏开发一个生命值条时&#xff0c;遇到了一个问题。代码使用了循环来减少生命…

Mysql之不使用部署在k8s集群的Mysql而是选择单独部署的Mysql的原因

测试准备&#xff1a; 线程组&#xff1a;并发数100&#xff0c;持续时间2min 两个请求&#xff1a;使用k8s集群中的mysql的wordpress对应端口30011 使用单独部署的mysql的wordpress的对应端口为30022 访问同一个博客 测试结果&#xff1a; 汇总报告&#xff1a; 响应时间图&…

DevOps学习回顾01-技能发展路线-岗位能力-体系认知(射箭和拉弓的区别)

事为先&#xff0c;人为重–事在人为 参考来源&#xff1a; 极客时间专栏&#xff1a;DevOps实战笔记&#xff0c;作者&#xff1a;石雪峰 课程链接&#xff1a;https://time.geekbang.org/column/intro/235 时代的典型特征 VUCA VUCA 是指易变性&#xff08;Volatility&…

深度学习 - CNN

第一部分&#xff1a;基础知识 1. 什么是卷积神经网络&#xff08;CNN&#xff09; 定义和基本概念 卷积神经网络&#xff08;CNN&#xff09;是一种专门用于处理具有网格结构数据&#xff08;如图像&#xff09;的深度学习模型。它们在图像识别和计算机视觉领域表现尤为突出…

欢乐钓鱼大师通关必备秘籍!云手机游戏辅助!

《欢乐钓鱼大师》是一款让玩家沉浸在放松钓鱼乐趣中的手机游戏。不同于传统钓鱼游戏&#xff0c;它融合了收集、升级和竞技等元素&#xff0c;让每位玩家可以根据自己的喜好和目标来发展钓鱼技艺。本攻略将为您详细介绍如何在游戏中迅速提升实力&#xff0c;达到通关的最高境界…

idea Alt+/ 自动补全变量名开头是大写 改 选择小写开头变量名

idea 中自动补全变量名是非常常见的操作&#xff0c;变量名一般都需要小写开头&#xff0c;但是idea中 Alt / 自动补全变量名时 补全的变量名是大写的&#xff0c;这就很难受了。如下图所示&#xff1a; AutowiredLogService LogService;Ctrl 空格 快捷键 虽然不像 Alt / 一…