Using OpenAI API with Java
Introduction
This tutorial demonstrates how to integrate and use the OpenAI API with Java to incorporate advanced AI functionalities into your applications.
1. Setting Up Your OpenAI API Key
Before getting started, ensure you have your OpenAI API key ready. You can obtain it from the OpenAI website after signing up for an account.
2. Making API Requests
To make requests to the OpenAI API using Java, you'll use libraries like Apache HttpClient or OkHttp. Here are examples of different API requests:
2.1 Text Completion Example
Example of completing text using the API:
import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class OpenAITextCompletionExample { public static void main(String[] args) throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost request = new HttpPost("https://api.openai.com/v1/completions"); request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer YOUR_API_KEY_HERE"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); StringEntity entity = new StringEntity( "{\"prompt\":\"Translate English to French: Hello, how are you?\",\"max_tokens\":50}", "UTF-8"); request.setEntity(entity); HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { System.out.println(EntityUtils.toString(responseEntity)); } } }
Output : Bonjour, comment vas-tu ?
2.2 Image Captioning Example
Example of generating a caption for an image:
import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class OpenAIImageCaptioningExample { public static void main(String[] args) throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost request = new HttpPost("https://api.openai.com/v1/images/caption"); request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer YOUR_API_KEY_HERE"); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); StringEntity entity = new StringEntity( "{\"image\":\"URL_TO_YOUR_IMAGE\"}", "UTF-8"); request.setEntity(entity); HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { System.out.println(EntityUtils.toString(responseEntity)); } } }
Output: A dog running in the park.
3. Handling Responses
Once you receive a response from the API, you can handle it in your Java code. Here’s how you might handle the completion response:
// Assuming 'responseEntity' contains the API response System.out.println(EntityUtils.toString(responseEntity));
In this example, EntityUtils.toString(responseEntity)
contains the JSON response from the OpenAI API.
Conclusion
Integrating the OpenAI API with Java allows you to incorporate state-of-the-art AI capabilities into your applications. Explore more API endpoints and capabilities to enhance your projects.