From 7d28fd1e34653204089c6cfe70600f29a07afb80 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 18 Jul 2023 11:20:55 +0800 Subject: [PATCH 1/6] Rename some entity --- .../org/devlive/sdk/openai/DefaultApi.java | 4 ++-- .../org/devlive/sdk/openai/DefaultClient.java | 4 ++-- ...mpletionChatEntity.java => ChatEntity.java} | 18 +++++++++--------- .../sdk/openai/entity/ChoiceChatEntity.java | 2 +- ...onMessageEntity.java => MessageEntity.java} | 14 +++++++------- .../sdk/openai/AzureOpenAiClientTest.java | 12 ++++++------ .../devlive/sdk/openai/OpenAiClientTest.java | 12 ++++++------ ...ChatEntityTest.java => ChatEntityTest.java} | 8 ++++---- 8 files changed, 37 insertions(+), 37 deletions(-) rename src/main/java/org/devlive/sdk/openai/entity/{CompletionChatEntity.java => ChatEntity.java} (87%) rename src/main/java/org/devlive/sdk/openai/entity/{CompletionMessageEntity.java => MessageEntity.java} (82%) rename src/test/java/org/devlive/sdk/openai/entity/{CompletionChatEntityTest.java => ChatEntityTest.java} (72%) diff --git a/src/main/java/org/devlive/sdk/openai/DefaultApi.java b/src/main/java/org/devlive/sdk/openai/DefaultApi.java index e70e40f..6bb121e 100644 --- a/src/main/java/org/devlive/sdk/openai/DefaultApi.java +++ b/src/main/java/org/devlive/sdk/openai/DefaultApi.java @@ -3,7 +3,7 @@ import io.reactivex.Single; import okhttp3.MultipartBody; import okhttp3.RequestBody; -import org.devlive.sdk.openai.entity.CompletionChatEntity; +import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; import org.devlive.sdk.openai.entity.EmbeddingEntity; import org.devlive.sdk.openai.entity.ImageEntity; @@ -57,7 +57,7 @@ Single fetchCompletions(@Url String url, */ @POST Single fetchChatCompletions(@Url String url, - @Body CompletionChatEntity configure); + @Body ChatEntity configure); /** * Get all keys diff --git a/src/main/java/org/devlive/sdk/openai/DefaultClient.java b/src/main/java/org/devlive/sdk/openai/DefaultClient.java index 7eedebd..0ef32e9 100644 --- a/src/main/java/org/devlive/sdk/openai/DefaultClient.java +++ b/src/main/java/org/devlive/sdk/openai/DefaultClient.java @@ -5,7 +5,7 @@ import okhttp3.OkHttpClient; import org.apache.commons.lang3.ObjectUtils; import org.devlive.sdk.openai.entity.AudioEntity; -import org.devlive.sdk.openai.entity.CompletionChatEntity; +import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; import org.devlive.sdk.openai.entity.EmbeddingEntity; import org.devlive.sdk.openai.entity.ImageEntity; @@ -50,7 +50,7 @@ public CompleteResponse createCompletion(CompletionEntity configure) .blockingGet(); } - public CompleteChatResponse createChatCompletion(CompletionChatEntity configure) + public CompleteChatResponse createChatCompletion(ChatEntity configure) { return this.api.fetchChatCompletions(ProviderUtils.getUrl(provider, UrlModel.FETCH_CHAT_COMPLETIONS), configure) .blockingGet(); diff --git a/src/main/java/org/devlive/sdk/openai/entity/CompletionChatEntity.java b/src/main/java/org/devlive/sdk/openai/entity/ChatEntity.java similarity index 87% rename from src/main/java/org/devlive/sdk/openai/entity/CompletionChatEntity.java rename to src/main/java/org/devlive/sdk/openai/entity/ChatEntity.java index 8c16b76..6d71877 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/CompletionChatEntity.java +++ b/src/main/java/org/devlive/sdk/openai/entity/ChatEntity.java @@ -21,13 +21,13 @@ @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class CompletionChatEntity +public class ChatEntity { @JsonProperty(value = "model") private String model; @JsonProperty(value = "messages") - private List messages; + private List messages; @JsonProperty(value = "temperature") private Double temperature; @@ -38,7 +38,7 @@ public class CompletionChatEntity @JsonProperty(value = "top_p") private Double topP; - private CompletionChatEntity(CompletionChatEntityBuilder builder) + private ChatEntity(ChatEntityBuilder builder) { if (ObjectUtils.isEmpty(builder.model)) { builder.model(CompletionModel.GPT_35_TURBO.getName()); @@ -62,9 +62,9 @@ private CompletionChatEntity(CompletionChatEntityBuilder builder) this.topP = builder.topP; } - public static class CompletionChatEntityBuilder + public static class ChatEntityBuilder { - public CompletionChatEntityBuilder model(String model) + public ChatEntityBuilder model(String model) { if (StringUtils.isEmpty(model)) { model = CompletionModel.GPT_35_TURBO.getName(); @@ -94,7 +94,7 @@ public CompletionChatEntityBuilder model(String model) return this; } - public CompletionChatEntityBuilder temperature(Double temperature) + public ChatEntityBuilder temperature(Double temperature) { if (temperature < 0 || temperature > 2) { throw new ParamException(String.format("Invalid temperature: %s , between 0 and 2", temperature)); @@ -103,7 +103,7 @@ public CompletionChatEntityBuilder temperature(Double temperature) return this; } - public CompletionChatEntityBuilder maxTokens(Integer maxTokens) + public ChatEntityBuilder maxTokens(Integer maxTokens) { CompletionModel completionModel = EnumsUtils.getCompleteModel(this.model); if (ObjectUtils.isNotEmpty(this.model) && maxTokens > completionModel.getMaxTokens()) { @@ -113,9 +113,9 @@ public CompletionChatEntityBuilder maxTokens(Integer maxTokens) return this; } - public CompletionChatEntity build() + public ChatEntity build() { - return new CompletionChatEntity(this); + return new ChatEntity(this); } } } diff --git a/src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java b/src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java index 995d2a9..71b32c4 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java +++ b/src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java @@ -20,7 +20,7 @@ public class ChoiceChatEntity private String index; @JsonProperty(value = "message") - private CompletionMessageEntity message; + private MessageEntity message; @JsonProperty(value = "finish_reason") private String finishReason; diff --git a/src/main/java/org/devlive/sdk/openai/entity/CompletionMessageEntity.java b/src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java similarity index 82% rename from src/main/java/org/devlive/sdk/openai/entity/CompletionMessageEntity.java rename to src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java index 2d22a13..f2ee1af 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/CompletionMessageEntity.java +++ b/src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java @@ -19,7 +19,7 @@ @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class CompletionMessageEntity +public class MessageEntity { @JsonProperty(value = "role") private String role; @@ -30,7 +30,7 @@ public class CompletionMessageEntity @JsonProperty(value = "name") private String name; - private CompletionMessageEntity(CompletionMessageEntityBuilder builder) + private MessageEntity(MessageEntityBuilder builder) { if (StringUtils.isEmpty(builder.role)) { builder.role(CompletionMessageModel.USER.getName()); @@ -48,9 +48,9 @@ private CompletionMessageEntity(CompletionMessageEntityBuilder builder) this.name = builder.name; } - public static class CompletionMessageEntityBuilder + public static class MessageEntityBuilder { - public CompletionMessageEntityBuilder role(String role) + public MessageEntityBuilder role(String role) { CompletionMessageModel completionMessageModel = EnumsUtils.getCompleteMessageModel(role); if (ObjectUtils.isEmpty(completionMessageModel)) { @@ -60,7 +60,7 @@ public CompletionMessageEntityBuilder role(String role) return this; } - public CompletionMessageEntityBuilder content(String content) + public MessageEntityBuilder content(String content) { if (StringUtils.isEmpty(content)) { throw new ParamException("Content must not be empty"); @@ -69,9 +69,9 @@ public CompletionMessageEntityBuilder content(String content) return this; } - public CompletionMessageEntity build() + public MessageEntity build() { - return new CompletionMessageEntity(this); + return new MessageEntity(this); } } } diff --git a/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java b/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java index 3e0dfbb..b2414ef 100644 --- a/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java @@ -1,9 +1,9 @@ package org.devlive.sdk.openai; import com.google.common.collect.Lists; -import org.devlive.sdk.openai.entity.CompletionChatEntity; +import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; -import org.devlive.sdk.openai.entity.CompletionMessageEntity; +import org.devlive.sdk.openai.entity.MessageEntity; import org.devlive.sdk.openai.exception.RequestException; import org.devlive.sdk.openai.model.CompletionModel; import org.devlive.sdk.openai.model.ProviderModel; @@ -58,12 +58,12 @@ public void testCreateChatCompletion() .version("2023-03-15-preview") .build(); - List messages = Lists.newArrayList(); - messages.add(CompletionMessageEntity.builder() + List messages = Lists.newArrayList(); + messages.add(MessageEntity.builder() .content("Hello, my name is openai-java-sdk") .build()); - CompletionChatEntity configure = CompletionChatEntity.builder() + ChatEntity configure = ChatEntity.builder() .messages(messages) .build(); @@ -71,7 +71,7 @@ public void testCreateChatCompletion() .getChoices() .forEach(choice -> messages.add(choice.getMessage())); - messages.add(CompletionMessageEntity.builder() + messages.add(MessageEntity.builder() .content("What is my name?") .build()); diff --git a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java index 372b83b..a6a1d0a 100644 --- a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java @@ -3,9 +3,9 @@ import com.google.common.collect.Lists; import okhttp3.OkHttpClient; import org.devlive.sdk.openai.entity.AudioEntity; -import org.devlive.sdk.openai.entity.CompletionChatEntity; +import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; -import org.devlive.sdk.openai.entity.CompletionMessageEntity; +import org.devlive.sdk.openai.entity.MessageEntity; import org.devlive.sdk.openai.entity.EmbeddingEntity; import org.devlive.sdk.openai.entity.ImageEntity; import org.devlive.sdk.openai.entity.ModerationEntity; @@ -95,12 +95,12 @@ public void testCreateCompletion() @Test public void testCreateChatCompletion() { - List messages = Lists.newArrayList(); - messages.add(CompletionMessageEntity.builder() + List messages = Lists.newArrayList(); + messages.add(MessageEntity.builder() .content("Hello, my name is openai-java-sdk") .build()); - CompletionChatEntity configure = CompletionChatEntity.builder() + ChatEntity configure = ChatEntity.builder() .messages(messages) .build(); @@ -108,7 +108,7 @@ public void testCreateChatCompletion() .getChoices() .forEach(choice -> messages.add(choice.getMessage())); - messages.add(CompletionMessageEntity.builder() + messages.add(MessageEntity.builder() .content("What is my name?") .build()); diff --git a/src/test/java/org/devlive/sdk/openai/entity/CompletionChatEntityTest.java b/src/test/java/org/devlive/sdk/openai/entity/ChatEntityTest.java similarity index 72% rename from src/test/java/org/devlive/sdk/openai/entity/CompletionChatEntityTest.java rename to src/test/java/org/devlive/sdk/openai/entity/ChatEntityTest.java index 18c03f5..89a3c67 100644 --- a/src/test/java/org/devlive/sdk/openai/entity/CompletionChatEntityTest.java +++ b/src/test/java/org/devlive/sdk/openai/entity/ChatEntityTest.java @@ -6,21 +6,21 @@ import org.junit.Assert; import org.junit.Test; -public class CompletionChatEntityTest +public class ChatEntityTest { @Test public void testModel() { - CompletionChatEntity entity = CompletionChatEntity.builder() + ChatEntity entity = ChatEntity.builder() .build(); Assert.assertTrue(entity.getModel().equals(CompletionModel.GPT_35_TURBO.getName())); - entity = CompletionChatEntity.builder() + entity = ChatEntity.builder() .model(CompletionModel.GPT_4.getName()) .build(); Assert.assertTrue(entity.getModel().equals(CompletionModel.GPT_4.getName())); - Assert.assertThrows(ParamException.class, () -> CompletionChatEntity.builder() + Assert.assertThrows(ParamException.class, () -> ChatEntity.builder() .model(CompletionModel.TEXT_ADA_001.getName()) .build()); } From 0136545a46da5386d5a8b0105201a856757a84c2 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 18 Jul 2023 11:26:09 +0800 Subject: [PATCH 2/6] Split choice --- src/main/java/org/devlive/sdk/openai/DefaultApi.java | 6 +++--- src/main/java/org/devlive/sdk/openai/DefaultClient.java | 4 ++-- .../{entity/ChoiceEntity.java => choice/BasicChoice.java} | 4 ++-- .../ChoiceChatEntity.java => choice/ChatChoice.java} | 5 +++-- .../java/org/devlive/sdk/openai/entity/MessageEntity.java | 8 ++++---- .../{CompletionMessageModel.java => MessageModel.java} | 4 ++-- .../{CompleteChatResponse.java => ChatResponse.java} | 6 +++--- .../org/devlive/sdk/openai/response/CompleteResponse.java | 4 ++-- .../java/org/devlive/sdk/openai/utils/EnumsUtils.java | 6 +++--- .../java/org/devlive/sdk/openai/OpenAiClientTest.java | 1 + 10 files changed, 25 insertions(+), 23 deletions(-) rename src/main/java/org/devlive/sdk/openai/{entity/ChoiceEntity.java => choice/BasicChoice.java} (90%) rename src/main/java/org/devlive/sdk/openai/{entity/ChoiceChatEntity.java => choice/ChatChoice.java} (83%) rename src/main/java/org/devlive/sdk/openai/model/{CompletionMessageModel.java => MessageModel.java} (78%) rename src/main/java/org/devlive/sdk/openai/response/{CompleteChatResponse.java => ChatResponse.java} (87%) diff --git a/src/main/java/org/devlive/sdk/openai/DefaultApi.java b/src/main/java/org/devlive/sdk/openai/DefaultApi.java index 6bb121e..2457083 100644 --- a/src/main/java/org/devlive/sdk/openai/DefaultApi.java +++ b/src/main/java/org/devlive/sdk/openai/DefaultApi.java @@ -11,7 +11,7 @@ import org.devlive.sdk.openai.entity.ModerationEntity; import org.devlive.sdk.openai.entity.UserKeyEntity; import org.devlive.sdk.openai.response.AudioResponse; -import org.devlive.sdk.openai.response.CompleteChatResponse; +import org.devlive.sdk.openai.response.ChatResponse; import org.devlive.sdk.openai.response.CompleteResponse; import org.devlive.sdk.openai.response.EmbeddingResponse; import org.devlive.sdk.openai.response.ImageResponse; @@ -56,8 +56,8 @@ Single fetchCompletions(@Url String url, * Creates a model response for the given chat conversation. */ @POST - Single fetchChatCompletions(@Url String url, - @Body ChatEntity configure); + Single fetchChatCompletions(@Url String url, + @Body ChatEntity configure); /** * Get all keys diff --git a/src/main/java/org/devlive/sdk/openai/DefaultClient.java b/src/main/java/org/devlive/sdk/openai/DefaultClient.java index 0ef32e9..f8d3971 100644 --- a/src/main/java/org/devlive/sdk/openai/DefaultClient.java +++ b/src/main/java/org/devlive/sdk/openai/DefaultClient.java @@ -15,7 +15,7 @@ import org.devlive.sdk.openai.model.ProviderModel; import org.devlive.sdk.openai.model.UrlModel; import org.devlive.sdk.openai.response.AudioResponse; -import org.devlive.sdk.openai.response.CompleteChatResponse; +import org.devlive.sdk.openai.response.ChatResponse; import org.devlive.sdk.openai.response.CompleteResponse; import org.devlive.sdk.openai.response.EmbeddingResponse; import org.devlive.sdk.openai.response.ImageResponse; @@ -50,7 +50,7 @@ public CompleteResponse createCompletion(CompletionEntity configure) .blockingGet(); } - public CompleteChatResponse createChatCompletion(ChatEntity configure) + public ChatResponse createChatCompletion(ChatEntity configure) { return this.api.fetchChatCompletions(ProviderUtils.getUrl(provider, UrlModel.FETCH_CHAT_COMPLETIONS), configure) .blockingGet(); diff --git a/src/main/java/org/devlive/sdk/openai/entity/ChoiceEntity.java b/src/main/java/org/devlive/sdk/openai/choice/BasicChoice.java similarity index 90% rename from src/main/java/org/devlive/sdk/openai/entity/ChoiceEntity.java rename to src/main/java/org/devlive/sdk/openai/choice/BasicChoice.java index aa53ee2..a118583 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/ChoiceEntity.java +++ b/src/main/java/org/devlive/sdk/openai/choice/BasicChoice.java @@ -1,4 +1,4 @@ -package org.devlive.sdk.openai.entity; +package org.devlive.sdk.openai.choice; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -14,7 +14,7 @@ @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class ChoiceEntity +public class BasicChoice { @JsonProperty(value = "text") private String content; diff --git a/src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java b/src/main/java/org/devlive/sdk/openai/choice/ChatChoice.java similarity index 83% rename from src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java rename to src/main/java/org/devlive/sdk/openai/choice/ChatChoice.java index 71b32c4..bb76e91 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/ChoiceChatEntity.java +++ b/src/main/java/org/devlive/sdk/openai/choice/ChatChoice.java @@ -1,4 +1,4 @@ -package org.devlive.sdk.openai.entity; +package org.devlive.sdk.openai.choice; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -7,6 +7,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; +import org.devlive.sdk.openai.entity.MessageEntity; @Data @Builder @@ -14,7 +15,7 @@ @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class ChoiceChatEntity +public class ChatChoice { @JsonProperty(value = "index") private String index; diff --git a/src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java b/src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java index f2ee1af..8eb9984 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java +++ b/src/main/java/org/devlive/sdk/openai/entity/MessageEntity.java @@ -10,7 +10,7 @@ import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.devlive.sdk.openai.exception.ParamException; -import org.devlive.sdk.openai.model.CompletionMessageModel; +import org.devlive.sdk.openai.model.MessageModel; import org.devlive.sdk.openai.utils.EnumsUtils; @Data @@ -33,7 +33,7 @@ public class MessageEntity private MessageEntity(MessageEntityBuilder builder) { if (StringUtils.isEmpty(builder.role)) { - builder.role(CompletionMessageModel.USER.getName()); + builder.role(MessageModel.USER.getName()); } this.role = builder.role; @@ -52,8 +52,8 @@ public static class MessageEntityBuilder { public MessageEntityBuilder role(String role) { - CompletionMessageModel completionMessageModel = EnumsUtils.getCompleteMessageModel(role); - if (ObjectUtils.isEmpty(completionMessageModel)) { + MessageModel messageModel = EnumsUtils.getCompleteMessageModel(role); + if (ObjectUtils.isEmpty(messageModel)) { throw new ParamException(String.format("Not support completion role %s", role)); } this.role = role; diff --git a/src/main/java/org/devlive/sdk/openai/model/CompletionMessageModel.java b/src/main/java/org/devlive/sdk/openai/model/MessageModel.java similarity index 78% rename from src/main/java/org/devlive/sdk/openai/model/CompletionMessageModel.java rename to src/main/java/org/devlive/sdk/openai/model/MessageModel.java index c29ae94..fabdf0f 100644 --- a/src/main/java/org/devlive/sdk/openai/model/CompletionMessageModel.java +++ b/src/main/java/org/devlive/sdk/openai/model/MessageModel.java @@ -1,6 +1,6 @@ package org.devlive.sdk.openai.model; -public enum CompletionMessageModel +public enum MessageModel { SYSTEM("system"), USER("user"), @@ -14,7 +14,7 @@ public String getName() return name; } - CompletionMessageModel(String name) + MessageModel(String name) { this.name = name; } diff --git a/src/main/java/org/devlive/sdk/openai/response/CompleteChatResponse.java b/src/main/java/org/devlive/sdk/openai/response/ChatResponse.java similarity index 87% rename from src/main/java/org/devlive/sdk/openai/response/CompleteChatResponse.java rename to src/main/java/org/devlive/sdk/openai/response/ChatResponse.java index d5682d6..01cd490 100644 --- a/src/main/java/org/devlive/sdk/openai/response/CompleteChatResponse.java +++ b/src/main/java/org/devlive/sdk/openai/response/ChatResponse.java @@ -8,7 +8,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; -import org.devlive.sdk.openai.entity.ChoiceChatEntity; +import org.devlive.sdk.openai.choice.ChatChoice; import org.devlive.sdk.openai.entity.UsageEntity; import java.util.List; @@ -19,7 +19,7 @@ @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class CompleteChatResponse +public class ChatResponse { @JsonProperty(value = "id") private String name; @@ -35,7 +35,7 @@ public class CompleteChatResponse private String model; @JsonProperty(value = "choices") - private List choices; + private List choices; @JsonProperty(value = "usage") private UsageEntity usage; diff --git a/src/main/java/org/devlive/sdk/openai/response/CompleteResponse.java b/src/main/java/org/devlive/sdk/openai/response/CompleteResponse.java index 19fb711..7511ee8 100644 --- a/src/main/java/org/devlive/sdk/openai/response/CompleteResponse.java +++ b/src/main/java/org/devlive/sdk/openai/response/CompleteResponse.java @@ -8,7 +8,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; -import org.devlive.sdk.openai.entity.ChoiceEntity; +import org.devlive.sdk.openai.choice.BasicChoice; import org.devlive.sdk.openai.entity.UsageEntity; import java.util.List; @@ -35,7 +35,7 @@ public class CompleteResponse private String model; @JsonProperty(value = "choices") - private List choices; + private List choices; @JsonProperty(value = "usage") private UsageEntity usage; diff --git a/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java b/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java index eebfb36..03ec032 100644 --- a/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java +++ b/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java @@ -1,6 +1,6 @@ package org.devlive.sdk.openai.utils; -import org.devlive.sdk.openai.model.CompletionMessageModel; +import org.devlive.sdk.openai.model.MessageModel; import org.devlive.sdk.openai.model.CompletionModel; import java.util.Arrays; @@ -20,9 +20,9 @@ public static CompletionModel getCompleteModel(final String name) return optional.isPresent() ? optional.get() : null; } - public static CompletionMessageModel getCompleteMessageModel(final String name) + public static MessageModel getCompleteMessageModel(final String name) { - Optional optional = Arrays.stream(CompletionMessageModel.values()) + Optional optional = Arrays.stream(MessageModel.values()) .filter(item -> item.getName().equals(name)) .findFirst(); return optional.isPresent() ? optional.get() : null; diff --git a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java index a6a1d0a..29487a6 100644 --- a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java @@ -33,6 +33,7 @@ public void before() { client = OpenAiClient.builder() .apiKey(System.getProperty("openai.token")) + .apiKey("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") .build(); } From 28e12f2a81f9f33cebef0a6b5d89c0365ace48b6 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 18 Jul 2023 13:15:43 +0800 Subject: [PATCH 3/6] Support edits --- docs/docs/reference/edits.md | 51 ++++++ docs/docs/reference/edits.zh.md | 51 ++++++ docs/mkdocs.yml | 1 + .../org/devlive/sdk/openai/DefaultApi.java | 10 ++ .../org/devlive/sdk/openai/DefaultClient.java | 8 + .../devlive/sdk/openai/entity/EditEntity.java | 149 ++++++++++++++++++ .../devlive/sdk/openai/model/EditModel.java | 19 +++ .../devlive/sdk/openai/model/UrlModel.java | 3 +- .../sdk/openai/response/EditResponse.java | 36 +++++ .../sdk/openai/utils/ProviderUtils.java | 1 + .../devlive/sdk/openai/OpenAiClientTest.java | 15 +- .../sdk/openai/entity/EditEntityTest.java | 15 ++ 12 files changed, 357 insertions(+), 2 deletions(-) create mode 100644 docs/docs/reference/edits.md create mode 100644 docs/docs/reference/edits.zh.md create mode 100644 src/main/java/org/devlive/sdk/openai/entity/EditEntity.java create mode 100644 src/main/java/org/devlive/sdk/openai/model/EditModel.java create mode 100644 src/main/java/org/devlive/sdk/openai/response/EditResponse.java create mode 100644 src/test/java/org/devlive/sdk/openai/entity/EditEntityTest.java diff --git a/docs/docs/reference/edits.md b/docs/docs/reference/edits.md new file mode 100644 index 0000000..3a4920e --- /dev/null +++ b/docs/docs/reference/edits.md @@ -0,0 +1,51 @@ +--- +title: Edits +--- + +!!! Note + + Please build the client before calling, the build code is as follows: + + ```java + OpenAiClient client = OpenAiClient.builder() + .apiHost("https://api.openai.com") + .apiKey(System.getProperty("openai.token")) + .build(); + ``` + + `System.getProperty("openai.token")` is the key to access the API authorization. + +### Create edit + +--- + +Creates a new edit for the provided input, instruction, and parameters. + +```java +EditEntity configure = EditEntity.builder() + .model(EditModel.TEXT_DAVINCI_EDIT_001) + .input("Hello OpenAi Java SDK") + .instruction("Fix the spelling mistakes") + .build(); +client.edit(configure); +``` + +Returns + +```json +{ + "object": "edit", + "created": 1589478378, + "choices": [ + { + "text": "What day of the week is it?", + "index": 0, + } + ], + "usage": { + "prompt_tokens": 25, + "completion_tokens": 32, + "total_tokens": 57 + } +} +``` diff --git a/docs/docs/reference/edits.zh.md b/docs/docs/reference/edits.zh.md new file mode 100644 index 0000000..833e20f --- /dev/null +++ b/docs/docs/reference/edits.zh.md @@ -0,0 +1,51 @@ +--- +title: Edits +--- + +!!! Note + + 调用前请先构建客户端,构建代码如下: + + ```java + OpenAiClient client = OpenAiClient.builder() + .apiHost("https://api.openai.com") + .apiKey(System.getProperty("openai.token")) + .build(); + ``` + + `System.getProperty("openai.token")` 是访问 API 授权的关键。 + +### Create edit + +--- + +为提供的输入、指令和参数创建新的编辑。 + +```java +EditEntity configure = EditEntity.builder() + .model(EditModel.TEXT_DAVINCI_EDIT_001) + .input("Hello OpenAi Java SDK") + .instruction("Fix the spelling mistakes") + .build(); +client.edit(configure); +``` + +Returns + +```json +{ + "object": "edit", + "created": 1589478378, + "choices": [ + { + "text": "What day of the week is it?", + "index": 0, + } + ], + "usage": { + "prompt_tokens": 25, + "completion_tokens": 32, + "total_tokens": 57 + } +} +``` diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 546fb0d..d31b46c 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -66,6 +66,7 @@ nav: - reference/embeddings.md - reference/audio.md - reference/moderations.md + - reference/edits.md - Provider: - reference/provider/azure.md - released.md diff --git a/src/main/java/org/devlive/sdk/openai/DefaultApi.java b/src/main/java/org/devlive/sdk/openai/DefaultApi.java index 2457083..b5a8429 100644 --- a/src/main/java/org/devlive/sdk/openai/DefaultApi.java +++ b/src/main/java/org/devlive/sdk/openai/DefaultApi.java @@ -5,6 +5,7 @@ import okhttp3.RequestBody; import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; +import org.devlive.sdk.openai.entity.EditEntity; import org.devlive.sdk.openai.entity.EmbeddingEntity; import org.devlive.sdk.openai.entity.ImageEntity; import org.devlive.sdk.openai.entity.ModelEntity; @@ -13,6 +14,7 @@ import org.devlive.sdk.openai.response.AudioResponse; import org.devlive.sdk.openai.response.ChatResponse; import org.devlive.sdk.openai.response.CompleteResponse; +import org.devlive.sdk.openai.response.EditResponse; import org.devlive.sdk.openai.response.EmbeddingResponse; import org.devlive.sdk.openai.response.ImageResponse; import org.devlive.sdk.openai.response.ModelResponse; @@ -121,4 +123,12 @@ Single fetchAudioTranscriptions(@Url String url, @POST Single fetchModerations(@Url String url, @Body ModerationEntity configure); + + /** + * Creates a new edit for the provided input, instruction, and parameters. + * 为提供的输入、指令和参数创建新的编辑。 + */ + @POST + Single fetchEdits(@Url String url, + @Body EditEntity configure); } diff --git a/src/main/java/org/devlive/sdk/openai/DefaultClient.java b/src/main/java/org/devlive/sdk/openai/DefaultClient.java index f8d3971..9beb998 100644 --- a/src/main/java/org/devlive/sdk/openai/DefaultClient.java +++ b/src/main/java/org/devlive/sdk/openai/DefaultClient.java @@ -7,6 +7,7 @@ import org.devlive.sdk.openai.entity.AudioEntity; import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; +import org.devlive.sdk.openai.entity.EditEntity; import org.devlive.sdk.openai.entity.EmbeddingEntity; import org.devlive.sdk.openai.entity.ImageEntity; import org.devlive.sdk.openai.entity.ModelEntity; @@ -17,6 +18,7 @@ import org.devlive.sdk.openai.response.AudioResponse; import org.devlive.sdk.openai.response.ChatResponse; import org.devlive.sdk.openai.response.CompleteResponse; +import org.devlive.sdk.openai.response.EditResponse; import org.devlive.sdk.openai.response.EmbeddingResponse; import org.devlive.sdk.openai.response.ImageResponse; import org.devlive.sdk.openai.response.ModelResponse; @@ -121,6 +123,12 @@ public ModerationResponse moderations(ModerationEntity configure) .blockingGet(); } + public EditResponse edit(EditEntity configure) + { + return this.api.fetchEdits(ProviderUtils.getUrl(provider, UrlModel.FETCH_EDITS), configure) + .blockingGet(); + } + public void close() { if (ObjectUtils.isNotEmpty(this.client)) { diff --git a/src/main/java/org/devlive/sdk/openai/entity/EditEntity.java b/src/main/java/org/devlive/sdk/openai/entity/EditEntity.java new file mode 100644 index 0000000..1b13b63 --- /dev/null +++ b/src/main/java/org/devlive/sdk/openai/entity/EditEntity.java @@ -0,0 +1,149 @@ +package org.devlive.sdk.openai.entity; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.devlive.sdk.openai.exception.ParamException; +import org.devlive.sdk.openai.model.EditModel; + +import java.util.Arrays; + +@Data +@Builder +@ToString +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@Deprecated +public class EditEntity +{ + /** + * ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint. + * 要使用的模型的 ID。您可以对此端点使用 text-davinci-edit-001 或 code-davinci-edit-001 模型。 + * + * @see org.devlive.sdk.openai.model.EditModel + */ + @JsonProperty(value = "model") + private String model; + + /** + * The input text to use as a starting point for the edit. + * 用作编辑起点的输入文本。 + */ + @JsonProperty(value = "input") + private String input; + + /** + * The instruction that tells the model how to edit the prompt. + * 告诉模型如何编辑提示的指令。 + */ + @JsonProperty(value = "instruction") + private String instruction; + + /** + * How many edits to generate for the input and instruction. + * 为输入和指令生成多少编辑。 + */ + @JsonProperty(value = "n") + private Integer count; + + /** + * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + * 使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定性。 + *

+ * We generally recommend altering this or top_p but not both. + * 我们通常建议更改此值或 top_p,但不要同时更改两者。 + */ + @JsonProperty(value = "temperature") + private Double temperature; + + /** + * An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + * 温度采样的替代方法称为核采样,其中模型考虑具有 top_p 概率质量的标记的结果。因此 0.1 意味着仅考虑包含前 10% 概率质量的标记。 + *

+ * We generally recommend altering this or temperature but not both. + * 我们通常建议更改此值或温度,但不能同时更改两者。 + */ + @JsonProperty(value = "top_p") + private Double topP; + + private EditEntity(EditEntityBuilder builder) + { + if (ObjectUtils.isEmpty(builder.model)) { + throw new ParamException(String.format("Invalid model, Must be one of %s", Arrays.toString(EditModel.values()))); + } + this.model = builder.model; + + if (StringUtils.isEmpty(builder.input)) { + builder.input(null); + } + this.input = builder.input; + + if (StringUtils.isEmpty(builder.instruction)) { + builder.instruction(null); + } + this.instruction = builder.instruction; + + if (ObjectUtils.isEmpty(builder.count)) { + builder.count(1); + } + this.count = builder.count; + + if (ObjectUtils.isEmpty(builder.temperature)) { + builder.temperature(1D); + } + this.temperature = builder.temperature; + + if (ObjectUtils.isEmpty(builder.topP)) { + builder.topP(1D); + } + this.topP = builder.topP; + } + + public static class EditEntityBuilder + { + public EditEntityBuilder model(EditModel model) + { + this.model = model.getName(); + return this; + } + + public EditEntityBuilder input(String input) + { + if (StringUtils.isEmpty(input)) { + throw new ParamException("Invalid input must be not empty"); + } + this.input = input; + return this; + } + + public EditEntityBuilder instruction(String instruction) + { + if (StringUtils.isEmpty(instruction)) { + throw new ParamException("Invalid instruction must be not empty"); + } + this.instruction = instruction; + return this; + } + + public EditEntityBuilder temperature(Double temperature) + { + if (temperature < 0 || temperature > 2) { + throw new ParamException(String.format("Invalid temperature: %s , between 0 and 2", temperature)); + } + this.temperature = temperature; + return this; + } + + public EditEntity build() + { + return new EditEntity(this); + } + } +} diff --git a/src/main/java/org/devlive/sdk/openai/model/EditModel.java b/src/main/java/org/devlive/sdk/openai/model/EditModel.java new file mode 100644 index 0000000..d60ef55 --- /dev/null +++ b/src/main/java/org/devlive/sdk/openai/model/EditModel.java @@ -0,0 +1,19 @@ +package org.devlive.sdk.openai.model; + +public enum EditModel +{ + TEXT_DAVINCI_EDIT_001("text-davinci-edit-001"), + CODE_DAVINCI_EDIT_001("code-davinci-edit-001"); + + private final String name; + + public String getName() + { + return name; + } + + EditModel(String name) + { + this.name = name; + } +} diff --git a/src/main/java/org/devlive/sdk/openai/model/UrlModel.java b/src/main/java/org/devlive/sdk/openai/model/UrlModel.java index 23d50d9..4e2d267 100644 --- a/src/main/java/org/devlive/sdk/openai/model/UrlModel.java +++ b/src/main/java/org/devlive/sdk/openai/model/UrlModel.java @@ -13,5 +13,6 @@ public enum UrlModel FETCH_IMAGES_VARIATIONS, FETCH_EMBEDDINGS, FETCH_AUDIO_TRANSCRIPTIONS, - FETCH_MODERATIONS + FETCH_MODERATIONS, + FETCH_EDITS } diff --git a/src/main/java/org/devlive/sdk/openai/response/EditResponse.java b/src/main/java/org/devlive/sdk/openai/response/EditResponse.java new file mode 100644 index 0000000..d1936ab --- /dev/null +++ b/src/main/java/org/devlive/sdk/openai/response/EditResponse.java @@ -0,0 +1,36 @@ +package org.devlive.sdk.openai.response; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.devlive.sdk.openai.choice.BasicChoice; +import org.devlive.sdk.openai.entity.UsageEntity; + +import java.util.List; + +@Data +@Builder +@ToString +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class EditResponse +{ + @JsonProperty(value = "object") + private String object; + + @JsonProperty(value = "created") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private String createTime; + + @JsonProperty(value = "choices") + private List choices; + + @JsonProperty(value = "usage") + private UsageEntity usage; +} diff --git a/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java b/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java index 6334078..831b907 100644 --- a/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java +++ b/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java @@ -24,6 +24,7 @@ public class ProviderUtils DEFAULT_PROVIDER.put(UrlModel.FETCH_EMBEDDINGS, "v1/embeddings"); DEFAULT_PROVIDER.put(UrlModel.FETCH_AUDIO_TRANSCRIPTIONS, "v1/audio/transcriptions"); DEFAULT_PROVIDER.put(UrlModel.FETCH_MODERATIONS, "v1/moderations"); + DEFAULT_PROVIDER.put(UrlModel.FETCH_EDITS, "v1/edits"); AZURE_PROVIDER.put(UrlModel.FETCH_COMPLETIONS, "completions"); AZURE_PROVIDER.put(UrlModel.FETCH_CHAT_COMPLETIONS, "chat/completions"); diff --git a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java index 29487a6..475e28a 100644 --- a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java @@ -5,14 +5,16 @@ import org.devlive.sdk.openai.entity.AudioEntity; import org.devlive.sdk.openai.entity.ChatEntity; import org.devlive.sdk.openai.entity.CompletionEntity; -import org.devlive.sdk.openai.entity.MessageEntity; +import org.devlive.sdk.openai.entity.EditEntity; import org.devlive.sdk.openai.entity.EmbeddingEntity; import org.devlive.sdk.openai.entity.ImageEntity; +import org.devlive.sdk.openai.entity.MessageEntity; import org.devlive.sdk.openai.entity.ModerationEntity; import org.devlive.sdk.openai.entity.UserKeyEntity; import org.devlive.sdk.openai.exception.AuthorizedException; import org.devlive.sdk.openai.exception.RequestException; import org.devlive.sdk.openai.model.CompletionModel; +import org.devlive.sdk.openai.model.EditModel; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -199,4 +201,15 @@ public void testModerations() .build(); Assert.assertNotNull(client.moderations(configure)); } + + @Test + public void testEdits() + { + EditEntity configure = EditEntity.builder() + .model(EditModel.TEXT_DAVINCI_EDIT_001) + .input("Hello OpenAi Java SDK") + .instruction("Fix the spelling mistakes") + .build(); + Assert.assertNotNull(client.edit(configure)); + } } diff --git a/src/test/java/org/devlive/sdk/openai/entity/EditEntityTest.java b/src/test/java/org/devlive/sdk/openai/entity/EditEntityTest.java new file mode 100644 index 0000000..dda241d --- /dev/null +++ b/src/test/java/org/devlive/sdk/openai/entity/EditEntityTest.java @@ -0,0 +1,15 @@ +package org.devlive.sdk.openai.entity; + +import org.devlive.sdk.openai.exception.ParamException; +import org.junit.Assert; +import org.junit.Test; + +public class EditEntityTest +{ + @Test + public void test() + { + Assert.assertThrows(ParamException.class, () -> EditEntity.builder() + .build()); + } +} \ No newline at end of file From bb6569da2e5a3478a8403a7fe4bf7e1826fb51a6 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 18 Jul 2023 14:33:10 +0800 Subject: [PATCH 4/6] Split azure docs --- .../azure.md => azure/completions.md} | 11 +++- docs/docs/reference/azure/completions.zh.md | 48 +++++++++++++++ docs/docs/reference/azure/completions_chat.md | 60 +++++++++++++++++++ .../reference/azure/completions_chat.zh.md | 60 +++++++++++++++++++ docs/docs/reference/{ => openai}/audio.md | 0 docs/docs/reference/{ => openai}/audio.zh.md | 0 .../reference/{ => openai}/completions.md | 0 .../reference/{ => openai}/completions.zh.md | 0 .../{ => openai}/completions_chat.md | 0 .../{ => openai}/completions_chat.zh.md | 0 docs/docs/reference/{ => openai}/edits.md | 0 docs/docs/reference/{ => openai}/edits.zh.md | 0 .../docs/reference/{ => openai}/embeddings.md | 0 .../reference/{ => openai}/embeddings.zh.md | 0 docs/docs/reference/{ => openai}/images.md | 0 docs/docs/reference/{ => openai}/images.zh.md | 0 docs/docs/reference/{ => openai}/models.md | 0 docs/docs/reference/{ => openai}/models.zh.md | 0 .../reference/{ => openai}/moderations.md | 0 .../reference/{ => openai}/moderations.zh.md | 0 docs/docs/reference/{ => openai}/users.md | 0 docs/docs/reference/{ => openai}/users.zh.md | 0 docs/mkdocs.yml | 54 +++++++++++------ 23 files changed, 213 insertions(+), 20 deletions(-) rename docs/docs/reference/{provider/azure.md => azure/completions.md} (81%) create mode 100644 docs/docs/reference/azure/completions.zh.md create mode 100644 docs/docs/reference/azure/completions_chat.md create mode 100644 docs/docs/reference/azure/completions_chat.zh.md rename docs/docs/reference/{ => openai}/audio.md (100%) rename docs/docs/reference/{ => openai}/audio.zh.md (100%) rename docs/docs/reference/{ => openai}/completions.md (100%) rename docs/docs/reference/{ => openai}/completions.zh.md (100%) rename docs/docs/reference/{ => openai}/completions_chat.md (100%) rename docs/docs/reference/{ => openai}/completions_chat.zh.md (100%) rename docs/docs/reference/{ => openai}/edits.md (100%) rename docs/docs/reference/{ => openai}/edits.zh.md (100%) rename docs/docs/reference/{ => openai}/embeddings.md (100%) rename docs/docs/reference/{ => openai}/embeddings.zh.md (100%) rename docs/docs/reference/{ => openai}/images.md (100%) rename docs/docs/reference/{ => openai}/images.zh.md (100%) rename docs/docs/reference/{ => openai}/models.md (100%) rename docs/docs/reference/{ => openai}/models.zh.md (100%) rename docs/docs/reference/{ => openai}/moderations.md (100%) rename docs/docs/reference/{ => openai}/moderations.zh.md (100%) rename docs/docs/reference/{ => openai}/users.md (100%) rename docs/docs/reference/{ => openai}/users.zh.md (100%) diff --git a/docs/docs/reference/provider/azure.md b/docs/docs/reference/azure/completions.md similarity index 81% rename from docs/docs/reference/provider/azure.md rename to docs/docs/reference/azure/completions.md index 0f241ae..518bd4b 100644 --- a/docs/docs/reference/provider/azure.md +++ b/docs/docs/reference/azure/completions.md @@ -1,5 +1,5 @@ --- -title: Azure OpenAI +title: Completions --- !!! note @@ -22,10 +22,12 @@ title: Azure OpenAI | `model` | Model name deployed in Azure | | `version` | Model version deployed in Azure | -### Example +### Create completion --- +Creates a completion for the provided prompt and parameters. + ```java // Automatic resource release try(OpenAiClient client=OpenAiClient.builder() @@ -36,6 +38,11 @@ try(OpenAiClient client=OpenAiClient.builder() .version("2022-12-01") .build()) { + CompletionEntity configure = CompletionEntity.builder() + .model(CompletionModel.TEXT_DAVINCI_003.getName()) + .prompt("How to create a completion") + .temperature(2D) + .build(); client.createCompletion(configure).getChoices(); } ``` diff --git a/docs/docs/reference/azure/completions.zh.md b/docs/docs/reference/azure/completions.zh.md new file mode 100644 index 0000000..e531db1 --- /dev/null +++ b/docs/docs/reference/azure/completions.zh.md @@ -0,0 +1,48 @@ +--- +title: Completions +--- + +!!! note + + 支持微软提供的 Open Ai服务,产品地址: [https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/](https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/) + +### Required + +--- + +!!! note + + 以下是调用服务必须指定的一些配置. + +| name | description | +|:----------:|-----------------------------------------------------| +| `apiHost` | 以 `${your-resource-name}.openai.azure.com` 格式创建区域标记 | +| `apiKey` | Azure 令牌 | +| `provider` | 指定 `ProviderModel.azure` | +| `model` | Azure 中部署的模型名称 | +| `version` | Azure 中部署的模型版本 | + +### Create completion + +--- + +为提供的提示和参数创建补全。 + +```java +// 自动资源释放 +try(OpenAiClient client=OpenAiClient.builder() + .apiHost("https://eus-chatgpt.openai.azure.com") + .apiKey(System.getProperty("azure.token")) + .provider(ProviderModel.azure) + .model("text-davinci-002") + .version("2022-12-01") + .build()) +{ + CompletionEntity configure = CompletionEntity.builder() + .model(CompletionModel.TEXT_DAVINCI_003.getName()) + .prompt("How to create a completion") + .temperature(2D) + .build(); + client.createCompletion(configure).getChoices(); +} +``` diff --git a/docs/docs/reference/azure/completions_chat.md b/docs/docs/reference/azure/completions_chat.md new file mode 100644 index 0000000..470f7e7 --- /dev/null +++ b/docs/docs/reference/azure/completions_chat.md @@ -0,0 +1,60 @@ +--- +title: Chat Completions +--- + +!!! note + + Support the Open Ai service provided by Microsoft, product address: [https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/](https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/) + +### Required + +--- + +!!! note + + The following are some configurations that must be specified to invoke the service. + +| name | description | +|:----------:|-----------------------------------------------------------------------------| +| `apiHost` | Created zone markers in the format `${your-resource-name}.openai.azure.com` | +| `apiKey` | Azure token | +| `provider` | Specify `ProviderModel.azure` | +| `model` | Model name deployed in Azure | +| `version` | Model version deployed in Azure | + +### Create chat completion + +--- + +Creates a model response for the given chat conversation. + +```java +// Automatic resource release +try(OpenAiClient client=OpenAiClient.builder() + .apiHost("https://eus-chatgpt.openai.azure.com") + .apiKey(System.getProperty("azure.token")) + .provider(ProviderModel.azure) + .model("text-davinci-002") + .version("2022-12-01") + .build()) +{ + List messages = Lists.newArrayList(); + messages.add(CompletionMessageEntity.builder() + .content("Hello, my name is openai-java-sdk") + .build()); + + CompletionChatEntity configure = CompletionChatEntity.builder() + .messages(messages) + .build(); + + client.createChatCompletion(configure) + .getChoices() + .forEach(choice -> messages.add(choice.getMessage())); + + messages.add(CompletionMessageEntity.builder() + .content("What is my name?") + .build()); + client.createChatCompletion(configure).getChoices(); +} +``` + diff --git a/docs/docs/reference/azure/completions_chat.zh.md b/docs/docs/reference/azure/completions_chat.zh.md new file mode 100644 index 0000000..14f6fa1 --- /dev/null +++ b/docs/docs/reference/azure/completions_chat.zh.md @@ -0,0 +1,60 @@ +--- +title: Chat Completions +--- + +!!! note + + 支持微软提供的 Open Ai服务,产品地址: [https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/](https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/) + +### Required + +--- + +!!! note + + 以下是调用服务必须指定的一些配置. + +| name | description | +|:----------:|-----------------------------------------------------| +| `apiHost` | 以 `${your-resource-name}.openai.azure.com` 格式创建区域标记 | +| `apiKey` | Azure 令牌 | +| `provider` | 指定 `ProviderModel.azure` | +| `model` | Azure 中部署的模型名称 | +| `version` | Azure 中部署的模型版本 | + +### Create chat completion + +--- + +为给定的聊天对话创建模型响应。 + +```java +// Automatic resource release +try(OpenAiClient client=OpenAiClient.builder() + .apiHost("https://eus-chatgpt.openai.azure.com") + .apiKey(System.getProperty("azure.token")) + .provider(ProviderModel.azure) + .model("text-davinci-002") + .version("2022-12-01") + .build()) +{ + List messages = Lists.newArrayList(); + messages.add(CompletionMessageEntity.builder() + .content("Hello, my name is openai-java-sdk") + .build()); + + CompletionChatEntity configure = CompletionChatEntity.builder() + .messages(messages) + .build(); + + client.createChatCompletion(configure) + .getChoices() + .forEach(choice -> messages.add(choice.getMessage())); + + messages.add(CompletionMessageEntity.builder() + .content("What is my name?") + .build()); + client.createChatCompletion(configure).getChoices(); +} +``` + diff --git a/docs/docs/reference/audio.md b/docs/docs/reference/openai/audio.md similarity index 100% rename from docs/docs/reference/audio.md rename to docs/docs/reference/openai/audio.md diff --git a/docs/docs/reference/audio.zh.md b/docs/docs/reference/openai/audio.zh.md similarity index 100% rename from docs/docs/reference/audio.zh.md rename to docs/docs/reference/openai/audio.zh.md diff --git a/docs/docs/reference/completions.md b/docs/docs/reference/openai/completions.md similarity index 100% rename from docs/docs/reference/completions.md rename to docs/docs/reference/openai/completions.md diff --git a/docs/docs/reference/completions.zh.md b/docs/docs/reference/openai/completions.zh.md similarity index 100% rename from docs/docs/reference/completions.zh.md rename to docs/docs/reference/openai/completions.zh.md diff --git a/docs/docs/reference/completions_chat.md b/docs/docs/reference/openai/completions_chat.md similarity index 100% rename from docs/docs/reference/completions_chat.md rename to docs/docs/reference/openai/completions_chat.md diff --git a/docs/docs/reference/completions_chat.zh.md b/docs/docs/reference/openai/completions_chat.zh.md similarity index 100% rename from docs/docs/reference/completions_chat.zh.md rename to docs/docs/reference/openai/completions_chat.zh.md diff --git a/docs/docs/reference/edits.md b/docs/docs/reference/openai/edits.md similarity index 100% rename from docs/docs/reference/edits.md rename to docs/docs/reference/openai/edits.md diff --git a/docs/docs/reference/edits.zh.md b/docs/docs/reference/openai/edits.zh.md similarity index 100% rename from docs/docs/reference/edits.zh.md rename to docs/docs/reference/openai/edits.zh.md diff --git a/docs/docs/reference/embeddings.md b/docs/docs/reference/openai/embeddings.md similarity index 100% rename from docs/docs/reference/embeddings.md rename to docs/docs/reference/openai/embeddings.md diff --git a/docs/docs/reference/embeddings.zh.md b/docs/docs/reference/openai/embeddings.zh.md similarity index 100% rename from docs/docs/reference/embeddings.zh.md rename to docs/docs/reference/openai/embeddings.zh.md diff --git a/docs/docs/reference/images.md b/docs/docs/reference/openai/images.md similarity index 100% rename from docs/docs/reference/images.md rename to docs/docs/reference/openai/images.md diff --git a/docs/docs/reference/images.zh.md b/docs/docs/reference/openai/images.zh.md similarity index 100% rename from docs/docs/reference/images.zh.md rename to docs/docs/reference/openai/images.zh.md diff --git a/docs/docs/reference/models.md b/docs/docs/reference/openai/models.md similarity index 100% rename from docs/docs/reference/models.md rename to docs/docs/reference/openai/models.md diff --git a/docs/docs/reference/models.zh.md b/docs/docs/reference/openai/models.zh.md similarity index 100% rename from docs/docs/reference/models.zh.md rename to docs/docs/reference/openai/models.zh.md diff --git a/docs/docs/reference/moderations.md b/docs/docs/reference/openai/moderations.md similarity index 100% rename from docs/docs/reference/moderations.md rename to docs/docs/reference/openai/moderations.md diff --git a/docs/docs/reference/moderations.zh.md b/docs/docs/reference/openai/moderations.zh.md similarity index 100% rename from docs/docs/reference/moderations.zh.md rename to docs/docs/reference/openai/moderations.zh.md diff --git a/docs/docs/reference/users.md b/docs/docs/reference/openai/users.md similarity index 100% rename from docs/docs/reference/users.md rename to docs/docs/reference/openai/users.md diff --git a/docs/docs/reference/users.zh.md b/docs/docs/reference/openai/users.zh.md similarity index 100% rename from docs/docs/reference/users.zh.md rename to docs/docs/reference/openai/users.zh.md diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index d31b46c..51ce7e1 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -12,13 +12,6 @@ copyright: Copyright © 2023 Devlive Community use_directory_urls: false -plugins: - - i18n: - default_language: en - languages: - en: English - zh: Chinese (Simplified) - theme: name: material @@ -35,6 +28,11 @@ theme: - search.share - search.suggest - content.tooltips + - navigation.expand + - navigation.prune + - abbr + - attr_list + - pymdownx.snippets alternate: - name: English link: /en/ @@ -55,19 +53,39 @@ markdown_extensions: - pymdownx.snippets - abbr +plugins: + - i18n: + default_language: en + languages: + en: English + zh: Chinese (Simplified) + - redirects: + redirect_maps: + reference/users.md: reference/openai/users.md + reference/models.md: reference/openai/models.md + reference/completions.md: reference/openai/completions.md + reference/completions_chat.md: reference/openai/completions_chat.md + reference/images.md: reference/openai/images.md + reference/embeddings.md: reference/openai/embeddings.md + reference/audio.md: reference/openai/audio.md + reference/moderations.md: reference/openai/moderations.md + reference/edits.md: reference/openai/edits.md + nav: - index.md - Reference: - - reference/users.md - - reference/models.md - - reference/completions.md - - reference/completions_chat.md - - reference/images.md - - reference/embeddings.md - - reference/audio.md - - reference/moderations.md - - reference/edits.md - - Provider: - - reference/provider/azure.md + - Open Ai: + - reference/openai/users.md + - reference/openai/models.md + - reference/openai/completions.md + - reference/openai/completions_chat.md + - reference/openai/images.md + - reference/openai/embeddings.md + - reference/openai/audio.md + - reference/openai/moderations.md + - reference/openai/edits.md + - Azure Open Ai: + - reference/azure/completions.md + - reference/azure/completions_chat.md - released.md - powered_by.md From 6ba0ee2a22423523e76ddafffece7531545e7623 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 18 Jul 2023 15:27:17 +0800 Subject: [PATCH 5/6] Support claude --- .github/workflows/checker.yml | 2 +- docs/docs/reference/anthropic/completions.md | 29 +++++++++++++++++ .../reference/anthropic/completions.zh.md | 28 +++++++++++++++++ docs/mkdocs.yml | 2 ++ .../org/devlive/sdk/openai/OpenAiClient.java | 23 +++++++++++--- .../sdk/openai/entity/CompletionEntity.java | 2 ++ .../openai/interceptor/ClaudeInterceptor.java | 30 ++++++++++++++++++ .../sdk/openai/model/CompletionModel.java | 11 ++++++- .../sdk/openai/model/ProviderModel.java | 17 ++++++++-- .../sdk/openai/utils/ProviderUtils.java | 14 +++++++-- .../sdk/openai/AzureOpenAiClientTest.java | 4 +-- .../devlive/sdk/openai/ClaudeClientTest.java | 31 +++++++++++++++++++ .../devlive/sdk/openai/OpenAiClientTest.java | 1 - 13 files changed, 179 insertions(+), 15 deletions(-) create mode 100644 docs/docs/reference/anthropic/completions.md create mode 100644 docs/docs/reference/anthropic/completions.zh.md create mode 100644 src/main/java/org/devlive/sdk/openai/interceptor/ClaudeInterceptor.java create mode 100644 src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java diff --git a/.github/workflows/checker.yml b/.github/workflows/checker.yml index 7f84c79..800d180 100644 --- a/.github/workflows/checker.yml +++ b/.github/workflows/checker.yml @@ -71,7 +71,7 @@ jobs: distribution: 'temurin' - run: chmod 755 ./mvnw - run: | - ./mvnw clean install test -Dfindbugs.skip -Dcheckstyle.skip -Dgpg.skip -Dskip.yarn -Dopenai.token=${{ secrets.OPENAI_TOKEN }} -Dproxy.token=${{ secrets.PROXY_TOKEN }} -Dproxy.host=${{ secrets.PROXY_HOST }} -Dazure.token=${{ secrets.AZURE_TOKEN}} + ./mvnw clean install test -Dfindbugs.skip -Dcheckstyle.skip -Dgpg.skip -Dskip.yarn -Dopenai.token=${{ secrets.OPENAI_TOKEN }} -Dproxy.token=${{ secrets.PROXY_TOKEN }} -Dproxy.host=${{ secrets.PROXY_HOST }} -Dazure.token=${{ secrets.AZURE_TOKEN}} -Dclaude.token=${{ secrets.CLAUDE_TOKEN }} before_checker_package: runs-on: ubuntu-latest diff --git a/docs/docs/reference/anthropic/completions.md b/docs/docs/reference/anthropic/completions.md new file mode 100644 index 0000000..b4fb905 --- /dev/null +++ b/docs/docs/reference/anthropic/completions.md @@ -0,0 +1,29 @@ +--- +title: Completions +--- + +!!! note + + Support the claude, product address: [https://claude.ai/](https://claude.ai/) + +### Create completion + +--- + +Creates a completion for the provided prompt and parameters. + +```java +// Automatic resource release +try(OpenAiClient client=OpenAiClient.builder() + .apiKey(System.getProperty("claude.token")) + .provider(ProviderModel.CLAUDE) + .build()) +{ + CompletionEntity configure = CompletionEntity.builder() + .model(CompletionModel.CLAUDE_2.getName()) + .prompt("How to create a completion") + .build(); + client.createCompletion(configure).getChoices(); +} +``` + diff --git a/docs/docs/reference/anthropic/completions.zh.md b/docs/docs/reference/anthropic/completions.zh.md new file mode 100644 index 0000000..05c576a --- /dev/null +++ b/docs/docs/reference/anthropic/completions.zh.md @@ -0,0 +1,28 @@ +--- +title: Completions +--- + +!!! note + + 支持 claude,产品地址: [https://claude.ai/](https://claude.ai/) + +### Create completion + +--- + +为提供的提示和参数创建补全。 + +```java +// Automatic resource release +try(OpenAiClient client=OpenAiClient.builder() + .apiKey(System.getProperty("claude.token")) + .provider(ProviderModel.CLAUDE) + .build()) + { + CompletionEntity configure = CompletionEntity.builder() + .model(CompletionModel.CLAUDE_2.getName()) + .prompt("How to create a completion") + .build(); + client.createCompletion(configure).getChoices(); +} +``` diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 51ce7e1..e7a630b 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -87,5 +87,7 @@ nav: - Azure Open Ai: - reference/azure/completions.md - reference/azure/completions_chat.md + - Anthropic Claude: + - reference/anthropic/completions.md - released.md - powered_by.md diff --git a/src/main/java/org/devlive/sdk/openai/OpenAiClient.java b/src/main/java/org/devlive/sdk/openai/OpenAiClient.java index ef42435..60a8e99 100644 --- a/src/main/java/org/devlive/sdk/openai/OpenAiClient.java +++ b/src/main/java/org/devlive/sdk/openai/OpenAiClient.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils; import org.devlive.sdk.openai.exception.ParamException; import org.devlive.sdk.openai.interceptor.AzureInterceptor; +import org.devlive.sdk.openai.interceptor.ClaudeInterceptor; import org.devlive.sdk.openai.interceptor.DefaultInterceptor; import org.devlive.sdk.openai.interceptor.OpenAiInterceptor; import org.devlive.sdk.openai.model.ProviderModel; @@ -44,10 +45,10 @@ private OpenAiClient(OpenAiClientBuilder builder) } if (ObjectUtils.isEmpty(builder.provider)) { - builder.provider(ProviderModel.openai); + builder.provider(ProviderModel.OPENAI); } - if (builder.provider.equals(ProviderModel.azure)) { + if (builder.provider.equals(ProviderModel.AZURE)) { if (ObjectUtils.isEmpty(builder.model)) { throw new ParamException("Azure provider model not specified"); } @@ -93,7 +94,7 @@ public OpenAiClientBuilder apiKey(String apiKey) public OpenAiClientBuilder apiHost(String apiHost) { if (StringUtils.isEmpty(apiHost)) { - apiHost = "https://api.openai.com"; + apiHost = this.getDefaultHost(); } else { boolean flag = apiHost.startsWith("http") || apiHost.startsWith("https"); @@ -126,7 +127,7 @@ public OpenAiClientBuilder unit(TimeUnit unit) public OpenAiClientBuilder client(OkHttpClient client) { if (ObjectUtils.isEmpty(this.provider)) { - this.provider = ProviderModel.openai; + this.provider = ProviderModel.OPENAI; } if (ObjectUtils.isEmpty(client)) { @@ -140,11 +141,15 @@ public OpenAiClientBuilder client(OkHttpClient client) } // Add default interceptor DefaultInterceptor interceptor = new OpenAiInterceptor(); - if (this.provider.equals(ProviderModel.azure)) { + if (this.provider.equals(ProviderModel.AZURE)) { interceptor = new AzureInterceptor(); interceptor.setVersion(this.version); interceptor.setModel(this.model); } + // Anthropic claude interceptor + if (this.provider.equals(ProviderModel.CLAUDE)) { + interceptor = new ClaudeInterceptor(); + } interceptor.setApiKey(apiKey); client = client.newBuilder() .addInterceptor(interceptor) @@ -153,6 +158,14 @@ public OpenAiClientBuilder client(OkHttpClient client) return this; } + private String getDefaultHost() + { + if (this.provider.equals(ProviderModel.CLAUDE)) { + return "https://api.anthropic.com"; + } + return "https://api.openai.com"; + } + public OpenAiClient build() { return new OpenAiClient(this); diff --git a/src/main/java/org/devlive/sdk/openai/entity/CompletionEntity.java b/src/main/java/org/devlive/sdk/openai/entity/CompletionEntity.java index d0c9ca2..33337c6 100644 --- a/src/main/java/org/devlive/sdk/openai/entity/CompletionEntity.java +++ b/src/main/java/org/devlive/sdk/openai/entity/CompletionEntity.java @@ -1,5 +1,6 @@ package org.devlive.sdk.openai.entity; +import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; @@ -31,6 +32,7 @@ public class CompletionEntity private Double temperature; @JsonProperty(value = "max_tokens") + @JsonAlias(value = {"max_tokens_to_sample"}) private Integer maxTokens; @JsonProperty(value = "top_p") diff --git a/src/main/java/org/devlive/sdk/openai/interceptor/ClaudeInterceptor.java b/src/main/java/org/devlive/sdk/openai/interceptor/ClaudeInterceptor.java new file mode 100644 index 0000000..06bc13b --- /dev/null +++ b/src/main/java/org/devlive/sdk/openai/interceptor/ClaudeInterceptor.java @@ -0,0 +1,30 @@ +package org.devlive.sdk.openai.interceptor; + +import lombok.extern.slf4j.Slf4j; +import okhttp3.Request; +import org.apache.commons.lang3.StringUtils; +import org.devlive.sdk.openai.exception.ParamException; + +@Slf4j +public class ClaudeInterceptor + extends DefaultInterceptor +{ + public ClaudeInterceptor() + { + log.debug("Claude Interceptor"); + } + + @Override + protected Request prepared(Request original) + { + log.debug("Claude interceptor request url {}", original.url()); + if (StringUtils.isEmpty(this.getApiKey())) { + throw new ParamException("Invalid Claude token, must be non-empty"); + } + return original.newBuilder() + .header("x-api-key", this.getApiKey()) + .header("Content-Type", "application/json") + .method(original.method(), original.body()) + .build(); + } +} diff --git a/src/main/java/org/devlive/sdk/openai/model/CompletionModel.java b/src/main/java/org/devlive/sdk/openai/model/CompletionModel.java index 15f7cd0..765f703 100644 --- a/src/main/java/org/devlive/sdk/openai/model/CompletionModel.java +++ b/src/main/java/org/devlive/sdk/openai/model/CompletionModel.java @@ -85,7 +85,16 @@ public enum CompletionModel ADA("ada", "Capable of very simple tasks, usually the fastest model in the GPT-3 series, and lowest cost.", null, - 2049); + 2049), + /* ================================ Claude ================================ */ + CLAUDE_2("claude-2", + null, + null, + Integer.MAX_VALUE), + CLAUDE_INSTANT_1("claude-instant-1", + null, + null, + Integer.MAX_VALUE); private final String name; private final String description; diff --git a/src/main/java/org/devlive/sdk/openai/model/ProviderModel.java b/src/main/java/org/devlive/sdk/openai/model/ProviderModel.java index bac00c9..418e605 100644 --- a/src/main/java/org/devlive/sdk/openai/model/ProviderModel.java +++ b/src/main/java/org/devlive/sdk/openai/model/ProviderModel.java @@ -2,6 +2,19 @@ public enum ProviderModel { - openai, - azure + /** + * Open AI + * https://openai.com + */ + OPENAI, + /** + * Azure OpenAI service + * https://oai.azure.com/portal + */ + AZURE, + /** + * A next-generation AI assistant for your tasks, no matter the scale + * https://www.anthropic.com/product + */ + CLAUDE } diff --git a/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java b/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java index 831b907..dbcf0a6 100644 --- a/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java +++ b/src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java @@ -12,6 +12,7 @@ public class ProviderUtils { private static final Map DEFAULT_PROVIDER = new HashMap<>(); private static final Map AZURE_PROVIDER = new HashMap<>(); + private static final Map CLAUDE_PROVIDER = new HashMap<>(); static { DEFAULT_PROVIDER.put(UrlModel.FETCH_MODELS, "v1/models"); @@ -28,6 +29,8 @@ public class ProviderUtils AZURE_PROVIDER.put(UrlModel.FETCH_COMPLETIONS, "completions"); AZURE_PROVIDER.put(UrlModel.FETCH_CHAT_COMPLETIONS, "chat/completions"); + + CLAUDE_PROVIDER.put(UrlModel.FETCH_COMPLETIONS, "v1/complete"); } private ProviderUtils() @@ -43,9 +46,14 @@ private ProviderUtils() */ public static String getUrl(ProviderModel provider, UrlModel model) { - provider = Optional.ofNullable(provider).orElse(ProviderModel.openai); - Map selectedProvider = provider.equals(ProviderModel.azure) ? AZURE_PROVIDER : DEFAULT_PROVIDER; - + provider = Optional.ofNullable(provider).orElse(ProviderModel.OPENAI); + Map selectedProvider = DEFAULT_PROVIDER; + if (provider.equals(ProviderModel.AZURE)) { + selectedProvider = AZURE_PROVIDER; + } + if (provider.equals(ProviderModel.CLAUDE)) { + selectedProvider = CLAUDE_PROVIDER; + } ProviderModel finalProvider = provider; return Optional.ofNullable(selectedProvider.get(model)) .orElseThrow(() -> new RequestException(String.format("Provider %s not supported %s", finalProvider, model))); diff --git a/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java b/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java index b2414ef..5a69178 100644 --- a/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/AzureOpenAiClientTest.java @@ -24,7 +24,7 @@ public void before() client = OpenAiClient.builder() .apiHost("https://eus-chatgpt.openai.azure.com") .apiKey(System.getProperty("azure.token")) - .provider(ProviderModel.azure) + .provider(ProviderModel.AZURE) .model("text-davinci-002") .version("2022-12-01") .build(); @@ -53,7 +53,7 @@ public void testCreateChatCompletion() client = OpenAiClient.builder() .apiHost("https://eus-chatgpt.openai.azure.com") .apiKey(System.getProperty("azure.token")) - .provider(ProviderModel.azure) + .provider(ProviderModel.AZURE) .model("gpt-35-turbo-0613") .version("2023-03-15-preview") .build(); diff --git a/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java b/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java new file mode 100644 index 0000000..5a2cc83 --- /dev/null +++ b/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java @@ -0,0 +1,31 @@ +package org.devlive.sdk.openai; + +import org.devlive.sdk.openai.entity.CompletionEntity; +import org.devlive.sdk.openai.model.CompletionModel; +import org.devlive.sdk.openai.model.ProviderModel; +import org.junit.Before; +import org.junit.Test; + +public class ClaudeClientTest +{ + private OpenAiClient client; + + @Before + public void before() + { + client = OpenAiClient.builder() + .apiKey(System.getProperty("claude.token")) + .provider(ProviderModel.CLAUDE) + .build(); + } + + @Test + public void testCompletion() + { + CompletionEntity configure = CompletionEntity.builder() + .model(CompletionModel.CLAUDE_2.getName()) + .prompt("How to create a completion") + .build(); + System.out.println(configure); + } +} diff --git a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java index 475e28a..6e98fc1 100644 --- a/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java @@ -35,7 +35,6 @@ public void before() { client = OpenAiClient.builder() .apiKey(System.getProperty("openai.token")) - .apiKey("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") .build(); } From 8b1b6f8f17906fb26a0783937c08b17b1f62c549 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Tue, 18 Jul 2023 15:33:55 +0800 Subject: [PATCH 6/6] Released for 1.6.0 --- pom.xml | 2 +- src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java | 2 +- src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 6a8eb5a..e41ce9e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.devlive.sdk openai-java-sdk - 1.5.0 + 1.6.0 openai-java-sdk diff --git a/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java b/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java index 03ec032..dd06620 100644 --- a/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java +++ b/src/main/java/org/devlive/sdk/openai/utils/EnumsUtils.java @@ -1,7 +1,7 @@ package org.devlive.sdk.openai.utils; -import org.devlive.sdk.openai.model.MessageModel; import org.devlive.sdk.openai.model.CompletionModel; +import org.devlive.sdk.openai.model.MessageModel; import java.util.Arrays; import java.util.Optional; diff --git a/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java b/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java index 5a2cc83..f0f29dc 100644 --- a/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java +++ b/src/test/java/org/devlive/sdk/openai/ClaudeClientTest.java @@ -3,6 +3,7 @@ import org.devlive.sdk.openai.entity.CompletionEntity; import org.devlive.sdk.openai.model.CompletionModel; import org.devlive.sdk.openai.model.ProviderModel; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -26,6 +27,6 @@ public void testCompletion() .model(CompletionModel.CLAUDE_2.getName()) .prompt("How to create a completion") .build(); - System.out.println(configure); + Assert.assertThrows(RuntimeException.class, () -> client.createCompletion(configure)); } }