TheoKanning / openai-java

OpenAI Api Client in Java
MIT License
4.78k stars 1.2k forks source link

IntellIJ warning as "Deprecated API" #130

Closed jfoliveiraramos closed 1 year ago

jfoliveiraramos commented 1 year ago

This is the dependency I'm using on my gradle:

implementation 'com.theokanning.openai-gpt3-java:client:0.10.0'

IntellIJ is giving a warning of "Class OpenAI uses or overrides a deprecated API".

Not only this, but it has been exhibiting weird behaviors such as this prompt/answer:

Prompt:

how are you

Answer:

?

I'm good. How are you?

I'm good.

This is weird, 2/10 times, it gives a normal response, but it's been this weird.

My OpenAIClass:

package com.package.name;

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.image.CreateImageRequest;

public class OpenAI {

    private static final String API_KEY = "censored";
    private static OpenAI openAI = null;
    private OpenAiService service;

    private OpenAI(){
        service = new OpenAiService(API_KEY);
    }

    public static OpenAI getInstance(){

        if (openAI == null)
            openAI = new OpenAI();
        return openAI;
    }
    public String request(String message) {

        try {
            CompletionRequest request = CompletionRequest.builder()
                    .model("text-davinci-002")
                    .maxTokens(500)
                    .prompt(message)
                    .temperature(0.1)
                    .build();

            String response = service.createCompletion(request).getChoices().get(0).getText();

            return response;
        } catch (Exception e) {
            return "Something went wrong!";
        }
    }
}
cryptoapebot commented 1 year ago

Couple of things.

Add a timeout to your service.

        service = new OpenAiService(API_KEY, Duration.ofSeconds(45));

Switch to the newer davinci model.

                    .model("text-davinci-003")

You probably want to add stops to your submission. Change to fit your interaction. "How are you?\n" with a question mark newline as a stop. Or, "Answer:\n"

    public static final List<String> stops = Arrays.asList("\n", " Human:", " AI:");

                    .stop(stops)

Finally, maybe play w/ the prompt by giving it a few more examples of what's being requested, and play w/ the other params.

                    .temperature(0.97)
                    .topP(1.0)
                    .frequencyPenalty(0.0)
                    .presencePenalty(0.0)
jfoliveiraramos commented 1 year ago

Hi, thanks for the input! It got better, but there are still some weird interactions going on.

Config:


CompletionRequest request = CompletionRequest.builder()
                    .model("text-davinci-003")
                    .maxTokens(2048)
                    .prompt(message)
                    .temperature(0.01)
                    .topP(1.0)
                    .frequencyPenalty(0.5)
                    .presencePenalty(0.0)
                    .stop(stops)
                    .build();

I am using the stops() you suggested as well.

But here is an example of what's going on:

image

cryptoapebot commented 1 year ago

A couple more ideas.

1) You have to include in your prompt an instruction part. For instance: BoBAlpha is a chat bot that is friendly, helpful and very knowlegeable about technology and media. It answers in three or four sentences.

2) Change the number of completions to .n(5) or more. When you get the completions, iterate through and see if you get different or better answers.

3) Experiment on the Playground to tune your answers. It's not clear from the above what your prompt is asking for.

cryptoapebot commented 1 year ago
        List<CompletionChoice> choices = service.createCompletion(completionRequest).getChoices();
        assertEquals(5, choices.size());

      for (CompletionChoice c : choices) {
          System.out.println(c.getText()); 
// or whatever the exact command is. c.toString(() will give you the log_probs and finish reason which might give you clues.
      }
jfoliveiraramos commented 1 year ago

Hi, thanks for the help. I have concluded why I was having issues after playing with the playground, like you suggested.

Turns out that the AI is set to complete mode, hence why this happens:

image

The ", I'm not a Doctor" is him completing the prompt, and then the rest is the actual reply to said prompt.

I am able to turn this off in the playground, but I have no idea how to do it within the Java API. Any tips?

P.S.: Giving him context of being a helpful discord bot was GREAT! Thank you!!