Hello everyone ,
I’m trying to get the application to generate a response from API after inputting 2 fields in an Android application , but when i hit the generate button it gives me an error inside the app and this error in Android Studio :
W/System.err: org.json.JSONException: No value for choices
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
W/System.err: at com.example.testvows.OpenAIExample.getCompletion(OpenAIExample.java:42)
W/System.err: at com.example.testvows.OpenAIExample$GenerateVowTask.doInBackground(OpenAIExample.java:73)
W/System.err: at com.example.testvows.OpenAIExample$GenerateVowTask.doInBackground(OpenAIExample.java:63)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err: at java.lang.Thread.run(Thread.java:761)
Here is my code and forgive me for sounding like a noob its because i am in this field and trying to learn , all help is appreciated .
package com.example.testvows;
import android.os.AsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OpenAIExample {
private static final String API_URL = "https://api.openai.com/v1/chat/completions";
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
private static OkHttpClient client = new OkHttpClient();
public static String getCompletion(String apiKey, String prompt, String s, int maxTokens) throws IOException, JSONException {
JSONObject requestBody = new JSONObject();
requestBody.put("prompt", prompt);
requestBody.put("max_tokens", maxTokens);
RequestBody body = RequestBody.create(requestBody.toString(), JSON);
Request request = new Request.Builder()
.url(API_URL)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + apiKey)
.post(body)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
JSONObject responseObject = new JSONObject(responseData);
String completions = responseObject.getJSONArray("choices").getJSONObject(0).getString("text");
return completions;
}
public static String getCompletion(String name1, String name2) {
try {
String prompt = "Write a vow between " + name1 + " and " + name2 + ":";
String apiKey = "I Used the key i got from API";
String vow = new GenerateVowTask().execute(apiKey, prompt, "", "50").get();
return vow;
} catch (Exception e) {
e.printStackTrace();
return "Error generating vow.";
}
}
public String getCompletion(String s) {
return s;
}
private static class GenerateVowTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String apiKey = params[0];
String prompt = params[1];
String s = params[2];
int maxTokens = Integer.parseInt(params[3]);
try {
return getCompletion(apiKey, prompt, s, maxTokens);
} catch (IOException | JSONException e) {
e.printStackTrace();
return "Error generating vow.";
}
}
}
}