openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Feature: Implementing the new Chat completions

Open dehidehidehi opened this issue 2 years ago • 15 comments

Edit: aaaaand done https://github.com/TheoKanning/openai-java/pull/135

As I do not see a discussions tab for this repository, let's start a conversation on implementing the new Chat completions endpoint.

I'll get started on this :)

Current work in progress (my fork of the project) https://github.com/dehidehidehi/openai-java/tree/chat-completions

Documentation: https://platform.openai.com/docs/guides/chat References: https://platform.openai.com/docs/api-reference/chat/create

dehidehidehi avatar Mar 01 '23 20:03 dehidehidehi

Much appreciated! I tried implementing my own workaround but honestly this is no replacement for updating the library itself, just a quick jerry-rig/concept:

//OpenAiService.java:
        public ChatCompletionResult createChatCompletion(ChatCompletionRequest request) {
        return api.createChatCompletion(request).blockingGet();
    }

//OpenAiApi.java:
      @POST("/v1/chat/completions")
    Single<ChatCompletionResult> createChatCompletion(@Body ChatCompletionRequest var1);

//Other:
@Data
public class ChatCompletionResult {
    String id;
    String object;
    long created;
    String model;
    List<ChatCompletionChoice> choices;
    Usage usage;

    public ChatCompletionResult() {
    }

    public String getId() {
        return this.id;
    }

    public String getObject() {
        return this.object;
    }

    public long getCreated() {
        return this.created;
    }

    public String getModel() {
        return this.model;
    }

    public List<ChatCompletionChoice> getChoices() {
        return this.choices;
    }

    public Usage getUsage() {
        return this.usage;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setObject(String object) {
        this.object = object;
    }

    public void setCreated(long created) {
        this.created = created;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void setChoices(List<ChatCompletionChoice> choices) {
        this.choices = choices;
    }

    public void setUsage(Usage usage) {
        this.usage = usage;
    }

    protected boolean canEqual(Object other) {
        return other instanceof CompletionResult;
    }

    public String toString() {
        return "CompletionResult(id=" + this.getId() + ", object=" + this.getObject() + ", created=" + this.getCreated() + ", model=" + this.getModel() + ", choices=" + this.getChoices() + ", usage=" + this.getUsage() + ")";
    }
}

@Data
public class ChatCompletionChoice {
    ChatMessage message;
    Integer index;
    String finish_reason;

    public ChatCompletionChoice() {
    }

    public ChatMessage getMessage() {
        return message;
    }

    public Integer getIndex() {
        return this.index;
    }

    public String getFinish_reason() {
        return this.finish_reason;
    }

    public void setMessage(ChatMessage message) {
        this.message = message;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public void setFinish_reason(String finish_reason) {
        this.finish_reason = finish_reason;
    }
}

public class ChatMessage {
    private String role;
    private String content;

    public ChatMessage(String role, String content) {
        this.role = role;
        this.content = content;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

I tried implementing this quick workaround here in another project to get this working in my own project temporarily but ran into some retrofit dependency errors. Definitely would prefer if the library itself gets updated, the necessary changes are pretty minor (above is the extent of them).

Edit: Improved naming scheme to match library precedent

ZMobile avatar Mar 01 '23 20:03 ZMobile

Biggest feature request is conversation id.

@ZMobile if you use the @Data tag, you don't have to declare getters and setters. They get generated automatically with import lombok.Data if you have it configured correctly in your environment.

But thank you both!

cryptoapebot avatar Mar 01 '23 21:03 cryptoapebot

Implementation surprisingly almost done, currently doing some fixes to make tests pass.

dehidehidehi avatar Mar 01 '23 21:03 dehidehidehi

Done https://github.com/TheoKanning/openai-java/pull/135

dehidehidehi avatar Mar 01 '23 21:03 dehidehidehi

Much appreciated! Hope it gets merged soon

ZMobile avatar Mar 01 '23 22:03 ZMobile

nice speed

dongyado avatar Mar 02 '23 02:03 dongyado

thank you so much

uptown avatar Mar 02 '23 05:03 uptown

please take my knee!!!

liangtian123 avatar Mar 02 '23 07:03 liangtian123

how to fix this ,Response{protocol=h2, code=400, message=, url=https://api.openai.com/v1/chat/completions}

ly0611 avatar Mar 02 '23 09:03 ly0611

Edit: aaaaand done #135

As I do not see a discussions tab for this repository, let's start a conversation on implementing the new Chat completions endpoint.

I'll get started on this :)

Current work in progress (my fork of the project) https://github.com/dehidehidehi/openai-java/tree/chat-completions

Documentation: https://platform.openai.com/docs/guides/chat References: https://platform.openai.com/docs/api-reference/chat/create

how to fix this ,Response{protocol=h2, code=400, message=, url=https://api.openai.com/v1/chat/completions}

ly0611 avatar Mar 02 '23 09:03 ly0611

Thanks! When will it be merged?

michaelsiebers avatar Mar 02 '23 13:03 michaelsiebers

No clue, depends on the maintainer

dehidehidehi avatar Mar 02 '23 13:03 dehidehidehi

Can you please explain how we can compile your pr and publish it to the local maven repo? As I am new to gradle at all Im not able to do that myself :(

jan-herzog avatar Mar 02 '23 14:03 jan-herzog

Can you please explain how we can compile your pr and publish it to the local maven repo? As I am new to gradle at all Im not able to do that myself :(

Here's an alternative piece of code for you to obtain this functionality. Look at my code above for the rest of missing objects.

public class ChatGptDaoImpl implements ChatGptDao {
    private Gson gson;
    private String openAiApiKey;

    public ChatGptDaoImpl(String openAiApiKey) {
        this.gson = new Gson();
        this.openAiApiKey = openAiApiKey;
    }

    public String askChatGpt(List<ChatGptMessage> messages, String model) {
        ChatGptRequestResource chatGptRequestResource = new ChatGptRequestResource(model);
        chatGptRequestResource.setMessages(messages);
        try {
            URL url = new URL("https://api.openai.com/v1/chat/completions");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Authorization", "Bearer " + openAiApiKey);
            con.setDoOutput(true);
            String jsonInputString = gson.toJson(chatGptRequestResource);
            try (OutputStream os = con.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try (InputStream is = con.getInputStream()) {
                String response = IOUtils.toString(is, StandardCharsets.UTF_8);
                ChatGptCompletionResultResource chatGptCompletionResultResource = gson.fromJson(response, ChatGptCompletionResultResource.class);
                return chatGptCompletionResultResource.getChoices().get(0).getMessage().getContent();
            } catch (IOException e) {
                System.out.println(IOUtils.toString(con.getErrorStream(), StandardCharsets.UTF_8));
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

ZMobile avatar Mar 02 '23 14:03 ZMobile

I'll do my best to get your PR merged tonight, unfortunately OpenAI didn't give me early access so I heard about this at the same time as everyone else. Thanks for adding this!

TheoKanning avatar Mar 02 '23 15:03 TheoKanning

Just released 0.11.0 with ChatGPT support 👍 Thanks again for the PR! I

TheoKanning avatar Mar 02 '23 22:03 TheoKanning

Hi, @TheoKanning do we have any documentation around this API, because I don't see it over the internet?

visrut7 avatar Apr 30 '23 08:04 visrut7

Hi, @TheoKanning do we have any documentation around this API, because I don't see it over the internet?

Not to be facetious, but you are getting closer.... https://github.com/TheoKanning/openai-java

cryptoapebot avatar Apr 30 '23 15:04 cryptoapebot