ContentView.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2024 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 FirebaseAI
  15. import SwiftUI
  16. struct ContentView: View {
  17. // TODO: Revert changes in this file. For prototyping purposes only.
  18. let liveModel: LiveGenerativeModel = {
  19. // let firebaseAI = FirebaseAI.firebaseAI(backend: .vertexAI())
  20. let firebaseAI = FirebaseAI.firebaseAI()
  21. return firebaseAI.liveModel(
  22. modelName: "gemini-2.0-flash-live-001",
  23. generationConfig: LiveGenerationConfig(responseModalities: [.text])
  24. )
  25. }()
  26. @State private var responses: [String] = []
  27. var body: some View {
  28. VStack {
  29. List(responses, id: \.self) {
  30. Text($0)
  31. }
  32. }
  33. .padding()
  34. .task {
  35. do {
  36. let liveSession = liveModel.connect()
  37. try await liveSession.sendMessage("Why is the sky blue?")
  38. for try await response in liveSession.responses {
  39. responses.append(String(describing: response))
  40. }
  41. } catch {
  42. print(error)
  43. }
  44. }
  45. }
  46. }
  47. #Preview {
  48. ContentView()
  49. }