Skip to content

Commit

Permalink
Merge pull request #172 from Grt1228/1.0.14
Browse files Browse the repository at this point in the history
1.0.14
  • Loading branch information
Grt1228 authored Jun 26, 2023
2 parents 9ecfd94 + e659000 commit 47eac82
Show file tree
Hide file tree
Showing 54 changed files with 707 additions and 58 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122
- 支持自定义ApiKey的获取策略
- 支持余额查询
- 支持个人账户信息查询
- 支持GPT3、GPT3.5、GPT4.0
- 支持GPT3、GPT3.5、GPT4.0、GPT3.5—0614、GPT4.0—0614...
- 支持全部OpenAI的Api

# 📑 更新日志
- [ ] 1.0.14 升级支持最新版0614模型,支持function-calling
- [x] 1.0.14 升级支持最新版Gpt-3.5—0614、Gpt-4.0—0614等模型, 支持function-calling完整使用案例参考:[OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/1.0.14/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java)
- [x] 1.0.13 支持当key异常(失效、过期、封禁)时,自定义动态处理key,参考实现[DynamicKeyOpenAiAuthInterceptor](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/interceptor/DynamicKeyOpenAiAuthInterceptor.java) ,支持key异常时的告警处理(钉钉、飞书、email、企业微信等等需要自定义开发)
- [x] 1.0.12 tokens计算优化、删除模型接口修改、语音接口更新支持官方最新参数
- [x] 1.0.11 增加新的余额查询接口参考:[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java)[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) ,修复tokens计算慢的问题,
Expand All @@ -86,9 +86,10 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122

# 🚀 快速开始
本项目支持**默认输出****流式输出**。完整SDK测试案例参考:
SDK测试案例 | Tokens测试案例 |
---|---|
[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java)[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) | Tokens计算参考:[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java)|

SDK测试案例 | Tokens计算测试案例 | 完整Function-Call调用案例
---| --- | ---
[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java)[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) | Tokens计算参考:[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java) | [OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java)

## 方式一

Expand All @@ -97,7 +98,7 @@ SDK测试案例 | Tokens测试案例 |
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.13</version>
<version>1.0.14-beta1</version>
</dependency>
```
### 2、流式客户端使用示例:
Expand Down
12 changes: 6 additions & 6 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ WebSocket Reference:[OpenAIWebSocketEventSourceListener](https://github.com/Gr
- Supports customizing the key acquisition strategy.
- Supports balance inquiry.
- Supports personal account information inquiry.
- Supports GPT3, GPT3.5, GPT4.0.
- Supports GPT3, GPT3.5, GPT4.0, GPT3.5—0614, GPT4.0—0614...
- Supports all OpenAI APIs.

# 📑 Update Log
- [ ] 1.0.14 Upgrade to support the latest version 0614 model, support function-calling
- [x] 1.0.14 Upgrade to support the latest version gpt-3.5—0614、gpt-4.0—0614 models, support function-calling full test e.g.:[OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java)
- [x] 1.0.13 Added support for custom handling of key exceptions (invalid, expired, blocked) with the implementation reference [DynamicKeyOpenAiAuthInterceptor](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/interceptor/DynamicKeyOpenAiAuthInterceptor.java). Also added support for alerting on key exceptions (DingTalk, Feishu, email, WeChat Enterprise, etc.), which requires custom development.
- [x] 1.0.12 Optimized token calculation, modified delete model interface, and updated speech interface to support the latest official parameters.
- [x] 1.0.11 Added new balance query interface with reference to [OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) and [OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java). Fixed slow token calculation issue.
Expand All @@ -85,9 +85,9 @@ WebSocket Reference:[OpenAIWebSocketEventSourceListener](https://github.com/Gr

This project supports both **default output** and **streaming output**. For a complete SDK test case, see:

SDK Test Cases | Token Test Cases |
---|---|
[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) and [OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) | Token calculation reference: [TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java)|
SDK Test Cases | TikToken Test Cases | Full Function Call Test Cases
---|---|---|
[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) and [OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) | Token calculation reference: [TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java) | [OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java)

## Method 1

Expand All @@ -96,7 +96,7 @@ SDK Test Cases | Token Test Cases |
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.13</version>
<version>1.0.14</version>
</dependency>
```
### 2. Streaming client usage example:
Expand Down
6 changes: 3 additions & 3 deletions Tokens_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
chatgpt-java 更新到1.0.10支持Tokens计算,增加[TikTokensUtil](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/utils/TikTokensUtil.java) 工具类.

## tokens计算说明
openai 的tokens计算规则适合模型先关的,不同的模型计算方法是不一样的。大致的表格如下:
openai 的tokens计算规则和模型相关的,不同的模型计算方法是不一样的。大致的表格如下:

## 关于流式返回
流式返回的数据,返回行数-2=返回tokens
Expand All @@ -28,7 +28,7 @@ openai 的tokens计算规则适合模型先关的,不同的模型计算方法
## 使用示例
完整使用示例请参考:[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java)
结合chat模型使用示例:
### 结合chat模型使用示例:
完整示例参考:[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java)
```
public void chatTokensTest() {
Expand All @@ -51,7 +51,7 @@ openai 的tokens计算规则适合模型先关的,不同的模型计算方法
log.info("Open AI 官方计算的返回的tokens数{}", chatCompletionResponse.getUsage().getCompletionTokens());
}
```
单独使用示例:
### 单独使用示例:
```java
public class TikTokensTest {
String text;
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.13</version>
<version>1.0.14</version>
<name>chatgpt-java</name>
<description>OpenAI Java SDK, OpenAI Api for Java. ChatGPT Java SDK .</description>
<url>https://www.unfbx.com</url>
Expand Down Expand Up @@ -115,7 +115,7 @@
<dependency>
<groupId>com.knuddels</groupId>
<artifactId>jtokkit</artifactId>
<version>0.2.0</version>
<version>0.5.0</version>
</dependency>
</dependencies>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/unfbx/chatgpt/OpenAiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface OpenAiApi {
/**
* models 返回的数据id
*
* @param id
* @param id 模型主键
* @return Single Model
*/
@GET("v1/models/{id}")
Expand Down Expand Up @@ -314,7 +314,7 @@ Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,
/**
* 账户信息查询:里面包含总金额(美元)等信息
*
* @return
* @return 账户信息
*/
@GET("v1/dashboard/billing/subscription")
Single<Subscription> subscription();
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/unfbx/chatgpt/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class OpenAiClient {
/**
* 构造器
*
* @return
* @return OpenAiClient.Builder
*/
public static OpenAiClient.Builder builder() {
return new OpenAiClient.Builder();
Expand Down Expand Up @@ -189,8 +189,8 @@ public List<Model> models() {
/**
* openAi模型详细信息
*
* @param id
* @return Model
* @param id 模型主键
* @return Model 模型类
*/
public Model model(String id) {
if (Objects.isNull(id) || "".equals(id)) {
Expand Down Expand Up @@ -240,7 +240,7 @@ public EditResponse edit(Edit edit) {
/**
* 根据描述生成图片
*
* @param prompt
* @param prompt 描述信息
* @return ImageResponse
*/
public ImageResponse genImages(String prompt) {
Expand Down Expand Up @@ -407,7 +407,7 @@ private void checkImageSize(java.io.File image) {
/**
* 向量计算:单文本
*
* @param input
* @param input 单文本
* @return EmbeddingResponse
*/
public EmbeddingResponse embeddings(String input) {
Expand Down Expand Up @@ -480,7 +480,7 @@ public UploadFileResponse uploadFile(String purpose, java.io.File file) {
/**
* 上传文件
*
* @param file
* @param file 文件
* @return UploadFileResponse
*/
public UploadFileResponse uploadFile(java.io.File file) {
Expand Down Expand Up @@ -538,7 +538,7 @@ public ModerationResponse moderations(List<String> input) {
/**
* 文本审核
*
* @param moderation
* @param moderation 审核参数
* @return ModerationResponse
*/
public ModerationResponse moderations(Moderation moderation) {
Expand Down Expand Up @@ -592,7 +592,7 @@ public FineTuneResponse retrieveFineTune(String fineTuneId) {
/**
* 取消微调作业
*
* @param fineTuneId
* @param fineTuneId 主键
* @return FineTuneResponse
*/
public FineTuneResponse cancelFineTune(String fineTuneId) {
Expand All @@ -615,7 +615,7 @@ public List<Event> fineTuneEvents(String fineTuneId) {
* 删除微调作业模型
* Delete a fine-tuned model. You must have the Owner role in your organization.
*
* @param model
* @param model 模型名称
* @return FineTuneDeleteResponse
*/
public FineTuneDeleteResponse deleteFineTuneModel(String model) {
Expand Down Expand Up @@ -771,7 +771,7 @@ private void checkSpeechFileSize(java.io.File file) {
* ## 官方已经禁止使用此api
* OpenAi账户余额查询
*
* @return
* @return 余额
* @see #subscription()
* @see #billingUsage(LocalDate, LocalDate)
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/unfbx/chatgpt/OpenAiStreamClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public Subscription subscription() {
*
* @param starDate 开始时间
* @param endDate 结束时间
* @return
* @return 消耗金额信息
*/
public BillingUsage billingUsage(@NotNull LocalDate starDate, @NotNull LocalDate endDate) {
Single<BillingUsage> billingUsage = this.openAiApi.billingUsage(starDate, endDate);
Expand All @@ -320,7 +320,7 @@ public BillingUsage billingUsage(@NotNull LocalDate starDate, @NotNull LocalDate
/**
* 构造
*
* @return
* @return Builder
*/
public static OpenAiStreamClient.Builder builder() {
return new OpenAiStreamClient.Builder();
Expand Down Expand Up @@ -361,7 +361,7 @@ public Builder apiKey(@NotNull List<String> val) {

/**
* @param val api请求地址,结尾处有斜杠
* @return
* @return Builder
* @see com.unfbx.chatgpt.constant.OpenAIConst
*/
public Builder apiHost(String val) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -13,6 +14,7 @@
* @since 2023-04-08
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class BillingUsage {

@JsonProperty("object")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -13,6 +14,7 @@
* @since 2023-03-18
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CreditGrantsResponse implements Serializable {
private String object;
/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/DailyCost.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -12,6 +13,7 @@
* @since 2023-04-08
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class DailyCost {
/**
* 时间戳
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/Datum.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -12,6 +13,7 @@
* @since 2023-03-18
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Datum {
private String object;
private String id;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/Grants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -12,6 +13,7 @@
* @since 2023-03-18
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Grants {
private String object;
@JsonProperty("data")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/LineItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.math.BigDecimal;
Expand All @@ -11,6 +12,7 @@
* @since 2023-04-08
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class LineItem {
/**
* 模型名称
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/billing/Plan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

/**
Expand All @@ -9,6 +10,7 @@
* @since 2023-04-08
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Plan {
private String title;
private String id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -10,6 +11,7 @@
* @since 2023-04-08
*/
@Data
//@JsonIgnoreProperties(ignoreUnknown = true)
public class Subscription {
@JsonProperty("object")
private String object;
Expand Down Expand Up @@ -49,4 +51,6 @@ public class Subscription {
private Object billingAddress;
@JsonProperty("business_address")
private Object businessAddress;
@JsonProperty("primary")
private Boolean primary;
}
2 changes: 2 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/chat/ChatChoice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.chat;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -12,6 +13,7 @@
* @since 2023-03-02
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChatChoice implements Serializable {
private long index;
/**
Expand Down
Loading

0 comments on commit 47eac82

Please sign in to comment.