Skip to content
Closed
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
8 changes: 6 additions & 2 deletions samples/text_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
class UnitTests(absltest.TestCase):
def test_text_gen_text_only_prompt(self):
# [START text_gen_text_only_prompt]
import google.generativeai as genai
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could require the import in each sample.

The trade-off is just signal-to-noise ratio.

If we expect each sample to be runnable with just a copy-paste from the site.. some things become difficult: A sample for tunedModel.delete requires that you train a tuned model first, that's a case where the signal to noise ratio is really bad.

import os

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the API key is set as an environment variable the SDK will automatically pick it up. Do we still want this?

model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.")
response = model.generate_content("Explain how AI works")
print(response.text)
# [END text_gen_text_only_prompt]

def test_text_gen_text_only_prompt_streaming(self):
# [START text_gen_text_only_prompt_streaming]
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.", stream=True)
response = model.generate_content("Explain how AI works", stream=True)
for chunk in response:
print(chunk.text)
print("_" * 80)
Expand Down
Loading