Giter Site home page Giter Site logo

sendbird / sendbird-chatgpt-sample-ios Goto Github PK

View Code? Open in Web Editor NEW
1.0 7.0 1.0 101 KB

GPT Sample in iOS

Home Page: https://sendbird.com/docs/ai-chatbot/guide/v1/overview

License: MIT License

Swift 100.00%
api-for-chat bard chat-api chat-api-platform chat-platform chat-sdk chatbot-api chatbot-sdk chatgpt communications-platform

sendbird-chatgpt-sample-ios's Introduction

Sendbird ChatGPT Sample for iOS

This is an example of Sendbird ChatGPT for iOS, implemented using Sendbird UIKit.

Sendbird ChatGPT Bot is a GPT3-powered bot that's built on top of Sendbird's existing bots. It can provide your users with highly engaging and natural conversational experience. It's integrated natively inside Sendbird so that you don't have to worry about developing and deploying OpenAI services separately.

Requirements

The minimum requirements for this sample are:

  • iOS 14.0 +
  • Xcode 14.1 + (Swift 5.7.1 +)
  • Sendbird UIKit 3.5.6

Getting Started

  1. Create your Sendbird application on the dashboard.
  2. Register the ChatGPT bot in your Sendbird application.
  3. In this example, we're connecting to a test purpose Sendbird application and pre-defined bots. To connect yours, replace applicationId and others in the AppDelegate.swift file and replace ChatBot in the ChatBot.swift file as follows:
// AppDelegate.swift
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // TODO: Replace with your own APP_ID
        SendbirdUI.initialize(applicationId: "BDD627AC-AC88-45F4-B277-2B3B5C4610E3") { error in
            //
        }
        
        SBUGlobals.currentUser = SBUUser(userId: "j_sung_0o0")
        
        // ...
    }
    
    // ...
}
// ChatBot.swift
enum ChatBot: Int, CaseIterable {
    // TODO: Replace with your own user IDs of bots
    case chatGPT
    
    var botID: String {
        switch self {
        case .chatGPT: return "gpt_bot"
    }
}
  1. Build & Run

How to connect a channel to a bot

This sample supports chat with 3 different bots. They are trained with 3 different characteristics. Currently, Only 1:1 chat with ChatGPT bot is supoorted.When you create a channel, you just need to enter the user ID of the bot you want to chat with.

createChannelViewModel?.createChannel(userIds: [ChatBot.chatGPT.botID])

For more information, see our documentation and our tutorial.

Implementation

Bot type selector

// 1
class BotTypeSelector: SBUCreateChannelTypeSelector {
    // 3
    weak var botSelectorDelegate: BotTypeSelectorDelegate?

    // 4
    @objc
    func onTapChatGPTBot() {
        self.botSelectorDelegate?.botTypeSelector(self, didSelectBot: .chatGPT)
    }
}

// 2
protocol BotTypeSelectorDelegate: AnyObject {
    func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelectBot bot: ChatBot)
}
  1. Override SBUCreateChannelTypeSelector to use the same design.
  2. Define protocol called BotTypeSelectorDelegate as an event delegate
  3. Declare delegate property as a weak reference.
  4. Call botTypeSelector(_:didSelectBot:) delegate method when the bot button selected.

Channel list - SBUCreateChannelViewModel

class ChatGPTChannelListViewController: SBUGroupChannelListViewController, SBUCreateChannelViewModelDelegate {
    var createChannelViewModel: SBUCreateChannelViewModel?

    // 1
    override func createViewModel(channelListQuery: GroupChannelListQuery?) {
        super.createViewModel(channelListQuery: channelListQuery)
        self.createChannelViewModel = SBUCreateChannelViewModel(delegate: self)
    }

    // 2
    func createChannelViewModel(_ viewModel: SBUCreateChannelViewModel, didCreateChannel channel: BaseChannel?, withMessageListParams messageListParams: MessageListParams?) {
        guard let channelURL = channel?.channelURL else { return }
        SendbirdUI.moveToChannel(channelURL: channelURL, messageListParams: messageListParams)
    }
}
  1. Create SBUCreateChannelViewModel instance after super.createViewModel(channelListQuery:) is called.
  2. When didCreateChannel is called, call SendbirdUI.moveToChannel(channelURL:messageListParams:)

Channel list - BotTypeSelector

extension ChatGPTChannelListViewController: BotTypeSelectorDelegate {
    // 1
    override func loadChannelTypeSelector() {
        if self.createChannelTypeSelector == nil {
            let botTypeSelector = BotTypeSelector(delegate: self)
            botTypeSelector.botSelectorDelegate = self
            botTypeSelector.isHidden = true
            self.createChannelTypeSelector = botTypeSelector
        }
        
        guard let createChannelTypeSelector else { return }
        self.navigationController?.view.addSubview(createChannelTypeSelector)
    }

    // 2
    override func baseChannelListModule(_ headerComponent: SBUBaseChannelListModule.Header, didTapRightItem rightItem: UIBarButtonItem) {
        self.showCreateChannelTypeSelector()
    }

    // 3
    func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelectBot bot: ChatBot) {
        botTypeSelect.dismiss()
        createChannelViewModel?.createChannel(userIds: [bot.botID])
    }
}
  1. Override loadChannelTypeSelector to use BotTypeSelector.
  2. Override didTapRightItem delegate method to call showCreateChannelTypeSelector(). This will show BotTypeSelector.
  3. When bot type is selected from BotTypeSelector, botTypeSelector(_:didSelectBot:) delegate method is called. In the method, call SBUCreateChannelViewModel/createChannel(userIds:).

sendbird-chatgpt-sample-ios's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dagfinn1962

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.