Skip to content

Commit

Permalink
feature:1.0.11 支持tokens、增加新的余额查询、Embeddings支持数组传入, BUG修复
Browse files Browse the repository at this point in the history
  • Loading branch information
guorutao committed Apr 9, 2023
1 parent 622cbbf commit e7354a4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122
<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="二维码" />
---
## 更新日志
- [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计算慢的问题,
- [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)
- [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接口
- [x] 1.0.8 修改OpenAiClient和OpenAiStreamClient的自定义相关实现,超时设置,代理设置,自定义拦截器设置改为通过自定义OkHttpClient实现,将OkHttpClient交由用户自定义控制更加合理,可以实现更多的参数自定义。支持多Api Keys配置。
Expand Down
2 changes: 1 addition & 1 deletion 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.10</version>
<version>1.0.11</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
2 changes: 1 addition & 1 deletion src/main/java/com/unfbx/chatgpt/OpenAiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Single<ImageResponse> variationsImages(@Part() MultipartBody.Part image,
);

/**
* Creates an embedding vector representing the input text.
* 文本向量计算
*
* @param embedding
* @return Single EmbeddingResponse
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/unfbx/chatgpt/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,25 @@ private void checkImageSize(java.io.File image) {
}

/**
* Creates an embedding vector representing the input text.
* 向量计算:单文本
*
* @param input
* @return EmbeddingResponse
*/
public EmbeddingResponse embeddings(String input) {
List<String> inputs = new ArrayList<>(1);
inputs.add(input);
Embedding embedding = Embedding.builder().input(inputs).build();
return this.embeddings(embedding);
}

/**
* 向量计算:集合文本
*
* @param input
* @return EmbeddingResponse
*/
public EmbeddingResponse embeddings(List<String> input) {
Embedding embedding = Embedding.builder().input(input).build();
return this.embeddings(embedding);
}
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/unfbx/chatgpt/entity/embeddings/Embedding.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -29,7 +30,7 @@ public class Embedding implements Serializable {
* 必选项:长度不能超过:8192
*/
@NonNull
private String input;
private List<String> input;

private String user;

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

public void setInput(String input) {
if (input == null || "".equals(input)) {
log.error("input不能为空");
throw new BaseException(CommonError.PARAM_ERROR);
}
if (input.length() > 8192) {
log.error("input超长");
throw new BaseException(CommonError.PARAM_ERROR);
}
this.input = input;
}

public void setUser(String user) {
this.user = user;
Expand Down
32 changes: 28 additions & 4 deletions src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.unfbx.chatgpt.entity.billing.BillingUsage;
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
import com.unfbx.chatgpt.entity.billing.Subscription;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import com.unfbx.chatgpt.entity.chat.Message;
Expand Down Expand Up @@ -40,6 +42,7 @@

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -78,10 +81,26 @@ public void before() {
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
// .apiHost("https://自己代理的服务器地址/")
.build();
}
@Test
public void subscription() {
Subscription subscription = v2.subscription();
log.info("用户名:{}", subscription.getAccountName());
log.info("用户总余额(美元):{}", subscription.getHardLimitUsd());
log.info("更多信息看Subscription类");
}

@Test
public void billingUsage() {
LocalDate start = LocalDate.of(2023, 3, 7);
BillingUsage billingUsage = v2.billingUsage(start, LocalDate.now());
log.info("总使用金额(美分):{}", billingUsage.getTotalUsage());
log.info("更多信息看BillingUsage类");
}


@Test
public void chatTokensTest() {
Expand Down Expand Up @@ -229,7 +248,7 @@ public void editText() {

@Test
public void genImages() {
Image image = Image.builder().prompt("电脑画面").build();
Image image = Image.builder().prompt("电脑画面").responseFormat(ResponseFormat.B64_JSON.getName()).build();
ImageResponse imageResponse = v2.genImages(image);
System.out.println(imageResponse);
}
Expand Down Expand Up @@ -283,15 +302,20 @@ public void variationsImages() {

@Test
public void embeddingsV2() {
Embedding embedding = Embedding.builder().input("我爱你亲爱的姑娘").build();
Embedding embedding = Embedding.builder().input(Arrays.asList("我爱你亲爱的姑娘", "i love you")).build();
EmbeddingResponse embeddings = v2.embeddings(embedding);
System.out.println(embeddings);
}

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

@Test
public void embeddings() {
EmbeddingResponse embeddings = v2.embeddings("The food was delicious and the waiter...");
EmbeddingResponse embeddings = v2.embeddings("我爱你");
System.out.println(embeddings);
}

Expand Down
25 changes: 22 additions & 3 deletions src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.unfbx.chatgpt;

import com.unfbx.chatgpt.entity.billing.BillingUsage;
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
import com.unfbx.chatgpt.entity.billing.Subscription;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.Message;
import com.unfbx.chatgpt.entity.completions.Completion;
Expand All @@ -15,6 +17,7 @@

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand All @@ -33,14 +36,14 @@ public class OpenAiStreamClientTest {
@Before
public void before() {
//国内访问需要做代理,国外服务器不需要
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
//!!!!千万别再生产或者测试环境打开BODY级别日志!!!!
//!!!生产或者测试环境建议设置为这三种级别:NONE,BASIC,HEADERS,!!!
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.proxy(proxy)
// .proxy(proxy)
.addInterceptor(httpLoggingInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
Expand All @@ -52,10 +55,26 @@ public void before() {
// .keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传
//自己做了代理就传代理地址,没有可不不传((关注公众号回复:openai ,获取免费的测试代理地址))
// .apiHost("https://自己代理的服务器地址/")
.build();
}

@Test
public void subscription() {
Subscription subscription = client.subscription();
log.info("用户名:{}", subscription.getAccountName());
log.info("用户总余额(美元):{}", subscription.getHardLimitUsd());
log.info("更多信息看Subscription类");
}

@Test
public void billingUsage() {
LocalDate start = LocalDate.of(2023, 3, 7);
BillingUsage billingUsage = client.billingUsage(start, LocalDate.now());
log.info("总使用金额(美分):{}", billingUsage.getTotalUsage());
log.info("更多信息看BillingUsage类");
}
@Test
public void creditGrants() {
CreditGrantsResponse creditGrantsResponse = client.creditGrants();
Expand Down

0 comments on commit e7354a4

Please sign in to comment.