17
17
package com .example .gemini ;
18
18
19
19
import com .google .genai .Client ;
20
- import com .google .genai .types .*;
21
- import org .apache .http .HttpException ;
20
+ import com .google .genai .types .GenerateContentConfig ;
21
+ import com .google .genai .types .GenerateContentResponse ;
22
+ import com .google .genai .types .Part ;
23
+ import com .google .genai .types .Tool ;
24
+ import com .google .genai .types .ToolCodeExecution ;
22
25
23
- import java .io .IOException ;
24
26
import java .util .Collections ;
25
27
26
- public class CodeExecutionRequestOverride {
27
- public static void main (String [] args ) throws IOException , HttpException {
28
+ @ SuppressWarnings ("resource" )
29
+ public class CodeExecution {
30
+ public static GenerateContentResponse codeExecutionBasic () throws Exception {
31
+ // [START code_execution_basic]
32
+ Client client = new Client ();
33
+
34
+ String prompt = """
35
+ Write and execute code that calculates the sum of the first 50 prime numbers.
36
+ Ensure that only the executable code and its resulting output are generated.
37
+ """ ;
38
+
39
+ GenerateContentResponse response =
40
+ client .models .generateContent (
41
+ "gemini-2.0-pro-exp-02-05" ,
42
+ prompt ,
43
+ null );
44
+
45
+ for (Part part : response .candidates ().get ().getFirst ().content ().get ().parts ().get ()) {
46
+ System .out .println (part + "\n " );
47
+ }
48
+
49
+ System .out .println ("-" .repeat (80 ));
50
+ System .out .println (response .text ());
51
+ // [END code_execution_basic]
52
+
53
+ /*
54
+ [START code_execution_basic_return]
55
+ Expected output:
56
+ --------------------------------------------------------------------------------
57
+ ```python
58
+ import math
59
+
60
+ def is_prime(n):
61
+ """Checks if a number is prime."""
62
+ if n < 2:
63
+ return False
64
+ if n == 2:
65
+ return True
66
+ if n % 2 == 0:
67
+ return False
68
+ # Check only odd divisors up to the square root
69
+ for i in range(3, int(math.sqrt(n)) + 1, 2):
70
+ if n % i == 0:
71
+ return False
72
+ return True
73
+
74
+ count = 0
75
+ num = 2
76
+ prime_sum = 0
77
+ target_count = 50
78
+
79
+ while count < target_count:
80
+ if is_prime(num):
81
+ prime_sum += num
82
+ count += 1
83
+ num += 1
84
+
85
+ print(prime_sum)
86
+ ```
87
+
88
+ Output:
89
+ ```
90
+ 5117
91
+ ```
92
+ [END code_execution_basic_return]
93
+ */
94
+ return response ;
95
+ }
96
+
97
+ public static GenerateContentResponse codeExecutionRequestOverride () throws Exception {
28
98
// [START code_execution_request_override]
29
99
Client client = new Client ();
30
100
@@ -42,13 +112,13 @@ public static void main(String[] args) throws IOException, HttpException {
42
112
)
43
113
).build ();
44
114
45
-
46
115
GenerateContentResponse response =
47
116
client .models .generateContent (
48
117
"gemini-2.0-flash" ,
49
118
prompt ,
50
119
config );
51
120
121
+ System .out .println ("-" .repeat (80 ));
52
122
System .out .println (response .executableCode ());
53
123
System .out .println (response .codeExecutionResult ());
54
124
// [END code_execution_request_override]
@@ -57,7 +127,6 @@ public static void main(String[] args) throws IOException, HttpException {
57
127
[START code_execution_request_override_return]
58
128
Expected output:
59
129
--------------------------------------------------------------------------------
60
-
61
130
def is_prime(n):
62
131
if n <= 1:
63
132
return False
@@ -84,8 +153,8 @@ def is_prime(n):
84
153
85
154
primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]
86
155
sum(primes)=5117
87
-
88
156
[END code_execution_request_override_return]
89
157
*/
158
+ return response ;
90
159
}
91
160
}
0 commit comments