openai-java
openai-java copied to clipboard
Almost never get a response when using OpenAiService service
openAiService = new OpenAiService(API_TOKEN);
completionRequest = CompletionRequest.builder().model("text-davinci-003").maxTokens(MAX_TOKENS).build(); completionRequest.setPrompt(getMessageTrainedMessage());
List<CompletionChoice> completionChoices = new ArrayList<>(); completionChoices = Executors.newSingleThreadScheduledExecutor().submit(new Callable<List<CompletionChoice>>() { @Override public List<CompletionChoice> call() throws Exception { return openAiService.createCompletion(completionRequest).getChoices(); } }).get(40L, TimeUnit.SECONDS);
// running this in android studio
I've had an issue in a diff project where I had to explicitly do a synchronized thread. A couple things to try, actually declare the <> types. Assign the result to a Future before getting the value and returning the value in diff steps. The other thing to try is just inline a Thread to see if you can't isolate it. It's just like a SingleThreadScheduledExecutor, except you can force the join to see if it is completing. Good luck!
Thread t = new Thread(new Runnable() {
public void run() {
try {
// whatever
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Thank you @cryptoapebot your suggestion pointed me to the right direction. I was running an ExecutorService inside another ExecutorService and updating the UI in the inner service, that's what caused the issue with the android main UI thread which makes it looks like there's no response Now everything works fine
Whoah. I think you just helped me understand my other problem too. I was doing a threaded call to the HttpClient inside a lamba of another threaded call. I'm glad I could help, but it sounds like you solved it on your own!