StructuredOutputSnippets.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 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 StructuredOutputSnippets: XCTestCase {
  21. override func setUpWithError() throws {
  22. try FirebaseApp.configureDefaultAppForSnippets()
  23. }
  24. override func tearDown() async throws {
  25. await FirebaseApp.deleteDefaultAppForSnippets()
  26. }
  27. func testStructuredOutputJSONBasic() async throws {
  28. // Provide a JSON schema object using a standard format.
  29. // Later, pass this schema object into `responseSchema` in the generation config.
  30. let jsonSchema = Schema.object(
  31. properties: [
  32. "characters": Schema.array(
  33. items: .object(
  34. properties: [
  35. "name": .string(),
  36. "age": .integer(),
  37. "species": .string(),
  38. "accessory": .enumeration(values: ["hat", "belt", "shoes"]),
  39. ],
  40. optionalProperties: ["accessory"]
  41. )
  42. ),
  43. ]
  44. )
  45. // Initialize the Vertex AI service and the generative model.
  46. // Use a model that supports `responseSchema`, like one of the Gemini 1.5 models.
  47. let model = FirebaseAI.firebaseAI().generativeModel(
  48. modelName: "gemini-2.0-flash",
  49. // In the generation config, set the `responseMimeType` to `application/json`
  50. // and pass the JSON schema object into `responseSchema`.
  51. generationConfig: GenerationConfig(
  52. responseMIMEType: "application/json",
  53. responseSchema: jsonSchema
  54. )
  55. )
  56. let prompt = "For use in a children's card game, generate 10 animal-based characters."
  57. let response = try await model.generateContent(prompt)
  58. print(response.text ?? "No text in response.")
  59. }
  60. func testStructuredOutputEnumBasic() async throws {
  61. // Provide an enum schema object using a standard format.
  62. // Later, pass this schema object into `responseSchema` in the generation config.
  63. let enumSchema = Schema.enumeration(values: ["drama", "comedy", "documentary"])
  64. // Initialize the Vertex AI service and the generative model.
  65. // Use a model that supports `responseSchema`, like one of the Gemini 1.5 models.
  66. let model = FirebaseAI.firebaseAI().generativeModel(
  67. modelName: "gemini-2.0-flash",
  68. // In the generation config, set the `responseMimeType` to `text/x.enum`
  69. // and pass the enum schema object into `responseSchema`.
  70. generationConfig: GenerationConfig(
  71. responseMIMEType: "text/x.enum",
  72. responseSchema: enumSchema
  73. )
  74. )
  75. let prompt = """
  76. The film aims to educate and inform viewers about real-life subjects, events, or people.
  77. It offers a factual record of a particular topic by combining interviews, historical footage,
  78. and narration. The primary purpose of a film is to present information and provide insights
  79. into various aspects of reality.
  80. """
  81. let response = try await model.generateContent(prompt)
  82. print(response.text ?? "No text in response.")
  83. }
  84. }