FunctionCallingViewModel.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseVertexAI
  15. import Foundation
  16. import UIKit
  17. @MainActor
  18. class FunctionCallingViewModel: ObservableObject {
  19. /// This array holds both the user's and the system's chat messages
  20. @Published var messages = [ChatMessage]()
  21. /// Indicates we're waiting for the model to finish
  22. @Published var busy = false
  23. @Published var error: Error?
  24. var hasError: Bool {
  25. return error != nil
  26. }
  27. /// Function calls pending processing
  28. private var functionCalls = [FunctionCallPart]()
  29. private var model: GenerativeModel
  30. private var chat: Chat
  31. private var chatTask: Task<Void, Never>?
  32. init() {
  33. model = VertexAI.vertexAI().generativeModel(
  34. modelName: "gemini-1.5-flash",
  35. tools: [.functionDeclarations([
  36. FunctionDeclaration(
  37. name: "get_exchange_rate",
  38. description: "Get the exchange rate for currencies between countries",
  39. parameters: [
  40. "currency_from": .enumeration(
  41. values: ["USD", "EUR", "JPY", "GBP", "AUD", "CAD"],
  42. description: "The currency to convert from in ISO 4217 format"
  43. ),
  44. "currency_to": .enumeration(
  45. values: ["USD", "EUR", "JPY", "GBP", "AUD", "CAD"],
  46. description: "The currency to convert to in ISO 4217 format"
  47. ),
  48. ]
  49. ),
  50. ])]
  51. )
  52. chat = model.startChat()
  53. }
  54. func sendMessage(_ text: String, streaming: Bool = true) async {
  55. error = nil
  56. chatTask?.cancel()
  57. chatTask = Task {
  58. busy = true
  59. defer {
  60. busy = false
  61. }
  62. // first, add the user's message to the chat
  63. let userMessage = ChatMessage(message: text, participant: .user)
  64. messages.append(userMessage)
  65. // add a pending message while we're waiting for a response from the backend
  66. let systemMessage = ChatMessage.pending(participant: .system)
  67. messages.append(systemMessage)
  68. print(messages)
  69. do {
  70. repeat {
  71. if streaming {
  72. try await internalSendMessageStreaming(text)
  73. } else {
  74. try await internalSendMessage(text)
  75. }
  76. } while !functionCalls.isEmpty
  77. } catch {
  78. self.error = error
  79. print(error.localizedDescription)
  80. messages.removeLast()
  81. }
  82. }
  83. }
  84. func startNewChat() {
  85. stop()
  86. error = nil
  87. chat = model.startChat()
  88. messages.removeAll()
  89. }
  90. func stop() {
  91. chatTask?.cancel()
  92. error = nil
  93. }
  94. private func internalSendMessageStreaming(_ text: String) async throws {
  95. let functionResponses = try await processFunctionCalls()
  96. let responseStream: AsyncThrowingStream<GenerateContentResponse, Error>
  97. if functionResponses.isEmpty {
  98. responseStream = try chat.sendMessageStream(text)
  99. } else {
  100. for functionResponse in functionResponses {
  101. messages.insert(functionResponse.chatMessage(), at: messages.count - 1)
  102. }
  103. responseStream = try chat.sendMessageStream([functionResponses.modelContent()])
  104. }
  105. for try await chunk in responseStream {
  106. processResponseContent(content: chunk)
  107. }
  108. }
  109. private func internalSendMessage(_ text: String) async throws {
  110. let functionResponses = try await processFunctionCalls()
  111. let response: GenerateContentResponse
  112. if functionResponses.isEmpty {
  113. response = try await chat.sendMessage(text)
  114. } else {
  115. for functionResponse in functionResponses {
  116. messages.insert(functionResponse.chatMessage(), at: messages.count - 1)
  117. }
  118. response = try await chat.sendMessage([functionResponses.modelContent()])
  119. }
  120. processResponseContent(content: response)
  121. }
  122. func processResponseContent(content: GenerateContentResponse) {
  123. guard let candidate = content.candidates.first else {
  124. fatalError("No candidate.")
  125. }
  126. for part in candidate.content.parts {
  127. switch part {
  128. case let textPart as TextPart:
  129. // replace pending message with backend response
  130. messages[messages.count - 1].message += textPart.text
  131. messages[messages.count - 1].pending = false
  132. case let functionCallPart as FunctionCallPart:
  133. messages.insert(functionCallPart.chatMessage(), at: messages.count - 1)
  134. functionCalls.append(functionCallPart)
  135. default:
  136. fatalError("Unsupported response part: \(part)")
  137. }
  138. }
  139. }
  140. func processFunctionCalls() async throws -> [FunctionResponsePart] {
  141. var functionResponses = [FunctionResponsePart]()
  142. for functionCall in functionCalls {
  143. switch functionCall.name {
  144. case "get_exchange_rate":
  145. let exchangeRates = getExchangeRate(args: functionCall.args)
  146. functionResponses.append(FunctionResponsePart(
  147. name: "get_exchange_rate",
  148. response: exchangeRates
  149. ))
  150. default:
  151. fatalError("Unknown function named \"\(functionCall.name)\".")
  152. }
  153. }
  154. functionCalls = []
  155. return functionResponses
  156. }
  157. // MARK: - Callable Functions
  158. func getExchangeRate(args: JSONObject) -> JSONObject {
  159. // 1. Validate and extract the parameters provided by the model (from a `FunctionCall`)
  160. guard case let .string(from) = args["currency_from"] else {
  161. fatalError("Missing `currency_from` parameter.")
  162. }
  163. guard case let .string(to) = args["currency_to"] else {
  164. fatalError("Missing `currency_to` parameter.")
  165. }
  166. // 2. Get the exchange rate
  167. let allRates: [String: [String: Double]] = [
  168. "AUD": ["CAD": 0.89265, "EUR": 0.6072, "GBP": 0.51714, "JPY": 97.75, "USD": 0.66379],
  169. "CAD": ["AUD": 1.1203, "EUR": 0.68023, "GBP": 0.57933, "JPY": 109.51, "USD": 0.74362],
  170. "EUR": ["AUD": 1.6469, "CAD": 1.4701, "GBP": 0.85168, "JPY": 160.99, "USD": 1.0932],
  171. "GBP": ["AUD": 1.9337, "CAD": 1.7261, "EUR": 1.1741, "JPY": 189.03, "USD": 1.2836],
  172. "JPY": ["AUD": 0.01023, "CAD": 0.00913, "EUR": 0.00621, "GBP": 0.00529, "USD": 0.00679],
  173. "USD": ["AUD": 1.5065, "CAD": 1.3448, "EUR": 0.91475, "GBP": 0.77907, "JPY": 147.26],
  174. ]
  175. guard let fromRates = allRates[from] else {
  176. return ["error": .string("No data for currency \(from).")]
  177. }
  178. guard let toRate = fromRates[to] else {
  179. return ["error": .string("No data for currency \(to).")]
  180. }
  181. // 3. Return the exchange rates as a JSON object (returned to the model in a `FunctionResponse`)
  182. return ["rates": .number(toRate)]
  183. }
  184. }
  185. private extension FunctionCallPart {
  186. func chatMessage() -> ChatMessage {
  187. let encoder = JSONEncoder()
  188. encoder.outputFormatting = .prettyPrinted
  189. let jsonData: Data
  190. do {
  191. jsonData = try encoder.encode(self)
  192. } catch {
  193. fatalError("JSON Encoding Failed: \(error.localizedDescription)")
  194. }
  195. guard let json = String(data: jsonData, encoding: .utf8) else {
  196. fatalError("Failed to convert JSON data to a String.")
  197. }
  198. let messageText = "Function call requested by model:\n```\n\(json)\n```"
  199. return ChatMessage(message: messageText, participant: .system)
  200. }
  201. }
  202. private extension FunctionResponsePart {
  203. func chatMessage() -> ChatMessage {
  204. let encoder = JSONEncoder()
  205. encoder.outputFormatting = .prettyPrinted
  206. let jsonData: Data
  207. do {
  208. jsonData = try encoder.encode(self)
  209. } catch {
  210. fatalError("JSON Encoding Failed: \(error.localizedDescription)")
  211. }
  212. guard let json = String(data: jsonData, encoding: .utf8) else {
  213. fatalError("Failed to convert JSON data to a String.")
  214. }
  215. let messageText = "Function response returned by app:\n```\n\(json)\n```"
  216. return ChatMessage(message: messageText, participant: .user)
  217. }
  218. }
  219. private extension [FunctionResponsePart] {
  220. func modelContent() -> ModelContent {
  221. return ModelContent(role: "function", parts: self)
  222. }
  223. }