Skip to content

Commit

Permalink
Revert "Add a schema in the CLI tool for manual testing"
Browse files Browse the repository at this point in the history
This reverts commit 309b186.
  • Loading branch information
andrewheard committed May 27, 2024
1 parent 26003f7 commit d0fdcba
Showing 1 changed file with 5 additions and 72 deletions.
77 changes: 5 additions & 72 deletions Examples/GenerativeAICLI/Sources/GenerateContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ import ArgumentParser
import Foundation
import GoogleGenerativeAI

// TODO(andrewheard): Revert the changes in this file after manual testing.

struct CartoonCharacter: Decodable {
let firstName: String
let lastName: String
let occupation: String
let children: [ChildCartoonCharacter]
let birthYear: Int
}

struct ChildCartoonCharacter: Decodable {
let firstName: String
let lastName: String
}

@main
struct GenerateContent: AsyncParsableCommand {
@Option(help: "The API key to use when calling the Generative Language API.")
Expand Down Expand Up @@ -69,47 +54,8 @@ struct GenerateContent: AsyncParsableCommand {
}

mutating func run() async throws {
let responseSchema = Schema(
type: .array,
description: "List of characters.",
items: Schema(
type: .object,
description: "Details about a character.",
properties: [
"firstName": Schema(type: .string, description: "The character's first name."),
"lastName": Schema(type: .string, description: "The character's last name."),
"occupation": Schema(type: .string, description: "The character's occupation."),
"children": Schema(
type: .array,
description: "A list of the character's children ordered from oldest to youngest.",
items: Schema(
type: .object,
description: "Details about a child character.",
properties: [
"firstName": Schema(type: .string,
description: "The child character's first name."),
"lastName": Schema(type: .string, description: "The child character's last name."),
],
requiredProperties: ["firstName", "lastName"]
)
),
"birthYear": Schema(type: .integer, format: "int32",
description: "The character's birth year."),
],
requiredProperties: ["firstName", "lastName", "occupation", "children", "birthYear"]
)
)
let generationConfig = GenerationConfig(
responseMIMEType: "application/json",
responseSchema: responseSchema
)

do {
let model = GenerativeModel(
name: modelNameOrDefault(),
apiKey: apiKey,
generationConfig: generationConfig
)
let model = GenerativeModel(name: modelNameOrDefault(), apiKey: apiKey)

var parts = [ModelContent.Part]()

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

let input = [ModelContent(parts: parts)]

var generatedText = ""
if isStreaming {
let contentStream = model.generateContentStream(input)
print("Generated Content <streaming>:")
for try await content in contentStream {
guard let text = content.text else {
fatalError("No text generated.")
if let text = content.text {
print(text)
}
generatedText += text
}
} else {
let content = try await model.generateContent(input)
guard let text = content.text else {
fatalError("No text generated.")
if let text = content.text {
print("Generated Content:\n\(text)")
}
generatedText += text
}

guard let jsonData = generatedText.data(using: .utf8) else {
fatalError("Generated text is not UTF-8 compatible.")
}

let jsonDecoder = JSONDecoder()
let cartoonCharacters = try jsonDecoder.decode([CartoonCharacter].self, from: jsonData)
for cartoonCharacter in cartoonCharacters {
print(cartoonCharacter)
}
} catch {
print("Generate Content Error: \(error)")
Expand Down

0 comments on commit d0fdcba

Please sign in to comment.