💫 Резюме
本视频介绍了如何使用OpenAI新的Assistants API,使用代码实现了一个简单的例子,并通过线程、轮询机制、代码解释器等工具帮助我们理解如何构建助手,从而使代码实现更复杂的功能。视频提供了代码和资源的链接。
✦
这部分视频是关于如何使用OpenAI的Assistants API进行编码教程。
00:00视频作者将展示如何使用线程和轮询机制来使用Assistants API。
作者提到了使用Code Interpreter来理解如何使用这些工具。
视频中提供了资源、代码、依赖项的链接,以及一些未在教程中展示的步骤。
视频作者强调阅读Assistance API的文档的重要性。
✦
本节视频主要讲解了如何使用OpenAI Assistants API,包括设置API密钥、导入所需的npm包、创建连接、以及编写主要逻辑函数等。
02:59首先需要设置OpenAI API密钥,并确保在代码中正确拼写。
需要导入dotenv和openai npm包。
使用readline模块来在终端中获取用户输入。
创建连接并获取密钥。
有两个主要逻辑函数,一个用于提问,另一个是主函数,包含所有的逻辑,并在最后被调用执行。
✦
在这一部分中,介绍了如何使用OpenAI助手API,包括提供指令给助手、使用工具和创建线程。
05:49提供指令告诉助手你的目的和要做什么
可以向助手提供工具来解答问题或解决挑战
使用代码解释器来运行Python代码
创建线程来跟踪上下文,并使用状态管理来控制代码的执行
✦
该部分介绍了如何使用OpenAI Assistants API进行编码。
08:46我们通过传递用户提供的角色和问题来调用OpenAI。
使用轮询机制来检查运行状态,直到运行完成。
轮询机制会循环检查运行状态,直到设置为已完成。
✦
代码将从线程中检索最近的答案,并询问用户是否要继续。
11:41通过 thread ID 获取问题和答案列表
显示最近的答案并询问用户是否要继续
如果用户回答是,则循环执行代码,否则终止程序
✦
代码在实践中的工作方式
14:36使用循环来获取问题和答案
代码解释器会根据之前的信息给出答案
作者希望这个教程能帮助读者更快地开始使用OpenAI助手API
作者计划在将来提供更好的代码和更有趣的用例
00:00Hey everyone, Ralf here. In this video I want to show you how you can get started
00:05with OpenAI's new Assistants API that was released earlier today and it's
00:12going to be a simple example that allows you to understand how to use threads, how
00:17to use the polling mechanism to know when you have an answer, and we're going
00:21to be using the Code Interpreter so you understand how you can use these tools.
00:24Hopefully this will allow you to get started and understand how you can build
00:29with assistants and make something much more complicated. But enough about that
00:35let's jump in and show you what the result will look like and then jump in
00:41to the code. So all of the resources, the code, the dependencies, links, etc that I'm
00:51using here is going to be available in a link in the video description to make
00:55sure that you look at that, you don't have to follow along and copy what I do
00:59on my screen. So check that, that has all of the resources along with a couple of
01:04steps that I'm not showing in this tutorial. One of the most important
01:09things that I would point out is going ahead and reading the documentation for
01:13the Assistance API and again I'm going to be linking that in the video
01:18description. Then let's look at what our outcome will be. I'm running Node locally
01:25on my Apple computer and what I have set up is a script that will be asking me a
01:33question or allowing me to ask a question, provide me with an answer and
01:38then asking if I have another question and I can continue to ask questions
01:43about the context in the previous answer and continue having that context
01:50available for to me question after question after question. So here I got a
01:55couple of questions that takes the Fibonacci sequence numbers, do some math
02:00on them and continue to do some math just to trigger that code interpreter
02:05that is running behind the scenes. But enough about that I'll show you in a
02:11minute what this looks like in action but let's actually go ahead and start
02:17building it. So I will be using VS Code here or cursor in VS Code so let's go
02:27ahead and create the first file and let's go ahead and call that math match
02:35math teacher dot JSS. You can call it whatever you want and then we're also
02:42going to be creating a file for our open API open AI API key so let's call
02:51and create an environment variable for that and then just hit save and let's
02:59actually start with that. Let's start with the open AI API key here. I call it
03:06open AI API key you can call it whatever you want just make sure that it is
03:10correctly spelled in the code that we have so just save that in here and then
03:17jump over to math teacher and I'm not gonna code this as we go I'm gonna be
03:22copy pasting some code and explaining it to speed this up. Nobody wants to see me
03:28code and I don't want to put that on YouTube so I'll just paste it as we go
03:34and explain what it is that we're doing and again this code along with
03:39dependencies etc is available in a link in the video description. First of all
03:45we're gonna require dot envs to make sure that we can import our environment
03:50variable we're gonna be using the open AI npm package. Note that it was updated
03:56this morning or earlier today so if you already have open AI's npm package
04:01installed make sure that you update it to the most recent version because
04:06otherwise you won't have access to new features that were released today and
04:10then I'm gonna be using readline here that allows me to prompt or ask the user
04:16for input in our terminal and then next we're just gonna be using plain vanilla
04:24open AI code to get the secret key not technically open AI but get that from
04:32the end variable here and here we're using that to create a connection with
04:36open AI. Next I got two different functions I'm gonna copy paste both of
04:43them and they essentially hold all of the logic that we're using in this
04:50tutorial. So first I got a function that is called ask question which just
04:56manages the prompt that we're providing the user asking what is it that you want
05:02to ask a lot of questions and asking there but that is stored in this
05:07function that we called from within this main function and this main function has
05:13the meat of our logic it has basically everything and it's ultimately what we
05:18call at the very end here to execute our logic. So let's walk through what that
05:25main function does. So if you have built with open AI in the past you're gonna be
05:33recognizing some of this but it looks a little bit different so in the open AI
05:38assistant API which is currently in beta we have this assistant create so you can
05:44provide a name I think mine is math tutor because it's what in the docs I
05:49copy pasted that then you provide instructions telling essentially the
05:54assistant what is your purpose what are you trying to do and here you're a
05:58personal math tutor write and run code to answer math questions. Then next
06:04something that's very important to know is that you can ask or you can provide
06:09the assistant with tools essentially capabilities that it can use to answer
06:14the questions or to solve the challenges or problems that it is provided with.
06:20We're gonna be using the code interpreter which allows it to run
06:24Python code behind the scenes and then lastly and importantly the model is a
06:29new one that got released today which has support for the assistants it's
06:35called GPT-4 1106 because today is the 6th of November and it's currently in
06:41preview so make sure to use this as your model when you're doing this tutorial.
06:49Next let's move on to this we're just logging a first greeting in the code
06:56telling user hey there I'm your personal math tutor ask me some complicated
07:01questions and then we're on to some new open AI logic for assistants here so
07:08I'll walk you through it step by step. So assistants are using
07:14something called threads it's essentially the way that it keeps track
07:21of context over time it's going to use what's called a thread so we're creating
07:27a thread here and we'll keep using that further down in the code so we'll get
07:33back to using this thread and then I'm using a little bit of state management
07:39here to know if we should continue running this code or to stop it so I set
07:45a variable I call keep asking to true and while keep asking is true we're
07:52going to be executing some code and what we're doing here first foremost is that
07:59we're calling out to the function that we're created up here passing in what is
08:06your question and that will then prompt the user in the terminal what is your
08:12question and allowing the user to respond with whatever it is that we want
08:19to ask our assistant and we get that back here so we then move on to the next
08:25step and here you can see we're continuing to use threads here so
08:30we're using OpenAI.beta.threads then message and we create a message and note
08:36here that we're passing in the thread ID so that's how we tell OpenAI that I
08:42already have a thread I want to use that I want to pass in some information into
08:46it and the information that we're passing in is a the role which is the
08:51user we're telling OpenAI that hey the user is providing something here the
08:56content that we're providing in is the user question so that's what the user
09:00put in as a question in the terminal and then we're essentially creating
09:14something that is called run or runs and this is just OpenAI executing whatever
09:23we've asked the assistant to do and they do that in something that is called runs
09:29and then something that is important to understand as well with a lot of other
09:35OpenAI functionality you send a request you wait around you get a response and
09:41you can show that to your user with assistance is a little bit different so
09:46here we got to set up something that is called a polling mechanism so what we're
09:51doing is we're setting run status as a function of OpenAI.beta.threads.run
10:00and something called retreat so we're trying to get the status of a run to see
10:06is it completed is it running so we're passing in the thread ID that we have
10:11and the run ID which is the current run that we're waiting for I'm gonna link
10:16something in the video description I think it's important to walk through how
10:21these work so I have a run lifecycle here that essentially explains what the
10:27different statuses are and how these runs are working because you need to
10:33sit and poll and wait for a run to be completed you're not going to be told
10:37that it's completed you have to check is it done is it done is it done and I set
10:43up this polling mechanism here to do that and let me just add that this this
10:51should be made more robust so this is just something that I put together it's
10:56pretty simple but it won't capture errors and other things that might
11:03happen so my polling mechanism just checks for this run status that we set
11:08here if that is not yet completed we're just gonna hang around for two seconds
11:15then we're gonna check again to see if it is completed and if it isn't we're
11:21just gonna this while loop is just gonna loop that until it is set as completed
11:26and this is where it should be made more robust because if it's never sets
11:30get set as completed for whatever reason well this code never ends so consider
11:37updating this to something better and more robust I definitely will do
11:41eventually myself once this is completed we're now gonna be able to retrieve the
11:50messages that we have in our thread so we do that here const messages for the
11:57thread ID we get a list of all of those and that will contain all of the
12:02questions and answers that we've had the first time there's only going to be one
12:06question one answer but as we continue to ask more questions there are going to
12:11be more questions and answers and in my case I don't want to show all of them I
12:15just want to show the most recent question or sorry the most recent
12:20answer because we're sort of adding to two previous answers in the UI that we
12:27have in our terminal so what we want to do here is just go ahead and get the
12:33most recent answer from the assistant again we are the user the assistant is
12:39providing the answer to want to make sure that we grab the message from the
12:45role assistant here and then we're just going ahead and console logging that and
12:53showing the answer essentially and next we're gonna be asking the user do you
12:59want to continue with this and if the answer is anything other than yes we're
13:07gonna be terminating the code and showing a little message here that says
13:13alrighty then I hope you learned something and do this read line close
13:17but if you answer or if the user answers yes on this we're just gonna be looping
13:23this over again and rerunning all of this and next time if it's the second
13:30third fourth etc we're gonna have all of that context available to us in the
13:36thread let's go ahead and look at this in action so I'm gonna open up my
13:44terminal here and then we're gonna do node math teacher dot JS so here we have
13:55it hello there I'm your personal math tutor ask some complicated questions and
14:00then we have this what is your question so let's ask something similar to what
14:06we asked previously what are the 44th and say 66th VPU not she numbers that
14:21might be misspelling that and now behind the scenes it is running code
14:30interpreter and giving us a response here that says the 44th Fibonacci number
14:36is 4433 million and 66 is something that I'm not sure what is called in English
14:45but it's a lot so do we want to ask another question yes or no let's go
14:51ahead and type yes and then we're looping so we're just getting this what
14:55is your question again now because we're using a thread here the assistant is
15:03going to have context about our question and answer here so we can go
15:07ahead and reference that same way that you can do with any chat agent where it
15:11has context of previous questions and answers so let's go ahead and do this
15:16divide the ladder with the former then find the closest Fibonacci number to
15:28that and tell me its value and which order in the sequence it is and then we
15:39hit enter the same code is running but now we're referencing this information
15:45right here code interpreter is going to take that give us a number and say that
15:50dividing this with the other is approximately thirty nine thousand six
15:55hundred and two the closest Fibonacci number to this value is forty six
16:00thousand three hundred sixty eight which is the twenty-fourth number in the
16:05Fibonacci sequence so you can continue asking additional questions and it
16:12continues to essentially loop through this but hopefully these two questions
16:17and answers sort of explains how this code is working in practice so I'm just
16:24going to go ahead and type no here and we get the last little bit here all
16:29righty then I hope you learned something and that's sort of the message that I
16:34hope I can leave you with here as well I hope you learned something here this was
16:38a very scrappy tutorial with very scrappy code that I put together today a
16:42few hours after this API and documentation documentation was
16:48announced hopefully it can help you get started with the open AI assistant API a
16:55little bit more quickly that's sort of what I'm aiming for I will put together
17:00some content in the future as well where I provide hopefully better code some
17:05better and more fun use cases so if you want to see those make sure to subscribe
17:10to this channel and if you can like the video that's going to be helpful as well
17:17but I'll leave it at that again hope that this was useful and maybe I'll see
17:22you in a future video thanks