99import java .net .HttpURLConnection ;
1010import java .net .URI ;
1111import java .net .URL ;
12- import java .net .URLConnection ;
12+ // import java.net.URLConnection;
1313import java .nio .file .Files ;
1414import java .nio .file .Path ;
1515import java .nio .file .Paths ;
1616
1717public 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 );
0 commit comments