FunctionCallingViewModel.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 = [FunctionCall]()
  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: [Tool(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.textValue
  131. messages[messages.count - 1].pending = false
  132. case let functionCallPart as FunctionCallPart:
  133. let functionCall = functionCallPart.functionCall
  134. messages.insert(functionCall.chatMessage(), at: messages.count - 1)
  135. functionCalls.append(functionCall)
  136. default:
  137. fatalError("Unsupported response content.")
  138. }
  139. }
  140. }
  141. func processFunctionCalls() async throws -> [FunctionResponse] {
  142. var functionResponses = [FunctionResponse]()
  143. for functionCall in functionCalls {
  144. switch functionCall.name {
  145. case "get_exchange_rate":
  146. let exchangeRates = getExchangeRate(args: functionCall.args)
  147. functionResponses.append(FunctionResponse(
  148. name: "get_exchange_rate",
  149. response: exchangeRates
  150. ))
  151. default:
  152. fatalError("Unknown function named \"\(functionCall.name)\".")
  153. }
  154. }
  155. functionCalls = []
  156. return functionResponses
  157. }
  158. // MARK: - Callable Functions
  159. func getExchangeRate(args: JSONObject) -> JSONObject {
  160. // 1. Validate and extract the parameters provided by the model (from a `FunctionCall`)
  161. guard case let .string(from) = args["currency_from"] else {
  162. fatalError("Missing `currency_from` parameter.")
  163. }
  164. guard case let .string(to) = args["currency_to"] else {
  165. fatalError("Missing `currency_to` parameter.")
  166. }
  167. // 2. Get the exchange rate
  168. let allRates: [String: [String: Double]] = [
  169. "AUD": ["CAD": 0.89265, "EUR": 0.6072, "GBP": 0.51714, "JPY": 97.75, "USD": 0.66379],
  170. "CAD": ["AUD": 1.1203, "EUR": 0.68023, "GBP": 0.57933, "JPY": 109.51, "USD": 0.74362],
  171. "EUR": ["AUD": 1.6469, "CAD": 1.4701, "GBP": 0.85168, "JPY": 160.99, "USD": 1.0932],
  172. "GBP": ["AUD": 1.9337, "CAD": 1.7261, "EUR": 1.1741, "JPY": 189.03, "USD": 1.2836],
  173. "JPY": ["AUD": 0.01023, "CAD": 0.00913, "EUR": 0.00621, "GBP": 0.00529, "USD": 0.00679],
  174. "USD": ["AUD": 1.5065, "CAD": 1.3448, "EUR": 0.91475, "GBP": 0.77907, "JPY": 147.26],
  175. ]
  176. guard let fromRates = allRates[from] else {
  177. return ["error": .string("No data for currency \(from).")]
  178. }
  179. guard let toRate = fromRates[to] else {
  180. return ["error": .string("No data for currency \(to).")]
  181. }
  182. // 3. Return the exchange rates as a JSON object (returned to the model in a `FunctionResponse`)
  183. return ["rates": .number(toRate)]
  184. }
  185. }
  186. private extension FunctionCall {
  187. func chatMessage() -> ChatMessage {
  188. let encoder = JSONEncoder()
  189. encoder.outputFormatting = .prettyPrinted
  190. let jsonData: Data
  191. do {
  192. jsonData = try encoder.encode(self)
  193. } catch {
  194. fatalError("JSON Encoding Failed: \(error.localizedDescription)")
  195. }
  196. guard let json = String(data: jsonData, encoding: .utf8) else {
  197. fatalError("Failed to convert JSON data to a String.")
  198. }
  199. let messageText = "Function call requested by model:\n```\n\(json)\n```"
  200. return ChatMessage(message: messageText, participant: .system)
  201. }
  202. }
  203. private extension FunctionResponse {
  204. func chatMessage() -> ChatMessage {
  205. let encoder = JSONEncoder()
  206. encoder.outputFormatting = .prettyPrinted
  207. let jsonData: Data
  208. do {
  209. jsonData = try encoder.encode(self)
  210. } catch {
  211. fatalError("JSON Encoding Failed: \(error.localizedDescription)")
  212. }
  213. guard let json = String(data: jsonData, encoding: .utf8) else {
  214. fatalError("Failed to convert JSON data to a String.")
  215. }
  216. let messageText = "Function response returned by app:\n```\n\(json)\n```"
  217. return ChatMessage(message: messageText, participant: .user)
  218. }
  219. }
  220. private extension [FunctionResponse] {
  221. func modelContent() -> [ModelContent] {
  222. return self.map { ModelContent(
  223. role: "function",
  224. parts: [FunctionResponsePart(functionResponse: $0)]
  225. )
  226. }
  227. }
  228. }