ChatGPT API in Python

Exploring the ChatGPT (GPT3.5) API from OpenAI and building some simple chat applications with it.
Github code: github.com/Sentdex/ChatGPT-AP...
Neural Networks from Scratch book: nnfs.io
Channel membership: / @sentdex
Discord: / discord
Reddit: / sentdex
Support the content: pythonprogramming.net/support...
Twitter: / sentdex
Instagram: / sentdex
Facebook: / pythonprogramming.net
Twitch: / sentdex
Contents:
0:00 - Why use the ChatGPT API
2:10 - How to query the ChatGPT API
7:45 - History and dynamic input w/ ChatGPT API
15:30 - Comining everything so far
18:25 - Building a Gradio chat application with ChatGPT

Пікірлер: 184

  • @isaacandrewdixon
    @isaacandrewdixon Жыл бұрын

    I have always loved that you don't edit out errors and mistakes, and show us your process of trying to understand them.

  • @kenchang3456

    @kenchang3456

    Жыл бұрын

    I always love that he laughs at his mistakes.

  • @satya.antonio

    @satya.antonio

    9 ай бұрын

    I agree! That's very valuable. To see how intelligent people go through analyzing a problem and looking for a solution. And it also gives time for us to think along the video.

  • @krzysiek6293
    @krzysiek6293 Жыл бұрын

    This is a beginning for numerous of startups

  • @CodingWithLewis
    @CodingWithLewis Жыл бұрын

    I love how simple the API is! We really are in the gold rush of AI based applications.

  • @nickwinn

    @nickwinn

    Жыл бұрын

    We are indeed, it's exciting and scary. I'm writing my first chatgpt app that will troll scammers on craigslist.

  • @tomb.8375

    @tomb.8375

    Жыл бұрын

    @@nickwinn simple but it would have been nice if they managed the history?

  • @fordtify
    @fordtify Жыл бұрын

    I started watching your videos in 2017 in college. Thanks to you and specifically your pygame series I'm a mid level SWE

  • @awesomefeldmanfamily
    @awesomefeldmanfamily Жыл бұрын

    Amazing!!! Thank you!!! I was always waiting for this!! I have notifications set for your channel but I never get any notifications, also I didn't even see any of your videos for the past year on my feed or anything

  • @nikitaserov33
    @nikitaserov33 Жыл бұрын

    Greatest guy on the internet, always loved the way you follow your passion and work not on just some classical stuff but playing with whatever is interesting for you If you’ll ever want to work on AI-aided chemistry/medicine - our Chemistry and Artificial Intelligence lab in ITMO university is fully open for you 🥰 Continue making great stuff 👍

  • @real_Emmanuel
    @real_Emmanuel Жыл бұрын

    Your awesome. Keep making simple videos like this! I just subscribed because of how simple this was.

  • @GiantsOnTheHorizon
    @GiantsOnTheHorizon Жыл бұрын

    Total side note but I wanted to tell you how amazing your Neural Networks from Scratch book is. Ive started down a few roads with NNs and I normally prefer video but you have really made it so clear and so much fun to learn. Congratulations on creating the perfect technology book!

  • @sentdex

    @sentdex

    Жыл бұрын

    Awesome to hear this! Thank you!

  • @adilmajeed8439

    @adilmajeed8439

    Жыл бұрын

    Please share the book name

  • @kweekkikker

    @kweekkikker

    Жыл бұрын

    ​@@sentdexI am a painter, and like all you do, even the mind blowing 1st hand errors you can't hold your laugh about, I rofl than 2, everything. Simply because u rite

  • @riveradam
    @riveradam Жыл бұрын

    I was about to write "you don't need an API key" but then I did a sanity check. I thought I was using `gpt-3.5-turbo` API for free, but what's actually true is ... if OPENAI_API_KEY is in your environment variables, then `import openai` will automatically find that key and use it. I'd previously set the env var for testing `text-davinci-003` (GPT-3) AND I'd included `openai.api_key = os.getenv('OPENAI_API_KEY')` in my code, but when I tested `gpt-3.5-turbo` for the first time I forgot the second line and then when it worked I assumed they'd removed the need for a key. Great video! Thanks!

  • @nickpalladinos
    @nickpalladinos Жыл бұрын

    Great work! The API is extremely easy to use, and I was able to create a small hack (little-reasoner) that combines the power of ChatGPT and the Z3 theorem prover.

  • @pookiehd
    @pookiehd Жыл бұрын

    Perfect timing, I was just about to use it in my next project.

  • @yellowboat8773
    @yellowboat8773 Жыл бұрын

    I’ve been a sub of your for years, funny thing is I am not a programmer, or even remotely work in the field you produce videos on. I just love watching your curiosity take you around the j Ferber and take your time to teach others. Well done mate

  • @kweekkikker

    @kweekkikker

    Жыл бұрын

    So true❤

  • @YashPachchigar69
    @YashPachchigar69 Жыл бұрын

    Always loved your tutorials videos

  • @kensai7
    @kensai7 Жыл бұрын

    Tons of wisdom, as always. We thank you! 🤓

  • @nuclear_AI
    @nuclear_AI Жыл бұрын

    The longer between this video and your next one, the more excited I get.😂😂

  • @caioflores4918
    @caioflores4918 Жыл бұрын

    Nice job, man. Regards from Brazil!

  • @AllThatInterestingStuff
    @AllThatInterestingStuff Жыл бұрын

    I have already done and deploy a ChatGPT "proxy" via web and an API for mobile apps. It works great, I'm here for the assistant history part. :) Can't wait to see that because for now, I'm truncating the history down to 500 tokens, which is working pretty good for me.

  • @AllThatInterestingStuff

    @AllThatInterestingStuff

    Жыл бұрын

    Unfortunately I did not get the answer from this vid. I can't wait for the next one. :) Using user+assistant role instead of system role is a very clever idea. :) thanks! Btw, if you have any chance to get Whisper API working on Android that would be awesome. I could not find any good solutions to that. Uploading mp3 to the server is not an option 😃

  • @javiers2133
    @javiers2133 Жыл бұрын

    awesome video as always thank you very much, I hope you have a great day!

  • @tanmaydash2108
    @tanmaydash2108 Жыл бұрын

    Always loved your work .thank u for ur inspiration.m deep fan of urs.

  • @raomuhammadsharjeel8029
    @raomuhammadsharjeel8029 Жыл бұрын

    Who has been waiting for this for a long time now?

  • @funkahontas
    @funkahontas Жыл бұрын

    In my experience, the system role is really useful for things like restraining the bot as to what it can do , and also to give it some background information like how it would like to be called or what tasks it can perform. So when the user asks :" what can you do for me ?" The chatbot can answer what the system says it will be able to do or what it's main purpose is , and the personality you want the bot to have. The system message may be something like "You are a language translation helping bot, you cannot talk about anything else, your name is Bob and you are a stern but calm teacher."

  • @kweekkikker

    @kweekkikker

    Жыл бұрын

    I smell spam . I Red this 1 twice already

  • @msaeed5961
    @msaeed5961 Жыл бұрын

    Best channel to learn python.

  • @davidwu3247
    @davidwu3247 Жыл бұрын

    thank you so much for this brotha. real lifesaver

  • @naidol
    @naidol Жыл бұрын

    Bro. This is a great tutorial. Most other people are just publishing some nonsense gpt-api stuff. You are bit of a whacko (compliment) but i 😍 your speed of tutoring. You did not waste our time by going back to check on AI reply about which moon it was sizing. Great stuff dude.

  • @schlechtestergtaspielerdek3851
    @schlechtestergtaspielerdek385111 ай бұрын

    Amazing! I am building a cocktail maschine with this. I recognise voice with speech to text, feed it into the API. Like "I want a martini please", I will feed in with the custom add on to convert the cocktail to a json with a given format, my cocktail machine can use and make a cocktail with. :)

  • @samahirrao
    @samahirrao Жыл бұрын

    A big thanks from an Indian. Amazing stuff you post. God bless you.

  • @abilovestotrade
    @abilovestotrade Жыл бұрын

    Which extension in VS Code helps with the completion of syntax like that ?

  • @patricksweetman3285
    @patricksweetman3285 Жыл бұрын

    Great video, thanks.

  • @davidobembe5302
    @davidobembe5302 Жыл бұрын

    I just love your videos ♥️

  • @Brickkzz
    @Brickkzz Жыл бұрын

    Back to tutorials! Hell yeah!

  • @goonie79
    @goonie79 Жыл бұрын

    Is Azure's generative AI solutions the only option to both fine-tune and build guardrails for niche chatbots? It seems to be the only option to feed custom indexes in a GUI so that the chatbot is bespoken for specialized use cases.

  • @DigitalSamTV
    @DigitalSamTV Жыл бұрын

    hi, how do u set if the api uses gpt3.5 or gpt 4 ? there is no setting when you generate the key as far as I can see... please help. cheers

  • @edgarbernal-martinez4698
    @edgarbernal-martinez469811 ай бұрын

    question. to remove the user's input from the textbox in Gradio. Do you need to use "with gr.Blocks () as demo? i noticed I was using 'gr.interface': demo = gr.Interface(( fn=CustomChatGPT, inputs=input_textbox, outputs=output_textbox, title="" ) )

  • @111wizo
    @111wizo Жыл бұрын

    Great vid but was just wondering what the rate limit on gpt-3.5-turbo is since I couldn't find any solid documentation online and wanted to know it since I plan on mostly using it for my own recreational use which will involve quite a bit of requests being sent? Currently still on the free plan but I want to confirm this before going paid.

  • @UncleEngineer
    @UncleEngineer Жыл бұрын

    Wow, Thank you

  • @user-cy2on3gc3k
    @user-cy2on3gc3k11 ай бұрын

    Thank you so much for sharing 💚💚💚💚

  • @real_Emmanuel
    @real_Emmanuel Жыл бұрын

    How would I customize the page where you are asking questions. For example, if you wanted to turn the textbox green and chatbot box red?

  • @mytechnotalent
    @mytechnotalent Жыл бұрын

    ahh yeah this one it is cheap to use and this is a great example thank you!

  • @monstercameron
    @monstercameron Жыл бұрын

    @20:19 isnt that type of prompt what they give as an example in the docs for system prompts?

  • @dansu1503
    @dansu1503 Жыл бұрын

    Thanks for the great content, sentdex! If I understand the process correctly, every time the user adds a message, we need to extend message_history and pass the entire message_history to ChatGPT is that right? My concern is that the cost for giving N responses would scale on the order of N^2 (if all future messages require the full history). Although I cannot think of any other way to use ChatGPT currently--unless there is some "delta" api call that can pass in new messages and load past tokens for free? I think this is a rather big barrier to "indie" developers adding ChatGPT to certain applications--wonder if you have any thoughts on this!

  • @anthoniG

    @anthoniG

    Жыл бұрын

    Yeah I'd like to know this as well. I can see it getting out of hand and just swallowing tokens.

  • @generichuman_
    @generichuman_ Жыл бұрын

    System is where I define the persona of the bot, any special instructions, and most importantly, where I dump any additional information that will be useful to the bot. Text retrieved using semantic search, summarized chat logs from previous conversations etc.

  • @sanattaori6209
    @sanattaori6209 Жыл бұрын

    Thank you

  • @Riitabobita
    @Riitabobita Жыл бұрын

    Hi, I want to learn about data analysis. I don't know anything about it, but I'm interested in starting something new. I was looking into starting the data analysis course on Coursera, but just wanted to see if I should take another route before doing that. What would you recommend? Thanks in advance!

  • @Jlane31
    @Jlane31 Жыл бұрын

    Great video, thanks for posting it. Can you try editing the first prompt and have it say what is the circumference of Earth's moon? My guess is the script could reference the message history but since there are so many moons it wasn't sure you meant Earth's. Anywho, good content!

  • @borisdominguez9089
    @borisdominguez9089 Жыл бұрын

    Man, I love you.

  • @edellenburg78
    @edellenburg78 Жыл бұрын

    I used the api to make a chat bot for my discord community, but they used it too much, and I could not afford to keep it going, but man having a group chat with AI is crazy.

  • @tomb.8375
    @tomb.8375 Жыл бұрын

    Thanks for your video. I am wondering, how much will it cost as we keep sending the message history? My question is really: if we keep the history building between messages, will our cost increase because we keep submitting the history ?

  • @sptoo123
    @sptoo123 Жыл бұрын

    It's quite interesting. I try to make it a role with the "spell" : "You are a well-trained AI multi-task language translator. When I input non-Chinese sentences, you should output Chinese translation sentences. While I input Chinese sentences, you should output Vietnamese. You only need to ouput the translation result, no need other words or explanation. If you understand, say OK." It succeed in the first, but with more sentences input, it confused, even I input a chinese sentence, it return a chinese sentence "translation" (which is the same words because no need to translate) but not a vietnamese one. I'm not sure why but it just can't understand or forget the tasks when I input like 5~6 non-chinese sentences and some chinese sentences.

  • @jalladcom-sq1wk
    @jalladcom-sq1wk7 ай бұрын

    You are amazing .. thanxxxxxxxxx

  • @alexgts1234
    @alexgts1234 Жыл бұрын

    I love how you took one of the previous top comments (previous video) into consideration to "live" code again, like "back then" when your channel was small.

  • @mohamedelbahichi876
    @mohamedelbahichi876 Жыл бұрын

    hi I have a question please. How can I activate the autocomplete that you are using

  • @jfjesus1980
    @jfjesus1980 Жыл бұрын

    Although they say that the assistant role is needed in order for ChatGPT remember the previous responses, from my experience, it only works if you also define a system role.

  • @OZtwo
    @OZtwo Жыл бұрын

    Been meaning to get around to it, but really would like to setup ChatGPT so that it can talk with itself on two different systems just to see what happens. :)

  • @metaltech3944
    @metaltech3944 Жыл бұрын

    Wouldn't having to send message history every time you want a predication get very expensive token wise?

  • @nisargbhavsar25
    @nisargbhavsar25 Жыл бұрын

    Which intellisense are you using in VS Code?

  • @konstantinrebrov675
    @konstantinrebrov675 Жыл бұрын

    What is the VS Code extension that you use for interactive Python?

  • @akoslaszlo974
    @akoslaszlo974 Жыл бұрын

    thx. the video! how can I publish it to public? so not run only locally?

  • @thefigmaster3519
    @thefigmaster3519 Жыл бұрын

    In my experience, its difficult to restrain GPT. For example if you make a request to it that future messages should conform to some format but you later ask it to stop that, it will stop. No matter how ademant you are that it should not violate a rule in a message, this can be overruled in a future message. Thoughts?

  • @xbox9092
    @xbox9092 Жыл бұрын

    amazing !

  • @rudrakshcodes
    @rudrakshcodes Жыл бұрын

    What font family are you using for the vs code?

  • @alroygama6166
    @alroygama6166 Жыл бұрын

    Which vs code plugin are u using to get those code suggestions?

  • @michaelfekadu6116
    @michaelfekadu6116 Жыл бұрын

    15:23 Since the initial question never said “Earth’s moon” the AI had to infer that’s what you meant. It is technically true that if you had referenced the “Earth’s moon” in some prior conversation then the history of that prior conversation would not be given to the AI. The AI can access chat history, but only the current chat history.

  • @Veptis
    @Veptis6 ай бұрын

    how do you feel about using a terminal within vscode?

  • @trentleslie2888
    @trentleslie2888 Жыл бұрын

    You are a nerd's nerd, and I love it.

  • @harvey.28
    @harvey.28 Жыл бұрын

    How can I use this and create my own using my own answers for the bot? Thanks~

  • @miscellaneous9932
    @miscellaneous9932 Жыл бұрын

    Is there a limit to how long the message history could be?

  • @AbhishekKumar-se2ub
    @AbhishekKumar-se2ub Жыл бұрын

    I was wondering when you will upload such video

  • @wobbler8473
    @wobbler8473 Жыл бұрын

    What extension is it that autocompletes the code? Thanks

  • @houssem009
    @houssem009 Жыл бұрын

    What os are you using? And was that a bash terminal? Looks more like zsh terminal, would love to see a video about your setup

  • @HubbiNashrullahM

    @HubbiNashrullahM

    Жыл бұрын

    It looks like Ubuntu

  • @tielessin
    @tielessin Жыл бұрын

    I wonder where the conversations would end up if you have another ChatGPT model play the role of the user

  • @JoeTinnySpace

    @JoeTinnySpace

    Жыл бұрын

    Black holes my friend

  • @Primarycolours-

    @Primarycolours-

    Жыл бұрын

    @@brandonbahret5632 it still need user's prompt in it to answer any questions, in short: it doesn't change a thing about how it interacts with the user.

  • @brandonbahret5632

    @brandonbahret5632

    Жыл бұрын

    @@Primarycolours- what? No, you can totally have chatgpt interview an instance of itself. Its just like asking GPT-3 to generate a transcript and not providing any stop codes.

  • @caspercby
    @caspercby8 ай бұрын

    Love your python setup in V.S. for openAI !! Do you have a video tutorial on it? Thanks!

  • @Ironmechanic-ct2yw
    @Ironmechanic-ct2yw Жыл бұрын

    This API is crazy.

  • @xntumrfo9ivrnwf
    @xntumrfo9ivrnwf Жыл бұрын

    lol I'm following along but I didn't get this fluke with the "which moon is this in reference to" question - worked fine for me

  • @fdanisndsa
    @fdanisndsa Жыл бұрын

    Why do I keep getting the error that AttributeError: module 'openai' has no attribute 'ChatCompletion'?

  • @DMan6285
    @DMan6285 Жыл бұрын

    What extension are you using for auto complete, co-pilot?

  • @DMan6285

    @DMan6285

    Жыл бұрын

    Nevermind. You answered it in the video. ;-)

  • @Dkingprime
    @Dkingprime Жыл бұрын

    Hi, sendtex. Python or JavaScript for backend, which would you recommend? I can't decide between them

  • @development3383

    @development3383

    Жыл бұрын

    That's entirely dependent on you (and your team) and your project. Most of the website stuff I do is fairly simple so I just use nodeJS but if I were to write more complex endpoints I'd use django (python) or springboot (java). If you know one of those languages already, go with that. otherwise chose one of them and learn it

  • @extrememike
    @extrememike Жыл бұрын

    15:20 I was using the website free version yesterday and ran in to a similar problem. I requested an output based on information I had provided previously (above) and it said it could not refer to my previous messages. Maybe is something they temp discontinued to increase speed.

  • @Lolguyj

    @Lolguyj

    Жыл бұрын

    You probably went past the token limit

  • @pd2437
    @pd2437 Жыл бұрын

    It would be great if you could make a series on Transformer models!!!

  • @markp2381
    @markp2381 Жыл бұрын

    You could explore building something with Langchain

  • @antoniosalzano6235
    @antoniosalzano6235 Жыл бұрын

    you said that the API itself isn't going to manage your history for you, so how might we do that?just start with some sort of message history variable for now to keep it simple, but we might use a database or some other storage method. Can you explain how we can do that using a database for example?

  • @funkahontas

    @funkahontas

    Жыл бұрын

    one way to do this would be to store each message along with its associated metadata (such as sender, timestamp, etc.) in a database table. Then, when generating responses using the ChatGPT API, you could query the database for relevant messages and use them to provide context for the API.

  • @antoniosalzano6235

    @antoniosalzano6235

    Жыл бұрын

    @@funkahontas ok I got it, but how can you relate the content ID with the answer. For example: if I say to the AI, my name is "X". The AI says "Hi X, nice to meet you". I store this two entries in the DB. But then? I have to do function that will scan the entire DB in order to search something "my name is: ..." and take the context?

  • @willhasnofriends
    @willhasnofriends Жыл бұрын

    Couldn't even get past the first run. "openai.error.InvalidRequestError: The model `gpt-3.5-turbo0302` does not exist"

  • @eaudesolero5631
    @eaudesolero5631 Жыл бұрын

    so what is the difference between these LLM's (gpt3/4, Alpaca, etc), what was AlphaFold/ESM/2, and the types of systems that were used to create efficient biologically similar structures like frames for vehicles or furniture? And Alpha Tensor? Wolfram Alpha? what other types of AI/ML systems are there? some are trying to do things as good as humans, some are doing things we can not do. how are these different things coded? what are the ideas these are based upon? how can they be merged? can each be used to improve the others? what are evoformers vs transformers? and what other things are there?

  • @varghapayandeh3024
    @varghapayandeh3024 Жыл бұрын

    Why do I get "module 'openai' has no attribute "ChatCompletion"!?

  • @Manivelarino
    @Manivelarino Жыл бұрын

    they gotta come out with a self-hosted version. Not having to send all the data to openai to get a prediction would be a game changer.

  • @israelRaizer

    @israelRaizer

    Жыл бұрын

    First we need to figure out a way to make these models smaller, currently you need a very beefy computer to run them at any reasonable speed

  • @metaltech3944

    @metaltech3944

    Жыл бұрын

    actually they do have one: kzread.info/dash/bejne/pHun0M2kqrmYj9I.html

  • @kai_onlineaaa
    @kai_onlineaaa Жыл бұрын

    1:41 whats the downside to using jupyter notebook?? ;-;

  • @goodnightut
    @goodnightut15 күн бұрын

    i got similar behavior my first go yesterday. seems like it's confused by my role and it's role

  • @mbonuchinedu2420
    @mbonuchinedu2420 Жыл бұрын

    Yeah, sometimes they depreciate it on purpose. Thanks sentdex.

  • @T13Nemo
    @T13Nemo Жыл бұрын

    Is it possible to actually give internet access to the gpt levrraging google api for example? So that bot can search on the internet and get knowledge of the most recent events?

  • @Raffsy
    @Raffsy Жыл бұрын

    What theme is he using?

  • @himalkunwar9
    @himalkunwar9 Жыл бұрын

    can AI replace ml/Ai engineers?

  • @ChaiTimeDataScience
    @ChaiTimeDataScience Жыл бұрын

    Me: Sad, having a rough day.... Laptop: "What is going on everybody..." and I'm happy again!

  • @jonclement
    @jonclement Жыл бұрын

    Lets' not forget that other planets in our solar system have named moons -- ours is actually named, "the moon" -- hopefully that doesn't send the AI into an infinite loop

  • @mittamoa

    @mittamoa

    Жыл бұрын

    Luna ?

  • @moe3551
    @moe3551 Жыл бұрын

    Are you gonna be continuing the nnfs series? :(

  • @sentdex

    @sentdex

    Жыл бұрын

    Yes

  • @HellboyMob
    @HellboyMob Жыл бұрын

    man this world is getting good

  • @serafinalcantara5520
    @serafinalcantara55209 ай бұрын

    12:44 maybe it did not catch the message history. It probably answered from the data it was trained on.

  • @JamesTJoseph
    @JamesTJoseph Жыл бұрын

    Do you see any changes in the GitHub Copilot since ChatGPT released?

  • @fordimension

    @fordimension

    Жыл бұрын

    Not sure if it's to do with chatGPT but I noticed copilot seems to be reading the code underneath now. Before if you insert a new line above a line of code and type something similar to the below line, it would act as if it only reads the code above, but now it appears to make predictions based on whats written below.

  • @mattizzle81

    @mattizzle81

    Жыл бұрын

    @@fordimension Yes noticed that too recently

  • @gamedark4765
    @gamedark47655 ай бұрын

    you use wich linux???

  • @iamtowbee
    @iamtowbee Жыл бұрын

    Great work. How do you get the code to autocomplete?

  • @sentdex

    @sentdex

    Жыл бұрын

    Github copilot

  • @mccleod6235

    @mccleod6235

    Жыл бұрын

    Github Copilot.

  • @davidfonseca87
    @davidfonseca87 Жыл бұрын

    Sentdex using Copilot?? 😯 I remember when he used IDLE

  • @sentdex

    @sentdex

    Жыл бұрын

    I have been a mega fan of copilot since release tbh.

  • @davidfonseca87

    @davidfonseca87

    Жыл бұрын

    @@sentdex I have found rubberduck on VSCode, which has been updated with the latest changes on the API, I really it, I would guess rubberduck has more options I may need to try more copilot to see its uniqueness

  • @HG77K
    @HG77K Жыл бұрын

    Thought on NeRF / Luma labs AI