Skip to content

Commit 82f98d1

Browse files
committed
Update sample apps
1 parent c13d297 commit 82f98d1

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

FirebaseVertexAI/Sample/ChatSample/ViewModels/ConversationViewModel.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ class ConversationViewModel: ObservableObject {
3030
}
3131

3232
private var model: GenerativeModel
33-
private var chat: Chat
33+
private var chat: Chat? = nil
3434
private var stopGenerating = false
3535

3636
private var chatTask: Task<Void, Never>?
3737

3838
init() {
3939
model = VertexAI.vertexAI().generativeModel(modelName: "gemini-1.5-flash")
40-
chat = model.startChat()
4140
}
4241

4342
func sendMessage(_ text: String, streaming: Bool = true) async {
43+
if chat == nil {
44+
chat = await model.startChat()
45+
}
4446
error = nil
4547
if streaming {
4648
await internalSendMessageStreaming(text)
@@ -52,7 +54,7 @@ class ConversationViewModel: ObservableObject {
5254
func startNewChat() {
5355
stop()
5456
error = nil
55-
chat = model.startChat()
57+
chat = nil
5658
messages.removeAll()
5759
}
5860

@@ -79,7 +81,10 @@ class ConversationViewModel: ObservableObject {
7981
messages.append(systemMessage)
8082

8183
do {
82-
let responseStream = chat.sendMessageStream(text)
84+
guard let chat else {
85+
throw ChatError.notInitialized
86+
}
87+
let responseStream = try await chat.sendMessageStream(text)
8388
for try await chunk in responseStream {
8489
messages[messages.count - 1].pending = false
8590
if let text = chunk.text {
@@ -112,10 +117,12 @@ class ConversationViewModel: ObservableObject {
112117
messages.append(systemMessage)
113118

114119
do {
115-
var response: GenerateContentResponse?
116-
response = try await chat.sendMessage(text)
120+
guard let chat = chat else {
121+
throw ChatError.notInitialized
122+
}
123+
let response = try await chat.sendMessage(text)
117124

118-
if let responseText = response?.text {
125+
if let responseText = response.text {
119126
// replace pending message with backend response
120127
messages[messages.count - 1].message = responseText
121128
messages[messages.count - 1].pending = false
@@ -127,4 +134,8 @@ class ConversationViewModel: ObservableObject {
127134
}
128135
}
129136
}
137+
138+
enum ChatError: Error {
139+
case notInitialized
140+
}
130141
}

FirebaseVertexAI/Sample/FunctionCallingSample/ViewModels/FunctionCallingViewModel.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FunctionCallingViewModel: ObservableObject {
3333
private var functionCalls = [FunctionCall]()
3434

3535
private var model: GenerativeModel
36-
private var chat: Chat
36+
private var chat: Chat? = nil
3737

3838
private var chatTask: Task<Void, Never>?
3939

@@ -62,7 +62,6 @@ class FunctionCallingViewModel: ObservableObject {
6262
),
6363
])]
6464
)
65-
chat = model.startChat()
6665
}
6766

6867
func sendMessage(_ text: String, streaming: Bool = true) async {
@@ -75,6 +74,10 @@ class FunctionCallingViewModel: ObservableObject {
7574
busy = false
7675
}
7776

77+
if chat == nil {
78+
chat = await model.startChat()
79+
}
80+
7881
// first, add the user's message to the chat
7982
let userMessage = ChatMessage(message: text, participant: .user)
8083
messages.append(userMessage)
@@ -103,7 +106,7 @@ class FunctionCallingViewModel: ObservableObject {
103106
func startNewChat() {
104107
stop()
105108
error = nil
106-
chat = model.startChat()
109+
chat = nil
107110
messages.removeAll()
108111
}
109112

@@ -114,14 +117,17 @@ class FunctionCallingViewModel: ObservableObject {
114117

115118
private func internalSendMessageStreaming(_ text: String) async throws {
116119
let functionResponses = try await processFunctionCalls()
120+
guard let chat else {
121+
throw ChatError.notInitialized
122+
}
117123
let responseStream: AsyncThrowingStream<GenerateContentResponse, Error>
118124
if functionResponses.isEmpty {
119-
responseStream = chat.sendMessageStream(text)
125+
responseStream = try await chat.sendMessageStream(text)
120126
} else {
121127
for functionResponse in functionResponses {
122128
messages.insert(functionResponse.chatMessage(), at: messages.count - 1)
123129
}
124-
responseStream = chat.sendMessageStream(functionResponses.modelContent())
130+
responseStream = try await chat.sendMessageStream(functionResponses.modelContent())
125131
}
126132
for try await chunk in responseStream {
127133
processResponseContent(content: chunk)
@@ -130,6 +136,9 @@ class FunctionCallingViewModel: ObservableObject {
130136

131137
private func internalSendMessage(_ text: String) async throws {
132138
let functionResponses = try await processFunctionCalls()
139+
guard let chat else {
140+
throw ChatError.notInitialized
141+
}
133142
let response: GenerateContentResponse
134143
if functionResponses.isEmpty {
135144
response = try await chat.sendMessage(text)
@@ -181,6 +190,10 @@ class FunctionCallingViewModel: ObservableObject {
181190
return functionResponses
182191
}
183192

193+
enum ChatError: Error {
194+
case notInitialized
195+
}
196+
184197
// MARK: - Callable Functions
185198

186199
func getExchangeRate(args: JSONObject) -> JSONObject {

FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class PhotoReasoningViewModel: ObservableObject {
8484
}
8585
}
8686

87-
let outputContentStream = model.generateContentStream(prompt, images)
87+
let outputContentStream = try await model.generateContentStream(prompt, images)
8888

8989
// stream response
9090
for try await outputContent in outputContentStream {

FirebaseVertexAI/Sample/GenerativeAITextSample/ViewModels/SummarizeViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class SummarizeViewModel: ObservableObject {
5050

5151
let prompt = "Summarize the following text for me: \(inputText)"
5252

53-
let outputContentStream = model.generateContentStream(prompt)
53+
let outputContentStream = try await model.generateContentStream(prompt)
5454

5555
// stream response
5656
for try await outputContent in outputContentStream {

0 commit comments

Comments
 (0)