generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: [OpenAI] Stable convenience - Latest #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
8d5bc33
Full openapi generated code for openai module
rpanackal d289d36
Fix rearrangement of code
rpanackal 1ddfe62
Convenience level separated out
rpanackal fd619dd
Reflect deprecation of direct string input on client with unit tests
rpanackal 973480c
E2e for both new and old apis
rpanackal a0756f7
Increasing test coverage
rpanackal 183e57f
Test coverage for tool
rpanackal 5afc335
Fix type
rpanackal 3c13c35
PR changes
rpanackal b33976b
Remove docs on OpenAi controller
rpanackal 0dc7bab
Remove docs on OpenAi controller
rpanackal 05b93bc
tag public api with `@since`
rpanackal 041adc8
Clean up test code for better separation and reduce repetition
rpanackal ca2bd65
Adapt tool test for better usecase
rpanackal c9b9532
Remove deprecation annotation and java doc tag
rpanackal e807687
Beta annotation on all new classes and client methods returning/takin…
rpanackal ed42cd1
Fix changes for new generated model class location
rpanackal db75ca7
Update @since because of latest release
rpanackal f7b7717
Convert request and response classes to Value classes
rpanackal abd7acb
Fix @With equality check for Boolean
rpanackal f33bfad
Move NewOpenAiService to test
rpanackal 90de7fe
utility class introduced for mapping to low level request message
rpanackal 52610c2
Remove embedding convenience from PR
rpanackal d4a03e5
Method renaming
rpanackal 4bc7853
Charles suggested changes
rpanackal dc58ce4
Method renaming and make OpenAiChatCompletionDelta constructor access…
rpanackal fe88ff5
Method renaming, add utility method for jackson and change constructo…
rpanackal 0aee603
Improve one plus list construction
rpanackal 4420573
Make new delta test more readable
rpanackal 1a7cf85
Improve java doc in client and request
rpanackal 5494c32
Remove the redundant test for usage in low level
rpanackal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...ion-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/JacksonMixins.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.sap.ai.sdk.foundationmodels.openai; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
| import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
| import com.sap.ai.sdk.foundationmodels.openai.generated.model.CreateChatCompletionStreamResponse; | ||
| import lombok.AccessLevel; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| /** | ||
| * This class contains Jackson Mixins for customizing the serialization and deserialization behavior | ||
| * of certain classes in the OpenAI SDK. | ||
| */ | ||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| final class JacksonMixins { | ||
|
|
||
| /** | ||
| * Mixin interface to customize the deserialization of CreateChatCompletionStreamResponse. | ||
| * | ||
| * <p>Disables type information inclusion and specifies the concrete class to use for | ||
| * deserialization. | ||
| */ | ||
| @JsonTypeInfo(use = JsonTypeInfo.Id.NONE) | ||
| @JsonDeserialize(as = CreateChatCompletionStreamResponse.class) | ||
| interface DefaultChatCompletionCreate200ResponseMixIn {} | ||
| } |
37 changes: 37 additions & 0 deletions
37
...s/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiAssistantMessage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.sap.ai.sdk.foundationmodels.openai; | ||
|
|
||
| import com.google.common.annotations.Beta; | ||
| import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionRequestAssistantMessage; | ||
| import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionRequestAssistantMessageContent; | ||
| import javax.annotation.Nonnull; | ||
| import lombok.Value; | ||
| import lombok.experimental.Accessors; | ||
|
|
||
| /** | ||
| * Represents a chat message as 'assistant' to OpenAI service. | ||
| * | ||
| * @since 1.4.0 | ||
| */ | ||
| @Beta | ||
| @Value | ||
| @Accessors(fluent = true) | ||
| class OpenAiAssistantMessage implements OpenAiMessage { | ||
|
|
||
| /** The role of the message. */ | ||
| @Nonnull String role = "assistant"; | ||
|
|
||
| /** The content of the message. */ | ||
| @Nonnull String content; | ||
|
|
||
| /** | ||
| * Converts the message to a serializable object. | ||
| * | ||
| * @return the corresponding {@code ChatCompletionRequestAssistantMessage} object. | ||
| */ | ||
| @Nonnull | ||
| ChatCompletionRequestAssistantMessage toChatCompletionRequestSystemMessage() { | ||
| return new ChatCompletionRequestAssistantMessage() | ||
| .role(ChatCompletionRequestAssistantMessage.RoleEnum.fromValue(role())) | ||
| .content(ChatCompletionRequestAssistantMessageContent.create(content)); | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
...penai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiChatCompletionDelta.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package com.sap.ai.sdk.foundationmodels.openai; | ||
|
|
||
| import static lombok.AccessLevel.NONE; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.google.common.annotations.Beta; | ||
| import com.sap.ai.sdk.core.common.StreamedDelta; | ||
| import com.sap.ai.sdk.foundationmodels.openai.generated.model.CompletionUsage; | ||
| import com.sap.ai.sdk.foundationmodels.openai.generated.model.CreateChatCompletionStreamResponse; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.Value; | ||
|
|
||
| /** | ||
| * Represents an OpenAI chat completion output delta for streaming. | ||
| * | ||
| * @since 1.4.0 | ||
| */ | ||
| @Beta | ||
| @Value | ||
| @RequiredArgsConstructor(onConstructor_ = @JsonCreator) | ||
| @Setter(value = NONE) | ||
| public class OpenAiChatCompletionDelta implements StreamedDelta { | ||
| /** The original response from the chat completion stream. */ | ||
| @Nonnull final CreateChatCompletionStreamResponse originalResponse; | ||
|
|
||
| /** | ||
| * Retrieves the delta content from the original response. | ||
| * | ||
| * @return The delta content as a string, or an empty string if not available. | ||
| */ | ||
rpanackal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Nonnull | ||
| @Override | ||
| public String getDeltaContent() { | ||
| final var choices = getOriginalResponse().getChoices(); | ||
| if (!choices.isEmpty() && choices.get(0).getIndex() == 0) { | ||
| final var message = choices.get(0).getDelta(); | ||
| return Objects.requireNonNullElse(message.getContent(), ""); | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the finish reason from the original response. | ||
| * | ||
| * @return The finish reason as a string, or null if not available. | ||
| */ | ||
rpanackal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Nullable | ||
| @Override | ||
| public String getFinishReason() { | ||
| final var choices = getOriginalResponse().getChoices(); | ||
| if (!choices.isEmpty()) { | ||
| final var finishReason = choices.get(0).getFinishReason(); | ||
| return finishReason != null ? finishReason.getValue() : null; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the completion usage from the response, or null if it is not available. | ||
| * | ||
| * @param objectMapper The object mapper to use for conversion. | ||
| * @return The completion usage or null. | ||
| */ | ||
| @Nullable | ||
| public CompletionUsage getCompletionUsage(@Nonnull final ObjectMapper objectMapper) { | ||
| if (getOriginalResponse().getCustomFieldNames().contains("usage") | ||
| && getOriginalResponse().getCustomField("usage") instanceof Map<?, ?> usage) { | ||
| return objectMapper.convertValue(usage, CompletionUsage.class); | ||
| } | ||
| return null; | ||
| } | ||
CharlesDuboisSAP marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.