How To Build Your Own AI With ChatGPT API

Web Dev Simplified
18 Mar 202309:51

Summary

TLDRThis video tutorial by Kyle from Web Dev Simplified introduces viewers to integrating Chat GPT into their applications via OpenAI's API. It guides through creating an account, obtaining an API key, setting up a .env file, and installing necessary dependencies. The script demonstrates a simple Node.js terminal application that interacts with Chat GPT, showcasing how to send messages and receive responses. The video also highlights the ease of use and potential of incorporating AI into custom projects.

Takeaways

  • 🚀 OpenAI has expanded their API to include Chat GPT, allowing developers to integrate AI chat capabilities into their applications.
  • 🔑 To get started, developers need to create an account on platform.openai.com and generate an API key for authentication.
  • 💰 Chat GPT is a paid API, but it's cost-effective with pricing based on usage, and there's a free trial available for initial testing.
  • 📝 The script demonstrates creating a .env file to securely store the API key and using the 'dotenv' package to load it into the application.
  • 🛠️ Dependencies such as 'dotenv' and 'openai' are installed via npm to facilitate the API interaction within the application.
  • 🔍 The 'openai' package is used to configure the API connection and interact with the Chat GPT model.
  • 🗣️ The script includes creating a chat completion by sending messages with roles (e.g., 'user', 'assistant') and receiving responses.
  • 🔁 The application uses a readline interface to continuously prompt the user for input and interact with Chat GPT in a loop.
  • 📊 The response from Chat GPT includes a 'choices' array that contains the AI's messages and other relevant information.
  • 🔎 The script shows how to parse the Chat GPT response to display the AI's message content to the user.
  • 🔄 The video concludes with a demonstration of the interactive chat application, highlighting the quick response times and ease of integration.

Q & A

  • What is the main topic of the video?

    -The video is about integrating Chat GPT into one's own applications using the OpenAI API.

  • What does OpenAI offer with their API expansion?

    -OpenAI offers the ability to call their API and get responses directly from Chat GPT, allowing developers to integrate AI into their applications.

  • How can one get started with Chat GPT using the OpenAI API?

    -To get started, one needs to visit platform.openai.com, create an account, and then manage the account to get an API key.

  • What is the cost associated with using the Chat GPT API?

    -Chat GPT API is a paid service, but it is relatively cheap, costing fractions of a penny per usage.

  • What is the importance of the API key in this context?

    -The API key is crucial as it is used to authenticate and authorize requests to the OpenAI API within one's application.

  • What is the recommended first step in setting up the application to use Chat GPT?

    -The first step is to create a .env file to store the API key securely and then install necessary dependencies like 'dotenv' and 'openai'.

  • What are the two main dependencies needed for the application to interact with the OpenAI API?

    -The two main dependencies are 'dotenv' for managing environment variables and 'openai' for interacting with the OpenAI API.

  • How does the video demonstrate the interaction with the Chat GPT API?

    -The video demonstrates by creating a simple Node.js terminal application that sends messages to Chat GPT and logs the responses.

  • What is the role of the 'readline' library in the application?

    -The 'readline' library is used to create a user interface for inputting information and interacting with the Chat GPT API.

  • How can the user get responses from Chat GPT in the application?

    -The user can input text through the readline interface, which then sends the input to Chat GPT and logs the response back to the user.

  • What does the video suggest about the future implications of AI like Chat GPT on jobs?

    -The video suggests that there might be concerns about AI taking jobs, and it refers to another video for a discussion on this topic.

Outlines

00:00

🚀 Introduction to ChatGPT API Integration

The paragraph introduces the new API expansion by OpenAI, allowing developers to directly integrate ChatGPT into their own applications. This integration enables the creation of AI-powered features within apps. The API is highlighted as being user-friendly, with the video set to demonstrate how to get started with this technology.

05:00

🔑 Setting Up Your OpenAI Account and API Key

This paragraph explains the steps to set up an OpenAI account, including signing up and managing your account. It mentions the free trial available and the cost-effectiveness of the API, especially for small-scale applications. It also covers the process of creating an API key, a critical step for using the API in your projects.

💻 Installing Dependencies and Setting Up the Project

The paragraph details the initial setup for the project, including installing dependencies like 'dotenv' and 'openai' via npm. It explains how to create a configuration file in VS Code, where the API key is stored, and setting up the package.json file to run scripts. The basic project structure is outlined, preparing the environment for further development.

⚙️ Configuring and Connecting to the OpenAI API

This section guides through configuring the OpenAI API in the project by setting up the necessary imports and creating an OpenAI object with the API key. It mentions the importance of checking the documentation for up-to-date information and demonstrates how to configure the chat model (GPT-3.5 Turbo) for generating responses.

📝 Sending Messages and Handling API Responses

The paragraph walks through the process of sending messages to the OpenAI API and handling the responses. It shows how to structure messages, access the API's response data, and extract the relevant information like message content. The importance of understanding the returned data and its structure is emphasized.

📜 Creating a User Interface for Input in Node.js

This section explains how to create a user interface in Node.js using the 'readline' library. It describes setting up a prompt for user input, processing the input asynchronously, and sending it to the API. The paragraph covers setting up a continuous loop to allow for multiple inputs, making the application interactive.

🔄 Running the Application and Getting Responses

The final paragraph demonstrates running the application and interacting with ChatGPT via the command line. It highlights the speed of the responses compared to the web interface and shows how the application can be expanded and integrated into larger projects. The video concludes by reassuring viewers about the practical applications of the API in their projects.

Mindmap

Keywords

💡API

API stands for Application Programming Interface, which is a set of rules and protocols for building software applications. In the context of the video, the OpenAI API allows developers to integrate Chat GPT's capabilities into their own applications. The script mentions creating an account on platform.openai.com to access the API and using an API key for authentication, which is fundamental to the video's theme of building AI applications.

💡Chat GPT

Chat GPT refers to a conversational AI developed by OpenAI that can generate human-like text based on user inputs. The video's main theme revolves around integrating Chat GPT into custom applications, demonstrating its potential to enhance user interaction and provide dynamic responses, as shown by the script's examples of creating a chat application.

💡Web Dev Simplified

Web Dev Simplified is likely the name of the channel or brand associated with the video's host, Kyle. It represents the educational aspect of the video, aiming to simplify complex web development concepts for the audience. The script uses this term to introduce the presenter and establish the video's educational purpose.

💡Node.js

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, allowing developers to run JavaScript on the server side. The video script describes setting up a Node.js application to demonstrate the integration of Chat GPT's API, using it to create a simple chat application that interacts with the AI.

💡Readline

Readline is a built-in Node.js module that provides an interface for reading data from a Readable stream (like process.stdin) one line at a time. In the script, Readline is used to create a user interface that prompts the user for input, which is then sent to Chat GPT for processing, illustrating the interactive aspect of the application.

💡.env file

The .env file is used to store environment variables in a project. In the video script, a .env file is created to store the API key for OpenAI's services, which is a common practice to keep sensitive information like API keys out of the source code and maintain security.

💡NPM

NPM stands for Node Package Manager, a package manager for the JavaScript programming language, and is used to manage dependencies in Node.js projects. The script mentions using NPM to initialize a project and install necessary packages like 'dotenv' and 'openai', which are key to the video's demonstration of integrating Chat GPT.

💡Configuration

In the context of the video, configuration refers to the setup process of the OpenAI API, including specifying the API key and other necessary parameters. The script describes creating a configuration object to pass to the OpenAI API, which is crucial for establishing a connection and making API calls.

💡Chat Completion

Chat Completion is a feature of the Chat GPT model that generates responses to user inputs in a conversational format. The video script details how to use this feature by sending messages to the Chat GPT model and receiving responses, which is central to the video's demonstration of creating a chat application.

💡Promise

A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. The script mentions awaiting a Promise when calling the Chat GPT API, which is a way to handle asynchronous operations in JavaScript, allowing the script to wait for the API response before proceeding.

💡Documentation

Documentation in the context of the video refers to the official guides and references provided by OpenAI for using their API. The script mentions referring to the documentation to understand how to set up and use the OpenAI API, emphasizing the importance of documentation in the development process.

Highlights

Open AI has expanded their API to include Chat GPT, enabling direct responses integration into custom applications.

The API is user-friendly and allows building AI capabilities into applications with powerful chat functionalities.

A step-by-step guide is provided to get started with Chat GPT by creating an account on platform.openai.com.

Chat GPT is a paid API, but it's cost-effective with usage costing fractions of a penny.

An API key is essential for accessing the Chat GPT service and should be securely stored and used within applications.

A .env file is used to store and manage the API key for environmental configuration in the application.

Dependencies such as 'dotenv' and 'openai' are required for the application to interact with the Chat GPT API.

The script.js file is the core of the application where all the code for interacting with Chat GPT will reside.

The 'openai' module is imported to configure and access the Chat GPT API within the application.

Documentation is key for setting up and understanding the capabilities of the openai module.

Creating chat completions involves passing messages with roles and content to the Chat GPT model.

GPT-3.5-turbo is the current version of Chat GPT used in the example, but documentation should be checked for updates.

The API response includes a data property with choices that contain the AI's message content and other relevant information.

Readline is a built-in Node.js library used to create a user interface for input and output in the terminal.

The readline interface allows for continuous user interaction with Chat GPT through a loop that prompts and logs responses.

Chat GPT's responses are immediate and can be integrated into custom applications for various chat-related AI needs.

The video also addresses concerns about AI job displacement and provides a link for further discussion on the topic.

Transcripts

play00:00

open AI which is the team behind chat

play00:02

GPT just expanded upon their API and

play00:05

added in chat GPT this means that you

play00:07

can call their API and get responses

play00:09

directly from chat GPT and include them

play00:12

into your own applications which means

play00:13

you can essentially build an AI into

play00:15

your own applications which is super

play00:17

powerful not only that but this API is

play00:19

really easy to use and in this video I'm

play00:21

going to show you how you can get

play00:22

started by creating your own application

play00:24

using chat GPT

play00:26

[Music]

play00:28

welcome back to web dev simplified my

play00:31

name is Kyle and my job is to simplify

play00:32

the web for you so you can start

play00:34

building your dream project sooner to

play00:35

get started with chat GPT the first

play00:37

thing you want to go to is

play00:39

platform.openai.com overview I'll leave

play00:41

a link to that in the description down

play00:43

below and all you need to do is create

play00:45

an account so you can just click on sign

play00:46

up to create your account once you've

play00:48

done that you should probably get

play00:49

redirected back to the same exact page

play00:50

but now you'll see that you have an

play00:52

account over here and if you click on

play00:53

that you can go to manage your account

play00:54

you can see that we have different usage

play00:56

and also you should have a free trial

play00:58

that has some amount of money added to

play01:00

your account that's because chat gbt is

play01:02

a paid API it's rather cheap in that it

play01:05

only costs you fractions of a penny per

play01:07

usage that you're doing as you can see

play01:08

so far my usage has been less than one

play01:10

penny in all the different trial stuff

play01:12

that I've done and if you're working on

play01:13

a small application it's going to cost

play01:15

you almost nothing to run but obviously

play01:17

if you scale up it'll be quite expensive

play01:18

on a larger scale application now the

play01:20

most important thing to get started with

play01:21

the project is you need to go down to

play01:23

your API keys and you're going to need

play01:24

to create a brand new secret key when

play01:26

you do this you can see your secret key

play01:28

has popped up here this is the only time

play01:30

you'll have to copy this so just make

play01:31

sure you click copy on this and you're

play01:33

going to use that inside of your

play01:34

application other than that you don't

play01:36

really need to mess with this at all and

play01:37

just know that I'm going to be deleting

play01:39

this secret key after this video so this

play01:40

won't actually work for you now that we

play01:42

have that created we can just close out

play01:43

of this and we're going to come over to

play01:45

our application in vs code I'm going to

play01:46

create a file called D dot EnV and I'm

play01:49

just going to paste that in here we'll

play01:50

just say API key it's equal to whatever

play01:53

that API key is going to be now the next

play01:55

thing I want to do is actually install

play01:56

all the dependencies we're going to need

play01:57

so I can call npm init Dash y as you can

play02:01

see that's generated a package Json for

play02:02

me and then I can call npmi and I want

play02:04

to install two dependencies the first

play02:06

one is called dot EnV that's just going

play02:08

to help us load our EnV file and the

play02:10

next one is called open AI which is the

play02:12

actual API we're trying to work with so

play02:14

pretty self-explanatory what we're

play02:15

working with there and now once we have

play02:16

that done we can create a simple

play02:18

script.js file where we're going to run

play02:20

all of our code our package Json let's

play02:22

just have a really simple

play02:24

run in here so we're going to say Dev

play02:26

and then inside of here we can say that

play02:27

we want to run script.js every time we

play02:31

run npm run Dev it's going to run all

play02:33

the code in this file which is currently

play02:35

nothing now in order to show you how

play02:36

this works I'm just going to be doing a

play02:38

really simple node terminal application

play02:40

but you can obviously expand this to

play02:41

whatever you want the very first thing

play02:43

we could do I'll Zoom this in so it's a

play02:45

little easier to read we can get our

play02:46

config set up so we're going to say

play02:47

import config from and we want to get

play02:50

that from dot EnV and we can just call

play02:51

this method what that's going to do is

play02:53

it's just going to set up our config

play02:54

file for us now if we console.log

play02:57

process.env Dot and we called it API URL

play03:01

we run our script it should hopefully

play03:03

print out that URL for us you're going

play03:04

to notice we're getting an error though

play03:05

and this is because if you want to use

play03:07

modules like I have inside of node all

play03:09

you need to do is come in here and set

play03:11

your type here equal to module now if we

play03:13

do that and we run this you can see it

play03:15

should work and right now it's printing

play03:16

out undefined the reason for this is

play03:18

obviously this should say API key

play03:19

instead of URL so now if I rerun that

play03:21

you can see that it's printing out that

play03:22

API key so we're able to get that in

play03:24

information next we need to get all the

play03:26

stuff from our openai we can come in

play03:28

here we can import from open Ai and the

play03:31

most important things that we're going

play03:32

to need is we're going to need some type

play03:33

of configuration and then we're also

play03:35

going to need access to the openai API

play03:37

so configuration is how you set

play03:38

everything up like your API key and

play03:40

openai API is actually what you call to

play03:42

do everything inside of the AI now if

play03:44

you're curious how I found all this out

play03:45

everything is inside the documentation

play03:47

here for how you can set up openai and

play03:49

if you go down they have guides for

play03:51

things like chat so you can set up all

play03:52

of your chat related stuff most of these

play03:54

guides are going to be in PHP because

play03:55

that's what they expect you to use but

play03:57

you can kind of translate over to what

play03:58

Json or JavaScript is going to look like

play04:00

it's not too much of a translation

play04:01

anyway once we have that done we can set

play04:03

up our open Ai and this is going to be

play04:05

coming from

play04:06

a new open AI object so we can create a

play04:09

new version of our open Ai and here we

play04:11

just need to pass it a new version of

play04:12

our configuration and the only thing we

play04:14

need to pass to our configuration is an

play04:15

API key we know that our API key here is

play04:18

just this let's just copy it up there we

play04:20

go so now we have access to that open

play04:22

API now this open API has a lot of

play04:24

things you can do inside of it so we can

play04:26

say open AI Dot and as you can see

play04:27

there's a ton of things but the one we

play04:29

care about is going to be creating a

play04:30

chat completion and inside here we need

play04:32

to pass in some information the first we

play04:34

pass to the model of the AI that we want

play04:36

to run in our case we want to use GPT

play04:38

hyphen 3.5 turbo that is the current

play04:41

most up-to-date version of Chad GPT that

play04:43

you can use at this time but again if

play04:45

you want to get a more up-to-date or

play04:47

different version you can check the

play04:48

documentation for whatever the best

play04:49

version is going to be then we want to

play04:52

send it the messages and the message is

play04:53

just an array of all the messages you

play04:55

want to send and each object in the

play04:57

array is an object it's going to have a

play04:58

role so this is going to be who sends

play05:00

this message whether it comes from a

play05:02

system whether it comes from chat gbt

play05:03

themself which is the assistant or if it

play05:05

comes from a user in most cases you're

play05:07

going to be sending messages that come

play05:09

from a user so you're going to say the

play05:10

role is user

play05:11

then you need to specify the content so

play05:13

whatever message you want to say like

play05:15

hello chat CPT that's what we could have

play05:18

our message be let's just capitalize it

play05:20

properly and that's going to be calling

play05:22

this and this gives us a promise which

play05:23

comes out with a result

play05:25

but for now let's just console.log that

play05:28

result and we're going to see exactly

play05:29

what this looks like it's going to give

play05:30

this quick save we're going to run this

play05:32

and we're going to see what our result

play05:33

prints out to us so give it a second and

play05:35

I'll just expand this up so it's a

play05:36

little easier to see you can see it

play05:38

gives it a ton of different information

play05:39

being returned to us but most

play05:42

importantly we have this section down

play05:43

here which gives us information on our

play05:45

choices as well as the different amounts

play05:46

of usage that we have which is going to

play05:48

determine our cost and all this is

play05:50

coming inside of a data property so what

play05:52

we can do is we can say dot data.choices

play05:55

and that's the thing that we really care

play05:56

about the most so if we just rerun this

play05:58

real quick I'll expand this up I'll

play06:00

rerun exactly what we did and we should

play06:02

hopefully get back a result as you can

play06:04

see it's an array of all the different

play06:05

messages because you can ask it to give

play06:06

you more than one message if you want

play06:08

you can see it contains a message the

play06:10

role is assistant because this is coming

play06:12

from chat GPT as well as the actual

play06:14

message content that it's going to be

play06:15

printing out to us and a few other

play06:17

things such as why it stopped giving you

play06:18

a response because there are certain

play06:20

limits on the amount of text you can

play06:21

return and if it goes over that limit

play06:23

this reason may change for the most part

play06:25

all we want to do is is get the first

play06:26

element from this array and we want to

play06:27

get the message and we want to get the

play06:29

content of that message so we can do

play06:31

that right now we want to get the very

play06:32

first choice we want to get the content

play06:34

which is going to be our message inside

play06:36

that we want to get the content so now

play06:38

if I run this again it should hopefully

play06:40

just print to us whatever chatgpt

play06:42

response hello how can I assist you

play06:44

today that's exactly what we expect now

play06:46

what we need to do is add a way for the

play06:47

user to input information and be able to

play06:49

react to that with chat GPT the best way

play06:51

for us to do that inside of node.js is

play06:54

going to be with a library called

play06:55

readline which is built into node so we

play06:57

can say read line

play06:58

from readline just like that that's

play07:01

coming straight from node and all we

play07:03

need to do is set up some type of user

play07:05

interface this is going to be how we

play07:06

input information so I'm going to call

play07:08

this user interface this is just going

play07:10

to be set to read line

play07:11

dot create

play07:13

interface

play07:15

this is going to take into properties

play07:16

our input which is going to be our

play07:18

standard in so we can say process that's

play07:20

standard in and then we have our output

play07:23

which is just process.standard out there

play07:27

we go and now here our user interface we

play07:29

can actually tell it to do certain

play07:30

things so for example we can call user

play07:32

interface.prompt and all that's going to

play07:34

do is it's going to prompt the user to

play07:35

have some type of input so if really

play07:37

quickly I just comment out what we have

play07:38

down here and I run this we should just

play07:40

see a prompt show up as you can see we

play07:41

have a prompt and we can type inside of

play07:43

that prompt hit enter and it'll do

play07:44

certain stuff now the next thing we want

play07:46

to do we just comment this back in is we

play07:49

can set up the listener in here so we

play07:50

can say user interface dot on and I want

play07:52

to listen online so every single time I

play07:54

hit enter that's going to be saying hey

play07:56

send this along that's going to be a

play07:57

brand new line and all I want to do is I

play07:59

want to get our input I'm going to do

play08:00

this asynchronously so we're going to

play08:02

say async input we're going to use that

play08:04

as an arrow function if you're not

play08:05

familiar with async await I have a full

play08:07

video on it I'll link in the cards and

play08:08

description for you but essentially it

play08:10

allows you just to wait for promises to

play08:11

finish which in our case this is a

play08:13

promise right here we can await this and

play08:15

we can get the result back just like

play08:17

that so we don't even need to worry

play08:19

about this dot then we comment this out

play08:20

for now and our content here we just

play08:22

want to put as our input is whatever we

play08:24

type into our prompt and hit enter

play08:25

that's going to be this input right here

play08:27

then we can take this response and we

play08:29

can log it out to the user so we can

play08:31

just essentially copy this line right

play08:33

here paste it down right there that's

play08:35

going to be most of what we need all we

play08:36

want to do is then just show The Prompt

play08:38

back to the user so if we just delete

play08:40

all that code and we look at what we

play08:42

have here essentially we're saying okay

play08:43

prompt the user for some input whenever

play08:45

they give us input send that over to

play08:47

chatgpt log out the value to the screen

play08:49

and then ask them for input again and

play08:51

just Loop and loop forever so if we run

play08:53

this you can see it's going to ask us

play08:55

for something so I can say how are you

play08:57

hit enter it's going to give us back a

play08:59

response and it says a bunch of stuff if

play09:01

I move my camera out of the way you can

play09:03

see it says it doesn't have feelings

play09:04

blah blah blah super straightforward we

play09:06

can say who is web dev simplified

play09:10

hit enter and it's going to give us

play09:11

hopefully an accurate response we'll see

play09:13

if it does or not span this a little bit

play09:14

we'll have to simplifies YouTube Channel

play09:16

created by me okay yeah it's rather

play09:18

accurate I'm not going to read it all

play09:19

but at least I got my name right and you

play09:21

can see that we can interact with chat

play09:22

gbt and the responses are actually

play09:23

rather quick that it gives it to us back

play09:25

it's a lot quicker than when you use the

play09:27

website because it makes you type out

play09:28

each character individually while here

play09:30

it's just showing you the response

play09:31

immediately which is really nice the

play09:32

best of all you can integrate this into

play09:34

your own application for your own chat

play09:35

related AI needs

play09:37

and that's really all it takes to

play09:39

implement this API now if you're afraid

play09:41

that chat GPT is going to take your job

play09:43

or change how you work you're going to

play09:44

want to check out this video right over

play09:45

here where I talk about that exact topic

play09:47

with that said thank you very much for

play09:49

watching and have a good day

Rate This

5.0 / 5 (0 votes)

Связанные теги
AI IntegrationChat GPTWeb DevelopmentAPI UsageNode.jsReadline LibraryOpenAI APIJavaScriptTutorialAI ChatbotTech Education
Вам нужно краткое изложение на английском?