Skip to content

Commit 78c1c2f

Browse files
authored
[Vertex AI] Add text-only generateContent and Chat snippets (#13926)
1 parent b317cbc commit 78c1c2f

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 TextSnippets: 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 testTextOnlyNonStreaming() async throws {
35+
// Provide a prompt that contains text
36+
let prompt = "Write a story about a magic backpack."
37+
38+
// To generate text output, call generateContent with the text input
39+
let response = try await model.generateContent(prompt)
40+
print(response.text ?? "No text in response.")
41+
}
42+
43+
func testTextOnlyStreaming() async throws {
44+
// Provide a prompt that contains text
45+
let prompt = "Write a story about a magic backpack."
46+
47+
// To stream generated text output, call generateContentStream with the text input
48+
let contentStream = try model.generateContentStream(prompt)
49+
for try await chunk in contentStream {
50+
if let text = chunk.text {
51+
print(text)
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)