|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.gemini; |
| 18 | + |
| 19 | +import com.google.genai.Client; |
| 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; |
| 25 | + |
| 26 | +import java.util.Collections; |
| 27 | + |
| 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 | + |
| 51 | + System.out.println(response.text()); |
| 52 | + // [END code_execution_basic] |
| 53 | + |
| 54 | + /* |
| 55 | + [START code_execution_basic_return] |
| 56 | + { |
| 57 | + text: '```python\n' + |
| 58 | + 'def is_prime(n):\n' + |
| 59 | + ' if n <= 1:\n' + |
| 60 | + ' return False\n' + |
| 61 | + ' for i in range(2, int(n**0.5) + 1):\n' + |
| 62 | + ' if n % i == 0:\n' + |
| 63 | + ' return False\n' + |
| 64 | + ' return True\n' + |
| 65 | + '\n' + |
| 66 | + 'sum_of_primes = 0\n' + |
| 67 | + 'count = 0\n' + |
| 68 | + 'num = 2\n' + |
| 69 | + 'while count < 50:\n' + |
| 70 | + ' if is_prime(num):\n' + |
| 71 | + ' sum_of_primes += num\n' + |
| 72 | + ' count += 1\n' + |
| 73 | + ' num += 1\n' + |
| 74 | + '\n' + |
| 75 | + 'print(sum_of_primes)\n' + |
| 76 | + '```\n' + |
| 77 | + '\n' + |
| 78 | + '```output\n' + |
| 79 | + '5117\n' + |
| 80 | + '```\n' |
| 81 | + } |
| 82 | + -------------------------------------------------------------------------------- |
| 83 | + ```python |
| 84 | + import math |
| 85 | +
|
| 86 | + def is_prime(n): |
| 87 | + """Checks if a number is prime.""" |
| 88 | + if n < 2: |
| 89 | + return False |
| 90 | + if n == 2: |
| 91 | + return True |
| 92 | + if n % 2 == 0: |
| 93 | + return False |
| 94 | + # Check only odd divisors up to the square root |
| 95 | + for i in range(3, int(math.sqrt(n)) + 1, 2): |
| 96 | + if n % i == 0: |
| 97 | + return False |
| 98 | + return True |
| 99 | +
|
| 100 | + count = 0 |
| 101 | + num = 2 |
| 102 | + prime_sum = 0 |
| 103 | + target_count = 50 |
| 104 | +
|
| 105 | + while count < target_count: |
| 106 | + if is_prime(num): |
| 107 | + prime_sum += num |
| 108 | + count += 1 |
| 109 | + num += 1 |
| 110 | +
|
| 111 | + print(prime_sum) |
| 112 | + ``` |
| 113 | +
|
| 114 | + Output: |
| 115 | + ``` |
| 116 | + 5117 |
| 117 | + ``` |
| 118 | + [END code_execution_basic_return] |
| 119 | + */ |
| 120 | + return response; |
| 121 | + } |
| 122 | + |
| 123 | + public static GenerateContentResponse CodeExecutionRequestOverride() throws Exception { |
| 124 | + // [START code_execution_request_override] |
| 125 | + Client client = new Client(); |
| 126 | + |
| 127 | + String prompt = """ |
| 128 | + What is the sum of the first 50 prime numbers? |
| 129 | + Generate and run code for the calculation, and make sure you get all 50. |
| 130 | + """; |
| 131 | + |
| 132 | + GenerateContentConfig config = |
| 133 | + GenerateContentConfig.builder() |
| 134 | + .tools(Collections.singletonList( |
| 135 | + Tool.builder() |
| 136 | + .codeExecution(ToolCodeExecution.builder().build()) |
| 137 | + .build() |
| 138 | + ) |
| 139 | + ).build(); |
| 140 | + |
| 141 | + GenerateContentResponse response = |
| 142 | + client.models.generateContent( |
| 143 | + "gemini-2.0-flash", |
| 144 | + prompt, |
| 145 | + config); |
| 146 | + |
| 147 | + System.out.println(response.executableCode()); |
| 148 | + System.out.println(response.codeExecutionResult()); |
| 149 | + // [END code_execution_request_override] |
| 150 | + |
| 151 | + /* |
| 152 | + [START code_execution_request_override_return] |
| 153 | + def is_prime(n): |
| 154 | + if n <= 1: |
| 155 | + return False |
| 156 | + if n <= 3: |
| 157 | + return True |
| 158 | + if n % 2 == 0 or n % 3 == 0: |
| 159 | + return False |
| 160 | + i = 5 |
| 161 | + while i * i <= n: |
| 162 | + if n % i == 0 or n % (i + 2) == 0: |
| 163 | + return False |
| 164 | + i += 6 |
| 165 | + return True |
| 166 | +
|
| 167 | + primes = [] |
| 168 | + num = 2 |
| 169 | + while len(primes) < 50: |
| 170 | + if is_prime(num): |
| 171 | + primes.append(num) |
| 172 | + num += 1 |
| 173 | +
|
| 174 | + print(f'{primes=}') |
| 175 | + print(f'{sum(primes)=}') |
| 176 | +
|
| 177 | + 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] |
| 178 | + sum(primes)=5117 |
| 179 | + [END code_execution_request_override_return] |
| 180 | + */ |
| 181 | + return response; |
| 182 | + } |
| 183 | +} |
0 commit comments