From edc6fa0cf739452f037969485c553525967b23f3 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Wed, 21 Aug 2024 12:41:10 -0700 Subject: [PATCH 1/4] Use flash more often. Change-Id: If20e5d5e8462d160681d9dc2bfec965fd94fb633 --- README.md | 2 +- samples/code_execution.py | 6 ++--- samples/count_tokens.py | 6 ++--- samples/rest/code_execution.sh | 4 ++-- samples/rest/controlled_generation.sh | 4 ++-- samples/rest/models.sh | 2 +- samples/rest/safety_settings.sh | 32 ++++++++++++--------------- samples/rest/system_instruction.sh | 2 +- samples/tuned_models.py | 4 ++-- 9 files changed, 29 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 99d387bd7..19db267de 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ genai.configure(api_key=os.environ["GEMINI_API_KEY"]) 3. Create a model and run a prompt. ```python -model = genai.GenerativeModel('gemini-1.0-pro-latest') +model = genai.GenerativeModel('gemini-1.5-flash') response = model.generate_content("The opposite of hot is") print(response.text) ``` diff --git a/samples/code_execution.py b/samples/code_execution.py index 019c6b344..9b5ad3638 100644 --- a/samples/code_execution.py +++ b/samples/code_execution.py @@ -29,7 +29,7 @@ def test_code_execution_basic(self): ) # Each `part` either contains `text`, `executable_code` or an `execution_result` - for part in result.candidates[0].content.parts: + for part in response.candidates[0].content.parts: print(part, "\n") print("-" * 80) @@ -92,7 +92,7 @@ def test_code_execution_basic(self): def test_code_execution_request_override(self): # [START code_execution_request_override] - model = genai.GenerativeModel(model_name="gemini-1.5-pro") + model = genai.GenerativeModel(model_name="gemini-1.5-flash") response = model.generate_content( ( "What is the sum of the first 50 prime numbers? " @@ -140,7 +140,7 @@ def test_code_execution_request_override(self): def test_code_execution_chat(self): # [START code_execution_chat] - model = genai.GenerativeModel(model_name="gemini-1.5-pro", tools="code_execution") + 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!"?') response = chat.send_message( diff --git a/samples/count_tokens.py b/samples/count_tokens.py index 74a9e4881..50b9d5516 100644 --- a/samples/count_tokens.py +++ b/samples/count_tokens.py @@ -23,7 +23,7 @@ class UnitTests(absltest.TestCase): def test_tokens_context_window(self): # [START tokens_context_window] - model_info = genai.get_model("models/gemini-1.0-pro-001") + model_info = genai.get_model("models/gemini-1.5-flash") # Returns the "context window" for the model, # which is the combined input and output token limits. @@ -91,7 +91,7 @@ def test_tokens_multimodal_image_inline(self): model = genai.GenerativeModel("models/gemini-1.5-flash") prompt = "Tell me about this image" - your_image_file = PIL.Image.open("image.jpg") + your_image_file = PIL.Image.open(media/"organ.jpg") # Call `count_tokens` to get the input token count # of the combined text and file (`total_tokens`). @@ -115,7 +115,7 @@ def test_tokens_multimodal_image_file_api(self): model = genai.GenerativeModel("models/gemini-1.5-flash") prompt = "Tell me about this image" - your_image_file = genai.upload_file(path="image.jpg") + your_image_file = genai.upload_file(path=media/"organ.jpg") # Call `count_tokens` to get the input token count # of the combined text and file (`total_tokens`). diff --git a/samples/rest/code_execution.sh b/samples/rest/code_execution.sh index e7efe2e1f..73c297777 100644 --- a/samples/rest/code_execution.sh +++ b/samples/rest/code_execution.sh @@ -2,7 +2,7 @@ set -eu echo "[START code_execution_basic]" # [START code_execution_basic] -curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -d ' {"tools": [{'code_execution': {}}], "contents": { @@ -16,7 +16,7 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-lat echo "[START code_execution_chat]" # [START code_execution_chat] -curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"tools": [{'code_execution': {}}], "contents": [ diff --git a/samples/rest/controlled_generation.sh b/samples/rest/controlled_generation.sh index 69da2dac7..533870649 100644 --- a/samples/rest/controlled_generation.sh +++ b/samples/rest/controlled_generation.sh @@ -2,7 +2,7 @@ set -eu echo "json_controlled_generation" # [START json_controlled_generation] -curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "contents": [{ @@ -27,7 +27,7 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-lat echo "json_no_schema" # [START json_no_schema] -curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "contents": [{ diff --git a/samples/rest/models.sh b/samples/rest/models.sh index ebcd378ff..a03d5585b 100644 --- a/samples/rest/models.sh +++ b/samples/rest/models.sh @@ -7,5 +7,5 @@ curl https://generativelanguage.googleapis.com/v1beta/models?key=$GOOGLE_API_KEY echo "[START models_get]" # [START models_get] -curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro?key=$GOOGLE_API_KEY +curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash?key=$GOOGLE_API_KEY # [END models_get] diff --git a/samples/rest/safety_settings.sh b/samples/rest/safety_settings.sh index f7eb45186..713d25c06 100644 --- a/samples/rest/safety_settings.sh +++ b/samples/rest/safety_settings.sh @@ -2,37 +2,33 @@ set -eu echo "[START safety_settings]" # [START safety_settings] -echo '{ + echo '{ "safetySettings": [ - {'category': HARM_CATEGORY_HARASSMENT, 'threshold': BLOCK_ONLY_HIGH} + {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"} ], "contents": [{ "parts":[{ "text": "'I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them.'"}]}]}' > request.json - curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GOOGLE_API_KEY" \ + curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ - -d @request.json 2> /dev/null > tee response.json - - jq .promptFeedback > response.json + -d @request.json 2> /dev/null # [END safety_settings] echo "[START safety_settings_multi]" # [START safety_settings_multi] -echo '{ - "safetySettings": [ - {'category': HARM_CATEGORY_HARASSMENT, 'threshold': BLOCK_ONLY_HIGH}, - {'category': HARM_CATEGORY_HATE_SPEECH, 'threshold': BLOCK_MEDIUM_AND_ABOVE} - ], - "contents": [{ - "parts":[{ - "text": "'I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them.'"}]}]}' > request.json + echo '{ + "safetySettings": [ + {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"}, + {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"} + ], + "contents": [{ + "parts":[{ + "text": "'I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them.'"}]}]}' > request.json - curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GOOGLE_API_KEY" \ + curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ - -d @request.json 2> /dev/null > response.json - - jq .promptFeedback > response.json + -d @request.json 2> /dev/null # [END safety_settings_multi] diff --git a/samples/rest/system_instruction.sh b/samples/rest/system_instruction.sh index 6a32c8f58..1e4c36d6c 100644 --- a/samples/rest/system_instruction.sh +++ b/samples/rest/system_instruction.sh @@ -2,7 +2,7 @@ set -eu echo "[START system_instruction]" # [START system_instruction] -curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "system_instruction": { "parts": diff --git a/samples/tuned_models.py b/samples/tuned_models.py index d328d8c30..8ecad73de 100644 --- a/samples/tuned_models.py +++ b/samples/tuned_models.py @@ -27,7 +27,7 @@ def test_tuned_models_create(self): # [START tuned_models_create] import time - base_model = "models/gemini-1.0-pro-001" + base_model = "models/gemini-1.5-flash-001-tuning" training_data = [ {"text_input": "1", "output": "2"}, # ... more examples ... @@ -94,7 +94,7 @@ def test_tuned_models_list(self): def test_tuned_models_delete(self): import time - base_model = "models/gemini-1.0-pro-001" + base_model = "models/gemini-1.5-flash-001-tuning" training_data = samples / "increment_tuning_data.json" try: operation = genai.create_tuned_model( From 203a4a4d40451ba7ade2a8d236fc0f425c20dafa Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 22 Aug 2024 10:58:41 -0700 Subject: [PATCH 2/4] format Change-Id: I5a47b80da6f07b26a8079e33b2350ace3454bb50 --- samples/count_tokens.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/count_tokens.py b/samples/count_tokens.py index 50b9d5516..4d52684d9 100644 --- a/samples/count_tokens.py +++ b/samples/count_tokens.py @@ -91,7 +91,7 @@ def test_tokens_multimodal_image_inline(self): model = genai.GenerativeModel("models/gemini-1.5-flash") prompt = "Tell me about this image" - your_image_file = PIL.Image.open(media/"organ.jpg") + your_image_file = PIL.Image.open(media / "organ.jpg") # Call `count_tokens` to get the input token count # of the combined text and file (`total_tokens`). @@ -115,7 +115,7 @@ def test_tokens_multimodal_image_file_api(self): model = genai.GenerativeModel("models/gemini-1.5-flash") prompt = "Tell me about this image" - your_image_file = genai.upload_file(path=media/"organ.jpg") + your_image_file = genai.upload_file(path=media / "organ.jpg") # Call `count_tokens` to get the input token count # of the combined text and file (`total_tokens`). From 63dca1fb7d4b122d88ce46117551c6b5c8a864e8 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 22 Aug 2024 12:51:25 -0700 Subject: [PATCH 3/4] fix link Change-Id: If23c8b48e53238c99d71d68f3669e521a5a82c2f --- samples/README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/samples/README.md b/samples/README.md index cc56cda75..82f63364d 100644 --- a/samples/README.md +++ b/samples/README.md @@ -8,19 +8,19 @@ Each file is structured as a runnable test case, ensuring that samples are execu ## Contents -| File | Description | -| ---- | ----------- | -| [cache.py](./cache.py) | Context caching | -| [chat.py](./chat.py) | Multi-turn chat conversations | -| [code_execution.py](./code_execution.py) | Executing code | +| File | Description | +|------------------------------------------------------------------| ----------- | +| [cache.py](./cache.py) | Context caching | +| [chat.py](./chat.py) | Multi-turn chat conversations | +| [code_execution.py](./samples/code_execution.py) | Executing code | | [configure_model_parameters.py](./configure_model_parameters.py) | Setting model parameters | -| [controlled_generation.py](./controlled_generation.py) | Generating content with output constraints (e.g. JSON mode) | -| [count_tokens.py](./count_tokens.py) | Counting input and output tokens | -| [embed.py](./embed.py) | Generating embeddings | -| [files.py](./files.py) | Managing files with the File API | -| [function_calling.py](./function_calling.py) | Using function calling | -| [models.py](./models.py) | Listing models and model metadata | -| [safety_settings.py](./safety_settings.py) | Setting and using safety controls | -| [system_instruction.py](./system_instruction.py) | Setting system instructions | -| [text_generation.py](./text_generation.py) | Generating text | -| [tuned_models.py](./tuned_models.py) | Creating and managing tuned models | +| [controlled_generation.py](./controlled_generation.py) | Generating content with output constraints (e.g. JSON mode) | +| [count_tokens.py](./count_tokens.py) | Counting input and output tokens | +| [embed.py](./embed.py) | Generating embeddings | +| [files.py](./files.py) | Managing files with the File API | +| [function_calling.py](./function_calling.py) | Using function calling | +| [models.py](./models.py) | Listing models and model metadata | +| [safety_settings.py](./safety_settings.py) | Setting and using safety controls | +| [system_instruction.py](./system_instruction.py) | Setting system instructions | +| [text_generation.py](./text_generation.py) | Generating text | +| [tuned_models.py](./tuned_models.py) | Creating and managing tuned models | From a239c8968dfeffcfb9a101c09dfb427db7e75fc0 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 22 Aug 2024 12:56:48 -0700 Subject: [PATCH 4/4] fix check Change-Id: Iac2f1bd545395949cf013fc2fbc6c91716766d1d --- .github/workflows/samples.yaml | 1 + samples/README.md | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index 71c457cfa..04f2611aa 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -28,6 +28,7 @@ jobs: for file in ${NEW_FILES}; do echo "Testing $file" + name=$(basename $file) if [[ -f ${file} ]]; then # File exists, so needs to be listed. if ! grep -q $name ${README}; then diff --git a/samples/README.md b/samples/README.md index 82f63364d..ce2e9d243 100644 --- a/samples/README.md +++ b/samples/README.md @@ -8,19 +8,19 @@ Each file is structured as a runnable test case, ensuring that samples are execu ## Contents -| File | Description | -|------------------------------------------------------------------| ----------- | -| [cache.py](./cache.py) | Context caching | -| [chat.py](./chat.py) | Multi-turn chat conversations | -| [code_execution.py](./samples/code_execution.py) | Executing code | +| File | Description | +|----------------------------------------------------------| ----------- | +| [cache.py](./cache.py) | Context caching | +| [chat.py](./chat.py) | Multi-turn chat conversations | +| [code_execution.py](./code_execution.py) | Executing code | | [configure_model_parameters.py](./configure_model_parameters.py) | Setting model parameters | -| [controlled_generation.py](./controlled_generation.py) | Generating content with output constraints (e.g. JSON mode) | -| [count_tokens.py](./count_tokens.py) | Counting input and output tokens | -| [embed.py](./embed.py) | Generating embeddings | -| [files.py](./files.py) | Managing files with the File API | -| [function_calling.py](./function_calling.py) | Using function calling | -| [models.py](./models.py) | Listing models and model metadata | -| [safety_settings.py](./safety_settings.py) | Setting and using safety controls | -| [system_instruction.py](./system_instruction.py) | Setting system instructions | -| [text_generation.py](./text_generation.py) | Generating text | -| [tuned_models.py](./tuned_models.py) | Creating and managing tuned models | +| [controlled_generation.py](./controlled_generation.py) | Generating content with output constraints (e.g. JSON mode) | +| [count_tokens.py](./count_tokens.py) | Counting input and output tokens | +| [embed.py](./embed.py) | Generating embeddings | +| [files.py](./files.py) | Managing files with the File API | +| [function_calling.py](./function_calling.py) | Using function calling | +| [models.py](./models.py) | Listing models and model metadata | +| [safety_settings.py](./safety_settings.py) | Setting and using safety controls | +| [system_instruction.py](./system_instruction.py) | Setting system instructions | +| [text_generation.py](./text_generation.py) | Generating text | +| [tuned_models.py](./tuned_models.py) | Creating and managing tuned models |