Skip to content

Commit ae60f57

Browse files
authored
Simplify top-level schema declaration in FunctionDeclaration (#117)
1 parent 44edbb1 commit ae60f57

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

Examples/GenerativeAICLI/Sources/GenerateContent.swift

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,21 @@ struct GenerateContent: AsyncParsableCommand {
8181
FunctionDeclaration(
8282
name: "get_exchange_rate",
8383
description: "Get the exchange rate for currencies between countries",
84-
parameters: Schema(
85-
type: .object,
86-
properties: [
87-
"currency_from": Schema(
88-
type: .string,
89-
format: "enum",
90-
description: "The currency to convert from in ISO 4217 format",
91-
enumValues: ["USD", "EUR", "JPY", "GBP", "AUD", "CAD"]
92-
),
93-
"currency_to": Schema(
94-
type: .string,
95-
format: "enum",
96-
description: "The currency to convert to in ISO 4217 format",
97-
enumValues: ["USD", "EUR", "JPY", "GBP", "AUD", "CAD"]
98-
),
99-
],
100-
required: ["currency_from", "currency_to"]
101-
)
84+
parameters: [
85+
"currency_from": Schema(
86+
type: .string,
87+
format: "enum",
88+
description: "The currency to convert from in ISO 4217 format",
89+
enumValues: ["USD", "EUR", "JPY", "GBP", "AUD", "CAD"]
90+
),
91+
"currency_to": Schema(
92+
type: .string,
93+
format: "enum",
94+
description: "The currency to convert to in ISO 4217 format",
95+
enumValues: ["USD", "EUR", "JPY", "GBP", "AUD", "CAD"]
96+
),
97+
],
98+
requiredParameters: ["currency_from", "currency_to"]
10299
),
103100
])],
104101
requestOptions: RequestOptions(apiVersion: "v1beta")

Sources/GoogleAI/FunctionCalling.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Schema: Encodable {
4141

4242
let properties: [String: Schema]?
4343

44-
let required: [String]?
44+
let requiredProperties: [String]?
4545

4646
enum CodingKeys: String, CodingKey {
4747
case type
@@ -51,22 +51,22 @@ public class Schema: Encodable {
5151
case enumValues = "enum"
5252
case items
5353
case properties
54-
case required
54+
case requiredProperties = "required"
5555
}
5656

5757
public init(type: DataType, format: String? = nil, description: String? = nil,
5858
nullable: Bool? = nil,
5959
enumValues: [String]? = nil, items: Schema? = nil,
6060
properties: [String: Schema]? = nil,
61-
required: [String]? = nil) {
61+
requiredProperties: [String]? = nil) {
6262
self.type = type
6363
self.format = format
6464
self.description = description
6565
self.nullable = nullable
6666
self.enumValues = enumValues
6767
self.items = items
6868
self.properties = properties
69-
self.required = required
69+
self.requiredProperties = requiredProperties
7070
}
7171
}
7272

@@ -88,10 +88,15 @@ public struct FunctionDeclaration {
8888

8989
let parameters: Schema?
9090

91-
public init(name: String, description: String, parameters: Schema?) {
91+
public init(name: String, description: String, parameters: [String: Schema]?,
92+
requiredParameters: [String]?) {
9293
self.name = name
9394
self.description = description
94-
self.parameters = parameters
95+
self.parameters = Schema(
96+
type: .object,
97+
properties: parameters,
98+
requiredProperties: requiredParameters
99+
)
95100
}
96101
}
97102

0 commit comments

Comments
 (0)