Skip to content

Commit 42d952a

Browse files
Ce rest (#495)
* Code execution for shell * Update code_execution.sh * Add code execution chat * Tested code execution examples * Use multi-round chat Change-Id: Ib11b69899c136550871c670b947f9cff0f480d6d --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent b5b20ed commit 42d952a

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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/rest/code_execution.sh

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,55 @@ echo "[START code_execution_basic]"
44
# [START code_execution_basic]
55
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \
66
-H 'Content-Type: application/json' \
7-
-d ' {
8-
"tools": [{"code_execution": {}}],
7+
-d ' {"tools": [{'code_execution': {}}],
98
"contents": {
10-
"parts": {
11-
"text": "What is the sum of the first 50 prime numbers? Generate
12-
and run code for the calculation, and make sure you get all 50."
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."
1312
}
14-
}
13+
},
1514
}'
1615
# [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)