iOS开发之集成生成式AI大模型Gemini

2023-12-17  本文已影响0人  YungFan

Gemini 是 Google 最新的生成式 AI 大模型,功能十分强大。它可以很容易地集成到 iOS 中,进而帮助开发者产出各种类型的智能 App。

环境要求

  1. Xcode 15.0 及以上。
  2. iOS 15.0 及以上。

项目设置

  1. 官网申请 API Key
  2. 创建 iOS 项目。
  3. 官方建议将 API Key 放入一个 plist 文件,然后解析得到 API Key。
  4. 通过 Swift Package Manager 集成 Gemini SDK

基本使用

  1. 导入GoogleAI模块。
import GoogleGenerativeAI
  1. 创建模型。
let model = GenerativeModel(name: "MODEL_NAME", apiKey: "API_KEY")
  1. text-only 输入。
let prompt = "Write a story about a magic backpack."
let response = try await model.generateContent(prompt)
if let text = response.text {
  print(text)
}
  1. text-and-image 输入。
let image1 = UIImage(...)
let image2 = UIImage(...)

let prompt = "What's different between these pictures?"

let response = try await model.generateContent(prompt, image1, image2)
if let text = response.text {
  print(text)
}

案例

import GoogleGenerativeAI
import SwiftUI
import UIKit

struct ContentView: View {
    @State private var prompt = "Swift是谁发明的?最新版本是多少?"
    @State private var prompt2 = "根据内容创作一首四言绝句。"
    @State private var result = ""
    @State private var result2 = ""

    var body: some View {
        ScrollView(showsIndicators: false) {
            VStack {
                Text("text-only")
                    .font(.title)

                TextEditor(text: $prompt)
                    .foregroundStyle(.black)
                    .border(Color.black, width: 2)
                    .frame(height: 60)

                HStack {
                    Button("整个输出") {
                        result = ""
                        Task {
                            let response = try await Model.textModel.generateContent(prompt)
                            if let text = response.text {
                                result = text
                            }
                        }
                    }

                    Button("逐句输出") {
                        result = ""
                        Task {
                            let contentStream = Model.textModel.generateContentStream(prompt)
                            for try await chunk in contentStream {
                                if let text = chunk.text {
                                    result += text
                                }
                            }
                        }
                    }
                }

                Text(result)
                    .foregroundStyle(.red)
            }

            VStack {
                Text("text-and-image")
                    .font(.title)

                Image("test")
                    .resizable()
                    .frame(width: 100, height: 200)

                TextEditor(text: $prompt2)
                    .foregroundStyle(.black)
                    .border(Color.black, width: 2)
                    .frame(height: 60)

                HStack {
                    Button("整个输出") {
                        let image = UIImage(named: "test")!
                        result2 = ""

                        Task {
                            let response = try await Model.textImageModel.generateContent(prompt2, image)
                            if let text = response.text {
                                result2 = text
                            }
                        }
                    }

                    Button("逐句输出") {
                        let image = UIImage(named: "test")!
                        result2 = ""

                        Task {
                            let contentStream = Model.textImageModel.generateContentStream(prompt2, image)
                            for try await chunk in contentStream {
                                if let text = chunk.text {
                                    result2 += text
                                }
                            }
                        }
                    }
                }

                Text(result2)
                    .foregroundStyle(.red)
            }
        }
        .padding()
    }
}

效果

效果图
上一篇下一篇

猜你喜欢

热点阅读