Closed dehidehidehi closed 1 year ago
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
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!
Implementation surprisingly almost done, currently doing some fixes to make tests pass.
Much appreciated! Hope it gets merged soon
nice speed
thank you so much
please take my knee!!!
how to fix this ,Response{protocol=h2, code=400, message=, url=https://api.openai.com/v1/chat/completions}
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}
Thanks! When will it be merged?
No clue, depends on the maintainer
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 :(
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);
}
}
}
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!
Just released 0.11.0 with ChatGPT support 👍 Thanks again for the PR! I
Hi, @TheoKanning do we have any documentation around this API, because I don't see it over the internet?
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
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