|
| 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 | + |
| 15 | +import FirebaseCore |
| 16 | +import FirebaseVertexAI |
| 17 | +import XCTest |
| 18 | + |
| 19 | +// These snippet tests are intentionally skipped in CI jobs; see the README file in this directory |
| 20 | +// for instructions on running them manually. |
| 21 | + |
| 22 | +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 23 | +final class ChatSnippets: XCTestCase { |
| 24 | + lazy var model = VertexAI.vertexAI().generativeModel(modelName: "gemini-1.5-flash") |
| 25 | + |
| 26 | + override func setUpWithError() throws { |
| 27 | + try FirebaseApp.configureDefaultAppForSnippets() |
| 28 | + } |
| 29 | + |
| 30 | + override func tearDown() async throws { |
| 31 | + await FirebaseApp.deleteDefaultAppForSnippets() |
| 32 | + } |
| 33 | + |
| 34 | + func testChatNonStreaming() async throws { |
| 35 | + // Optionally specify existing chat history |
| 36 | + let history = [ |
| 37 | + ModelContent(role: "user", parts: "Hello, I have 2 dogs in my house."), |
| 38 | + ModelContent(role: "model", parts: "Great to meet you. What would you like to know?"), |
| 39 | + ] |
| 40 | + |
| 41 | + // Initialize the chat with optional chat history |
| 42 | + let chat = model.startChat(history: history) |
| 43 | + |
| 44 | + // To generate text output, call sendMessage and pass in the message |
| 45 | + let response = try await chat.sendMessage("How many paws are in my house?") |
| 46 | + print(response.text ?? "No text in response.") |
| 47 | + } |
| 48 | + |
| 49 | + func testChatStreaming() async throws { |
| 50 | + // Optionally specify existing chat history |
| 51 | + let history = [ |
| 52 | + ModelContent(role: "user", parts: "Hello, I have 2 dogs in my house."), |
| 53 | + ModelContent(role: "model", parts: "Great to meet you. What would you like to know?"), |
| 54 | + ] |
| 55 | + |
| 56 | + // Initialize the chat with optional chat history |
| 57 | + let chat = model.startChat(history: history) |
| 58 | + |
| 59 | + // To stream generated text output, call sendMessageStream and pass in the message |
| 60 | + let contentStream = try chat.sendMessageStream("How many paws are in my house?") |
| 61 | + for try await chunk in contentStream { |
| 62 | + if let text = chunk.text { |
| 63 | + print(text) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments