OpenAI Tutorial #2 - Chat Completion

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·3y ago

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial series teaches you how to build a chat completion application using OpenAI's API and the GPT-3.5 Turbo model. You'll learn how to generate text based on user input, create meta data for YouTube video titles, and use template strings to generate text.

Key Takeaways
  1. Create an interface using the readline module to get user input from the terminal
  2. Use OpenAI's chat completion API to generate a response based on user input
  3. Specify the model used for chat completion as GPT-3.5 Turbo
  4. Create a new folder for controllers and require OpenAI configuration
  5. Use OpenAI API to create chat completion with model and messages
  6. Get response from OpenAI API with raw property and choices
  7. Export a module with a function to generate meta data for YouTube video title
  8. Use OpenAI to generate tags for YouTube video title based on title
💡 The GPT-3.5 Turbo model is a powerful tool for chat completion tasks, and using template strings can help generate more accurate and relevant text.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →