Skip to content

Commit e7354a4

Browse files
author
guorutao
committed
feature:1.0.11 支持tokens、增加新的余额查询、Embeddings支持数组传入, BUG修复
1 parent 622cbbf commit e7354a4

File tree

7 files changed

+69
-22
lines changed

7 files changed

+69
-22
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122
3131
<img src="https://user-images.githubusercontent.com/27008803/225246389-7b452214-f3fe-4a70-bd3e-832a0ed34288.jpg" width="210" height="300" alt="二维码" /> | <img src="https://g-photo.oss-cn-shanghai.aliyuncs.com/hd15.jpg" width="210" height="210" alt="二维码" /> | <img src="https://user-images.githubusercontent.com/27008803/225246581-15e90f78-5438-4637-8e7d-14c68ca13b59.jpg" width="210" height="300" alt="二维码" />
3232
---
3333
## 更新日志
34+
- [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计算慢的问题,
3435
- [x] 1.0.10 支持tokens计算:[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java) ,更多详细的资料参考文档:[Tokens_README.md](https://github.com/Grt1228/chatgpt-java/blob/main/Tokens_README.md)
3536
- [x] 1.0.9 支持自定义key使用策略参考:[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) ,弃用ChatGPTClient,优化Moderation接口
3637
- [x] 1.0.8 修改OpenAiClient和OpenAiStreamClient的自定义相关实现,超时设置,代理设置,自定义拦截器设置改为通过自定义OkHttpClient实现,将OkHttpClient交由用户自定义控制更加合理,可以实现更多的参数自定义。支持多Api Keys配置。

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.unfbx</groupId>
77
<artifactId>chatgpt-java</artifactId>
8-
<version>1.0.10</version>
8+
<version>1.0.11</version>
99
<name>chatgpt-java</name>
1010
<description>OpenAI Java SDK, OpenAI Api for Java. ChatGPT Java SDK .</description>
1111
<url>https://www.unfbx.com</url>

src/main/java/com/unfbx/chatgpt/OpenAiApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Single<ImageResponse> variationsImages(@Part() MultipartBody.Part image,
120120
);
121121

122122
/**
123-
* Creates an embedding vector representing the input text.
123+
* 文本向量计算
124124
*
125125
* @param embedding
126126
* @return Single EmbeddingResponse

src/main/java/com/unfbx/chatgpt/OpenAiClient.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,25 @@ private void checkImageSize(java.io.File image) {
382382
}
383383

384384
/**
385-
* Creates an embedding vector representing the input text.
385+
* 向量计算:单文本
386386
*
387387
* @param input
388388
* @return EmbeddingResponse
389389
*/
390390
public EmbeddingResponse embeddings(String input) {
391+
List<String> inputs = new ArrayList<>(1);
392+
inputs.add(input);
393+
Embedding embedding = Embedding.builder().input(inputs).build();
394+
return this.embeddings(embedding);
395+
}
396+
397+
/**
398+
* 向量计算:集合文本
399+
*
400+
* @param input
401+
* @return EmbeddingResponse
402+
*/
403+
public EmbeddingResponse embeddings(List<String> input) {
391404
Embedding embedding = Embedding.builder().input(input).build();
392405
return this.embeddings(embedding);
393406
}

src/main/java/com/unfbx/chatgpt/entity/embeddings/Embedding.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.extern.slf4j.Slf4j;
88

99
import java.io.Serializable;
10+
import java.util.List;
1011
import java.util.Objects;
1112

1213
/**
@@ -29,7 +30,7 @@ public class Embedding implements Serializable {
2930
* 必选项:长度不能超过:8192
3031
*/
3132
@NonNull
32-
private String input;
33+
private List<String> input;
3334

3435
private String user;
3536

@@ -40,17 +41,6 @@ public void setModel(Model model) {
4041
this.model = model.getName();
4142
}
4243

43-
public void setInput(String input) {
44-
if (input == null || "".equals(input)) {
45-
log.error("input不能为空");
46-
throw new BaseException(CommonError.PARAM_ERROR);
47-
}
48-
if (input.length() > 8192) {
49-
log.error("input超长");
50-
throw new BaseException(CommonError.PARAM_ERROR);
51-
}
52-
this.input = input;
53-
}
5444

5545
public void setUser(String user) {
5646
this.user = user;

src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.fasterxml.jackson.databind.DeserializationFeature;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.fasterxml.jackson.databind.SerializationFeature;
8+
import com.unfbx.chatgpt.entity.billing.BillingUsage;
89
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
10+
import com.unfbx.chatgpt.entity.billing.Subscription;
911
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
1012
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
1113
import com.unfbx.chatgpt.entity.chat.Message;
@@ -40,6 +42,7 @@
4042

4143
import java.net.InetSocketAddress;
4244
import java.net.Proxy;
45+
import java.time.LocalDate;
4346
import java.util.*;
4447
import java.util.concurrent.TimeUnit;
4548

@@ -78,10 +81,26 @@ public void before() {
7881
//.keyStrategy(new KeyRandomStrategy())
7982
.keyStrategy(new FirstKeyStrategy())
8083
.okHttpClient(okHttpClient)
81-
//自己做了代理就传代理地址,没有可不不传
84+
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
8285
// .apiHost("https://自己代理的服务器地址/")
8386
.build();
8487
}
88+
@Test
89+
public void subscription() {
90+
Subscription subscription = v2.subscription();
91+
log.info("用户名:{}", subscription.getAccountName());
92+
log.info("用户总余额(美元):{}", subscription.getHardLimitUsd());
93+
log.info("更多信息看Subscription类");
94+
}
95+
96+
@Test
97+
public void billingUsage() {
98+
LocalDate start = LocalDate.of(2023, 3, 7);
99+
BillingUsage billingUsage = v2.billingUsage(start, LocalDate.now());
100+
log.info("总使用金额(美分):{}", billingUsage.getTotalUsage());
101+
log.info("更多信息看BillingUsage类");
102+
}
103+
85104

86105
@Test
87106
public void chatTokensTest() {
@@ -229,7 +248,7 @@ public void editText() {
229248

230249
@Test
231250
public void genImages() {
232-
Image image = Image.builder().prompt("电脑画面").build();
251+
Image image = Image.builder().prompt("电脑画面").responseFormat(ResponseFormat.B64_JSON.getName()).build();
233252
ImageResponse imageResponse = v2.genImages(image);
234253
System.out.println(imageResponse);
235254
}
@@ -283,15 +302,20 @@ public void variationsImages() {
283302

284303
@Test
285304
public void embeddingsV2() {
286-
Embedding embedding = Embedding.builder().input("我爱你亲爱的姑娘").build();
305+
Embedding embedding = Embedding.builder().input(Arrays.asList("我爱你亲爱的姑娘", "i love you")).build();
287306
EmbeddingResponse embeddings = v2.embeddings(embedding);
288307
System.out.println(embeddings);
289308
}
290309

310+
@Test
311+
public void embeddingsV3() {
312+
EmbeddingResponse embeddings = v2.embeddings(Arrays.asList("我爱你亲爱的姑娘", "i love you"));
313+
System.out.println(embeddings);
314+
}
291315

292316
@Test
293317
public void embeddings() {
294-
EmbeddingResponse embeddings = v2.embeddings("The food was delicious and the waiter...");
318+
EmbeddingResponse embeddings = v2.embeddings("我爱你");
295319
System.out.println(embeddings);
296320
}
297321

src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.unfbx.chatgpt;
22

3+
import com.unfbx.chatgpt.entity.billing.BillingUsage;
34
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
5+
import com.unfbx.chatgpt.entity.billing.Subscription;
46
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
57
import com.unfbx.chatgpt.entity.chat.Message;
68
import com.unfbx.chatgpt.entity.completions.Completion;
@@ -15,6 +17,7 @@
1517

1618
import java.net.InetSocketAddress;
1719
import java.net.Proxy;
20+
import java.time.LocalDate;
1821
import java.util.Arrays;
1922
import java.util.concurrent.CountDownLatch;
2023
import java.util.concurrent.TimeUnit;
@@ -33,14 +36,14 @@ public class OpenAiStreamClientTest {
3336
@Before
3437
public void before() {
3538
//国内访问需要做代理,国外服务器不需要
36-
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
39+
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
3740
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
3841
//!!!!千万别再生产或者测试环境打开BODY级别日志!!!!
3942
//!!!生产或者测试环境建议设置为这三种级别:NONE,BASIC,HEADERS,!!!
4043
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
4144
OkHttpClient okHttpClient = new OkHttpClient
4245
.Builder()
43-
.proxy(proxy)
46+
// .proxy(proxy)
4447
.addInterceptor(httpLoggingInterceptor)
4548
.connectTimeout(30, TimeUnit.SECONDS)
4649
.writeTimeout(30, TimeUnit.SECONDS)
@@ -52,10 +55,26 @@ public void before() {
5255
// .keyStrategy(new KeyRandomStrategy())
5356
.keyStrategy(new FirstKeyStrategy())
5457
.okHttpClient(okHttpClient)
55-
//自己做了代理就传代理地址,没有可不不传
58+
//自己做了代理就传代理地址,没有可不不传((关注公众号回复:openai ,获取免费的测试代理地址))
5659
// .apiHost("https://自己代理的服务器地址/")
5760
.build();
5861
}
62+
63+
@Test
64+
public void subscription() {
65+
Subscription subscription = client.subscription();
66+
log.info("用户名:{}", subscription.getAccountName());
67+
log.info("用户总余额(美元):{}", subscription.getHardLimitUsd());
68+
log.info("更多信息看Subscription类");
69+
}
70+
71+
@Test
72+
public void billingUsage() {
73+
LocalDate start = LocalDate.of(2023, 3, 7);
74+
BillingUsage billingUsage = client.billingUsage(start, LocalDate.now());
75+
log.info("总使用金额(美分):{}", billingUsage.getTotalUsage());
76+
log.info("更多信息看BillingUsage类");
77+
}
5978
@Test
6079
public void creditGrants() {
6180
CreditGrantsResponse creditGrantsResponse = client.creditGrants();

0 commit comments

Comments
 (0)