OpenAI Tutorial #2 - Chat Completion
Key Takeaways
This video tutorial series covers the basics of leveraging AI to make small applications that generate text and images using OpenAI's chat completion service, specifically with the GPT-3.5 Turbo model. It demonstrates how to use OpenAI's API to generate text based on user input and create meta data for YouTube video titles.
Full Transcript
all right then gang so now we've set up open AI in the project and now we want to start interacting with open AI to do things for us so the first thing I want to do is I want to try using the chat completion service which is basically going to give us the same kind of behavior that chat GPT has where we ask a question and the AI assistant produces a response for us now the question we're going to be asking to begin with is that we'd like the AI assistant to come up with a video description based on a video title that we provide it with so we'll be sending that question or that prompt to open Ai and then we'll get a response which will hopefully be the video description and in order to do this we're going to need some user input to get the title of the video now later on we'll be creating a front end so that the user can just type that video title in an input field and press submit but since we don't have that front end yet we're going to use the terminal to get the title from the user instead so to do that we need the readline module and we can and require that at the top of the app.js file so that we can use it to create an interface in which we can then Define the input and also the output of the interface now we're going to store this in a constant and I'm going to call that RL and in our case the input of this interface is going to be process dot standard in or STD in which basically means the input is going to be from our keyboard when we type in the terminal and the output is going to be process.standard out which again means the output is going to be output into the terminal basically so once we have that interface we can then use it to ask the user a question in the terminal and we do that by using the question method on the interface we just created so RL dot question and invoke it and now as an argument we can pass in a string which is basically the question we want to ask so we can just ask for a YouTube video title but also as a second argument we can fire a function once the user submits an answer in the terminal so once they press enter and that function takes as an argument the answer that the user provided us with and we can do something with it so for now I just want to log this title to the console to make sure that everything works and we can test that this is going to be running correctly by opening up the terminal and typing node and then the name of the file we want to run which is app and when we do that we can then see the question and we can answer the question and then hit enter to see the response and the response is basically just the answer that we gave it it's just logging it to the console right awesome so now we have some input the next thing we want to do is use that user import with openai to generate some kind of response alright so I'm just on the API reference and then I'm going to go to chat right here and create chat completion so we can see this is how we create a chat response right here so we have this constant completion that we're storing the response in and then we say open AI which is what we've already created remember when we set up the configuration and then create chat completion and we invoke that and pass in an object right here okay with a couple of different properties the first one is the model so there's different models that we can use when we're using open Ai and they kind of work a little bit differently in the way that they provide responses now if you want to see other models you can do that by clicking here and if we scroll down we're going to see all of the different models I think at some point oh wait no we're not okay if you go to documentation and then go to models you certainly do so this is the one we'll be using GPT 3.5 it's actually turbo so if we click on that it's this one right here that we're using but there's many different models that you can use and you can see the descriptions of them right here so we're using this one um which is also here and then the second property is the messages property and that's an array of different messages so inside this array each kind of message or prompt if you like that we want to send the AI as an object now we define the role of that message which is in our case the user now when we get a response we're also going to get a raw property in that response and that is going to be the assistant because that is the AI role the assistant okay in our case the role is the user because it's the user who's asking the question second property inside this object is the content and that's the actual question or prompt that we're sending to open AI in order to generate that response okay now when we get a response we have to grab it like this right here so completion.data dot choices and then the first element inside that and then the message so that is the actual message response okay now if we come down here we can see the entire response that we get so what we're doing is going into this object then into the choices then the message and then the content we can see here the role like I said is the assistant in the response but these other properties we can use as well okay so let's try this out this function create chat completion inside our project okay so let's give this a whirl so I'm going to create a couple of functions to kind of ask openai a couple of different questions a question for the video description and also I want to ask a question um so it can produce some tags as well based on the title of the video so in order to create these functions I'm going to create a new folder and it's going to be called controllers now the reason I'm doing this is because later on we're going to be creating an Express app and an API Ai and it makes sense to kind of organize the code a little bit now you don't have to if you don't want to you can put the functions in app.js or wherever you prefer I'm going to stick it inside a controller's folder and we're going to call this open AI controller dot JS all right so inside here the first thing we need to do is require our config thing right here this thing that we export so let's say const open AI is equal to or require and then we want to say dot dot forward slash to come out of the current directory which is controllers then into the config folder then we want the open AI config so we're grabbing that export open Ai and now we can use that to interact with open AI so I'm going to create a function first of all called generate meta so by meta I mean the video description and the tags but call the function whatever you want this is going to be an async function because we're going to use a um a weight inside it and then as an argument we're going to take in the title of the video now remember currently we're grabbing that title right here when we ask a question in the terminal so we can pass it in as an argument all right so inside here now we're going to communicate with open AI so I will say const description is equal to a weight and then it's going to be open AI Dot and we use that method create chat completion and inside here we pass an object with those two properties right we have the model which is going to be GPT 3.5 hyphen turbo like so the second property is going to be the messages property and that is an array remember where each object is a message the role of this one is going to be User it's the user asking the question and the content is going to be the question that we're asking so I will use backticks here because I want to Output a variable inside it so we're going to use a template string so I'll say to the AI come up with a description for a YouTube video called and then we'll output that variable so dollar sign curly braces title which is this thing we're taking right here so that's what we're asking to um the AI all right now there is a third property I want to add and that is going to be the max tokens property and this basically says how many tokens you're willing to spend on this question and it will cost roughly one token per letter it's not exact um but kind of see it as one token per letter it does change but I'm going to say about 100 here all right so that is all we need to do and then once we have the description we can access the message the response message on that by saying first of all console.log so we can see it then description dot data dot choices and then it was the first element inside choices and then it was the message property all right so let's see if this works I am now going to import this thing or rather we need to export it so export uh sorry module dot exports and we set that equal to an object and inside here we'll say generate meta there is going to be another function that we export later which is why we're doing it this way inside an object save that inside app.js I'm going to import it so I will say const and then we want generate if I can spell it laminate generate meta and that is equal to a require and then we want to go inside the controller or controllers function folder rather and then we want the open AI controller so now we can use that function right here generate meta like so and it's going to automatically pass in title right here okay so let's cross our fingers and hope this works I'm going to save it and open up the terminal I'm going to cancel out of this process and run it again node app so YouTube video title let's say complete HTML tutorial all right let's see what open air comes back with now it does take 10-15 seconds to generate a response but we should see in a minute and there it is so we can see the role which is assistant and then the content which is this big description right here I'm not going to read it out loud feel free to pause the video and read it for yourself but it is generally a pretty good description based on the title now if you wanted you know a more finely grained response then you need to kind of tweak the way that you prompt the AI make a better descriptive question but that will do for us so we have the description right here we also want to generate tags based on the title as well so we can go back to this open AI controller and inside the same generate meta function I'm also going to do a second request to open AI now I'm actually just going to copy all of this again because we're doing exactly the same but I'm just going to change a few things so this time it's going to be tags that we want to get back and then down here we want to do this differently so it will be come up with 10 keywords for a YouTube video called title and then down here we want to say tags instead of description and then I think my friends that is pretty much it I'm going to save that and open up the terminal again cancel out of the process and then we're going to run the application again the YouTube video title let's choose something different we'll say next Js crash course and press enter and see what it comes up with all right so there is the first response which is the description and here are the tags so we can see we have server-side rendering routing static generation code splitting these are the ones as well probably not the best tags but they're still pretty good right so that's the first kind of Step complete we've used this create chat completion method to get a response from open AI based on our question which is pretty cool and pretty simple to do so that's the first step in the next lesson we're going to look at how we can ask open AI to generate images as well based on our text description
Original Description
In this OpenAI tutorial series you'll learn the basics of how to leverage AI to make small application that generates text and images. In this lesson we'll look at chat completion to replicate a similar behaviour to ChatGPT.
🚀🥷🏼Get early access to this entire course now on Net Ninja Pro:
https://netninja.dev/p/openai-tutorial-the-basics
📂🥷🏼 Access the course files on GitHub:
https://github.com/iamshaunjp/openAI-basics
💻🥷🏼 Modern JavaScript Tutorial:
On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja
On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
💻🥷🏼 Node.js Crash Course:
On Net Ninja Pro - https://netninja.dev/p/node-js-crash-course
On YouTube - https://www.youtube.com/watch?v=zb3Qk8SG5Ms&list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU
🔗🥷🏼 OpenAI - https://openai.com/
🔗🥷🏼 VS Code - https://code.visualstudio.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI