Building ChatGPT for iOS with GPT4 and Chat API (SwiftUI)
Vložit
- čas přidán 4. 04. 2023
- In this step-by-step tutorial, we'll go through the whole process of building a super cool AI assistant that can chat with you using natural language.
We'll cover all the essential steps, from setting up your project to integrating the ChatGPT model with GPT-4 and Chat API, all while using SwiftUI for a sleek user interface. By the time we're done, you'll be well-equipped to make your own AI-powered apps!
#GPT4 #SwiftUI #iOSDevelopment #OpenAI - Věda a technologie
Amazing video! Clicked notifications for when you implement the stream property! 🔥
Dude, I had sooooo many issues with this thing and no tutorial helped me like this. I don't know if Open AI was down or something but I couldn't get my API to connect for days! but I finally got there. Thank you man. Really looking forward to your future videos
Thank you for sharing the tutorial ^_^ It's helpful!
Amazing video, thank you!
fantastic tutorial, thanks a ton!
Really like your teaching style, and have gone ahead and subscribed.
I really appreciate it. Thank you and welcome!
Very cool video. Thank you!
You deserve 400k subs not just 4k...Subbed!
Your last video helped me a lot and this one was even better :) Great work!
I would love to see the Stream capability implemented.
With access to GPT-4 it can apparently analyze other media such as images as well. This would be more complicated but I'd also love to see a video on this
stream capability or any form of loading indicator really
Im on it - working on one with stream capability. Glad you enjoyed it 🙏
@@mazenkourouche Can't wait!
Stream video is now up! Also the ability to process photos isn’t available yet but I’ll be on it once it is 💪
Mazen habibi! You KILLED it this video! No issues! Why don't you have super thanks enabled. I'd like to donate! I'm here for the OpenAI content. So fun! The stream and UI improvements would be so cool.
Thank you! Enjoying putting this kind of content out. Haha I don't think my channel is eligible yet - appreciate it though 🙌
@@mazenkourouche drop a PayPal or patreon link or something. let us show you some live!
@@mazenkourouche Helped so much! Thank You:)
Even a better video! Man great work! Even with the new chat completions. I can't subscribe you twice, but you get of course a second like. Just a question. Is this Alamofire important to use OpenAI API in SwiftUi? Because I learned it until now with async functions, with URLSession, JSONDecoder, Task etc. Never saw this Alamofire import before(ok I learn SwiftUI just some months). Or is it just a more faster way to combine post and get requests?
Thanks so much - glad you found it useful 🙏 it’s not a must but I like using it when making API calls. In an even newer video (I’ve been putting out a bunch for OpenAI) I actually use a Swift package without any API calls. Might even find that easier
Yes sir you are a good instructor
Thank you! 🙏
Awesome tutorial!
Appreciate it 🙏
Great tutorial! Even though I stumbled upon an issue of my own, I reached out and I was helped in no time! Truly a spectacular guy!
You're very welcome!
3:29 i suggest that you cover the way to install the alamfire cuz I’ve been struggling with adding the package
Im quite new to IOS development so it would be easy for me if you explain every single future implemented in the app and as always please keep up the good work i love your content 🌹🎬
I'll definitely make a video on that as I use it in many of my videos. Thanks for the suggestion
Bro this tut was fire
Thank you for the kind words! Glad you enjoyed it 🙏
Great Video! I Have a question: Doesn't sending the api the whole chat over again for each question exponentially increase the tokens openAI charges us?
Technically yes, however with gpt3.5, they recently changed the pricing for input and output tokens so input tokens cost less.
OpenAI also recommends keeping chats lean and possibly implementing a strategy eg omit messages once chats get too long. One idea could be to generate a chat summary for n messages and pass that instead of those messages (just a thought)
this is really good, thanks, works a charm. one question: how to do error handling for alamofire in this case? i'm not so familiar with swift/coding, and i cannot make heads or tails of the google responses so far...
Are you receiving errors from Alamofire? You can create your own Error struct and convert the alamofire errors into those so they're easier to understand or work with
So, for anyone who is wondering what's happening behind the scenes when the enum 'SenderRole' conforms to String:
'The raw values of the enum are implicitly set to "system", "user", and "assistant" for the respective cases.'
Awesome 🤩
Great tutorial, but I wonder how can I add a prompt to the assistant?
As if the ChatGPT series were your first Videos HUMBLING!
This comment is humbling 🙏 I appreciate it. Hope it was helpful!
@@mazenkourouche Keep it up, quick question. whats the best way to store messages so they dont disappear. I tried to use CoreData and my head hurt. very new to swift.
@@6lack5ushi yeah I wouldn’t recommend using CoreData for something simple in this case. Personally I always use firebase for its simplicity + you get cloud storage with it
@@mazenkourouche Thanks for the heads up man! 🙏
I got this to work, thank you so much! It's awesome.If I want to add more than one chatview (for example, chatview 1, I make ChatGPT focused on cars with blue paint from the year 1990, and ChatView 2 focused on Baking gluten free cakes). Do I simply duplicate the chatview and the chatviewmodel, and change the names? Of is there a better approach? My thinking is that each chatview topic has it's own viewcontroller. I hope my question makes sense!
I would probably not do it that way because you don’t know how many you’ll end up having. I would save the chat messages for in let’s say, a Chat object, and show the chats in a list. When you open a chat, it’ll load it up with the messages, and go from there.
I think what I'm trying to do, is in my app, I successfully set up the ChatGPT using your video.. In ChatViewModel, in the var messages: [Message] string, I created a cool customized initial chatGPT message. So, now I want to add a second section of my app that basically creates the exact same chatGPT implementation, but with a different var messages: [Message] string. The parts in the "" is different. so basically 2 separate chat screens with different chatGPT prompts built in.
@@mazenkourouche
22:04 Is the process here to make a custom gpt or is it a fine tuning process on the chatgpt website?
By the way, can you inform us about how we can fine-tune our artificial intelligence in Swift? Your videos are really great.
It’s just using the gpt api
There’s no fine tuning here
I’ll be looking more into those and encodings in future videos
Gpt 4 also can input voice, right
If you add scene configuration into Application Scene Manifest in Info of target of Project, that error 'looking for configuration named"(no name)" will be gone.
What theme do you use?
Thank you for the video! I watched your previous ChatGPT app video and once I built the app I didn't get any reply from the ChatGPT and no errors in the code found... I have the same issue now. Also how can I understand if I have access to GPT-4 model? I have purchased subscription for $20. It means that I have it, right?
Ah! Have you setup billing with OpenAI? (Not ChatGPT)
For GPT4 you need to apply on the waitlist. It’s different from the ChatGPT subscription. You need to do it through OpenAI
how longer will work the api key
Hi Mazen, I worked through your video, I getting an error inside the 'TASK' area with receivedOpenAIMessage, do you maybe why, thanks pierre
Hey Pierre! Do you mind sharing what the error was? Could it be that you didn’t mark the function as async?
19:59 have you a video with this "stream" already? it is faster, but it consumes not more tokens?
Check out this series. It’s got a bunch more improvements including the stream functionality. Yep it used the same tokens but better experience:
Build ChatGPT for iOS (Swift) Series
czcams.com/play/PLQIKuUl55mS8k_rIB0enPWsiRzkHUZ7lF.html
veryverycoool
Can we integrate these processes with the image creation method?
You can
I have a video doing just that
How can you limit response to be sort? On the first version yo have max_tokens that would limit the response
This API has the same parameter 😁 you can add max_tokens to limit your response.
@@mazenkourouche thank you just got it working 😁
At 13:00 on line 18 I keep getting the error "Cannot convert value of type 'Binding.Type' to expected argument type 'Binding'". Not sure how to fix it. I triple checked and my line of code matches yours exactly.
Nevermind. Kept watching and you changed that line. No more error.
how can we create an app like this which does not store the api keys hard coded like shown here, so it can not be stolen when decompiled? I can not figure out the correct way to do this. I could store it in the keychain - but how would I get it in there if I don't ship it within the code?
There are a few ways to do this:
1. You can host your own api on a server so your app never gets access to the API key
2. If you want to make requests in device you could send the API key to the device on initial launch and store it in the Secure Enclave. (You could also send it silently to the device using APNS)
Either way the best solution would be to not hardcode your API key or have it stored locally.
so I can only use system messege with chat4 ?
Yep! It if you want to get similar behaviour you can send an initial user message with instructions using 3.5
I think GPT 4 can take picture as input and text as output, can we upgrade this app for it?
Unfortunately it’s only available through ChatGPT at the moment - no API available
@@mazenkourouche got it, and also voice input is the same, I guess. Hope they can open for api call later. And even gpt-4 api call not open for everyone yet
HI Mazen
This is super important content. Thanks for creating it. I followed your video and build it.
Everything built fine but I am getting no response just No message received in simulator. What could have wrong?
Have you been able to check if your API is being called?
@@mazenkourouche I have the same issue. I don't think my API is being called, as when I went to the Open API page, it shows my key was not used. I am very new to SWIFTUI, so I don't know how to debug the issue and fix. There are no errors in the code.
@@aparnaavasarala3833hey! There’s a good chance your api endpoint could be incorrect or the model you’re using is for the wrong endpoint. Have you checked that?
Hi bro, try to check if your openai api free trial has expired?
@@jengyang1307 Yea thats what happened to me, my trial has expired and so now nothing streams,
Does this work with XCode 11?
I don’t believe it would because a lot of the code is ios16 and up. The basic functionality would still work but you will need to modify some of the functions for backward compatibility
@@mazenkourouche Thank you
Hi buddy! For build an App using chatgpt I need pay something to chatgpt for use api?
Hey! You need to set up billing. You then pay for what you use of the API. It’s extremely cheap though
@@mazenkourouche ty buddy!
@@mazenkourouche what if i want to use it in an app to release in the appstore?
@@marcoshenriquerp same thing :) unless you let your users add their own api key (be careful with that though - some apps get rejected depending on how they do it)
@@mazenkourouche ok, ty for this tip!
I'm thinking of developing an app using chatgpt but I thought it would be free, do you know any other free ones?
I tried to use another endpoint with the same params but it doesnt work there... why so?
It depends on what the endpoint takes in. You need to check the api reference to make sure you’re using valid parameters
Yes i figured that out now. Thanks!!
It is not getting past the have no received message in the guard let. But i made sure everything was as follows in the video. Any help?
Have you made sure the account your using has billing set up?
@@mazenkourouche just set up billing yesterday. Maybe it takes a minute to kick over. I even bought gpt plus
@@hunggarchristian feel free to shoot me an email or dm and I’ll be happy to help you debug
@@mazenkourouche will do
How do I hide the initial message I am sending? Using 3.5.
If you know you're always going to be using the first message as the prompt, you can do something like `let filteredArray = Array(unfilteredArray.dropFirst())`, and use the filtered array.
@@mazenkourouche awesome. I got on your patreon for more questions lol
@@cotiew Haha awesome, glad I was able to help
PLEASEE help me I did everything the same until 19:40, but when I try sending a message I get no response back, and I get message "Had no Received Message", can anyone help me pls I will really appreciate it!!
If you print the response from inside the openAIService do you get anything printed there? There’s a good chance your response isn’t being decided properly
@@mazenkourouche not going to lie to you I am pretty new to the swift language and coding in general, I am 18 years old and trying to start my journey early but I really don't understand what your asking for me to check, I am sry if it is a silly question but I am just starting and it will be very appreciated if you can help me and clarify the question a bit more for me?
No worries at all! You should check if your API is coming back with a response in the first place. Where you make the request, you should print the response and see. This should be in the OpenAIService file.
@@mazenkourouche So inside the class "OpenAIService" I printed "print (OpenAIMessages)" before the return, and when I tried it I got this: "[ChatChat.OpenAIChatMessage(role: ChatChat.SenderRole.user, content: "Hello")]
Had no Received Message" so does that mean the it isn't decided properly or is there something else wrong with the code? I really appreciate you helping man it means a lot, thank you soo muchh!
@@yousefahmed296 feel free to send me a message on Instagram, Twitter or via email. I’ll be able to help you debug more easily 💪
how I can have access for chat 4 API ?
You need to add yourself to the wait list. Once you’re approved you’re good to go :)
I was able to connect the build and receiving the following messages Had no received message in XCode when I send a Hello message to that AI assistant with a successful build. Any help would be greatly appreciated
Hey Roger! A few issued I've found others experiencing are:
- incorrect API URL
- not setting up payment in OpenAI account
Let me know if any of those solve your issue
@@mazenkourouche Thank you for your help! I updated previous steps and was able to connect and have a functional AI! Than you so much!
@@mazenkourouche Hey I'm having a similar issue. I set up payment too, but I'm getting the same error (code: 429). @rogerve can you also assist with what you had changed (if you still remember). Thanks
@@chiamakabrowneyes thanks for watching! I’d recommend watching my latest series as it uses more recent libraries :)
@@mazenkourouche Okay I'd go through it thanks!
i keep getting the error "cannot find 'messages in scope"
Which timestamp in the video? It's possible you haven't declared the messages variable correctly or its outside the scope of your class
@@mazenkourouche i get the error at 9:22
@@kianhosseinzadeh1215 Make sure the 'messages' parameter in the function is declared properly - that could be causing your error
@@mazenkourouche thank you sir
i keep getting hacked using chat gpt api :( somebody always abusing the api key forcing me to pay ridiculous amount of money for gpt services :( :( pls help
You should be setting limits on your openai account. And avoid storing your api key on device
Hi @mazenkourouche I have followed same flow but I am getting no message.
guard let recivedOpenAIMeesage = response?.choices.first?.message else{
print("Recived No Message")
return
}
It is paid ? if Yes is there any free subscription for trial period .
I dont believe there's a free subscription - it's just a "pay for your usage". It's really cheap though - you'll probably pay like $2-5 per month if youre just playing around. You need to have billing enabled though