WWSimpleChatGPT

main

Simply use the functionality of ChatGPT.
William-Weng/WWSimpleChatGPT

WWSimpleChatGPT

Swift-5.6 iOS-14.0 Swift Package Manager-SUCCESS LICENSE

  • Simply use the functionality of ChatGPT.
  • 簡單的使用ChatGPT的功能。

dependencies: [
    .package(url: "https://github.com/William-Weng/WWSimpleChatGPT.git", .upToNextMajor(from: "0.5.0"))
]
函式 功能
configure(bearerToken:version) 設定bearerToken
chat(model:role:temperature:content:) 執行聊天功能
image(model:prompt:n:size:) 文字生成圖片
speech(model:voice:speed:input:) 文字轉語音
whisper(model:audio:) 語音轉文字

Example - 範例

import UIKit
import WWHUD
import WWSimpleChatGPT

final class ViewController: UIViewController {
    
    private let bearerToken = "<bearerToken>"
    
    @IBOutlet weak var myTextField: UITextField!
    @IBOutlet weak var resultTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initSetting()
    }
    
    @IBAction func chat(_ sender: UIButton) { chatAction(content: myTextField.text) }
    @IBAction func images(_ sender: UIButton) { imagesAction(prompt: myTextField.text, count: 5) }
    @IBAction func speech(_ sender: UIButton) { speechAction(input: myTextField.text) }
    @IBAction func whisper(_ sender: UIButton) { whisperAction(filename: "speech.mp3") }
}

private extension ViewController {
    
    func initSetting() {
        WWSimpleChatGPT.configure(bearerToken: bearerToken)
    }
    
    func displayResult<T>(result: Result<T?, Error>) {
        
        switch result {
        case .failure(let error): resultTextView.text = "\(error)"
        case .success(let value): resultTextView.text = "\(String(describing: value))"
        }
        
        WWHUD.shared.dismiss(completion: nil)
    }
    
    func loading() {
        guard let url = Bundle.main.url(forResource: "loading.gif", withExtension: nil) else { return }
        WWHUD.shared.display(effect: .gif(url: url), height: 256)
    }
}

private extension ViewController {
    
    func chatAction(content: String?) {
        
        guard let content = content else { return }
        
        loading()
        
        Task {
            let result = await WWSimpleChatGPT.shared.chat(content: content)
            displayResult(result: result)
        }
    }
    
    func imagesAction(prompt: String?, count: Int) {
     
        guard let prompt = prompt else { return }
        
        loading()
        
        Task {
            let result = await WWSimpleChatGPT.shared.image(model: .v2, prompt: prompt, n: count, size: ._256x256)
            displayResult(result: result)
        }
    }
    
    func speechAction(input: String?) {
        
        guard let input = input else { return }
        
        loading()
        
        Task {
            let result = await WWSimpleChatGPT.shared.speech(input: input)
            displayResult(result: result)
        }
    }
    
    func whisperAction(filename: String) {
        
        guard let url = Bundle.main.url(forResource: filename, withExtension: nil),
              let data = try? Data(contentsOf: url)
        else {
            return
        }

        loading()
        
        Task {
            let result = await WWSimpleChatGPT.shared.whisper(model: .v1, audio: (type: .mp3, data: data))
            displayResult(result: result)
        }
    }
}

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

Last updated: Tue Oct 22 2024 13:52:53 GMT-0900 (Hawaii-Aleutian Daylight Time)