diff --git a/README.md b/README.md index 23c58f3..e7f287b 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122 二维码 | 二维码 | 二维码 --- ## 更新日志 +- [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配置。 diff --git a/pom.xml b/pom.xml index deb874e..6b37efb 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.unfbx chatgpt-java - 1.0.10 + 1.0.11 chatgpt-java OpenAI Java SDK, OpenAI Api for Java. ChatGPT Java SDK . https://www.unfbx.com diff --git a/src/main/java/com/unfbx/chatgpt/OpenAiApi.java b/src/main/java/com/unfbx/chatgpt/OpenAiApi.java index 7b1d6b5..e12675b 100644 --- a/src/main/java/com/unfbx/chatgpt/OpenAiApi.java +++ b/src/main/java/com/unfbx/chatgpt/OpenAiApi.java @@ -120,7 +120,7 @@ Single variationsImages(@Part() MultipartBody.Part image, ); /** - * Creates an embedding vector representing the input text. + * 文本向量计算 * * @param embedding * @return Single EmbeddingResponse diff --git a/src/main/java/com/unfbx/chatgpt/OpenAiClient.java b/src/main/java/com/unfbx/chatgpt/OpenAiClient.java index a53fd98..b79103b 100644 --- a/src/main/java/com/unfbx/chatgpt/OpenAiClient.java +++ b/src/main/java/com/unfbx/chatgpt/OpenAiClient.java @@ -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 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 input) { Embedding embedding = Embedding.builder().input(input).build(); return this.embeddings(embedding); } diff --git a/src/main/java/com/unfbx/chatgpt/entity/embeddings/Embedding.java b/src/main/java/com/unfbx/chatgpt/entity/embeddings/Embedding.java index 3421289..695ee0c 100644 --- a/src/main/java/com/unfbx/chatgpt/entity/embeddings/Embedding.java +++ b/src/main/java/com/unfbx/chatgpt/entity/embeddings/Embedding.java @@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j; import java.io.Serializable; +import java.util.List; import java.util.Objects; /** @@ -29,7 +30,7 @@ public class Embedding implements Serializable { * 必选项:长度不能超过:8192 */ @NonNull - private String input; + private List input; private String user; @@ -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; diff --git a/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java b/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java index e444534..7190fab 100644 --- a/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java +++ b/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java @@ -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; @@ -40,6 +42,7 @@ import java.net.InetSocketAddress; import java.net.Proxy; +import java.time.LocalDate; import java.util.*; import java.util.concurrent.TimeUnit; @@ -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() { @@ -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); } @@ -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); } diff --git a/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java b/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java index a0d7ce7..e3e3a53 100644 --- a/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java +++ b/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java @@ -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; @@ -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; @@ -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) @@ -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();