Skip to content

Commit 9d2f9ea

Browse files
authored
Add chat samples (#192)
Add samples with the standardized prompts for chatting with text and images. One difference from the canonical sample is that the image is uploaded directly as a data part instead of uploaded as a file, since the Dart SDK does not support uploading files.
1 parent 37f0c6d commit 9d2f9ea

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

Diff for: samples/dart/bin/chat.dart

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 'dart:io';
16+
17+
import 'package:google_generative_ai/google_generative_ai.dart';
18+
19+
final apiKey = () {
20+
final apiKey = Platform.environment['GEMINI_API_KEY'];
21+
if (apiKey == null) {
22+
stderr.writeln(r'No $GEMINI_API_KEY environment variable');
23+
exit(1);
24+
}
25+
return apiKey;
26+
}();
27+
28+
Future<void> chat() async {
29+
// [START chat]
30+
final model = GenerativeModel(
31+
model: 'gemini-1.5-flash',
32+
apiKey: apiKey,
33+
);
34+
final chat = model.startChat(history: [
35+
Content.text('hello'),
36+
Content.model([TextPart('Great to meet you. What would you like to know?')])
37+
]);
38+
var response =
39+
await chat.sendMessage(Content.text('I have 2 dogs in my house.'));
40+
print(response.text);
41+
response =
42+
await chat.sendMessage(Content.text('How many paws are in my house?'));
43+
print(response.text);
44+
// [END chat]
45+
}
46+
47+
Future<void> chatStreaming() async {
48+
// [START chat_streaming]
49+
final model = GenerativeModel(
50+
model: 'gemini-1.5-flash',
51+
apiKey: apiKey,
52+
);
53+
final chat = model.startChat(history: [
54+
Content.text('hello'),
55+
Content.model([TextPart('Great to meet you. What would you like to know?')])
56+
]);
57+
var responses =
58+
chat.sendMessageStream(Content.text('I have 2 dogs in my house.'));
59+
await for (final response in responses) {
60+
print(response.text);
61+
print('_' * 80);
62+
}
63+
responses =
64+
chat.sendMessageStream(Content.text('How many paws are in my house?'));
65+
await for (final response in responses) {
66+
print(response.text);
67+
print('_' * 80);
68+
}
69+
// [END chat_streaming]
70+
}
71+
72+
Future<void> chatStreamingWithImages() async {
73+
// [START chat_streaming_with_images]
74+
final model = GenerativeModel(
75+
model: 'gemini-1.5-flash',
76+
apiKey: apiKey,
77+
);
78+
Future<DataPart> fileToPart(String mimeType, String path) async {
79+
return DataPart(mimeType, await File(path).readAsBytes());
80+
}
81+
82+
final chat = model.startChat();
83+
var responses = chat.sendMessageStream(Content.text(
84+
"Hello, I'm interested in learning about musical instruments. "
85+
'Can I show you one?'));
86+
await for (final response in responses) {
87+
print(response.text);
88+
print('_' * 80);
89+
}
90+
final prompt = 'What family of instruments does this belong to?';
91+
final image = await fileToPart('image/jpeg', 'resources/organ.jpg');
92+
responses = chat.sendMessageStream(Content.multi([TextPart(prompt), image]));
93+
await for (final response in responses) {
94+
print(response.text);
95+
print('_' * 80);
96+
}
97+
// [END chat_streaming_with_images]
98+
}
99+
100+
void main() async {
101+
await chat();
102+
await chatStreaming();
103+
await chatStreamingWithImages();
104+
}

Diff for: samples/dart/resources/organ.jpg

375 KB
Loading

0 commit comments

Comments
 (0)