FunctionCallingScreen.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 GenerativeAIUIComponents
  16. import SwiftUI
  17. struct FunctionCallingScreen: View {
  18. @EnvironmentObject
  19. var viewModel: FunctionCallingViewModel
  20. @State
  21. private var userPrompt = "What is 100 Euros in U.S. Dollars?"
  22. enum FocusedField: Hashable {
  23. case message
  24. }
  25. @FocusState
  26. var focusedField: FocusedField?
  27. var body: some View {
  28. VStack {
  29. ScrollViewReader { scrollViewProxy in
  30. List {
  31. Text("Interact with a currency conversion API using function calling in Gemini.")
  32. ForEach(viewModel.messages) { message in
  33. MessageView(message: message)
  34. }
  35. if let error = viewModel.error {
  36. ErrorView(error: error)
  37. .tag("errorView")
  38. }
  39. }
  40. .listStyle(.plain)
  41. .onChange(of: viewModel.messages, perform: { newValue in
  42. if viewModel.hasError {
  43. // Wait for a short moment to make sure we can actually scroll to the bottom.
  44. DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
  45. withAnimation {
  46. scrollViewProxy.scrollTo("errorView", anchor: .bottom)
  47. }
  48. focusedField = .message
  49. }
  50. } else {
  51. guard let lastMessage = viewModel.messages.last else { return }
  52. // Wait for a short moment to make sure we can actually scroll to the bottom.
  53. DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
  54. withAnimation {
  55. scrollViewProxy.scrollTo(lastMessage.id, anchor: .bottom)
  56. }
  57. focusedField = .message
  58. }
  59. }
  60. })
  61. .onTapGesture {
  62. focusedField = nil
  63. }
  64. }
  65. InputField("Message...", text: $userPrompt) {
  66. Image(systemName: viewModel.busy ? "stop.circle.fill" : "arrow.up.circle.fill")
  67. .font(.title)
  68. }
  69. .focused($focusedField, equals: .message)
  70. .onSubmit { sendOrStop() }
  71. }
  72. .toolbar {
  73. ToolbarItem(placement: .primaryAction) {
  74. Button(action: newChat) {
  75. Image(systemName: "square.and.pencil")
  76. }
  77. }
  78. }
  79. .navigationTitle("Function Calling")
  80. .onAppear {
  81. focusedField = .message
  82. }
  83. }
  84. private func sendMessage() {
  85. Task {
  86. let prompt = userPrompt
  87. userPrompt = ""
  88. await viewModel.sendMessage(prompt, streaming: true)
  89. }
  90. }
  91. private func sendOrStop() {
  92. if viewModel.busy {
  93. viewModel.stop()
  94. } else {
  95. sendMessage()
  96. }
  97. }
  98. private func newChat() {
  99. viewModel.startNewChat()
  100. }
  101. }
  102. struct FunctionCallingScreen_Previews: PreviewProvider {
  103. struct ContainerView: View {
  104. @EnvironmentObject
  105. var viewModel: FunctionCallingViewModel
  106. var body: some View {
  107. FunctionCallingScreen()
  108. .onAppear {
  109. viewModel.messages = ChatMessage.samples
  110. }
  111. }
  112. }
  113. static var previews: some View {
  114. NavigationStack {
  115. FunctionCallingScreen().environmentObject(FunctionCallingViewModel())
  116. }
  117. }
  118. }