Skip to content

Commit

Permalink
Move imports into samples
Browse files Browse the repository at this point in the history
Change-Id: I86550a09c6555a36679dad517ed4239f7ded90ff
  • Loading branch information
MarkDaoust committed Nov 26, 2024
1 parent 0e5c5f2 commit 938ed40
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 14 deletions.
15 changes: 14 additions & 1 deletion samples/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
from absl.testing import absltest

import google.generativeai as genai

import pathlib

Expand All @@ -24,6 +23,8 @@
class UnitTests(absltest.TestCase):
def test_cache_create(self):
# [START cache_create]
import google.generativeai as genai

document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
Expand All @@ -41,6 +42,8 @@ def test_cache_create(self):

def test_cache_create_from_name(self):
# [START cache_create_from_name]
import google.generativeai as genai

document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
Expand All @@ -60,6 +63,8 @@ def test_cache_create_from_name(self):

def test_cache_create_from_chat(self):
# [START cache_create_from_chat]
import google.generativeai as genai

model_name = "gemini-1.5-flash-001"
system_instruction = "You are an expert analyzing transcripts."

Expand Down Expand Up @@ -92,6 +97,8 @@ def test_cache_create_from_chat(self):

def test_cache_delete(self):
# [START cache_delete]
import google.generativeai as genai

document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
Expand All @@ -104,6 +111,8 @@ def test_cache_delete(self):

def test_cache_get(self):
# [START cache_get]
import google.generativeai as genai

document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
Expand All @@ -117,6 +126,8 @@ def test_cache_get(self):

def test_cache_list(self):
# [START cache_list]
import google.generativeai as genai

document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
Expand All @@ -132,6 +143,8 @@ def test_cache_list(self):

def test_cache_update(self):
# [START cache_update]
import google.generativeai as genai

import datetime

document = genai.upload_file(path=media / "a11.txt")
Expand Down
7 changes: 6 additions & 1 deletion samples/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
from absl.testing import absltest

import google.generativeai as genai
import pathlib

media = pathlib.Path(__file__).parents[1] / "third_party"
Expand All @@ -23,6 +22,8 @@
class UnitTests(absltest.TestCase):
def test_chat(self):
# [START chat]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat(
history=[
Expand All @@ -38,6 +39,8 @@ def test_chat(self):

def test_chat_streaming(self):
# [START chat_streaming]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat(
history=[
Expand All @@ -59,6 +62,8 @@ def test_chat_streaming(self):

def test_chat_streaming_with_images(self):
# [START chat_streaming_with_images]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()

Expand Down
13 changes: 12 additions & 1 deletion samples/code_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
# limitations under the License.
from absl.testing import absltest

import google.generativeai as genai


class UnitTests(absltest.TestCase):
def test_code_execution_basic(self):
# [START code_execution_basic]
import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution")
response = model.generate_content(
(
Expand All @@ -38,6 +39,8 @@ def test_code_execution_basic(self):
# [END code_execution_basic]

# [START code_execution_basic_return]
import google.generativeai as genai

# text: "I can help with that! To calculate the sum of the first 50 prime numbers, we\'ll need to first identify all the prime numbers up to the 50th prime number. \n\nHere is the code to find and sum the first 50 prime numbers:\n\n"
#
# executable_code {
Expand Down Expand Up @@ -92,6 +95,8 @@ def test_code_execution_basic(self):

def test_code_execution_request_override(self):
# [START code_execution_request_override]
import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content(
(
Expand All @@ -103,6 +108,8 @@ def test_code_execution_request_override(self):
print(response.text)
# [END code_execution_request_override]
# [START code_execution_request_override_return]
import google.generativeai as genai

# ``` python
# def is_prime(n):
# """
Expand Down Expand Up @@ -140,6 +147,8 @@ def test_code_execution_request_override(self):

def test_code_execution_chat(self):
# [START code_execution_chat]
import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash", tools="code_execution")
chat = model.start_chat()
response = chat.send_message('Can you print "Hello world!"?')
Expand All @@ -152,6 +161,8 @@ def test_code_execution_chat(self):
print(response.text)
# [END code_execution_chat]
# [START code_execution_chat_return]
import google.generativeai as genai

# ``` python
# def is_prime(n):
# """
Expand Down
3 changes: 2 additions & 1 deletion samples/configure_model_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
# limitations under the License.
from absl.testing import absltest

import google.generativeai as genai


class UnitTests(absltest.TestCase):
def test_configure_model(self):
# [START configure_model_parameters]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(
"Tell me a story about a magic backpack.",
Expand Down
15 changes: 14 additions & 1 deletion samples/controlled_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
from absl.testing import absltest
import pathlib

import google.generativeai as genai

media = pathlib.Path(__file__).parents[1] / "third_party"


class UnitTests(absltest.TestCase):
def test_json_controlled_generation(self):
# [START json_controlled_generation]
import google.generativeai as genai

import typing_extensions as typing

class Recipe(typing.TypedDict):
Expand All @@ -39,6 +40,8 @@ class Recipe(typing.TypedDict):

def test_json_no_schema(self):
# [START json_no_schema]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-pro-latest")
prompt = """List a few popular cookie recipes in JSON format.
Expand All @@ -52,6 +55,8 @@ def test_json_no_schema(self):

def test_json_enum(self):
# [START json_enum]
import google.generativeai as genai

import enum

class Choice(enum.Enum):
Expand All @@ -75,6 +80,8 @@ class Choice(enum.Enum):

def test_enum_in_json(self):
# [START enum_in_json]
import google.generativeai as genai

import enum
from typing_extensions import TypedDict

Expand Down Expand Up @@ -103,6 +110,8 @@ class Recipe(TypedDict):

def test_json_enum_raw(self):
# [START json_enum_raw]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-pro-latest")

organ = genai.upload_file(media / "organ.jpg")
Expand All @@ -121,6 +130,8 @@ def test_json_enum_raw(self):

def test_x_enum(self):
# [START x_enum]
import google.generativeai as genai

import enum

class Choice(enum.Enum):
Expand All @@ -144,6 +155,8 @@ class Choice(enum.Enum):

def test_x_enum_raw(self):
# [START x_enum_raw]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-pro-latest")

organ = genai.upload_file(media / "organ.jpg")
Expand Down
21 changes: 20 additions & 1 deletion samples/count_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
from absl.testing import absltest

import google.generativeai as genai
import pathlib

media = pathlib.Path(__file__).parents[1] / "third_party"
Expand All @@ -23,6 +22,8 @@
class UnitTests(absltest.TestCase):
def test_tokens_context_window(self):
# [START tokens_context_window]
import google.generativeai as genai

model_info = genai.get_model("models/gemini-1.5-flash")

# Returns the "context window" for the model,
Expand All @@ -34,6 +35,8 @@ def test_tokens_context_window(self):

def test_tokens_text_only(self):
# [START tokens_text_only]
import google.generativeai as genai

model = genai.GenerativeModel("models/gemini-1.5-flash")

prompt = "The quick brown fox jumps over the lazy dog."
Expand All @@ -54,6 +57,8 @@ def test_tokens_text_only(self):

def test_tokens_chat(self):
# [START tokens_chat]
import google.generativeai as genai

model = genai.GenerativeModel("models/gemini-1.5-flash")

chat = model.start_chat(
Expand Down Expand Up @@ -86,6 +91,8 @@ def test_tokens_chat(self):

def test_tokens_multimodal_image_inline(self):
# [START tokens_multimodal_image_inline]
import google.generativeai as genai

import PIL.Image

model = genai.GenerativeModel("models/gemini-1.5-flash")
Expand All @@ -112,6 +119,8 @@ def test_tokens_multimodal_image_inline(self):

def test_tokens_multimodal_image_file_api(self):
# [START tokens_multimodal_image_file_api]
import google.generativeai as genai

model = genai.GenerativeModel("models/gemini-1.5-flash")

prompt = "Tell me about this image"
Expand All @@ -136,6 +145,8 @@ def test_tokens_multimodal_image_file_api(self):

def test_tokens_multimodal_video_audio_file_api(self):
# [START tokens_multimodal_video_audio_file_api]
import google.generativeai as genai

import time

model = genai.GenerativeModel("models/gemini-1.5-flash")
Expand Down Expand Up @@ -169,6 +180,8 @@ def test_tokens_multimodal_video_audio_file_api(self):

def test_tokens_multimodal_pdf_file_api(self):
# [START tokens_multimodal_pdf_file_api]
import google.generativeai as genai

model = genai.GenerativeModel("gemini-1.5-flash")
sample_pdf = genai.upload_file(media / "test.pdf")
token_count = model.count_tokens(["Give me a summary of this document.", sample_pdf])
Expand All @@ -180,6 +193,8 @@ def test_tokens_multimodal_pdf_file_api(self):

def test_tokens_cached_content(self):
# [START tokens_cached_content]
import google.generativeai as genai

import time

model = genai.GenerativeModel("models/gemini-1.5-flash")
Expand Down Expand Up @@ -220,6 +235,8 @@ def test_tokens_cached_content(self):

def test_tokens_system_instruction(self):
# [START tokens_system_instruction]
import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash")

prompt = "The quick brown fox jumps over the lazy dog."
Expand All @@ -239,6 +256,8 @@ def test_tokens_system_instruction(self):

def test_tokens_tools(self):
# [START tokens_tools]
import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash")

prompt = "I have 57 cats, each owns 44 mittens, how many mittens is that in total?"
Expand Down
5 changes: 4 additions & 1 deletion samples/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from absl.testing import absltest


import google.generativeai as genai


class UnitTests(absltest.TestCase):
def test_embed_content(self):
# [START embed_content]
import google.generativeai as genai


text = "Hello World!"
result = genai.embed_content(
Expand All @@ -31,6 +32,8 @@ def test_embed_content(self):

def batch_embed_contents(self):
# [START batch_embed_contents]
import google.generativeai as genai

texts = [
"What is the meaning of life?",
"How much wood would a woodchuck chuck?",
Expand Down
Loading

0 comments on commit 938ed40

Please sign in to comment.