ChatGPI API in Python ✨ How to Build a Custom AI Chat App

pixegami · Beginner ·🧠 Large Language Models ·3y ago

Key Takeaways

This video demonstrates how to use the ChatGPT API in Python to build a custom AI chat app, including setting up the OpenAI API, sending prompts, and storing chat history. The project utilizes the OpenAI Python Library and can be run directly from the terminal.

Full Transcript

if you want to get started building products or services that use AI then this video is for you today I want to share with you how to build a chat GPT client in Python that we can run directly from the terminal in doing so you will learn how to use the chatgpt API directly from Python and how to modify your prompts so that you can add additional functionality or personality to your app with the release of chat gpt's new API the business use cases of generative AI is booming right now just check out the Google search volume for any AI related topic I think there's going to be a lot of opportunities to create things that weren't possible up until now so if you're excited about Ai and want to learn how to use it then let's get started before we get started coding let's take a look at some of the benefits of using an open AI API first of all the tech is extremely powerful but it's also really cheap to use there's also many ways to use it and I think that these three things create the perfect storm for Innovation and creativity when it comes to using generative AI both for business and for just fun projects in general in this tutorial specifically we're going to learn how to use the GPT 3.5 turbo model this model was released recently and it is the same model that chatgpt uses behind the scenes but I think the best thing about it is that it's also extremely cheap it only costs 0.002 dollars per 1000 tokens and you can think of tokens like words so you can almost process an entire essay and it won't even cost you 10 cents to do that and aside from being extremely cheap I think that the API is also extremely accessible because anybody can go ahead and sign up to use it right now and there's no more waiting or a screening process that you need to do you you could sign up right now at the website to get an API key and start using it overall I think that this is going to be a great thing to learn whether you want to build your own original AI products or pick up a new skill for your resume and this entire project is going to take you less than half an hour to code to follow along with this project you're going to need to know the basics of python and a little bit about how rest API works we'll also be using the open AI python Library so you need to know how to use a package manager like pip as well and this is the command you need to run with Pip to install the openai library in Python now let's go over what we will be building in this tutorial our project is going to be contained within a single python file we'll be able to run this script directly from the terminal especially if we create an alias for it and then we can use it as GPT questions directly from our terminal for example I can ask it what are five books to get better at coding and the program will talk to GPT Direct from the terminal and then give us the response here and it will maintain this chat context so we can ask it follow-up questions as well like where can I buy them and here it will answer with where it thinks we can buy these books so here it says you can buy these books online from various retailers such as Amazon etc etc and to end the conversation we just press enter without typing anything in the prompt I'll also show you how to modify the program so that you can get the AI to respond to you with a specific personality in this case I prompted it to respond sarcastically and berate the user but still answer them anyway and if I ask the AI the same question you could see that it's added these sassy remarks to The Prompt but it still answers me anyways to do all of this stuff you're gonna need an API key from openai so head over to platform.openai.com and sign up for an account once you've signed up go over to your profile and then click view API keys and then click create a new secret key I already have the maximum number of API keys so I can't create any more but here you should see a pop-up with a big piece of text so copy that because that's the only time you're ever going to see it and that key is what you will eventually need for your python app to be able to talk to open Ai and use this chat gbd endpoint now before I start using an API I usually like to have the documentation open so I recommend you do that as well and to do that just click here in the documentation and then scroll down and look for chat completion which is the one we'll be using and here you'll see a bunch of text explaining what it's about an introduction and a couple of code examples and this is pretty straightforward so once we're in Python we can basically just import the openai client specify the model which is going to be this chatgpt turbo and then we can send it a series of messages and the messages are Json formatted and they have a role all and a content and there's three different roles we can include in our messages so here we could use the system role to tell the AI what type of Personality it should have or how it should respond to the user messages and if we want to give the AI examples of how to react we can also include a couple of sample user and assistant responses these won't be used in The Prompt but it will help the AI understand how it is expected to respond to user messages we can also use this to establish context for a conversation so if we have a conversation with 10 exchanges between the user and the AI ready we can basically put that whole list into this message so that the AI knows how to answer follow-up questions of the user finally The Prompt that the AI will pay attention to the most is this last user content prompt here and that is essentially what the AI will respond to and if we scroll down you can see the expected response format so we're going to get a message object with the role of assistant and the content of the AI so this text is what we're really looking for here in our response the response will also include usage information about this particular request so it will tell us how many tokens we used to generate this output and this is important to you because the tokens is how the pricing is calculated when you use open Ai and if you go to your user profile and then you click pricing you'll be able to see the pricing page for each of the models so you can see here the chat model will charge you 0.002 dollars for every 1 000 tokens to put into perspective how cheap this is if you get this chechi PT to write you an entire 10 000 word essay it's only going to cost you two cents I think we've now spent enough time reading the documentation now let's get to coding I'm in my editor vs code and I'm in a new project directory called GPT shell and it's completely empty so let's first start by creating our python file and I'm going to call this gpt.py you can call this whatever you want here I'm going to create the main function that I'm going to use to run the program and then run it when this file is executed this is how I like to start my projects so just an empty function called Main and I'm just going to pass and then I'm going to write this if name equals main run main so this will get the program to run when I execute it because ultimately I want to run this as a script in my terminal and let's go to this terminal um and see if we can run the file so I'm just going to type Python and then gpt.py and so it runs no errors but nothing shows up yet okay that's fine so now let's just print the hello world message and if I go back and run that again the message shows up so that's good and now let's import the openai library because that's critical to what we want to build and if you don't have this then don't forget to run pip install open AI in your environment and you might need to upgrade it if you don't have the latest version because the GPT client is only in the latest version I believe that's going to be version 0.27 and up if you've installed openai with Pip and it's still complaining in your vs code then just click at the bottom here and it will show all the python environments on your computer so just make sure that you're using the right one that you've installed open AI into now I want my program to be able to take a list of arguments from the terminal so if I open my program in the terminal I want to be able to type GPT and then send a message like hello world like this and this doesn't do anything because we don't recognize this input yet but I wanted to recognize that but I'm going to use the art parse library in Python which lets us deal with arguments passed into the command line if we use it like this so let's go ahead and import that as well and Arc parse is a default library in Python so you don't have to install anything it should just come by default to use awkwards we first have to create parser and then we can start adding arguments to it so I want to add an argument called a prompt which is going to be the user's input and I want the prompt to be able to have multiple words so I'm going to do n args equals plus I'm going to say the type is going to be a string and this nrx plus will make it so that if we don't put quotes around our prompt it's still able to understand that each one of those is part of that same prompt so this is going to become a list first we'll have to parse the arguments and then we access it by doing args.prompt here but since prompt is going to be a list we're going to have to join it on a space so that it turns it into one string and then we can print out the entire prompt and I can also print out the original prompt as received by the argument parser just so you can see how it looks let's go back to the terminal and try it again run the same command and you can see here because I've put it into quotes this is seen as one argument by our arcpass Command um so it comes as one element in the list but now I can also do something like this python GPT but then without the quotes So if I do hello world it will still understand that these are two separate arguments but it turns them into one sentence for our AI so now we have the prompt let's send it over to chatgpt and to do that I'm going to create a separate function just so I can keep my code clean and I'm going to create this function called ask GPT and it's going to accept the prompt as an input first we'll need to set up the open AI client with the API key that we've created earlier so here you could set it into your environment variable if you know how to do that you could do with the dot file if you know how to do that or you could just set it here directly as a string as well so whatever your key is just enter it here but I've already set it as my environment variable so this is what I'm going to use if you do it like me you're going to have to also import OS so that you can get this environment variable and once the key is set up for our client let's go back to the documentation and copy the code example that they showed us so now I'm back on the openai documentation for chat completion we've already imported openai and we've already set up our key so let's just copy the actual code making this request and I'm just going to paste it down here okay so let's take a look at this we use the SDK to create a chat completion and we use the model GPT Turbo and then the message is this list of things but let's get rid of that uh let's just put in the message that we prompted from our terminal so let's just put that here so we have this well how do we get a response I think the documentation has an example for that as well so if you scroll down it says the python assistance reply can be extracted with response choices zero message content so let's just copy that and put it into our app first we'll actually have to store the response in a variable and now we can do that I don't know I don't think it's happy with these quotes when I copy pasted it so you got to watch out for that sometimes when you pay stuff from websites it's not using the right characters for certain things so I'm just going to replace that with double quotes that my python interpreter understands and now I have the message I guess I'll call that content but this is going to be a string and I can print this out or I can return it I'm going to do both I'm going to print it out and return it so now my askgpt function is complete let's go ahead and use it here let's go back to our terminal and run the program again so this time we have a response from chat gbt we said hello world to it and it says hello there how can I assist you today uh to make this a little bit nicer I'm gonna do two things I'm gonna change the response text from Chachi PT to a different color maybe green so it stands out and I'm also going to reprint the question that we're asking it and I'm going to delete this because I don't need it anymore and I will make this a format string and I'm going to put Q for a question now to make Chachi PT's response appear in a different color I can add special characters to it that my terminal understands and it's going to render those text as a different color so let's do that and I'm using GitHub copilot so I don't even need to remember how to do this I just have to type in a comment and it will auto suggest it for me so this is how we can do it and if I run it again I could see that my question is now showing at the top and the response is showing in this green color I can also strip this white space so it doesn't have like a double space here so to do that I think that the response is usually returned with a couple of extra new lines um because this is a string I can actually call strip on it to get rid of the trailing white spaces before and after it uh but I still might want at least one new line so I'll just add that at the top here as well now let's try to ask it a more complex question so here I'm going to say um how do I sort a list in Python okay so this works it gives me a really long response it says to sort of listen python you could use the sorted function or the sort method and then it shows me quote examples for each of those things and now I have two problems with this first is I can't ask it follow-up questions because when I finish typing the program exits so if I ask it something now it doesn't have the context of this previous conversation for example I want to ask it which method do you recommend and here it doesn't really understand because it's lost this context the second problem I have is that this response is a bit too long for me I think that if it just provides a code snippet or maybe just a one-liner explaining how to do it that's usually enough so I want to tell it to just keep its responses shorter and more concise so how can we modify our application to fix those two problems let's break it down one problem at a time first let's make it understand the context of the chat so if you look here the input to the API is actually a list of messages all we would have to do is just store a certain number of messages from the user and send that entire list back to gbt but we'll also need to make it so that when we ask GPT something we don't terminate the program right away we run in a loop and keep prompting them for input until they decide to exit to do that let's first create an empty list called chat history and I'm also going to make this sgbt except chat history as an argument now I'm going to assume that this chat history is going to be a list of objects like these so it's not just going to be the string itself because I also need the role and the content in this structure so if it's a list of things like this I should be able to just expand it or spread it directly in the messages using this asterisk symbol and this will kind of unpack all of the items of chat history and just put it into this messages list instead when we run this for the first time chat history is going to be empty and this expansion will do nothing so our first input is always going to look the same like this now as we use this application we want to store all of the things we do into this chat history ideally we probably want to put a limit on it as well so that if we have more than 10 or maybe a hundred pieces of conversation we start evicting some of the previous elements just so that we don't have an infinitely long chat history going on forever because those still count towards our token use per interaction with the AI but we're not going to do that for now we're just gonna put everything in the chat history and assume that we don't use it more than a couple of times in one sitting so to do that let's first create this as a separate object and I'm going to call this user prompt and now I'm gonna append that to my chat history and I'm also going to put it into here well actually I want to append it after um I send this response because I don't want to actually have it appear in the chat history before I expand it and use it in the messages I'll also need the ai's response to be added to the chat history okay so now I should have a chat history being built up as I use this so now I've updated my askgpt function to also take a chat history and I'm updating the history as I go um the last part to this is to create a loop so that I keep prompting the user for input as long as they're still using the app so to start with I'm going to capture their input by using the inbuilt input function and here we can put a string the string can be anything you want it's just going to show up prompting the user to enter some text and we're going to store that text in the user input variable and here I'm going to create a loop so while the user input is not equal to an empty string which means that if they just press enter without typing anything the app will exit but while that's not true and while there is some text in the user input I'm going to ask GPT with that as the new prompt and I'm just going to keep repeating this input prompt until they decide to exit and we're not doing anything with this response at the moment so I don't even have to do this I could just type askgpt because it will be printed out within the function itself and if they do decide to exit the app and just press enter I can make the application uh just say buy and that it's ending the session so now let's pop over the terminal and see if that works so here let's ask it the same question again how do you sort a list in Python so now it's responded with code examples and how I could sort of listen python now I can ask it a follow-up question and it will actually have this context so I can ask now how about JavaScript and if I ask that you could see that it still remembers what I asked it before about sorting a list but now it's kind of transferred that context over to JavaScript and given me examples on how to sort things in JavaScript okay so this works really well and then if I press enter without having anything typed here it will interpret that as me exiting the session so it will say bye to me and the next thing I want to fix now is that these answers are really long and in fact I want more control of the style of the response that uh GPT will return to me so let's go back to our code editor and use that system role prompt that we saw in the example snippet now I'm going to make the system message a part of the argument to my ask GPD function and I'm going to add that right at the top of my messages list so the role is going to be system and the content is going to be the system message string and now I have to create a system message um so I could do that as a constant or I could do it here let's do it as a constant up here and for my system message I'm going to tell it to answer with short concise really condensed information so this is a system message I want to be included when I prompt GPT answer the user with short concise answers and code Snippets keep it to the point and don't go off topic okay so now I have this constant and I just have to put it into my function I'll put it in here and I'll also put it in here while I'm using it so now we should receive the system message it should add it to our list of messages let's see if that works back to our terminal let's clear this out and ask the exact same question again okay so this is a lot shorter it's not as verbose there's a Code snippet right away and there's an explanation as well and now let's try to ask it how to do it in JavaScript and still this is pretty short so that's quite good now you could also have a lot of fun with this you can prompt the system message to basically do anything you want another really interesting use case I just thought of is you could use it to write real estate listings for example if you're a real estate agent or you're just wanting to rent out your room we can tell it to do something like this so now my system message is that you are a real estate listing assistant and to generate a listing description based on the user's prompt and descriptions and use emotionally charged words to describe the property emojis and capitalization to emphasize the good qualities of the property uh let's see what it comes up with so here I'm going to say two bed two bath one car Northview River Storage um fourth floor near train and maybe I'll put a suburb as well red fern Sydney okay let's go ahead and run that and see what it does so this is pretty funny it's got the Emojis spot on and it's also got the capitalization and the type of language real estate agents love to use This Magnificent two build two bath apartment with one car garage is a true gem purged on the fourth floor with Gorgeous views of the river and the city's North the location offers a lifestyle of convenience and luxury um yeah and you can pause the video and read it yourself if you want but yeah this is I think this is a really interesting way to use the AI just to kind of make somebody's workflow a little bit more automated and a little bit easier so now that you have the script written in your folder somewhere how do you actually make it so that you can run it from anywhere in your terminal uh it depends on the type of terminal you're using but most people are using bash terminal or zshell terminal if you're on a Unix system which is Mac or Linux but I'm actually using fish because I'm just trying it out so my terminal is a little bit different but either way it's pretty simple we just need to know the location of the Python binary that we're using to um to run this so that we have all the environments and we also need to know the location of the file that we're using now a really easy way to check that is to just go here and then you see the exact path to your python binary in vs code and if you're in the terminal already you should also be able to get the location of your python executable by just typing which python so here it is so now if I run this command with this file uh let me just put this here like this I should be able to execute this from anywhere so even though this is a little bit longer than before now it's actually universally executable I also have to enter the prompt so I'll be below oh yeah it's still the real estate bot so I have to do two bed two bath one car okay so if I want to make this accessible from anywhere I can turn it into an alias and if you're using bash you could just type in Alias and then the name of the Alias you want to call it so for example you could call it chat and then you could type in in quotes the command that you want to run including the full path so in bash that should work but I'm using fish so I don't think this actually works oh maybe it does I don't know well okay it does work in fish as well so yeah just type this line Alias space and then chat or whatever command you want and equals to this and that should work for you and if you want this to be available every time you store your terminal you have to modify your dot RC file depending on what terminal you're using it could be bash RC or zshlrc so you can open that up and then just add that Alias line to the bottom there so now every time you start up your terminal it should be available for you to use on my old computer I've actually aliased this to the GPT command so um that's why I can just type in GPT from anywhere my terminal doesn't have to be in the same folder and I can type in the prompt right away um and because this is actually referencing the file on disk uh if I change and modify that file like add a new system personality it will automatically update when I use this command so that's it I hope you enjoyed the tutorial and if you want to take a closer look at the code then check the video description below for the GitHub link if you have any questions or feedback then please let me know in the comments and if you enjoyed this video and you want to see what else you could do with AI and python then check out this project where I generate an original Pokemon card collection using open Ai and mid-journey I hope you found this useful and thank you for watching

Original Description

Want to learn how to use the ChatGPT API in Python? In this video, you'll learn how to build a ChatGPT terminal client in Python and use the ChatGPT API to add functionality to your app. 👉 Learn how to use the ChatGPT API in Python 👉 Store chat history to support conversations with ChatGPT 👉 Modify AI personality and responses 🔗 Code: https://github.com/pixegami/gpt-shell 🔗 OpenAI API: https://platform.openai.com/ 🔗 Generate Pokemon Cards with AI/Python: https://youtu.be/IrDRHzT26F4 📚 Chapters 00:00 - 00:48: Introduction 00:48 - 03:32: Project Overview 03:32 - 06:27: ChatGPT API Documentation 06:27 - 19:27: Project Code 19:27 - 20:53 Customize ChatGPT with System Roles 20:53 - 22:20: Example Real Estate AI 22:20 - 25:02: Create an Alias For The Script 25:02 - 25:28: Wrapping Up
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from pixegami · pixegami · 32 of 60

1 How to Build an AWS Lambda Function in Python in Just 7 Minutes!
How to Build an AWS Lambda Function in Python in Just 7 Minutes!
pixegami
2 AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
pixegami
3 I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
pixegami
4 Create NFT Generative Art with Python! (Full Tutorial)
Create NFT Generative Art with Python! (Full Tutorial)
pixegami
5 Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
pixegami
6 NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
pixegami
7 Python Web Scraping Tutorial • Step by Step Beginner's Guide
Python Web Scraping Tutorial • Step by Step Beginner's Guide
pixegami
8 Build Wordle in Python • Word Game Python Project for Beginners
Build Wordle in Python • Word Game Python Project for Beginners
pixegami
9 How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
pixegami
10 Top 10 Python Modules 2022
Top 10 Python Modules 2022
pixegami
11 How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
pixegami
12 How To Write Unit Tests in Python • Pytest Tutorial
How To Write Unit Tests in Python • Pytest Tutorial
pixegami
13 How to Style Your React Landing Page with Tailwind CSS
How to Style Your React Landing Page with Tailwind CSS
pixegami
14 FastAPI Python Tutorial - Learn How to Build a REST API
FastAPI Python Tutorial - Learn How to Build a REST API
pixegami
15 How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
pixegami
16 PyScript • How to run Python in a browser
PyScript • How to run Python in a browser
pixegami
17 My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
pixegami
18 Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
pixegami
19 NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
pixegami
20 AWS Lambda Python functions with a database (DynamoDB)
AWS Lambda Python functions with a database (DynamoDB)
pixegami
21 How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
pixegami
22 How to Make a Discord Bot with Python
How to Make a Discord Bot with Python
pixegami
23 How To Use GitHub Copilot (with Python Examples)
How To Use GitHub Copilot (with Python Examples)
pixegami
24 PyTest • REST API Integration Testing with Python
PyTest • REST API Integration Testing with Python
pixegami
25 Python Beginner Project: Build a Caesar Cipher Encryption App
Python Beginner Project: Build a Caesar Cipher Encryption App
pixegami
26 Decorators in Python: How to Write Your Own Custom Decorators
Decorators in Python: How to Write Your Own Custom Decorators
pixegami
27 NextJS 13 Tutorial: Create a Static Blog from Markdown Files
NextJS 13 Tutorial: Create a Static Blog from Markdown Files
pixegami
28 Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
pixegami
29 How I Would Learn Python (if I had to start over) • A Roadmap for 2023
How I Would Learn Python (if I had to start over) • A Roadmap for 2023
pixegami
30 Build an AI Pokemon Generator with Python and Midjourney
Build an AI Pokemon Generator with Python and Midjourney
pixegami
31 Why You Should Learn Python in 2023 (as your first programming language)
Why You Should Learn Python in 2023 (as your first programming language)
pixegami
ChatGPI API in Python ✨ How to Build a Custom AI Chat App
ChatGPI API in Python ✨ How to Build a Custom AI Chat App
pixegami
33 Learn Python • #1 Installation and Setup • Get Started With Python!
Learn Python • #1 Installation and Setup • Get Started With Python!
pixegami
34 Learn Python • #2 Variables and Data Types • Python's Building Blocks
Learn Python • #2 Variables and Data Types • Python's Building Blocks
pixegami
35 Learn Python • #3 Operators • Add, Subtract and More...
Learn Python • #3 Operators • Add, Subtract and More...
pixegami
36 Learn Python • #4 Conditions • If / Else Statements
Learn Python • #4 Conditions • If / Else Statements
pixegami
37 Learn Python • #5 Lists • Storing Collections of Data
Learn Python • #5 Lists • Storing Collections of Data
pixegami
38 Learn Python • #6 Loops • How to Repeat Code Execution
Learn Python • #6 Loops • How to Repeat Code Execution
pixegami
39 Learn Python • #7 Dictionaries • The Most Useful Data Structure?
Learn Python • #7 Dictionaries • The Most Useful Data Structure?
pixegami
40 Learn Python • #8 Tuples and Sets • More Ways To Store Data!
Learn Python • #8 Tuples and Sets • More Ways To Store Data!
pixegami
41 Learn Python • #9 Functions • Python's Most Important Concept?
Learn Python • #9 Functions • Python's Most Important Concept?
pixegami
42 Learn Python • #10 User Input • 4 Ways To Get Input From Your User
Learn Python • #10 User Input • 4 Ways To Get Input From Your User
pixegami
43 Learn Python • #11 Classes • Create and Use Classes in Python
Learn Python • #11 Classes • Create and Use Classes in Python
pixegami
44 Learn Python • #12 Final Project • Build an Expense Tracking App!
Learn Python • #12 Final Project • Build an Expense Tracking App!
pixegami
45 Stripe & Firebase Tutorial • Add Payments To Your NextJS App
Stripe & Firebase Tutorial • Add Payments To Your NextJS App
pixegami
46 How To Use GitHub Actions • Automate Your AWS Deployments
How To Use GitHub Actions • Automate Your AWS Deployments
pixegami
47 How to Run a Python Docker Image on AWS Lambda
How to Run a Python Docker Image on AWS Lambda
pixegami
48 My MacOS Terminal Setup for HIGH Productivity
My MacOS Terminal Setup for HIGH Productivity
pixegami
49 Host a Python Discord Bot on AWS Lambda (Free and Easy)
Host a Python Discord Bot on AWS Lambda (Free and Easy)
pixegami
50 Python FastAPI Tutorial: Build a REST API in 15 Minutes
Python FastAPI Tutorial: Build a REST API in 15 Minutes
pixegami
51 Pydantic Tutorial • Solving Python's Biggest Problem
Pydantic Tutorial • Solving Python's Biggest Problem
pixegami
52 How to Get Started with AWS • Crash Course
How to Get Started with AWS • Crash Course
pixegami
53 Python Requests Tutorial: HTTP Requests and Web Scraping
Python Requests Tutorial: HTTP Requests and Web Scraping
pixegami
54 Amazon Bedrock Tutorial: Generative AI on AWS
Amazon Bedrock Tutorial: Generative AI on AWS
pixegami
55 How to Publish a Python Package to PyPI (pip)
How to Publish a Python Package to PyPI (pip)
pixegami
56 Langchain: The BEST Library For Building AI Apps In Python?
Langchain: The BEST Library For Building AI Apps In Python?
pixegami
57 RAG + Langchain Python Project: Easy AI/Chat For Your Docs
RAG + Langchain Python Project: Easy AI/Chat For Your Docs
pixegami
58 Python Dataclasses: Here's 7 Ways It Will Improve Your Code
Python Dataclasses: Here's 7 Ways It Will Improve Your Code
pixegami
59 Build a Custom AI RPG Game with OpenAI GPTs
Build a Custom AI RPG Game with OpenAI GPTs
pixegami
60 Create a Custom AI Assistant + API in 10 Mins
Create a Custom AI Assistant + API in 10 Mins
pixegami

This video teaches you how to build a custom AI chat app using the ChatGPT API in Python. You'll learn how to set up the OpenAI API, send prompts, and store chat history. The project utilizes the OpenAI Python Library and can be run directly from the terminal.

Key Takeaways
  1. Sign up for an OpenAI account
  2. Create a new secret API key
  3. Install OpenAI Python Library with Pip
  4. Run project directly from the terminal
  5. Use project to ask GPT questions and maintain chat context
  6. Create a function to send a prompt to ChatGPT using the OpenAI client
  7. Set up the OpenAI client with the API key and make a request to the ChatGPT API
  8. Extract response with `response.choices[0].message.content`
  9. Store response in a variable and print it out and return it
💡 The ChatGPT API can be used to build custom AI chat apps, and the OpenAI Python Library provides a convenient way to interact with the API.

Related AI Lessons

I Asked ChatGPT to Fix My Life. It Couldn’t — Until I Changed One Thing
Learn how to effectively use AI like ChatGPT to improve your life by changing your approach
Medium · AI
I Asked ChatGPT to Fix My Life. It Couldn’t — Until I Changed One Thing
Learn how to effectively use ChatGPT to solve personal problems by changing your approach
Medium · ChatGPT
Claude Sonnet 5 Is Here: Why It Might Replace Your Opus Subscription
Learn about Claude Sonnet 5, a new AI model that offers near-flagship performance at a lower price, and its potential to replace Opus subscriptions
Medium · Programming
Introducing Claude Sonnet 5 on AWS: Anthropic’s most capable Sonnet model
Learn about Claude Sonnet 5, Anthropic's most advanced Sonnet model, now available on AWS, and how it delivers top-tier intelligence for coding, agents, and professional tasks
AWS Machine Learning

Chapters (8)

00:48: Introduction
0:48 03:32: Project Overview
3:32 06:27: ChatGPT API Documentation
6:27 19:27: Project Code
19:27 20:53 Customize ChatGPT with System Roles
20:53 22:20: Example Real Estate AI
22:20 25:02: Create an Alias For The Script
25:02 25:28: Wrapping Up
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →