Skip to content

Commit 97dc3b6

Browse files
committed
added completions with Google search; updsated to model gemini-3-flash-preview
1 parent cbee42e commit 97dc3b6

2 files changed

Lines changed: 46 additions & 25 deletions

File tree

gemini-llm-client/src/main/java/com/markwatson/gemini/GeminiCompletions.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,47 @@
99
import java.net.HttpURLConnection;
1010
import java.net.URI;
1111
import java.net.URL;
12-
import java.net.URLConnection;
12+
//import java.net.URLConnection;
1313
import java.nio.file.Files;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
1616

1717
public class GeminiCompletions {
1818

19+
public static String model = "gemini-3-flash-preview";
20+
1921
public static void main(String[] args) throws Exception {
2022
String prompt = "How much is 11 + 22?";
2123
String completion = getCompletion(prompt);
2224
System.out.println("completion: " + completion);
2325
}
2426

2527
public static String getCompletion(String prompt) throws Exception {
28+
String jsonBody = "{\"contents\":[{\"parts\":[{\"text\":\"" + prompt + "\"}]}]}";
29+
return executeRequest(jsonBody);
30+
}
31+
32+
public static String getCompletionWithSearch(String prompt) throws Exception {
33+
String jsonBody = "{\"contents\":[{\"parts\":[{\"text\":\"" + prompt
34+
+ "\"}]}],\"tools\":[{\"google_search\":{}}]}";
35+
return executeRequest(jsonBody);
36+
}
37+
38+
private static String executeRequest(String jsonBody) throws Exception {
2639
String apiKey = System.getenv("GOOGLE_API_KEY");
2740
if (apiKey == null || apiKey.isEmpty()) {
2841
throw new IOException("GOOGLE_API_KEY environment variable not set.");
2942
}
30-
String model = "gemini-2.5-flash";
31-
URI uri = new URI("https://generativelanguage.googleapis.com/v1beta/models/" + model + ":generateContent?key=" + apiKey);
43+
URI uri = new URI(
44+
"https://generativelanguage.googleapis.com/v1beta/models/" + model + ":generateContent?key=" + apiKey);
3245
URL url = uri.toURL();
3346

3447
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3548
connection.setRequestMethod("POST");
36-
connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); String jsonBody = "{\"contents\":[{\"parts\":[{\"text\":\"" + prompt + "\"}]}]}"; try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonBody.getBytes("utf-8");
49+
connection.setRequestProperty("Content-Type", "application/json");
50+
connection.setDoOutput(true);
51+
try (OutputStream os = connection.getOutputStream()) {
52+
byte[] input = jsonBody.getBytes("utf-8");
3753
os.write(input, 0, input.length);
3854
}
3955

@@ -48,10 +64,10 @@ public static String getCompletion(String prompt) throws Exception {
4864

4965
connection.disconnect();
5066
JSONObject jsonObject = new JSONObject(response.toString());
51-
return jsonObject.getJSONArray("candidates").getJSONObject(0).getJSONObject("content").getJSONArray("parts").getJSONObject(0).getString("text");
67+
return jsonObject.getJSONArray("candidates").getJSONObject(0).getJSONObject("content").getJSONArray("parts")
68+
.getJSONObject(0).getString("text");
5269
}
5370

54-
5571
/***
5672
* Utilities for using the Gemini API
5773
*/
@@ -65,6 +81,7 @@ public static String readFileToString(String filePath) throws IOException {
6581
public static String replaceSubstring(String originalString, String substringToReplace, String replacementString) {
6682
return originalString.replace(substringToReplace, replacementString);
6783
}
84+
6885
public static String promptVar(String prompt0, String varName, String varValue) {
6986
String prompt = replaceSubstring(prompt0, varName, varValue);
7087
return replaceSubstring(prompt, varName, varValue);

gemini-llm-client/src/test/java/com/markwatson/gemini/GeminiCompletionsTest.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,33 @@
88
* Unit test for simple App.
99
*/
1010
public class GeminiCompletionsTest
11-
extends TestCase
12-
{
11+
extends TestCase {
1312
/**
1413
* Create the test case
1514
*
1615
* @param testName name of the test case
1716
*/
18-
public GeminiCompletionsTest( String testName )
19-
{
20-
super( testName );
17+
public GeminiCompletionsTest(String testName) {
18+
super(testName);
2119
}
2220

2321
/**
2422
* @return the suite of tests being tested
2523
*/
26-
public static Test suite()
27-
{
28-
return new TestSuite( GeminiCompletionsTest.class );
24+
public static Test suite() {
25+
return new TestSuite(GeminiCompletionsTest.class);
2926
}
3027

3128
/**
3229
* Rigourous Test :-)
33-
* @throws Exception
30+
*
31+
* @throws Exception
3432
*/
35-
public void testCompletion() throws Exception
36-
{
37-
String r = GeminiCompletions.getCompletion("Translate the following English text to French: 'Hello, how are you?'");
33+
public void testCompletion() throws Exception {
34+
String r = GeminiCompletions
35+
.getCompletion("Translate the following English text to French: 'Hello, how are you?'");
3836
System.out.println("completion: " + r);
39-
assertTrue( true );
37+
assertTrue(true);
4038
}
4139

4240
public void testTwoShotTemplate() throws Exception {
@@ -45,10 +43,9 @@ public void testTwoShotTemplate() throws Exception {
4543
System.out.println("prompt0: " + prompt0);
4644
String prompt = GeminiCompletions.promptVar(prompt0, "{input_text}", input_text);
4745
System.out.println("prompt: " + prompt);
48-
String r =
49-
GeminiCompletions.getCompletion(prompt);
46+
String r = GeminiCompletions.getCompletion(prompt);
5047
System.out.println("two shot extraction completion: " + r);
51-
assertTrue( true );
48+
assertTrue(true);
5249
}
5350

5451
public void testSummarization() throws Exception {
@@ -57,9 +54,16 @@ public void testSummarization() throws Exception {
5754
System.out.println("prompt0: " + prompt0);
5855
String prompt = GeminiCompletions.promptVar(prompt0, "{input_text}", input_text);
5956
System.out.println("prompt: " + prompt);
60-
String r =
61-
GeminiCompletions.getCompletion(prompt);
57+
String r = GeminiCompletions.getCompletion(prompt);
6258
System.out.println("summarization completion: " + r);
63-
assertTrue( true );
59+
assertTrue(true);
60+
}
61+
62+
public void testCompletionWithSearch() throws Exception {
63+
String prompt = "What is the current stock price of Google?";
64+
String r = GeminiCompletions.getCompletionWithSearch(prompt);
65+
System.out.println("Search completion: " + r);
66+
assertNotNull(r);
67+
assertFalse(r.isEmpty());
6468
}
6569
}

0 commit comments

Comments
 (0)