Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- public class OpenAIClient {
- private static final String API_URL = "https://api.openai.com/v1/completions";
- private static final String API_KEY = "heres my API key I won't show it sorry";
- private final OkHttpClient httpClient = new OkHttpClient();
- public String getResponse(String prompt) throws IOException {
- RequestBody body = RequestBody.create(
- MediaType.parse("application/json"),
- "{"
- + "\"model\": \"text-davinci-003\","
- + "\"prompt\": \"" + prompt + "\","
- + "\"max_tokens\": 150,"
- + "\"temperature\": 0.7"
- + "}"
- );
- Request request = new Request.Builder()
- .url(API_URL)
- .header("Authorization", "Bearer " + API_KEY)
- .post(body)
- .build();
- try (Response response = httpClient.newCall(request).execute()) {
- if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
- String responseBody = response.body().string();
- return responseBody;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement