let model = GenerativeModel(name: "gemini-1.5-flash", apiKey: APIKey.default)
3.3 使用场景
1-根据纯文本输入生成文本
1 2 3 4 5 6 7 8 9
import GoogleGenerativeAI
let model = GenerativeModel(name: "gemini-1.5-flash", apiKey: APIKey.default)
let prompt = "Write a story about a magic backpack." let response = try await model.generateContent(prompt) if let text = response.text { print(text) }
2-根据文本和图片输入生成文本(多模态)
1 2 3 4 5 6 7 8 9 10 11 12 13
import GoogleGenerativeAI
let model = GenerativeModel(name: "gemini-1.5-flash", apiKey: APIKey.default)
let https://cdn.jsdelivr.net/gh/PGzxc/CDN/blog-ai1 = UIhttps://cdn.jsdelivr.net/gh/PGzxc/CDN/blog-ai(...) let https://cdn.jsdelivr.net/gh/PGzxc/CDN/blog-ai2 = UIhttps://cdn.jsdelivr.net/gh/PGzxc/CDN/blog-ai(...)
let prompt = "What's different between these pictures?"
let response = try await model.generateContent(prompt, https://cdn.jsdelivr.net/gh/PGzxc/CDN/blog-ai1, https://cdn.jsdelivr.net/gh/PGzxc/CDN/blog-ai2) if let text = response.text { print(text) }
let config = GenerationConfig( maxOutputTokens: 100 )
let model = GenerativeModel( name: "gemini-1.5-flash", apiKey: APIKey.default, generationConfig: config )
let history = [ ModelContent(role: "user", parts: "Hello, I have 2 dogs in my house."), ModelContent(role: "model", parts: "Great to meet you. What would you like to know?"), ]
// Initialize the chat let chat = model.startChat(history: history) let response = try await chat.sendMessage("How many paws are in my house?") if let text = response.text { print(text) }