API

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class ChatGPTRequest { public static void main(String[] args) { try { // Define the prompt to be sent String prompt = “Please generate a simple blog post according to this title \”What is CHATGPT\””; // Enter E-mail to generate API String apiKey = ‘2d948723dc0637371cc4ad195f412ebd’; // Define the default model if none is specified String defaultModel = “gpt-3.5-turbo”; // Uncomment the model you want to use, and comment out the others // String model = “gpt-4”; // String model = “gpt-4-32k”; // String model = “gpt-3.5-turbo-0125”; String model = defaultModel; // Build the URL to call String apiUrl = “http://195.179.229.119/gpt/api.php?” + “prompt=” + URLEncoder.encode(prompt, “UTF-8”) + “&api_key=” + URLEncoder.encode(apiKey, “UTF-8”) + “&model=” + URLEncoder.encode(model, “UTF-8”); // Create URL object URL url = new URL(apiUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(“GET”); conn.setRequestProperty(“Accept”, “application/json”); // Get the response BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Print the response System.out.println(response.toString()); } catch (Exception e) { e.printStackTrace(); } } }

Leave a Comment

Your email address will not be published. Required fields are marked *