Skip to content

Commit 81aaf35

Browse files
Add samples for chat (#408)
* Add samples for chat * Update chat sample to be more than one round * multiple send_message calls. --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent ebfad33 commit 81aaf35

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

samples/chat.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
from absl.testing import absltest
16+
17+
import google.generativeai as genai
18+
import pathlib
19+
20+
media = pathlib.Path(__file__).parents[1] / "third_party"
21+
22+
23+
class UnitTests(absltest.TestCase):
24+
def test_chat(self):
25+
# [START chat]
26+
model = genai.GenerativeModel("gemini-1.5-flash")
27+
chat = model.start_chat(
28+
history=[
29+
{"role": "user", "parts": "Hello"},
30+
{"role": "model", "parts": "Great to meet you. What would you like to know?"},
31+
]
32+
)
33+
response = chat.send_message("I have 2 dogs in my house.")
34+
print(response.text)
35+
response = chat.send_message("How many paws are in my house?")
36+
print(response.text)
37+
# [END chat]
38+
39+
def test_chat_streaming(self):
40+
# [START chat_streaming]
41+
model = genai.GenerativeModel("gemini-1.5-flash")
42+
chat = model.start_chat(
43+
history=[
44+
{"role": "user", "parts": "Hello"},
45+
{"role": "model", "parts": "Great to meet you. What would you like to know?"},
46+
]
47+
)
48+
response = chat.send_message("I have 2 dogs in my house.", stream=True)
49+
for chunk in response:
50+
print(chunk.text)
51+
print("_" * 80)
52+
response = chat.send_message("How many paws are in my house?", stream=True)
53+
for chunk in response:
54+
print(chunk.text)
55+
print("_" * 80)
56+
57+
print(chat.history)
58+
# [END chat_streaming]
59+
60+
def test_chat_streaming_with_images(self):
61+
# [START chat_streaming_with_images]
62+
model = genai.GenerativeModel("gemini-1.5-flash")
63+
chat = model.start_chat()
64+
65+
response = chat.send_message("Hello, I'm interested in learning about musical instruments. Can I show you one?", stream=True)
66+
for chunk in response:
67+
print(chunk.text) # Yes.
68+
print("_" * 80)
69+
70+
organ = genai.upload_file(media / "organ.jpg")
71+
response = chat.send_message(
72+
["What family of intruments does this instrument belong to?", organ], stream=True
73+
)
74+
for chunk in response:
75+
print(chunk.text)
76+
print("_" * 80)
77+
# [END chat_streaming_with_images]
78+
79+
80+
if __name__ == "__main__":
81+
absltest.main()

0 commit comments

Comments
 (0)