TextSnippets.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 FirebaseAILogic
  15. import FirebaseCore
  16. import XCTest
  17. // These snippet tests are intentionally skipped in CI jobs; see the README file in this directory
  18. // for instructions on running them manually.
  19. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. final class TextSnippets: XCTestCase {
  21. lazy var model = FirebaseAI.firebaseAI().generativeModel(modelName: "gemini-2.0-flash")
  22. override func setUpWithError() throws {
  23. try FirebaseApp.configureDefaultAppForSnippets()
  24. }
  25. override func tearDown() async throws {
  26. await FirebaseApp.deleteDefaultAppForSnippets()
  27. }
  28. func testTextOnlyNonStreaming() async throws {
  29. // Provide a prompt that contains text
  30. let prompt = "Write a story about a magic backpack."
  31. // To generate text output, call generateContent with the text input
  32. let response = try await model.generateContent(prompt)
  33. print(response.text ?? "No text in response.")
  34. }
  35. func testTextOnlyStreaming() async throws {
  36. // Provide a prompt that contains text
  37. let prompt = "Write a story about a magic backpack."
  38. // To stream generated text output, call generateContentStream with the text input
  39. let contentStream = try model.generateContentStream(prompt)
  40. for try await chunk in contentStream {
  41. if let text = chunk.text {
  42. print(text)
  43. }
  44. }
  45. }
  46. }