Skip to content

Commit d0fdcba

Browse files
committed
Revert "Add a schema in the CLI tool for manual testing"
This reverts commit 309b186.
1 parent 26003f7 commit d0fdcba

File tree

1 file changed

+5
-72
lines changed

1 file changed

+5
-72
lines changed

Examples/GenerativeAICLI/Sources/GenerateContent.swift

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ import ArgumentParser
1616
import Foundation
1717
import GoogleGenerativeAI
1818

19-
// TODO(andrewheard): Revert the changes in this file after manual testing.
20-
21-
struct CartoonCharacter: Decodable {
22-
let firstName: String
23-
let lastName: String
24-
let occupation: String
25-
let children: [ChildCartoonCharacter]
26-
let birthYear: Int
27-
}
28-
29-
struct ChildCartoonCharacter: Decodable {
30-
let firstName: String
31-
let lastName: String
32-
}
33-
3419
@main
3520
struct GenerateContent: AsyncParsableCommand {
3621
@Option(help: "The API key to use when calling the Generative Language API.")
@@ -69,47 +54,8 @@ struct GenerateContent: AsyncParsableCommand {
6954
}
7055

7156
mutating func run() async throws {
72-
let responseSchema = Schema(
73-
type: .array,
74-
description: "List of characters.",
75-
items: Schema(
76-
type: .object,
77-
description: "Details about a character.",
78-
properties: [
79-
"firstName": Schema(type: .string, description: "The character's first name."),
80-
"lastName": Schema(type: .string, description: "The character's last name."),
81-
"occupation": Schema(type: .string, description: "The character's occupation."),
82-
"children": Schema(
83-
type: .array,
84-
description: "A list of the character's children ordered from oldest to youngest.",
85-
items: Schema(
86-
type: .object,
87-
description: "Details about a child character.",
88-
properties: [
89-
"firstName": Schema(type: .string,
90-
description: "The child character's first name."),
91-
"lastName": Schema(type: .string, description: "The child character's last name."),
92-
],
93-
requiredProperties: ["firstName", "lastName"]
94-
)
95-
),
96-
"birthYear": Schema(type: .integer, format: "int32",
97-
description: "The character's birth year."),
98-
],
99-
requiredProperties: ["firstName", "lastName", "occupation", "children", "birthYear"]
100-
)
101-
)
102-
let generationConfig = GenerationConfig(
103-
responseMIMEType: "application/json",
104-
responseSchema: responseSchema
105-
)
106-
10757
do {
108-
let model = GenerativeModel(
109-
name: modelNameOrDefault(),
110-
apiKey: apiKey,
111-
generationConfig: generationConfig
112-
)
58+
let model = GenerativeModel(name: modelNameOrDefault(), apiKey: apiKey)
11359

11460
var parts = [ModelContent.Part]()
11561

@@ -133,32 +79,19 @@ struct GenerateContent: AsyncParsableCommand {
13379

13480
let input = [ModelContent(parts: parts)]
13581

136-
var generatedText = ""
13782
if isStreaming {
13883
let contentStream = model.generateContentStream(input)
13984
print("Generated Content <streaming>:")
14085
for try await content in contentStream {
141-
guard let text = content.text else {
142-
fatalError("No text generated.")
86+
if let text = content.text {
87+
print(text)
14388
}
144-
generatedText += text
14589
}
14690
} else {
14791
let content = try await model.generateContent(input)
148-
guard let text = content.text else {
149-
fatalError("No text generated.")
92+
if let text = content.text {
93+
print("Generated Content:\n\(text)")
15094
}
151-
generatedText += text
152-
}
153-
154-
guard let jsonData = generatedText.data(using: .utf8) else {
155-
fatalError("Generated text is not UTF-8 compatible.")
156-
}
157-
158-
let jsonDecoder = JSONDecoder()
159-
let cartoonCharacters = try jsonDecoder.decode([CartoonCharacter].self, from: jsonData)
160-
for cartoonCharacter in cartoonCharacters {
161-
print(cartoonCharacter)
16295
}
16396
} catch {
16497
print("Generate Content Error: \(error)")

0 commit comments

Comments
 (0)