Skip to content

Commit 67321eb

Browse files
Merge branch 'main' into files_count_tokens_rest
2 parents 7c19d7e + db311dd commit 67321eb

File tree

7 files changed

+199
-7
lines changed

7 files changed

+199
-7
lines changed

.github/workflows/samples.yaml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Validate samples
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize] # new, updates
6+
7+
jobs:
8+
update-python-list:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout Code
13+
uses: actions/checkout@v3
14+
15+
- name: Get Changed Files
16+
id: changed_files
17+
uses: tj-actions/changed-files@v44
18+
with:
19+
files: |
20+
samples/*.py
21+
22+
- name: Check Python samples
23+
env:
24+
NEW_FILES: ${{ steps.changed_files.outputs.all_modified_files }}
25+
README: samples/README.md
26+
run: |
27+
#!/bin/bash
28+
29+
for file in ${NEW_FILES}; do
30+
echo "Testing $file"
31+
if [[ -f ${file} ]]; then
32+
# File exists, so needs to be listed.
33+
if ! grep -q $name ${README}; then
34+
echo "Error: Sample not listed in README ($name)"
35+
exit 1
36+
fi
37+
else
38+
# File does not exist, ensure it's not listed
39+
if grep -q $name ${README}; then
40+
echo "Error: Sample should not be listed in README ($name)"
41+
exit 1
42+
fi
43+
fi
44+
done
45+
46+
update-rest-list:
47+
runs-on: ubuntu-latest
48+
49+
steps:
50+
- name: Checkout Code
51+
uses: actions/checkout@v3
52+
53+
- name: Get Changed Files
54+
id: changed_files
55+
uses: tj-actions/changed-files@v44
56+
with:
57+
files: |
58+
samples/rest/*.sh
59+
60+
- name: Check REST samples
61+
env:
62+
NEW_FILES: ${{ steps.changed_files.outputs.all_modified_files }}
63+
README: samples/rest/README.md
64+
run: |
65+
#!/bin/bash
66+
67+
for file in ${NEW_FILES}; do
68+
echo "Testing $file"
69+
if [[ -f ${file} ]]; then
70+
# File exists, so needs to be listed.
71+
if ! grep -q $name ${README}; then
72+
echo "Error: Sample not listed in README ($name)"
73+
exit 1
74+
fi
75+
else
76+
# File does not exist, ensure it's not listed
77+
if grep -q $name ${README}; then
78+
echo "Error: Sample should not be listed in README ($name)"
79+
exit 1
80+
fi
81+
fi
82+
done

google/generativeai/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242

4343
from google.generativeai import version
4444

45+
from google.generativeai import caching
4546
from google.generativeai import protos
4647
from google.generativeai import types
47-
from google.generativeai.types import GenerationConfig
4848

49+
from google.generativeai.client import configure
4950

5051
from google.generativeai.discuss import chat
5152
from google.generativeai.discuss import chat_async
@@ -62,10 +63,6 @@
6263
from google.generativeai.generative_models import GenerativeModel
6364
from google.generativeai.generative_models import ChatSession
6465

65-
from google.generativeai.text import generate_text
66-
from google.generativeai.text import generate_embeddings
67-
from google.generativeai.text import count_text_tokens
68-
6966
from google.generativeai.models import list_models
7067
from google.generativeai.models import list_tuned_models
7168

@@ -80,8 +77,11 @@
8077
from google.generativeai.operations import list_operations
8178
from google.generativeai.operations import get_operation
8279

80+
from google.generativeai.text import generate_text
81+
from google.generativeai.text import generate_embeddings
82+
from google.generativeai.text import count_text_tokens
8383

84-
from google.generativeai.client import configure
84+
from google.generativeai.types import GenerationConfig
8585

8686
__version__ = version.__version__
8787

samples/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Gemini API Python SDK sample code
2+
3+
This directory contains sample code for key features of the SDK, organised by high level feature.
4+
5+
These samples are embedded in parts of the [documentation](https://ai.google.dev), most notably in the [API reference](https://ai.google.dev/api).
6+
7+
Each file is structured as a runnable test case, ensuring that samples are executable and functional. Each test demonstrates a single concept, and contains region tags that are used to demarcate the test scaffolding from the spotlight code. If you are contributing, code within region tags should follow sample code best practices - being clear, complete and concise.
8+
9+
## Contents
10+
11+
| File | Description |
12+
| ---- | ----------- |
13+
| [cache.py](./cache.py) | Context caching |
14+
| [chat.py](./chat.py) | Multi-turn chat conversations |
15+
| [code_execution.py](./code_execution.py) | Executing code |
16+
| [configure_model_parameters.py](./configure_model_parameters.py) | Setting model parameters |
17+
| [controlled_generation.py](./controlled_generation.py) | Generating content with output constraints (e.g. JSON mode) |
18+
| [count_tokens.py](./count_tokens.py) | Counting input and output tokens |
19+
| [embed.py](./embed.py) | Generating embeddings |
20+
| [files.py](./files.py) | Managing files with the File API |
21+
| [function_calling.py](./function_calling.py) | Using function calling |
22+
| [models.py](./models.py) | Listing models and model metadata |
23+
| [safety_settings.py](./safety_settings.py) | Setting and using safety controls |
24+
| [system_instruction.py](./system_instruction.py) | Setting system instructions |
25+
| [text_generation.py](./text_generation.py) | Generating text |
26+
| [tuned_models.py](./tuned_models.py) | Creating and managing tuned models |

samples/code_execution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def test_code_execution_chat(self):
142142
# [START code_execution_chat]
143143
model = genai.GenerativeModel(model_name="gemini-1.5-pro", tools="code_execution")
144144
chat = model.start_chat()
145+
response = chat.send_message('Can you print "Hello world!"?')
145146
response = chat.send_message(
146147
(
147148
"What is the sum of the first 50 prime numbers? "

samples/controlled_generation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Recipe(typing.TypedDict):
2727
result = model.generate_content(
2828
"List a few popular cookie recipes.",
2929
generation_config=genai.GenerationConfig(
30-
response_mime_type="application/json", response_schema=list([Recipe])
30+
response_mime_type="application/json", response_schema=list[Recipe]
3131
),
3232
)
3333
print(result)

samples/rest/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Gemini API REST sample code
2+
3+
This directory contains sample code for key features of the API, organised by high level feature.
4+
5+
These samples are embedded in parts of the [documentation](https://ai.google.dev), most notably in the [API reference](https://ai.google.dev/api).
6+
7+
Each file is structured as a runnable script, ensuring that samples are executable and functional. Each filee contains region tags that are used to demarcate the script from the spotlight code. If you are contributing, code within region tags should follow sample code best practices - being clear, complete and concise.
8+
9+
## Contents
10+
11+
| File | Description |
12+
| ---- | ----------- |
13+
| [cache.sh](./cache.sh) | Context caching |
14+
| [chat.sh](./chat.sh) | Multi-turn chat conversations |
15+
| [code_execution.sh](./code_execution.sh) | Executing code |
16+
| [configure_model_parameters.sh](./configure_model_parameters.sh) | Setting model parameters |
17+
| [controlled_generation.sh](./controlled_generation.sh) | Generating content with output constraints (e.g. JSON mode) |
18+
| [count_tokens.sh](./count_tokens.sh) | Counting input and output tokens |
19+
| [embed.sh](./embed.sh) | Generating embeddings |
20+
| [files.sh](./files.sh) | Managing files with the File API |
21+
| [function_calling.sh](./function_calling.sh) | Using function calling |
22+
| [models.sh](./models.sh) | Listing models and model metadata |
23+
| [safety_settings.sh](./safety_settings.sh) | Setting and using safety controls |
24+
| [system_instruction.sh](./system_instruction.sh) | Setting system instructions |
25+
| [text_generation.sh](./text_generation.sh) | Generating text |

samples/rest/code_execution.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
set -eu
2+
3+
echo "[START code_execution_basic]"
4+
# [START code_execution_basic]
5+
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \
6+
-H 'Content-Type: application/json' \
7+
-d ' {"tools": [{'code_execution': {}}],
8+
"contents": {
9+
"parts":
10+
{
11+
"text": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50."
12+
}
13+
},
14+
}'
15+
# [END code_execution_basic]
16+
17+
echo "[START code_execution_chat]"
18+
# [START code_execution_chat]
19+
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \
20+
-H 'Content-Type: application/json' \
21+
-d '{"tools": [{'code_execution': {}}],
22+
"contents": [
23+
{
24+
"role": "user",
25+
"parts": [{
26+
"text": "Can you print \"Hello world!\"?"
27+
}]
28+
},{
29+
"role": "model",
30+
"parts": [
31+
{
32+
"text": ""
33+
},
34+
{
35+
"executable_code": {
36+
"language": "PYTHON",
37+
"code": "\nprint(\"hello world!\")\n"
38+
}
39+
},
40+
{
41+
"code_execution_result": {
42+
"outcome": "OUTCOME_OK",
43+
"output": "hello world!\n"
44+
}
45+
},
46+
{
47+
"text": "I have printed \"hello world!\" using the provided python code block. \n"
48+
}
49+
],
50+
},{
51+
"role": "user",
52+
"parts": [{
53+
"text": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50."
54+
}]
55+
}
56+
]
57+
}'
58+
# [END code_execution_chat]

0 commit comments

Comments
 (0)