WWSimpleGeminiAI

main

Simply use the functionality of Google Gemini AI.
William-Weng/WWSimpleGeminiAI

WWSimpleGeminiAI

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

  • Simply use the functionality of Google Gemini AI.
  • 簡單的使用Google-Gemini-AI功能。

dependencies: [
    .package(url: "https://github.com/William-Weng/WWSimpleGeminiAI.git", .upToNextMajor(from: "0.1.0"))
]
函式 功能
configure(apiKey:version:model) 設定apiKey
chat(text:) 執行聊天功能
vision(text:image:compressionQuality:) 以文字解釋圖片功能
stream(text:) 串流輸出文字功能

Example - 範例

import UIKit
import WWPrint
import WWHUD
import WWSimpleGeminiAI

// MARK: - ViewController
final class ViewController: UIViewController {

    private let apiKey = "<API_Key>"
    
    @IBOutlet weak var myTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initSetting()
    }
    
    @IBAction func chatAction(_ sender: UIButton) {
        
        let text = "What is Google Gemini?"
        chat(text: text)
    }
    
    @IBAction func visionAction(_ sender: UIButton) {
        
        let text = "Explain in detail what do you see in this image?"
        let image = UIImage(named: "Who")
        
        vision(text: text, image: image)
    }
    
    @IBAction func steamAction(_ sender: UIButton) {
        
        let text = "請寫出一首短詩,有風、雨、太陽"
        steam(text: text)
    }
}

// MARK: - 小工具
private extension ViewController {
    
    /// 初始化設定
    func initSetting() {
        WWSimpleGeminiAI.configure(apiKey: apiKey)
    }
    
    /// 聊天功能
    /// - Parameter text: String
    func chat(text: String) {
        
        WWHUD.shared.display(effect: .default, height: 256)
        
        Task {
            
            let result = await WWSimpleGeminiAI.shared.chat(text: text)
            
            switch result {
            case .failure(let error): myTextView.text = "\(error)"
            case .success(let content): myTextView.text = "\(content ?? "")"
            }
            
            WWHUD.shared.dismiss()
        }
    }
    
    /// 以文字解釋圖片
    /// - Parameters:
    ///   - text: String
    ///   - image: UIImage?
    func vision(text: String, image: UIImage?) {
        
        WWHUD.shared.display(effect: .default, height: 256)

        Task {
            
            let result = await WWSimpleGeminiAI.shared.vision(text: text, image: image)
            
            switch result {
            case .failure(let error): myTextView.text = "\(error)"
            case .success(let content): myTextView.text = "\(content ?? "")"
            }
            
            WWHUD.shared.dismiss()
        }
    }
    
    /// 串流文字
    /// - Parameters:
    ///   - text: String
    func steam(text: String) {
        
        WWHUD.shared.display(effect: .default, height: 256)

        Task {
            
            let result = await WWSimpleGeminiAI.shared.stream(text: text)
            
            switch result {
            case .failure(let error): myTextView.text = "\(error)"
            case .success(let content): myTextView.text = "\(content.joined())"
            }
            
            WWHUD.shared.dismiss()
        }
    }
}

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

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