pom.xml
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>2.7.2</version>
</dependency>
eclipse
mvn clean install
Gpt3TextGenerator.java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
public class Gpt3TextGenerator {
private static final String API_KEY = "your-api-key";
public static String generateText(String prompt) throws UnirestException {
HttpResponse<String> response = Unirest.post("https://api.openai.com/v1/engines/davinci/jobs")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.body("{\"prompt\": \"" + prompt + "\", \"max_tokens\": 100, \"temperature\": 0.5}")
.asString();
return response.getBody();
}
}
Service