diff --git a/README.md b/README.md index 96e842b..f36bc9d 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,12 @@ new CreateChatCompletionRequest() ## Version +### 1.9.5 + +- Update: The CreateChatCompletionRequest now includes a new method, sendForResponse, which returns a + CreateChatCompletionResponse. This object not only contains the choice list returned by the sendForChoices method, but + also includes additional information such as the token usage for this request. + ### 1.9.4 - Update models in GptModel. (0125 -> gpt-4-turbo-2024-04-09) diff --git a/pom.xml b/pom.xml index 1b08c55..36fc84b 100755 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ https://github.com/WhiteMagic2014/gpt-magic.git io.github.whitemagic2014 gpt-magic - 1.9.4 + 1.9.5 diff --git a/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/CreateChatCompletionRequest.java b/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/CreateChatCompletionRequest.java index 0bcc83e..748bac7 100644 --- a/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/CreateChatCompletionRequest.java +++ b/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/CreateChatCompletionRequest.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; import com.github.WhiteMagic2014.gptApi.Chat.pojo.ChatCompletionChoice; import com.github.WhiteMagic2014.gptApi.Chat.pojo.ChatMessage; +import com.github.WhiteMagic2014.gptApi.Chat.pojo.CreateChatCompletionResponse; import com.github.WhiteMagic2014.gptApi.GptModel; import com.github.WhiteMagic2014.gptApi.GptRequest; import com.github.WhiteMagic2014.tool.FunctionTool; @@ -350,4 +351,10 @@ public List sendForChoices() { return JSON.parseArray(data.toJSONString(), ChatCompletionChoice.class); } + public CreateChatCompletionResponse sendForResponse() { + if (stream) { + throw new RuntimeException("If the 'stream' field is true, you need to set an OutputStream to receive the returned stream.Please use the send method"); + } + return JSON.parseObject(send().toJSONString(), CreateChatCompletionResponse.class); + } } diff --git a/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/pojo/CreateChatCompletionResponse.java b/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/pojo/CreateChatCompletionResponse.java new file mode 100644 index 0000000..78af1a6 --- /dev/null +++ b/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/pojo/CreateChatCompletionResponse.java @@ -0,0 +1,95 @@ +package com.github.WhiteMagic2014.gptApi.Chat.pojo; + +import java.util.Date; +import java.util.List; + +/** + * @Description: CreateChatCompletionResponse + * @author: magic chen + * @date: 2024/4/29 16:44 + **/ +public class CreateChatCompletionResponse { + + private Date created; + + private Usage usage; + + private String model; + + private String id; + + private List choices; + + private String systemFingerprint; + + private String object; + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Usage getUsage() { + return usage; + } + + public void setUsage(Usage usage) { + this.usage = usage; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public List getChoices() { + return choices; + } + + public void setChoices(List choices) { + this.choices = choices; + } + + public String getSystemFingerprint() { + return systemFingerprint; + } + + public void setSystemFingerprint(String systemFingerprint) { + this.systemFingerprint = systemFingerprint; + } + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + @Override + public String toString() { + return "CreateChatCompletionResponse{" + + "created=" + created + + ", usage=" + usage + + ", model='" + model + '\'' + + ", id='" + id + '\'' + + ", choices=" + choices + + ", systemFingerprint='" + systemFingerprint + '\'' + + ", object='" + object + '\'' + + '}'; + } +} diff --git a/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/pojo/Usage.java b/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/pojo/Usage.java new file mode 100644 index 0000000..2e0edac --- /dev/null +++ b/src/main/java/com/github/WhiteMagic2014/gptApi/Chat/pojo/Usage.java @@ -0,0 +1,48 @@ +package com.github.WhiteMagic2014.gptApi.Chat.pojo; + +/** + * @Description: token usage + * @author: magic chen + * @date: 2024/4/29 16:49 + **/ +public class Usage { + + private Long promptTokens; + + private Long completionTokens; + + private Long totalTokens; + + public Long getPromptTokens() { + return promptTokens; + } + + public void setPromptTokens(Long promptTokens) { + this.promptTokens = promptTokens; + } + + public Long getCompletionTokens() { + return completionTokens; + } + + public void setCompletionTokens(Long completionTokens) { + this.completionTokens = completionTokens; + } + + public Long getTotalTokens() { + return totalTokens; + } + + public void setTotalTokens(Long totalTokens) { + this.totalTokens = totalTokens; + } + + @Override + public String toString() { + return "Usage{" + + "promptTokens=" + promptTokens + + ", completionTokens=" + completionTokens + + ", totalTokens=" + totalTokens + + '}'; + } +}