Regarding GPT-3 Android Studio Project. (JAVA)

Viewed 20

I am not able to generate output in my resultTextview. App get crashed when I click generate button.

Below is my MainActivity.java or you can get it from github

    search = findViewById(R.id.search);
    resultTextView = findViewById(R.id.resultTextView);
    generateButton = findViewById(R.id.generateButton);


    generateButton.setOnClickListener(this::onClick);
}

private void onClick(View view) {
    String searchText = search.getText().toString();
    String token = System.getenv("OPENAI_TOKEN");
    OpenAiService service = new OpenAiService(token);
    Engine davinci = service.getEngine("davinci");
    ArrayList<CompletionChoice> storyArray = new ArrayList<CompletionChoice>();
    CompletionRequest completionRequest = CompletionRequest.builder()
            .prompt(searchText)
            .temperature(0.7)
            .maxTokens(96)
            .topP(1.0)
            .frequencyPenalty(0.0)
            .presencePenalty(0.3)
            .echo(true)     // echoes back the prompt in addition to tokens
            .build();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        service.createCompletion("davinci", completionRequest).getChoices().forEach(line -> {
            storyArray.add(line);
        });
    }
    String result = "";
    for (CompletionChoice line : storyArray) {
        result += line.getText() + "\n";
    }

    resultTextView.setText(storyArray.get(0).getText());

 }

}
0 Answers
Related