Skip to content

Commit ce5715a

Browse files
authored
Add code snippets for controlled generation (#188)
1 parent 74d2f2b commit ce5715a

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

samples/ControlledGeneration.swift

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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, macCatalyst 15.0, *)
25+
final class ControlledGenerationSnippets: 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 testJSONControlledGeneration() async throws {
34+
// [START json_controlled_generation]
35+
let jsonSchema = Schema(
36+
type: .array,
37+
description: "List of recipes",
38+
items: Schema(
39+
type: .object,
40+
properties: [
41+
"recipeName": Schema(type: .string, description: "Name of the recipe", nullable: false),
42+
],
43+
requiredProperties: ["recipeName"]
44+
)
45+
)
46+
47+
let generativeModel = GenerativeModel(
48+
// Specify a model that supports controlled generation like Gemini 1.5 Pro
49+
name: "gemini-1.5-pro",
50+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
51+
// above)
52+
apiKey: APIKey.default,
53+
generationConfig: GenerationConfig(
54+
responseMIMEType: "application/json",
55+
responseSchema: jsonSchema
56+
)
57+
)
58+
59+
let prompt = "List a few popular cookie recipes."
60+
let response = try await generativeModel.generateContent(prompt)
61+
if let text = response.text {
62+
print(text)
63+
}
64+
// [END json_controlled_generation]
65+
}
66+
67+
func testJSONNoSchema() async throws {
68+
// [START json_no_schema]
69+
let generativeModel = GenerativeModel(
70+
name: "gemini-1.5-flash",
71+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
72+
// above)
73+
apiKey: APIKey.default,
74+
generationConfig: GenerationConfig(responseMIMEType: "application/json")
75+
)
76+
77+
let prompt = """
78+
List a few popular cookie recipes using this JSON schema:
79+
80+
Recipe = {'recipeName': string}
81+
Return: Array<Recipe>
82+
"""
83+
let response = try await generativeModel.generateContent(prompt)
84+
if let text = response.text {
85+
print(text)
86+
}
87+
// [END json_no_schema]
88+
}
89+
}

0 commit comments

Comments
 (0)