Advertisement
Guest User

Untitled

a guest
Jun 4th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. public class OpenAIClient {
  4. private static final String API_URL = "https://api.openai.com/v1/completions";
  5. private static final String API_KEY = "heres my API key I won't show it sorry";
  6.  
  7. private final OkHttpClient httpClient = new OkHttpClient();
  8.  
  9. public String getResponse(String prompt) throws IOException {
  10. RequestBody body = RequestBody.create(
  11. MediaType.parse("application/json"),
  12. "{"
  13. + "\"model\": \"text-davinci-003\","
  14. + "\"prompt\": \"" + prompt + "\","
  15. + "\"max_tokens\": 150,"
  16. + "\"temperature\": 0.7"
  17. + "}"
  18. );
  19.  
  20. Request request = new Request.Builder()
  21. .url(API_URL)
  22. .header("Authorization", "Bearer " + API_KEY)
  23. .post(body)
  24. .build();
  25.  
  26. try (Response response = httpClient.newCall(request).execute()) {
  27. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  28.  
  29. String responseBody = response.body().string();
  30. return responseBody;
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement