How to Integrate ChatGPT with Node.js App using the OpenAI API | JSON Formatted Responses
Вставка
- Опубліковано 11 вер 2024
- #chatgpt #openai #nodejs
Hello Everyone and Welcome Back to Code Deep Dive Channel.
In this video, we will be focusing on using the OpenAI API to generate text in response to a prompt, and also retrieving the AI's response in JSON format in a Node.js application. We will show you how to set up the OpenAI API and authenticate your requests, as well as how to send a prompt to the API and retrieve the generated text in JSON format.
The source code written during the video is available on GitHub at
github.com/Bra...
you can use this code as a starting point for your own projects and refer to it as you follow along with the video.
For further information on using the OpenAI API, please refer to the official documentation at
platform.opena...
it provides detailed information on how to use the API, including authentication, usage limits, and more.
👉 𝐈𝐟 𝐲𝐨𝐮 𝐥𝐢𝐤𝐞 𝐭𝐡𝐢𝐬 𝐯𝐢𝐝𝐞𝐨, 𝐩𝐥𝐞𝐚𝐬𝐞 𝐥𝐢𝐤𝐞 𝐢𝐭 𝐚𝐧𝐝 𝐬𝐡𝐚𝐫𝐞 𝐢𝐭.
👉 𝐃𝐨𝐧'𝐭 𝐟𝐨𝐫𝐠𝐞𝐭 𝐭𝐨 𝐬𝐮𝐛𝐬𝐜𝐫𝐢𝐛𝐞 𝐭𝐨 𝐦𝐲 𝐜𝐡𝐚𝐧𝐧𝐞𝐥 𝐟𝐨𝐫 𝐦𝐨𝐫𝐞 𝐮𝐩𝐝𝐚𝐭𝐞𝐬.
/ @codedeepdive
Suggested videos for you:
1. Easily Generate Images in Node JS Using OpenAI (Dalle-2) Generation API : A Simple & Easy Guide • How To Generate Images...
2. Create a RichText Input in React (With Customized Toolbar Template) | ReactJS • Create a RichText Inpu...
3. Easily Add Slider Input in ReactJS using PrimeReact | User Input Tutorial • Easily Add Slider Inpu...
🔎 Related Keywords:
chatgpt,openai,chat gpt,chatgpt api,chatgpt tutorial,openai api,open ai,ai,artificial intelligence,nodejs,integrate chatgpt,chatgpt explained,chatbot,coding,programming,learn to code,programmer,code,javascript,javascript for beginners,javascript tutorial,learn javascript,javascript tutorial for beginners,javascript course,javascript basics,javascript crash course,javascript tutorials,javascript project,chatgpt examples,how to use chatgpt
🔎 Hashtags:
#howtousechatgpt
#chatgptexplained
#javascript
#learnjavascript
#coding
#javascripttutorial
My teeth were almost dropping out of the jaw in pressure of code, but Then I found your video. Thank you Sir. You helped a lot!!
So glad it helped, keep those teeth in!
@@TheTechDeepDive Yes, I am busy Repacking 📦📦😅
that is the typical gtp response haha
Why did I get the request failed with status code 429.?
🙃🙃🙃🙃🙃🙃
Thanks, CDD. I am working on a project and the response json from Openai was a big problem. thanks for showing the way to get the result in the expected format! 👍
No problem!! I was having the same issue, it's a huge pain.
Something I think I mentioned in the video, but never implemented was error handling. I would advise checking for an error when parsing the json, and if there is one, then run the prompt again. I usually do a 3 retry limit. Even with the best prompts I've come up with, I only get proper json back about 90% of the time. By trying 3 times, I can get it right pretty much every time!
Good luck!
I'm getting "Unexpected Token in JSON at position X" from time to time in the JSON sent by CHATGPT. Do you how to fix this by modifying the prompt?
I see a great UA-cam career ahead of you 💪
It sounds like the video helped! Glad to hear it!
@@TheTechDeepDive Definitely, I like the way you explained man. Respect from South London 🤝
Thank you for saving me time by creating short video and to the point. 😁
No problem! I’m glad the video helped!
Great video 👍
Glad it helped!
I have a task to make a custom chatbot using openai api using html,css and js and node.js express.js and building own chatbot I'm stuck to get the response from the api
THANK YOU SO MUCH OH MY GOD MY DAY WAS RUINED AND YOU SAVED IT
Glad your day was saved! :)
Dude how can you fix the import not recognized on the web browser in node it don't have error but on browser import not recognized how you fix this
You earned a new sub, keep it up 💪
Super glad it helped!!
Why I am getting Configuration is not a constructor error , when i run the node js server in this code ?
Got the same error here, could you solve it?
got the same errror here@@ogabrieljunqueira
Hi, thank you for your video which taught me to call openai. I know a little javascript. I want to use openai to make an app to help me learn English, and I have done it. It is a pity that the content cannot be output a little bit like chatgpt. When I set the stream to true, I don't know how to receive the data. I'm using typescript + vue3 + node.js. If you see, I'd like a finished example if you want, thanks.
I found a lot of code snippets on the Internet, But I couldn't get it to work.
Hey, I hope you've gotten this working. I haven't messed around too much with the data stream before. However, I've set it to true before, and seen the stream logged. It should be possible to do what you're looking to do. I would suggest looking at a websocket communication between the front and backend, and display the data live as it is fed through.
Good luck!
do you know how do I stop chatgpt API not to give answer other then the training that I gave to it?
I am using 3.5-turbo api model
You have to be super specific with the prompt, and reiterate multiple times you don’t want anything but the response structure you gave it.
I usually parse the response, and if it gives and error, I paese again. I give it 3 retries. It usually works 90% of the time, so with 3 tries it works 99.9% of the time
I would like to ask you something, is it possible to create an application, for example, "a job recruitment simulator" with chatgtp? is chagtp already stable? or is it better to wait for an interface or something? (I'm just starting to study programming)
It is absolutely possible to create something like this, and I see no reason not to.
It is stable in some sense. The API is stable and will not go away. However, the AI can be quite unpredictable, but I find it to generally be quite good, and it is continually improving. If you structure the prompt correctly, I suspect you'll get fairly good results.
Best of luck on your programming journey! It's difficult, rewarding, and fun.
All the best!
Hello, that is a fantastic explanation. How can I send an array of data in json format to chatgpt and get from chatgpt indicators of this data in json format? thank you
Jose, I’m very glad it helped.
The way I would suggest doing this is to specify in the prompt exactly the format and content of the response you would like to receive.
I would take the response and attempt to parse the string to JSON to get your results (as I did in the video). If the parsing fails, I would attempt the call to chat GPT again. If you work on optimizing your prompt (changing it until you get more consistent results), you can make it so the data received back is in the correct format almost every time. Since the AI tries to take some risks in the response, it will still sometimes return you an improper response. That is the case that you rerun the request and you will more than likely get a proper response the next time.
Hope this helps!
since the text-davinci-003 model is now deprecated can you show it with another model that would be very helpful ,thank you
why createAnswear is crossed out for me ?
I really want to know about the command i will use to install the packages
tranks bro
Glad it helped!
Yo, very cool thanks but i am getting TypeError: Configuration is not a constructor error over and over again, what might be the problem? 😬😬
Hi, I was wondering if you ever resolved this issue? I can't figure this out either.
@@woahmax hi, I opened a new account which gives you an API quota like 5$. Maybe this solved the problem but maybe I corrected implementation too, not sure about that but be sure you have some money on your OpenAI account.
@@egeozel80 thanks for getting back to me! I'll try that later today. Was there any changes to your code before getting it to work? Or was it literally just need to pay?
@@woahmax you are welcome and not sure unfortunately and I can't check that because I am close to my projects due, you can just ask ChatGPT for implementation of its own API, that was what I did lately.
@@egeozel80 I GOT IT TO WORK!!! Ok so turns out it wasn't because I didn't have enough money in my balance. I downloaded CDD's source code of the video and it works (as of writing this, I had to change the model to "gpt-3.5-turbo-instruct"). Then I compared to my own code and files on what the difference is.
Following instructions from this video, all of my code is correct.
Turns out the main difference is my axios and openai files in node_modules are different from CDD's. I replaced my axios and openai folders with Brady's source files and BAM it works. I'm not sure why the files are different when I use "yarn add openai" are different.
Hopefully this is helps for others running into this problem. Also hopefully someone can explain this happened to me.
Man I've managed to do this, but using the gpt 3.5 turbo model, so the structure of the createCompletion method is slightly different. I've gotten it to work, like in this video, in the server environment but I've been having trouble getting to use the api on the client side, for example done through an html web page. Do you know where I could get more info on how to implement this on the client side? Would appreciate any input. And thanks for the vid!
Looking for the same solution!
I'm not exactly sure what you're looking for here.
Since your API keys for openai should be private (to avoid costs being run up by someone stealing your keys), I would always call it from a backend. I would advise creating a backend, and using an endpoint with proper validation to communicate to your backend. I hope you found the solution you were looking for.
I downloaded your code and got the same error when i did on my own
const configuration = new Configuration({
^
TypeError: Configuration is not a constructor
GUide me
same issue did u find the sol?
Is there a reason you would need a "custom json" response from the api? Why not just take the original response and make a custom object in the code? I am asking cause, if you NEED 100% stable response, I would not risk that gpt is creating json object for me. Maybe you burn less tokens if gpt prepares you a json structure? @Code Deep Dive
Good point, you don't really need a json response. In the case I was using it for, I wanted 5 examples with 2 different fields each. I could've called each individually, but it wouldve taken much longer and used a few more tokens. It's up to you how you do it, but I found this seemed to work better for my use case!
Good vidoe, if I have a website, can I do this with only JS?
Yes, you could. I’d advise against it however as the end users could access your API key and use it to charge your account.
I'm getting "Unexpected Token in JSON at position X" from time to time in the JSON sent by CHATGPT. Anybody knows how to fix this by modifying the prompt?
I mentioned it briefly in the video but didn't go into detail:
By adjusting the prompt, I was only able to get about 90% of the time to return me the proper format. I would get that same error.
My way of getting around this would be to do a try-catch on the json parsing. If it gave me an error, I would go ahead and rerun the same prompt. I would give it a total of 3 tries (for me that got to about 99.9% of the time it works -- but you could obviously add more retries if you need).
I hope that helps!
@@TheTechDeepDive thanks a lot for a solution. 🤘
amazing video, if i may ask what vscode extension are you using that give auto complete suggestions?
Glad it helped! GitHub copilot was the extension!
How to make it customized for my own application ,this actually is responding only to one questions how can, i make it for my own applications so that it satisfy the user need can you please reply?
Can you give a little more context?
To get it to respond in the exact way you would like it to, you may need to play with the prompt a bit and adjust it until you find one that consistently returns the results you need.
It is likely it will still return incorrect responses from time to time. What I have done to get around that (in the case of needing a json response) is to attempt to parse the json, and if it gives an error during parsing, I then try the request again. I give it 3 total tries, and if it doesn’t work, there is likely a different issue occurring, so I don’t have it try again.
Is that what you mean?
@@TheTechDeepDive Thank you for your response ,the thing i needed is i need to create a custom support chat for my application which answers every questions for users (like checking the status of the order)is this possible with this code or else can you give me a tutorial regarding it if possible sir
@Gokula Indira,
This is absolutely possible, but it takes quite a bit more work to do. I would suggest looking into fine-tuning a model (you give openai 1000+ examples of prompts and answers, and it gives you a model that tries to respond according to those examples -- the more examples the better it will be). You can find some documentation on it here:
platform.openai.com/docs/guides/fine-tuning
In the documentation, they actually have your specific use case as an example, so that is quite helpful.
It's quite a bit more difficult than text generation, but I've played with it before, and it seems to work well. It is on my list to make a tutorial about, however it is quite a bit more complex so the video will be more difficult to make, and thus take longer. But I'm hoping to get a video out about it somewhat soon.
I wish you the best of luck!
@@TheTechDeepDive Thank you for your time
What is the name of the extension you are using on vscode which predicts the incoming commands? It's really cool.
GitHub Copilot!! It costs $10 a month or $100 a year. 100% worth it. It took me a couple weeks to adjust, but I’m much much more efficient now. Will never go back
@@TheTechDeepDive ya, best money I ever spent
hi bro,
I got this error "you exceeded your current quota please check your plan and billing details"
you gotta buy more tokens
yep @arbourgray is right. You've used up your free tokens.
Hey man! Awesome video, learn too much. Do you know how to create a fine tune from a JSONL file? and import it to join the "davinci-003" model? I'm breaking my head with this, but I couldn't get tutorials on NodeJs
Not yet! I’ve tried before but the problem is you need a ton of example data to create an effective model.
@@TheTechDeepDive Thanks for your prompt response man!
I do the same, I just catch the error in JSON parser, and then run the function again :D
Awesome! It’s not perfect, but it works well enough.
I'm getting "Unexpected Token in JSON at position X" from time to time in the JSON sent by CHATGPT. Do you how to fix this by modifying the prompt?
doesnt work for some reason 6.48
This is showing this error
message: 'You exceeded your current quota, please check your plan and billing details.',
I have never used this API before still it is showing that you have exceeded your quota. How can i resolve it.
The openAI API doesn’t have many free credits if any at all. Just log into your account where you found the api keys, and head to billing details. There should be more info there! Good luck!