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

news2025/3/12 17:14:04

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.coloradmin.cn/o/1826263.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

微信公众号打通与登录的实现

今天实现一下与微信公众号进行对接,通过扫描二维码的方式来进行注册与登录,获取用户的微信唯一标识作为用户的username,下面我们开始编写。 骨架建立: 建包: 第一步还是先将骨架建好,与网关骨架差不多&a…

树莓派4B_OpenCv学习笔记9:图片的腐蚀与膨胀

今日继续学习树莓派4B 4G:(Raspberry Pi,简称RPi或RasPi) 本人所用树莓派4B 装载的系统与版本如下: 版本可用命令 (lsb_release -a) 查询: Opencv 版本是4.5.1: 图像的膨胀与腐蚀一般用于灰度图或者二值图,今日便来学习…

基于python-CNN卷积网络训练识别牛油果和猕猴桃-含数据集+pyqt界面

代码下载地址: https://download.csdn.net/download/qq_34904125/89383066 本代码是基于python pytorch环境安装的。 下载本代码后,有个requirement.txt文本,里面介绍了如何安装环境,环境需要自行配置。 或可直接参考下面博文…

目前市面上DIY高端空心耳机壳使用的透明原材料是什么?

目前市面上DIY高端空心耳机壳使用的透明原材料是什么? DIY制作耳机壳的UV树脂胶是一种单组份、通过紫外线光固化的胶粘剂,具有低能量固化、收缩低、发热量低、高透明、耐盐酸、耐黄变好、高硬度、韧性好、成型好等特点。这种胶粘剂非常适合用于制作耳机壳…

Redis 主从集群 哨兵原理

一. Redis 主从集群 1.1 基本概念 主从架构:Redis主从集群采用“一主多从”的架构模式,其中主节点(Master)负责处理客户端的读写请求,而从节点(Slave)则负责处理读请求。这种读写分离的设计使…

Pytest 读取excel文件参数化应用

本文是基于Pytest框架,读取excel中的文件,传入页面表单中,并做相应的断言实现。 1、编辑媒体需求 首先明确一下需求,我们需要对媒体的表单数据进行编辑,步骤如下: 具体表单如下图所示 1、登录 2、点击我…

618洗地机全网热门推荐,跟着买错不了

步入酷热夏天,家中的清洁工作也迎来了新的挑战。天气炎热,细菌、异味滋生的困扰让日常打扫变得不再轻松,这时一台高性能的洗地机就成了提升生活品质的必备良品。不同于洗地机的技术与类别繁多,洗地机虽原理不复杂,但在…

堆栈溢出的攻击 -fno-stack-protector stack smash 检测

在程序返回的一条语句堆栈项目处&#xff0c;用新函数的起始地址覆盖&#xff0c;将会跳转到执行新函数。 现在系统对这个行为做了判断&#xff0c;已经无法实施这类攻击或技巧。 1&#xff0c;测试代码 #include <stdio.h> void cc() {printf("I am cc( )\n"…

【AI+编程】工作日常场景随时可以AI编程,记一个问答SQL快速导出数据日常示例

今天有个场景&#xff0c;我们有个老项目&#xff0c;由于历史原因差不多1年多没使用了&#xff0c;恰巧客户紧急情况要使用。因为当时没有需求&#xff0c;所以V1.0上线后 就没做更新。 需求很简单&#xff1a;我们假定 项目问题表、客户表、问题答案表&#xff0c; 实时查询…

VisionOS的未来愿景:苹果VisionPro创业者的愿望清单

随着苹果公司在增强现实(AR)领域的不断探索,VisionPro作为其前沿产品,已经开始展现出改变我们与数字世界互动方式的潜力。作为一名VisionPro创业者,对未来VisionOS的更新充满了期待,并提出了一系列愿望清单,这些愿望不仅代表了个人的需求,也反映了用户社区对苹果AR生态的…

【ETAS CP AUTOSAR基础软件】DET、Bfx、CRC、ComStack、rba_ArxmlGen模块详解

文章包含了AUTOSAR基础软件&#xff08;BSW&#xff09;中DET、Bfx、CRC、ComStack、rba_ArxmlGen模块相关的内容详解。本文从AUTOSAR规范解析&#xff0c;ISOLAR-AB配置以及模块相关代码分析三个维度来帮读者清晰的认识和了解DET、Bfx、CRC、ComStack、rba_ArxmlGen。文中涉及…

logTrick

贴一下灵神的题解里面的解释~ 就是一种优化策略&#xff0c;logtrick class Solution { public:int minimumDifference(vector<int>& nums, int k) {int res 0x3f3f3f3f;int n nums.size();for(int i0;i<n;i){res min(res,abs(nums[i]-k));for(int j i-1;j&g…

2024年6月15日 (周六) 叶子游戏新闻

期刊杂志: 聚合读者、意林、知音、故事会、花火以及国内各大知名报纸电子版&#xff0c;无需付费即可观看各种免费资源 中医自学宝典: 集合了中医医案&#xff0c;医经&#xff0c;方剂 药材知识的app&#xff0c;更方便的免费学习中医知识 《赛博朋克2077》被取消DLC泄露&…

20240615在WIN11下的串口调试助手的下载安装以及使用

20240615在WIN11下的串口调试助手的下载安装以及使用 2024/6/15 18:06 百度&#xff1a;串口调试助手 blob:https://apps.microsoft.com/df934d29-fd7a-4873-bb6b-a4ab5a7934c9 串口调试助手 Installer.exe 收发的LOG&#xff1a; rootok3588:/# ./uart_test /dev/ttyS0 11520…

springboot+vue+mybatis家电系统+PPT+论文+讲解+售后

随着信息互联网购物的飞速发展&#xff0c;一般企业都去创建属于自己的电商平台以及购物管理系统。本文介绍了家电销售系统的开发全过程。通过分析企业对于家电销售系统的需求&#xff0c;创建了一个计算机管理家电销售系统的方案。文章介绍了家电销售系统的系统分析部分&#…

cs与msf权限传递,以及mimikatz抓取明文密码

cs与msf权限传递&#xff0c;以及mimikatz抓取win10明文密码 1、环境准备2、Cobalt Strike ------> MSF2.1 Cobalt Strike拿权限2.2 将CS权限传递给msf 3、MSF ------> Cobalt Strike3.1 msf拿权限3.2 将msf权限传递给CS 4、使用mimikatz抓取明文密码 1、环境准备 攻击&…

【Apache Doris】Compaction 原理 | 实践全析

【Apache Doris】Compaction 原理 | 实践全析 一、Compaction 前文概要二、Compaction 版本策略三、Compaction 类型说明四、Compaction 工程实现五、Compaction 生产实践 作者 &#xff5c; 俞剑波 一、Compaction 前文概要 LSM-Tree 简介 LSM-Tree&#xff08; Log Structu…

Three.js做了一个网页版的我的世界

前言 笔者在前一阵子接触到 Three.js 后, 发现了它能为前端 3D 可视化 / 动画 / 游戏方向带来的无限可能, 正好最近在与朋友重温我的世界, 便有了用 Three.js 来仿制 MineCraft 的想法, 正好也可以通过一个有趣的项目来学习一下前端 3D 领域 介绍 游戏介绍 相信大家对我的世…

Swift开发——循环执行方式

本文将介绍 Swift 语言的循环执行方式 01、循环执行方式 在Swift语言中,主要有两种循环执行控制方式: for-in结构和while结构。while结构又细分为当型while结构和直到型while结构,后者称为repeat-while结构。下面首先介绍for-in结构。 循环控制方式for-in结构可用于区间中的…

推荐这两款非常良心的录屏和文字转语音工具,很是让人心动,不要错过

VPot FREE 吾爱大神制作的文字转音频工具&#xff0c;免费使用。 支持英语、韩语、法语、日语等语言&#xff0c;还是支持男声、女声和儿童声音。 支持将以导入文本的格式转换成音频&#xff0c;并保存为MP3、WAV等常见的音频格式。 VPot FREE提供智能断句的功能&#xff0…