Skip to content

Commit a11c20c

Browse files
committed
template to templateID
1 parent 6338983 commit a11c20c

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

FirebaseAI/Sources/TemplateChatSession.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import Foundation
1818
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1919
public final class TemplateChatSession: Sendable {
2020
private let model: TemplateGenerativeModel
21-
private let template: String
21+
private let templateID: String
2222
private let _history: History
2323

24-
init(model: TemplateGenerativeModel, template: String, history: [ModelContent]) {
24+
init(model: TemplateGenerativeModel, templateID: String, history: [ModelContent]) {
2525
self.model = model
26-
self.template = template
26+
self.templateID = templateID
2727
_history = History(history: history)
2828
}
2929

@@ -45,7 +45,7 @@ public final class TemplateChatSession: Sendable {
4545
let newContent = populateContentRole(ModelContent(parts: message.partsValue))
4646
let response = try await model.generateContentWithHistory(
4747
history: _history.history + [newContent],
48-
template: template,
48+
template: templateID,
4949
inputs: templateInputs,
5050
options: options
5151
)
@@ -64,7 +64,7 @@ public final class TemplateChatSession: Sendable {
6464
let newContent = populateContentRole(ModelContent(parts: message.partsValue))
6565
let stream = try model.generateContentStreamWithHistory(
6666
history: _history.history + [newContent],
67-
template: template,
67+
template: templateID,
6868
inputs: templateInputs,
6969
options: options
7070
)

FirebaseAI/Sources/TemplateGenerativeModel.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ public final class TemplateGenerativeModel: Sendable {
2929
/// Generates content from a prompt template and inputs.
3030
///
3131
/// - Parameters:
32-
/// - template: The prompt template to use.
33-
/// - variables: A dictionary of variables to substitute into the template.
32+
/// - templateID: The ID of the prompt template to use.
33+
/// - inputs: 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.
36-
public func generateContent(template: String,
36+
public func generateContent(templateID: String,
3737
inputs: [String: Any],
3838
options: RequestOptions = RequestOptions()) async throws
3939
-> GenerateContentResponse {
4040
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
4141
return try await generateContentWithHistory(
4242
history: [],
43-
template: template,
43+
template: templateID,
4444
inputs: templateInputs,
4545
options: options
4646
)
@@ -51,7 +51,7 @@ public final class TemplateGenerativeModel: Sendable {
5151
/// - Parameters:
5252
/// - history: The conversation history to use.
5353
/// - template: The prompt template to use.
54-
/// - variables: A dictionary of variables to substitute into the template.
54+
/// - inputs: A dictionary of variables to substitute into the template.
5555
/// - Returns: The content generated by the model.
5656
/// - Throws: A ``GenerateContentError`` if the request failed.
5757
func generateContentWithHistory(history: [ModelContent], template: String,
@@ -72,13 +72,20 @@ public final class TemplateGenerativeModel: Sendable {
7272
return response
7373
}
7474

75-
public func generateContentStream(template: String,
75+
/// Generates content from a prompt template and inputs, with streaming responses.
76+
///
77+
/// - Parameters:
78+
/// - templateID: The ID of the prompt template to use.
79+
/// - inputs: A dictionary of variables to substitute into the template.
80+
/// - Returns: An `AsyncThrowingStream` that yields `GenerateContentResponse` objects.
81+
/// - Throws: A ``GenerateContentError`` if the request failed.
82+
public func generateContentStream(templateID: String,
7683
inputs: [String: Any],
7784
options: RequestOptions = RequestOptions()) throws
7885
-> AsyncThrowingStream<GenerateContentResponse, Error> {
7986
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
8087
let request = TemplateGenerateContentRequest(
81-
template: template,
88+
template: templateID,
8289
inputs: templateInputs,
8390
history: [],
8491
projectID: generativeAIService.firebaseInfo.projectID,
@@ -108,14 +115,14 @@ public final class TemplateGenerativeModel: Sendable {
108115
/// Creates a new chat conversation using this model with the provided history and template.
109116
///
110117
/// - Parameters:
111-
/// - template: The prompt template to use.
118+
/// - templateID: The ID of the prompt template to use.
112119
/// - history: The conversation history to use.
113120
/// - Returns: A new ``TemplateChatSession`` instance.
114-
public func startChat(template: String,
121+
public func startChat(templateID: String,
115122
history: [ModelContent] = []) -> TemplateChatSession {
116123
return TemplateChatSession(
117124
model: self,
118-
template: template,
125+
templateID: templateID,
119126
history: history
120127
)
121128
}

FirebaseAI/Sources/TemplateImagenModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public final class TemplateImagenModel: Sendable {
3434
/// - variables: A dictionary of variables to substitute into the template.
3535
/// - Returns: The images generated by the model.
3636
/// - Throws: An error if the request failed.
37-
public func generateImages(template: String,
37+
public func generateImages(templateID: String,
3838
inputs: [String: Any],
3939
options: RequestOptions = RequestOptions()) async throws
4040
-> ImagenGenerationResponse<ImagenInlineImage> {
4141
let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
4242
let projectID = generativeAIService.firebaseInfo.projectID
4343
let request = TemplateGenerateImagesRequest(
44-
template: template,
44+
template: templateID,
4545
inputs: templateInputs,
4646
projectID: projectID,
4747
apiConfig: apiConfig,

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct ServerPromptTemplateIntegrationTests {
3333
let model = FirebaseAI.componentInstance(config).templateGenerativeModel()
3434
let userName = "paul"
3535
let response = try await model.generateContent(
36-
template: "greeting4",
36+
templateID: "greeting4",
3737
inputs: [
3838
"name": userName,
3939
"language": "Spanish",
@@ -51,7 +51,7 @@ struct ServerPromptTemplateIntegrationTests {
5151
let model = FirebaseAI.componentInstance(config).templateGenerativeModel()
5252
let userName = "paul"
5353
let stream = try model.generateContentStream(
54-
template: "greeting2",
54+
templateID: "greeting2",
5555
inputs: [
5656
"name": userName,
5757
"language": "English",
@@ -74,7 +74,7 @@ struct ServerPromptTemplateIntegrationTests {
7474
let imagenModel = FirebaseAI.componentInstance(config).templateImagenModel()
7575
let imagenPrompt = "A cat picture"
7676
let response = try await imagenModel.generateImages(
77-
template: "generate-images2",
77+
templateID: "generate-images2",
7878
variables: [
7979
"prompt": imagenPrompt,
8080
]
@@ -96,7 +96,7 @@ struct ServerPromptTemplateIntegrationTests {
9696
let base64Image = imageBytes.base64EncodedString()
9797

9898
let response = try await model.generateContent(
99-
template: "media",
99+
templateID: "media",
100100
inputs: [
101101
"imageData": [
102102
"isInline": true,
@@ -123,7 +123,7 @@ struct ServerPromptTemplateIntegrationTests {
123123
let base64Image = imageBytes.base64EncodedString()
124124

125125
let stream = try model.generateContentStream(
126-
template: "media.prompt",
126+
templateID: "media.prompt",
127127
inputs: [
128128
"imageData": [
129129
"isInline": true,
@@ -151,7 +151,7 @@ struct ServerPromptTemplateIntegrationTests {
151151
ModelContent(role: "user", parts: "Hello!"),
152152
ModelContent(role: "model", parts: "Hi there! How can I help?"),
153153
]
154-
let chatSession = model.startChat(template: "chat-history", history: initialHistory)
154+
let chatSession = model.startChat(templateID: "chat-history", history: initialHistory)
155155

156156
let userMessage = "What's the weather like?"
157157

@@ -173,13 +173,13 @@ struct ServerPromptTemplateIntegrationTests {
173173
ModelContent(role: "user", parts: "Hello!"),
174174
ModelContent(role: "model", parts: "Hi there! How can I help?"),
175175
]
176-
let chatSession = model.startChat(template: "chat_history.prompt", history: initialHistory)
176+
let chatSession = model.startChat(templateID: "chat_history.prompt", history: initialHistory)
177177

178178
let userMessage = "What's the weather like?"
179179

180180
let stream = try chatSession.sendMessageStream(
181181
userMessage,
182-
variables: ["message": userMessage]
182+
inputs: ["message": userMessage]
183183
)
184184
var resultText = ""
185185
for try await response in stream {

FirebaseAI/Tests/Unit/TemplateChatSessionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final class TemplateChatSessionTests: XCTestCase {
4242
subdirectory: "mock-responses/googleai",
4343
isTemplateRequest: true
4444
)
45-
let chat = model.startChat(template: "test-template")
45+
let chat = model.startChat(templateID: "test-template")
4646
let response = try await chat.sendMessage("Hello", inputs: ["name": "test"])
4747
XCTAssertEqual(chat.history.count, 2)
4848
XCTAssertEqual(chat.history[0].role, "user")
@@ -62,7 +62,7 @@ final class TemplateChatSessionTests: XCTestCase {
6262
subdirectory: "mock-responses/googleai",
6363
isTemplateRequest: true
6464
)
65-
let chat = model.startChat(template: "test-template")
65+
let chat = model.startChat(templateID: "test-template")
6666
let stream = try chat.sendMessageStream("Hello", inputs: ["name": "test"])
6767

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

FirebaseAI/Tests/Unit/TemplateGenerativeModelTests.swift

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

4646
let response = try await model.generateContent(
47-
template: "test-template",
47+
templateID: "test-template",
4848
inputs: ["name": "test"]
4949
)
5050
XCTAssertEqual(
@@ -62,7 +62,7 @@ final class TemplateGenerativeModelTests: XCTestCase {
6262
)
6363

6464
let stream = try model.generateContentStream(
65-
template: "test-template",
65+
templateID: "test-template",
6666
inputs: ["name": "test"]
6767
)
6868

FirebaseAI/Tests/Unit/TemplateImagenModelTests.swift

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

4545
let response = try await model.generateImages(
46-
template: "test-template",
46+
templateID: "test-template",
4747
inputs: ["prompt": "a cat picture"]
4848
)
4949
XCTAssertEqual(response.images.count, 4)

0 commit comments

Comments
 (0)