Building ChatGPT for iOS with GPT4 and Chat API (SwiftUI)

Sdílet
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

Komentáře • 123

  • @TheEliteBeats
    @TheEliteBeats Před rokem +3

    Amazing video! Clicked notifications for when you implement the stream property! 🔥

  • @ggmptv7904
    @ggmptv7904 Před 5 měsíci

    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

  • @CornuDev
    @CornuDev Před 3 dny

    Thank you for sharing the tutorial ^_^ It's helpful!

  • @SettingUpTheChairs
    @SettingUpTheChairs Před rokem +1

    Amazing video, thank you!

  • @saurabhbhambry
    @saurabhbhambry Před 10 měsíci

    fantastic tutorial, thanks a ton!
    Really like your teaching style, and have gone ahead and subscribed.

    • @mazenkourouche
      @mazenkourouche  Před 8 měsíci

      I really appreciate it. Thank you and welcome!

  • @dmitrykiri
    @dmitrykiri Před rokem

    Very cool video. Thank you!

  • @LukasPechar-oz4bp
    @LukasPechar-oz4bp Před měsícem

    You deserve 400k subs not just 4k...Subbed!

  • @jakeklawlor
    @jakeklawlor Před rokem +4

    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

    • @DDSTV2
      @DDSTV2 Před rokem

      stream capability or any form of loading indicator really

    • @mazenkourouche
      @mazenkourouche  Před rokem +3

      Im on it - working on one with stream capability. Glad you enjoyed it 🙏

    • @DDSTV2
      @DDSTV2 Před rokem

      @@mazenkourouche Can't wait!

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      Stream video is now up! Also the ability to process photos isn’t available yet but I’ll be on it once it is 💪

  • @DDSTV2
    @DDSTV2 Před rokem +2

    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.

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      Thank you! Enjoying putting this kind of content out. Haha I don't think my channel is eligible yet - appreciate it though 🙌

    • @DDSTV2
      @DDSTV2 Před rokem +2

      @@mazenkourouche drop a PayPal or patreon link or something. let us show you some live!

    • @EnderRobber101
      @EnderRobber101 Před rokem

      @@mazenkourouche Helped so much! Thank You:)

  • @daco9464
    @daco9464 Před 5 měsíci

    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?

    • @mazenkourouche
      @mazenkourouche  Před 5 měsíci

      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

  • @johnrambo7147
    @johnrambo7147 Před rokem +1

    Yes sir you are a good instructor

  • @GianCampos
    @GianCampos Před 11 měsíci

    Awesome tutorial!

  • @ugulut
    @ugulut Před rokem

    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!

  • @sam-2796
    @sam-2796 Před 9 měsíci

    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 🌹🎬

    • @mazenkourouche
      @mazenkourouche  Před 8 měsíci

      I'll definitely make a video on that as I use it in many of my videos. Thanks for the suggestion

  • @parkerrex
    @parkerrex Před 8 měsíci

    Bro this tut was fire

    • @mazenkourouche
      @mazenkourouche  Před 7 měsíci

      Thank you for the kind words! Glad you enjoyed it 🙏

  • @edefwewefwfwe1809
    @edefwewefwfwe1809 Před 10 měsíci

    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?

    • @mazenkourouche
      @mazenkourouche  Před 10 měsíci

      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)

  • @mgrth
    @mgrth Před 8 měsíci

    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...

    • @mazenkourouche
      @mazenkourouche  Před 8 měsíci

      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

  • @MosabbirSadman
    @MosabbirSadman Před 10 měsíci

    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.'

  • @JunaidKhan-ny8tu
    @JunaidKhan-ny8tu Před měsícem

    Awesome 🤩

  • @user-rg5nl2kz2p
    @user-rg5nl2kz2p Před 6 měsíci

    Great tutorial, but I wonder how can I add a prompt to the assistant?

  • @6lack5ushi
    @6lack5ushi Před rokem

    As if the ChatGPT series were your first Videos HUMBLING!

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      This comment is humbling 🙏 I appreciate it. Hope it was helpful!

    • @6lack5ushi
      @6lack5ushi Před rokem

      @@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.

    • @mazenkourouche
      @mazenkourouche  Před rokem

      @@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

    • @6lack5ushi
      @6lack5ushi Před rokem +1

      @@mazenkourouche Thanks for the heads up man! 🙏

  • @sigmapifsc
    @sigmapifsc Před 10 měsíci

    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!

    • @mazenkourouche
      @mazenkourouche  Před 10 měsíci +1

      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.

    • @sigmapifsc
      @sigmapifsc Před 10 měsíci

      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

  • @jackdanels3521
    @jackdanels3521 Před 4 měsíci

    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.

    • @mazenkourouche
      @mazenkourouche  Před 3 měsíci

      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

  • @chenkencheng925
    @chenkencheng925 Před 8 měsíci

    Gpt 4 also can input voice, right

  • @SushiMonChicago
    @SushiMonChicago Před rokem

    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.

  • @parkerrex
    @parkerrex Před 8 měsíci

    What theme do you use?

  • @alexeyyurkov7796
    @alexeyyurkov7796 Před 10 měsíci

    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?

    • @mazenkourouche
      @mazenkourouche  Před 10 měsíci

      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

  • @user-zw8rc2zy1o
    @user-zw8rc2zy1o Před měsícem

    how longer will work the api key

  • @pierremarais7669
    @pierremarais7669 Před rokem +1

    Hi Mazen, I worked through your video, I getting an error inside the 'TASK' area with receivedOpenAIMessage, do you maybe why, thanks pierre

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      Hey Pierre! Do you mind sharing what the error was? Could it be that you didn’t mark the function as async?

  • @daco9464
    @daco9464 Před 5 měsíci

    19:59 have you a video with this "stream" already? it is faster, but it consumes not more tokens?

    • @mazenkourouche
      @mazenkourouche  Před 5 měsíci

      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

  • @TracyTracci
    @TracyTracci Před 2 měsíci

    veryverycoool

  • @jackdanels3521
    @jackdanels3521 Před 4 měsíci

    Can we integrate these processes with the image creation method?

  • @DenzelAyeah
    @DenzelAyeah Před rokem +1

    How can you limit response to be sort? On the first version yo have max_tokens that would limit the response

    • @mazenkourouche
      @mazenkourouche  Před rokem +2

      This API has the same parameter 😁 you can add max_tokens to limit your response.

    • @DenzelAyeah
      @DenzelAyeah Před rokem +1

      @@mazenkourouche thank you just got it working 😁

  • @james.a
    @james.a Před rokem

    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.

    • @james.a
      @james.a Před rokem +1

      Nevermind. Kept watching and you changed that line. No more error.

  • @psiFellow
    @psiFellow Před rokem

    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?

    • @mazenkourouche
      @mazenkourouche  Před rokem

      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.

  • @user-tu6ju2pw4p
    @user-tu6ju2pw4p Před rokem +1

    so I can only use system messege with chat4 ?

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      Yep! It if you want to get similar behaviour you can send an initial user message with instructions using 3.5

  • @chenkencheng925
    @chenkencheng925 Před 8 měsíci

    I think GPT 4 can take picture as input and text as output, can we upgrade this app for it?

    • @mazenkourouche
      @mazenkourouche  Před 8 měsíci

      Unfortunately it’s only available through ChatGPT at the moment - no API available

    • @chenkencheng925
      @chenkencheng925 Před 8 měsíci

      @@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

  • @SushiMonChicago
    @SushiMonChicago Před rokem

    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?

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      Have you been able to check if your API is being called?

    • @aparnaavasarala3833
      @aparnaavasarala3833 Před rokem +1

      @@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.

    • @mazenkourouche
      @mazenkourouche  Před rokem

      @@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?

    • @jengyang1307
      @jengyang1307 Před rokem

      Hi bro, try to check if your openai api free trial has expired?

    • @josuesaavedra6175
      @josuesaavedra6175 Před rokem

      @@jengyang1307 Yea thats what happened to me, my trial has expired and so now nothing streams,

  • @jbaictocbh1286
    @jbaictocbh1286 Před rokem

    Does this work with XCode 11?

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      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

    • @jbaictocbh1286
      @jbaictocbh1286 Před rokem

      @@mazenkourouche Thank you

  • @marcoshenriquerp
    @marcoshenriquerp Před 11 měsíci

    Hi buddy! For build an App using chatgpt I need pay something to chatgpt for use api?

    • @mazenkourouche
      @mazenkourouche  Před 11 měsíci +1

      Hey! You need to set up billing. You then pay for what you use of the API. It’s extremely cheap though

    • @marcoshenriquerp
      @marcoshenriquerp Před 11 měsíci +1

      @@mazenkourouche ty buddy!

    • @marcoshenriquerp
      @marcoshenriquerp Před 11 měsíci

      @@mazenkourouche what if i want to use it in an app to release in the appstore?

    • @mazenkourouche
      @mazenkourouche  Před 11 měsíci +1

      @@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)

    • @marcoshenriquerp
      @marcoshenriquerp Před 11 měsíci

      @@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?

  • @infonity_yt
    @infonity_yt Před rokem

    I tried to use another endpoint with the same params but it doesnt work there... why so?

    • @mazenkourouche
      @mazenkourouche  Před rokem

      It depends on what the endpoint takes in. You need to check the api reference to make sure you’re using valid parameters

    • @infonity_yt
      @infonity_yt Před rokem

      Yes i figured that out now. Thanks!!

  • @hunggarchristian
    @hunggarchristian Před rokem

    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?

    • @mazenkourouche
      @mazenkourouche  Před rokem

      Have you made sure the account your using has billing set up?

    • @hunggarchristian
      @hunggarchristian Před rokem

      @@mazenkourouche just set up billing yesterday. Maybe it takes a minute to kick over. I even bought gpt plus

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      @@hunggarchristian feel free to shoot me an email or dm and I’ll be happy to help you debug

    • @hunggarchristian
      @hunggarchristian Před rokem

      @@mazenkourouche will do

  • @cotiew
    @cotiew Před rokem

    How do I hide the initial message I am sending? Using 3.5.

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      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.

    • @cotiew
      @cotiew Před rokem

      @@mazenkourouche awesome. I got on your patreon for more questions lol

    • @mazenkourouche
      @mazenkourouche  Před rokem

      @@cotiew Haha awesome, glad I was able to help

  • @yousefahmed296
    @yousefahmed296 Před rokem +2

    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!!

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      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

    • @yousefahmed296
      @yousefahmed296 Před rokem +1

      @@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?

    • @mazenkourouche
      @mazenkourouche  Před rokem +1

      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.

    • @yousefahmed296
      @yousefahmed296 Před rokem +1

      @@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!

    • @mazenkourouche
      @mazenkourouche  Před rokem +2

      @@yousefahmed296 feel free to send me a message on Instagram, Twitter or via email. I’ll be able to help you debug more easily 💪

  • @user-tu6ju2pw4p
    @user-tu6ju2pw4p Před rokem

    how I can have access for chat 4 API ?

    • @mazenkourouche
      @mazenkourouche  Před rokem

      You need to add yourself to the wait list. Once you’re approved you’re good to go :)

  • @rogerve
    @rogerve Před rokem

    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

    • @mazenkourouche
      @mazenkourouche  Před rokem

      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

    • @rogerve
      @rogerve Před rokem

      @@mazenkourouche Thank you for your help! I updated previous steps and was able to connect and have a functional AI! Than you so much!

    • @chiamakabrowneyes
      @chiamakabrowneyes Před 3 měsíci

      @@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

    • @mazenkourouche
      @mazenkourouche  Před 3 měsíci +1

      @@chiamakabrowneyes thanks for watching! I’d recommend watching my latest series as it uses more recent libraries :)

    • @chiamakabrowneyes
      @chiamakabrowneyes Před 3 měsíci

      @@mazenkourouche Okay I'd go through it thanks!

  • @kianhosseinzadeh1215
    @kianhosseinzadeh1215 Před rokem +1

    i keep getting the error "cannot find 'messages in scope"

    • @mazenkourouche
      @mazenkourouche  Před rokem

      Which timestamp in the video? It's possible you haven't declared the messages variable correctly or its outside the scope of your class

    • @kianhosseinzadeh1215
      @kianhosseinzadeh1215 Před rokem

      @@mazenkourouche i get the error at 9:22

    • @mazenkourouche
      @mazenkourouche  Před rokem

      @@kianhosseinzadeh1215 Make sure the 'messages' parameter in the function is declared properly - that could be causing your error

    • @kianhosseinzadeh1215
      @kianhosseinzadeh1215 Před rokem +1

      @@mazenkourouche thank you sir

  • @simply6162
    @simply6162 Před 9 měsíci

    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

    • @mazenkourouche
      @mazenkourouche  Před 9 měsíci

      You should be setting limits on your openai account. And avoid storing your api key on device

  • @saritbahuguna9603
    @saritbahuguna9603 Před 9 měsíci

    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 .

    • @mazenkourouche
      @mazenkourouche  Před 8 měsíci

      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