Skip to content

Commit a3c963b

Browse files
Add no json schema sample (#416)
1 parent 05bfb7f commit a3c963b

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

samples/chat.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_chat(self):
3131
]
3232
)
3333
response = chat.send_message("I have 2 dogs in my house.")
34-
print(response.text)
34+
print(response.text)
3535
response = chat.send_message("How many paws are in my house?")
3636
print(response.text)
3737
# [END chat]
@@ -62,11 +62,14 @@ def test_chat_streaming_with_images(self):
6262
model = genai.GenerativeModel("gemini-1.5-flash")
6363
chat = model.start_chat()
6464

65-
response = chat.send_message("Hello, I'm interested in learning about musical instruments. Can I show you one?", stream=True)
65+
response = chat.send_message(
66+
"Hello, I'm interested in learning about musical instruments. Can I show you one?",
67+
stream=True,
68+
)
6669
for chunk in response:
6770
print(chunk.text) # Yes.
6871
print("_" * 80)
69-
72+
7073
organ = genai.upload_file(media / "organ.jpg")
7174
response = chat.send_message(
7275
["What family of intruments does this instrument belong to?", organ], stream=True

samples/controlled_generation.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717

1818
class UnitTests(absltest.TestCase):
19-
def test_controlled_generation(self):
20-
# [START controlled_generation]
19+
def test_json_controlled_generation(self):
20+
# [START json_controlled_generation]
2121
import typing_extensions as typing
2222

2323
class Recipe(typing.TypedDict):
@@ -31,7 +31,18 @@ class Recipe(typing.TypedDict):
3131
),
3232
)
3333
print(result)
34-
# [END controlled_generation]
34+
# [END json_controlled_generation]
35+
36+
def test_json_no_schema(self):
37+
# [START json_no_schema]
38+
model = genai.GenerativeModel("gemini-1.5-pro-latest")
39+
prompt = """List a few popular cookie recipes using this JSON schema:
40+
41+
Recipe = {'recipe_name': str}
42+
Return: list[Recipe]"""
43+
result = model.generate_content(prompt)
44+
print(result)
45+
# [END json_no_schema]
3546

3647

3748
if __name__ == "__main__":

0 commit comments

Comments
 (0)