Skip to content

Commit 6338983

Browse files
committed
Rename template variables to template inputs
1 parent 67d40eb commit 6338983

11 files changed

+59
-53
lines changed

FirebaseAI/Sources/TemplateChatSession.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public final class TemplateChatSession: Sendable {
3838

3939
/// Sends a message to the model and returns the response.
4040
public func sendMessage(_ message: any PartsRepresentable,
41-
variables: [String: Any],
41+
inputs: [String: Any],
4242
options: RequestOptions = RequestOptions()) async throws
4343
-> GenerateContentResponse {
44-
let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
44+
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
4545
let newContent = populateContentRole(ModelContent(parts: message.partsValue))
4646
let response = try await model.generateContentWithHistory(
4747
history: _history.history + [newContent],
4848
template: template,
49-
variables: templateVariables,
49+
inputs: templateInputs,
5050
options: options
5151
)
5252
_history.append(newContent)
@@ -57,15 +57,15 @@ public final class TemplateChatSession: Sendable {
5757
}
5858

5959
public func sendMessageStream(_ message: any PartsRepresentable,
60-
variables: [String: Any],
60+
inputs: [String: Any],
6161
options: RequestOptions = RequestOptions()) throws
6262
-> AsyncThrowingStream<GenerateContentResponse, Error> {
63-
let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
63+
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
6464
let newContent = populateContentRole(ModelContent(parts: message.partsValue))
6565
let stream = try model.generateContentStreamWithHistory(
6666
history: _history.history + [newContent],
6767
template: template,
68-
variables: templateVariables,
68+
inputs: templateInputs,
6969
options: options
7070
)
7171
return AsyncThrowingStream { continuation in

FirebaseAI/Sources/TemplateGenerateContentRequest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1818
struct TemplateGenerateContentRequest: Sendable {
1919
let template: String
20-
let variables: [String: TemplateVariable]
20+
let inputs: [String: TemplateInput]
2121
let history: [ModelContent]
2222
let projectID: String
2323
let stream: Bool
@@ -28,13 +28,13 @@ struct TemplateGenerateContentRequest: Sendable {
2828
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2929
extension TemplateGenerateContentRequest: Encodable {
3030
enum CodingKeys: String, CodingKey {
31-
case variables = "inputs"
31+
case inputs
3232
case history
3333
}
3434

3535
func encode(to encoder: any Encoder) throws {
3636
var container = encoder.container(keyedBy: CodingKeys.self)
37-
try container.encode(variables, forKey: .variables)
37+
try container.encode(inputs, forKey: .inputs)
3838
try container.encode(history, forKey: .history)
3939
}
4040
}

FirebaseAI/Sources/TemplateGenerateImagesRequest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ class TemplateGenerateImagesRequest: @unchecked Sendable, GenerativeAIRequest {
3333
let apiConfig: APIConfig
3434

3535
let template: String
36-
let variables: [String: TemplateVariable]
36+
let inputs: [String: TemplateInput]
3737
let projectID: String
3838

39-
init(template: String, variables: [String: TemplateVariable], projectID: String,
39+
init(template: String, inputs: [String: TemplateInput], projectID: String,
4040
apiConfig: APIConfig, options: RequestOptions) {
4141
self.apiConfig = apiConfig
4242
self.options = options
4343
self.template = template
44-
self.variables = variables
44+
self.inputs = inputs
4545
self.projectID = projectID
4646
}
4747

4848
enum CodingKeys: String, CodingKey {
49-
case variables = "inputs"
49+
case inputs
5050
}
5151

5252
func encode(to encoder: Encoder) throws {
5353
var container = encoder.container(keyedBy: CodingKeys.self)
54-
try container.encode(variables, forKey: .variables)
54+
try container.encode(inputs, forKey: .inputs)
5555
}
5656
}

FirebaseAI/Sources/TemplateGenerativeModel.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ public final class TemplateGenerativeModel: Sendable {
2626
self.apiConfig = apiConfig
2727
}
2828

29-
/// Generates content from a prompt template and variables.
29+
/// Generates content from a prompt template and inputs.
3030
///
3131
/// - Parameters:
3232
/// - template: The prompt template to use.
3333
/// - variables: A dictionary of variables to substitute into the template.
3434
/// - Returns: The content generated by the model.
3535
/// - Throws: A ``GenerateContentError`` if the request failed.
3636
public func generateContent(template: String,
37-
variables: [String: Any],
37+
inputs: [String: Any],
3838
options: RequestOptions = RequestOptions()) async throws
3939
-> GenerateContentResponse {
40-
let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
40+
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
4141
return try await generateContentWithHistory(
4242
history: [],
4343
template: template,
44-
variables: templateVariables,
44+
inputs: templateInputs,
4545
options: options
4646
)
4747
}
4848

49-
/// Generates content from a prompt template, variables, and history.
49+
/// Generates content from a prompt template, inputs, and history.
5050
///
5151
/// - Parameters:
5252
/// - history: The conversation history to use.
@@ -55,12 +55,12 @@ public final class TemplateGenerativeModel: Sendable {
5555
/// - Returns: The content generated by the model.
5656
/// - Throws: A ``GenerateContentError`` if the request failed.
5757
func generateContentWithHistory(history: [ModelContent], template: String,
58-
variables: [String: TemplateVariable],
58+
inputs: [String: TemplateInput],
5959
options: RequestOptions = RequestOptions()) async throws
6060
-> GenerateContentResponse {
6161
let request = TemplateGenerateContentRequest(
6262
template: template,
63-
variables: variables,
63+
inputs: inputs,
6464
history: history,
6565
projectID: generativeAIService.firebaseInfo.projectID,
6666
stream: false,
@@ -73,13 +73,13 @@ public final class TemplateGenerativeModel: Sendable {
7373
}
7474

7575
public func generateContentStream(template: String,
76-
variables: [String: Any],
76+
inputs: [String: Any],
7777
options: RequestOptions = RequestOptions()) throws
7878
-> AsyncThrowingStream<GenerateContentResponse, Error> {
79-
let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
79+
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
8080
let request = TemplateGenerateContentRequest(
8181
template: template,
82-
variables: templateVariables,
82+
inputs: templateInputs,
8383
history: [],
8484
projectID: generativeAIService.firebaseInfo.projectID,
8585
stream: true,
@@ -90,12 +90,12 @@ public final class TemplateGenerativeModel: Sendable {
9090
}
9191

9292
func generateContentStreamWithHistory(history: [ModelContent], template: String,
93-
variables: [String: TemplateVariable],
93+
inputs: [String: TemplateInput],
9494
options: RequestOptions = RequestOptions()) throws
9595
-> AsyncThrowingStream<GenerateContentResponse, Error> {
9696
let request = TemplateGenerateContentRequest(
9797
template: template,
98-
variables: variables,
98+
inputs: inputs,
9999
history: history,
100100
projectID: generativeAIService.firebaseInfo.projectID,
101101
stream: true,

FirebaseAI/Sources/TemplateImagenModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public final class TemplateImagenModel: Sendable {
3535
/// - Returns: The images generated by the model.
3636
/// - Throws: An error if the request failed.
3737
public func generateImages(template: String,
38-
variables: [String: Any],
38+
inputs: [String: Any],
3939
options: RequestOptions = RequestOptions()) async throws
4040
-> ImagenGenerationResponse<ImagenInlineImage> {
41-
let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
41+
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
4242
let projectID = generativeAIService.firebaseInfo.projectID
4343
let request = TemplateGenerateImagesRequest(
4444
template: template,
45-
variables: templateVariables,
45+
inputs: templateInputs,
4646
projectID: projectID,
4747
apiConfig: apiConfig,
4848
options: options

FirebaseAI/Sources/TemplateVariable.swift renamed to FirebaseAI/Sources/TemplateInput.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
import Foundation
1616

17-
enum TemplateVariable: Encodable, Sendable {
17+
enum TemplateInput: Encodable, Sendable {
1818
case string(String)
1919
case int(Int)
2020
case double(Double)
2121
case bool(Bool)
22-
case array([TemplateVariable])
23-
case dictionary([String: TemplateVariable])
22+
case array([TemplateInput])
23+
case dictionary([String: TemplateInput])
2424

2525
init(value: Any) throws {
2626
switch value {
@@ -35,9 +35,9 @@ enum TemplateVariable: Encodable, Sendable {
3535
case let value as Bool:
3636
self = .bool(value)
3737
case let value as [Any]:
38-
self = try .array(value.map { try TemplateVariable(value: $0) })
38+
self = try .array(value.map { try TemplateInput(value: $0) })
3939
case let value as [String: Any]:
40-
self = try .dictionary(value.mapValues { try TemplateVariable(value: $0) })
40+
self = try .dictionary(value.mapValues { try TemplateInput(value: $0) })
4141
default:
4242
throw EncodingError.invalidValue(
4343
value,

FirebaseAI/Tests/TestApp/Tests/Integration/ServerPromptTemplateIntegrationTests.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ struct ServerPromptTemplateIntegrationTests {
3333
let model = FirebaseAI.componentInstance(config).templateGenerativeModel()
3434
let userName = "paul"
3535
let response = try await model.generateContent(
36-
template: "greeting2",
37-
variables: [
36+
template: "greeting4",
37+
inputs: [
3838
"name": userName,
3939
"language": "Spanish",
4040
]
@@ -43,13 +43,16 @@ struct ServerPromptTemplateIntegrationTests {
4343
#expect(text.contains("Paul"))
4444
}
4545

46-
@Test(arguments: testConfigs)
46+
@Test(arguments: [
47+
// The "greeting2" template is only available in the `global` location.
48+
InstanceConfig.vertexAI_v1beta_global,
49+
])
4750
func generateContentStream(_ config: InstanceConfig) async throws {
4851
let model = FirebaseAI.componentInstance(config).templateGenerativeModel()
4952
let userName = "paul"
5053
let stream = try model.generateContentStream(
51-
template: "greeting.prompt",
52-
variables: [
54+
template: "greeting2",
55+
inputs: [
5356
"name": userName,
5457
"language": "English",
5558
]
@@ -93,8 +96,8 @@ struct ServerPromptTemplateIntegrationTests {
9396
let base64Image = imageBytes.base64EncodedString()
9497

9598
let response = try await model.generateContent(
96-
template: "media.prompt",
97-
variables: [
99+
template: "media",
100+
inputs: [
98101
"imageData": [
99102
"isInline": true,
100103
"mimeType": "image/jpeg",
@@ -121,7 +124,7 @@ struct ServerPromptTemplateIntegrationTests {
121124

122125
let stream = try model.generateContentStream(
123126
template: "media.prompt",
124-
variables: [
127+
inputs: [
125128
"imageData": [
126129
"isInline": true,
127130
"mimeType": "image/jpeg",
@@ -138,14 +141,17 @@ struct ServerPromptTemplateIntegrationTests {
138141
#expect(!resultText.isEmpty)
139142
}
140143

141-
@Test(arguments: testConfigs)
144+
@Test(arguments: [
145+
// The "greeting2" template is only available in the `global` location.
146+
InstanceConfig.vertexAI_v1beta_global,
147+
])
142148
func chat(_ config: InstanceConfig) async throws {
143149
let model = FirebaseAI.componentInstance(config).templateGenerativeModel()
144150
let initialHistory = [
145151
ModelContent(role: "user", parts: "Hello!"),
146152
ModelContent(role: "model", parts: "Hi there! How can I help?"),
147153
]
148-
let chatSession = model.startChat(template: "chat_history.prompt", history: initialHistory)
154+
let chatSession = model.startChat(template: "chat-history", history: initialHistory)
149155

150156
let userMessage = "What's the weather like?"
151157

FirebaseAI/Tests/Unit/TemplateChatSessionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class TemplateChatSessionTests: XCTestCase {
4343
isTemplateRequest: true
4444
)
4545
let chat = model.startChat(template: "test-template")
46-
let response = try await chat.sendMessage("Hello", variables: ["name": "test"])
46+
let response = try await chat.sendMessage("Hello", inputs: ["name": "test"])
4747
XCTAssertEqual(chat.history.count, 2)
4848
XCTAssertEqual(chat.history[0].role, "user")
4949
XCTAssertEqual((chat.history[0].parts.first as? TextPart)?.text, "Hello")
@@ -63,7 +63,7 @@ final class TemplateChatSessionTests: XCTestCase {
6363
isTemplateRequest: true
6464
)
6565
let chat = model.startChat(template: "test-template")
66-
let stream = try chat.sendMessageStream("Hello", variables: ["name": "test"])
66+
let stream = try chat.sendMessageStream("Hello", inputs: ["name": "test"])
6767

6868
let content = try await GenerativeModelTestUtil.collectTextFromStream(stream)
6969

FirebaseAI/Tests/Unit/TemplateGenerativeModelTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class TemplateGenerativeModelTests: XCTestCase {
4545

4646
let response = try await model.generateContent(
4747
template: "test-template",
48-
variables: ["name": "test"]
48+
inputs: ["name": "test"]
4949
)
5050
XCTAssertEqual(
5151
response.text,
@@ -63,7 +63,7 @@ final class TemplateGenerativeModelTests: XCTestCase {
6363

6464
let stream = try model.generateContentStream(
6565
template: "test-template",
66-
variables: ["name": "test"]
66+
inputs: ["name": "test"]
6767
)
6868

6969
let content = try await GenerativeModelTestUtil.collectTextFromStream(stream)

FirebaseAI/Tests/Unit/TemplateImagenModelTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class TemplateImagenModelTests: XCTestCase {
4444

4545
let response = try await model.generateImages(
4646
template: "test-template",
47-
variables: ["prompt": "a cat picture"]
47+
inputs: ["prompt": "a cat picture"]
4848
)
4949
XCTAssertEqual(response.images.count, 4)
5050
XCTAssertNotNil(response.images.first?.data)

0 commit comments

Comments
 (0)