Skip to content

Add no json schema sample #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions samples/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_chat(self):
]
)
response = chat.send_message("I have 2 dogs in my house.")
print(response.text)
print(response.text)
response = chat.send_message("How many paws are in my house?")
print(response.text)
# [END chat]
Expand Down Expand Up @@ -62,11 +62,14 @@ def test_chat_streaming_with_images(self):
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()

response = chat.send_message("Hello, I'm interested in learning about musical instruments. Can I show you one?", stream=True)
response = chat.send_message(
"Hello, I'm interested in learning about musical instruments. Can I show you one?",
stream=True,
)
for chunk in response:
print(chunk.text) # Yes.
print("_" * 80)

organ = genai.upload_file(media / "organ.jpg")
response = chat.send_message(
["What family of intruments does this instrument belong to?", organ], stream=True
Expand Down
17 changes: 14 additions & 3 deletions samples/controlled_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@


class UnitTests(absltest.TestCase):
def test_controlled_generation(self):
# [START controlled_generation]
def test_json_controlled_generation(self):
# [START json_controlled_generation]
import typing_extensions as typing

class Recipe(typing.TypedDict):
Expand All @@ -31,7 +31,18 @@ class Recipe(typing.TypedDict):
),
)
print(result)
# [END controlled_generation]
# [END json_controlled_generation]

def test_json_no_schema(self):
# [START json_no_schema]
model = genai.GenerativeModel("gemini-1.5-pro-latest")
prompt = """List a few popular cookie recipes using this JSON schema:

Recipe = {'recipe_name': str}
Return: list[Recipe]"""
result = model.generate_content(prompt)
print(result)
# [END json_no_schema]


if __name__ == "__main__":
Expand Down
Loading