Skip to content

Java test cases #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Java examples

This directory contains examples of working with the Gemini API using the
[Google Gen AI SDK for Java](https://github.com/googleapis/java-genai).

## Install dependencies
open the project in intellliJ IDEA, load the maven build script.

## Run a test file
Before running the tests, set GOOGLE_API_KEY as environment variable for run/debug configuration.

mvn -Dtest=<filename> test

For example:

mvn -Dtest=CodeExecutionTest test

7 changes: 7 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
<artifactId>google-genai</artifactId>
<version>0.2.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion java/src/main/java/com/example/gemini/BuildConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
package com.example.gemini;

public class BuildConfig {
public static String media_path = "third_party/";
public static String media_path = "../third_party/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,84 @@
package com.example.gemini;

import com.google.genai.Client;
import com.google.genai.types.*;
import org.apache.http.HttpException;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import com.google.genai.types.Tool;
import com.google.genai.types.ToolCodeExecution;

import java.io.IOException;
import java.util.Collections;

public class CodeExecutionRequestOverride {
public static void main(String[] args) throws IOException, HttpException {
@SuppressWarnings("resource")
public class CodeExecution {
public static GenerateContentResponse codeExecutionBasic() throws Exception {
// [START code_execution_basic]
Client client = new Client();

String prompt = """
Write and execute code that calculates the sum of the first 50 prime numbers.
Ensure that only the executable code and its resulting output are generated.
""";

GenerateContentResponse response =
client.models.generateContent(
"gemini-2.0-pro-exp-02-05",
prompt,
null);

for (Part part : response.candidates().get().getFirst().content().get().parts().get()) {
System.out.println(part + "\n");
}

System.out.println("-".repeat(80));
System.out.println(response.text());
// [END code_execution_basic]

/*
[START code_execution_basic_return]
Expected output:
--------------------------------------------------------------------------------
```python
import math

def is_prime(n):
"""Checks if a number is prime."""
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
# Check only odd divisors up to the square root
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True

count = 0
num = 2
prime_sum = 0
target_count = 50

while count < target_count:
if is_prime(num):
prime_sum += num
count += 1
num += 1

print(prime_sum)
```

Output:
```
5117
```
[END code_execution_basic_return]
*/
return response;
}

public static GenerateContentResponse codeExecutionRequestOverride() throws Exception {
// [START code_execution_request_override]
Client client = new Client();

Expand All @@ -42,13 +112,13 @@ public static void main(String[] args) throws IOException, HttpException {
)
).build();


GenerateContentResponse response =
client.models.generateContent(
"gemini-2.0-flash",
prompt,
config);

System.out.println("-".repeat(80));
System.out.println(response.executableCode());
System.out.println(response.codeExecutionResult());
// [END code_execution_request_override]
Expand All @@ -57,7 +127,6 @@ public static void main(String[] args) throws IOException, HttpException {
[START code_execution_request_override_return]
Expected output:
--------------------------------------------------------------------------------

def is_prime(n):
if n <= 1:
return False
Expand All @@ -84,8 +153,8 @@ def is_prime(n):

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]
sum(primes)=5117

[END code_execution_request_override_return]
*/
return response;
}
}
92 changes: 0 additions & 92 deletions java/src/main/java/com/example/gemini/CodeExecutionBasic.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import org.apache.http.HttpException;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.util.List;

public class ConfigureModelParameters {
public static void main(String[] args) throws IOException, HttpException {
public static @Nullable String configureModelParameters() throws Exception {
// [START configure_model_parameters]
Client client = new Client();

Expand All @@ -45,5 +44,6 @@ public static void main(String[] args) throws IOException, HttpException {

System.out.println(response.text());
// [END configure_model_parameters]
return response.text();
}
}
Loading