|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import GoogleGenerativeAI |
| 16 | +import XCTest |
| 17 | + |
| 18 | +// Set up your API Key |
| 19 | +// ==================== |
| 20 | +// To use the Gemini API, you'll need an API key. To learn more, see the "Set up your API Key" |
| 21 | +// section in the Gemini API quickstart: |
| 22 | +// https://ai.google.dev/gemini-api/docs/quickstart?lang=swift#set-up-api-key |
| 23 | + |
| 24 | +@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) |
| 25 | +final class CodeExecutionSnippets: XCTestCase { |
| 26 | + override func setUpWithError() throws { |
| 27 | + try XCTSkipIf( |
| 28 | + APIKey.default.isEmpty, |
| 29 | + "`\(APIKey.apiKeyEnvVar)` environment variable not set." |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + func testCodeExecutionBasic() async throws { |
| 34 | + // [START code_execution_basic] |
| 35 | + let generativeModel = |
| 36 | + GenerativeModel( |
| 37 | + // Specify a Gemini model appropriate for your use case |
| 38 | + name: "gemini-1.5-flash", |
| 39 | + // Access your API key from your on-demand resource .plist file (see |
| 40 | + // "Set up your API key" above) |
| 41 | + apiKey: APIKey.default, |
| 42 | + tools: [Tool(codeExecution: CodeExecution())] |
| 43 | + ) |
| 44 | + |
| 45 | + let prompt = """ |
| 46 | + What is the sum of the first 50 prime numbers? |
| 47 | + Generate and run code for the calculation, and make sure you get all 50. |
| 48 | + """ |
| 49 | + let response = try await generativeModel.generateContent(prompt) |
| 50 | + if let text = response.text { |
| 51 | + print(text) |
| 52 | + } |
| 53 | + // [END code_execution_basic] |
| 54 | + } |
| 55 | + |
| 56 | + func testCodeExecutionChat() async throws { |
| 57 | + // [START code_execution_chat] |
| 58 | + let generativeModel = |
| 59 | + GenerativeModel( |
| 60 | + // Specify a Gemini model appropriate for your use case |
| 61 | + name: "gemini-1.5-flash", |
| 62 | + // Access your API key from your on-demand resource .plist file (see |
| 63 | + // "Set up your API key" above) |
| 64 | + apiKey: APIKey.default, |
| 65 | + tools: [Tool(codeExecution: CodeExecution())] |
| 66 | + ) |
| 67 | + |
| 68 | + let chat = generativeModel.startChat() |
| 69 | + |
| 70 | + let prompt = """ |
| 71 | + What is the sum of the first 50 prime numbers? |
| 72 | + Generate and run code for the calculation, and make sure you get all 50. |
| 73 | + """ |
| 74 | + let response = try await chat.sendMessage(prompt) |
| 75 | + if let text = response.text { |
| 76 | + print(text) |
| 77 | + } |
| 78 | + // [END code_execution_chat] |
| 79 | + } |
| 80 | +} |
0 commit comments