Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Crash while creating completion #52

Closed
waseem-gul opened this issue Dec 23, 2022 · 9 comments
Closed

Crash while creating completion #52

waseem-gul opened this issue Dec 23, 2022 · 9 comments

Comments

@waseem-gul
Copy link

Here's my code

` String token = "MY_KEYS";
OpenAiService service = new OpenAiService(token);

    System.out.println("\nCreating completion...");
    CompletionRequest completionRequest = CompletionRequest.builder()
            .model("ada")
            .prompt("Somebody once told me the world is gonna roll me")
            .echo(true)
            .user("testing")
            .build();

    btn.setOnClickListener(v -> {
        CompletionResult r = service.createCompletion(completionRequest); //Error arises here
    });`
@cryptoapebot
Copy link

cryptoapebot commented Dec 23, 2022

Okay, it looks like CompletionRequest changed to an implicit, inline json object which automagically gets converted into a CompletionRequest class using the json parser. Just wrap {} around the "field":"value" comma separated json data values or else explicitly create a CompletionRequest and set each value in the object. I don't see getters and setters, but it has a NoArgs default contructor and an AllArgs default constructor.

createCompletion({
  model: "text-davinci-003",
  prompt: "...",
  temperature: 0,
  max_tokens: 100,
  top_p: 1,
  frequency_penalty: 0.0,
  presence_penalty: 0.0,
  stop: ["\n"],
});

@cryptoapebot
Copy link

Sorry, meant to respond to issue #51 and was going to test your problem next.

@cryptoapebot
Copy link

Okay, tested 0.8.1 api and client jars w/ this code.

            final String question = Question.template.replace("${PROMPT}", msg.replace(trigger, "").trim());
            System.out.println(question);
            
			CompletionRequest request = CompletionRequest.builder()
					.model(full_engine)
					.prompt(question)
					.maxTokens(220)
					.temperature(0.0)
					.topP(1.0)
					.frequencyPenalty(0.0)
					.presencePenalty(0.0)
					.echo(false)
					.stop(stops)
					.build();
			List<String> responses = null;
					
			try {
				responses = service.createCompletion(request)
						.getChoices()
						.stream()
						.map(CompletionChoice::getText)
						.collect(Collectors.toList());	
				
				System.out.println(responses.toString());
								
				if (responses != null && responses.size() > 0) {
					responses.stream().forEach(System.out::println);
					response = responses.get(0);
				} else {
					System.out.println("Response is null or size=0");
				}
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}

@waseem-gul
Copy link
Author

Thanks for your reply, with this new code I get an exception with null message

exception: android.os.NetworkOnMainThreadException

@cryptoapebot
Copy link

Not sure that last exception is openai-java related. I think it's just complaining that the network call is blocking in your android thread. https://stackoverflow.com/questions/6343166/how-can-i-fix-android-os-networkonmainthreadexception

It looks like you can turn off the Android default behavior (not recommended as you could potentially lock up the whole OS) or inside your main program, just put your openai calls in a Runnable. I'm on a desktop app--not Android, so I don't have same restrictions.

@TheoKanning
Copy link
Owner

android.os.NetworkOnMainThreadException is an android specific issue. When I did android with RxJava i used something like this to make each call run on the IO thread instead.

api.createCompletion(request)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())

@mcfasa09
Copy link

mcfasa09 commented Jan 26, 2023

I'm using the below code in an Android app made in Android Studio, but still getting the "Unrecognized token 'data': was expecting (JSON String, etc." error. Ideas? Thanks in advance!

private class OpenAITask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        String question = params[0];
        String response = "";

            CompletionRequest request = CompletionRequest.builder()
                    .model("text-davinci-003")
                    .prompt(question)
                    .user("Testing")
                    .maxTokens(220)
                    .temperature(0.0)
                    .topP(1.0)
                    .frequencyPenalty(0.0)
                    .stream(true)
                    .echo(false)
                    .build();
        List<String> responses = null;

        try {
            responses = service.createCompletion(request)
                    .getChoices()
                    .stream()
                    .map(CompletionChoice::getText)
                    .collect(Collectors.toList());

            System.out.println(responses.toString());

            if (responses != null && responses.size() > 0) {
                responses.stream().forEach(System.out::println);
                response = responses.get(0);
            } else {
                System.out.println("Response is null or size=0");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return response;
    }

    @Override
    protected void onPostExecute(String response) {
        if (response != null && !response.isEmpty()) {
            // Do stuff.
    }
}

}

@waseem-gul
Copy link
Author

waseem-gul commented Jan 27, 2023

Add this piece to your code and it will start working (for me it did)

@Override protected String doInBackground(String... params) { SetThreadPolicy(); // other code }

Specify the method under doInBackground()

protected void SetThreadPolicy() { if (SDK_INT > 8) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); } }

@mcfasa09
Copy link

Add this piece to your code and it will start working (for me it did)

@Override protected String doInBackground(String... params) { SetThreadPolicy(); // other code }

Specify the method under doInBackground()

protected void SetThreadPolicy() { if (SDK_INT > 8) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); } }

Unfortunately that still results in the error: Unrecognized token 'data': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants