Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 Feb 6, 2025
d289d36
Fix rearrangement of code
rpanackal Feb 6, 2025
1ddfe62
Convenience level separated out
rpanackal Feb 6, 2025
fd619dd
Reflect deprecation of direct string input on client with unit tests
rpanackal Feb 6, 2025
973480c
E2e for both new and old apis
rpanackal Feb 6, 2025
a0756f7
Increasing test coverage
rpanackal Feb 6, 2025
183e57f
Test coverage for tool
rpanackal Feb 6, 2025
5afc335
Fix type
rpanackal Feb 7, 2025
3c13c35
PR changes
rpanackal Feb 7, 2025
b33976b
Remove docs on OpenAi controller
rpanackal Feb 7, 2025
0dc7bab
Remove docs on OpenAi controller
rpanackal Feb 7, 2025
05b93bc
tag public api with `@since`
rpanackal Feb 7, 2025
041adc8
Clean up test code for better separation and reduce repetition
rpanackal Feb 11, 2025
ca2bd65
Adapt tool test for better usecase
rpanackal Feb 13, 2025
c9b9532
Remove deprecation annotation and java doc tag
rpanackal Feb 13, 2025
e807687
Beta annotation on all new classes and client methods returning/takin…
rpanackal Feb 13, 2025
ed42cd1
Fix changes for new generated model class location
rpanackal Feb 14, 2025
db75ca7
Update @since because of latest release
rpanackal Feb 14, 2025
f7b7717
Convert request and response classes to Value classes
rpanackal Feb 17, 2025
abd7acb
Fix @With equality check for Boolean
rpanackal Feb 17, 2025
f33bfad
Move NewOpenAiService to test
rpanackal Feb 17, 2025
90de7fe
utility class introduced for mapping to low level request message
rpanackal Feb 17, 2025
52610c2
Remove embedding convenience from PR
rpanackal Feb 17, 2025
d4a03e5
Method renaming
rpanackal Feb 17, 2025
4bc7853
Charles suggested changes
rpanackal Feb 18, 2025
dc58ce4
Method renaming and make OpenAiChatCompletionDelta constructor access…
rpanackal Feb 18, 2025
fe88ff5
Method renaming, add utility method for jackson and change constructo…
rpanackal Feb 18, 2025
0aee603
Improve one plus list construction
rpanackal Feb 18, 2025
4420573
Make new delta test more readable
rpanackal Feb 18, 2025
1a7cf85
Improve java doc in client and request
rpanackal Feb 19, 2025
5494c32
Remove the redundant test for usage in low level
rpanackal Feb 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {}
}
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 createChatCompletionRequestMessage() {
return new ChatCompletionRequestAssistantMessage()
.role(ChatCompletionRequestAssistantMessage.RoleEnum.fromValue(role()))
.content(ChatCompletionRequestAssistantMessageContent.create(content));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.sap.ai.sdk.foundationmodels.openai;

import static com.sap.ai.sdk.foundationmodels.openai.OpenAiUtils.getOpenAiObjectMapper;
import static lombok.AccessLevel.PACKAGE;

import com.fasterxml.jackson.annotation.JsonCreator;
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.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
* Represents an OpenAI chat completion output delta for streaming.
*
* @since 1.4.0
*/
@Beta
@RequiredArgsConstructor(onConstructor_ = @JsonCreator, access = PACKAGE)
@Getter
@ToString
@EqualsAndHashCode
public class OpenAiChatCompletionDelta implements StreamedDelta {
/** The original response from the chat completion stream. */
@Nonnull private final CreateChatCompletionStreamResponse originalResponse;

@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 "";
}

@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.
*
* @return The completion usage or null.
*/
@Nullable
public CompletionUsage getCompletionUsage() {
if (getOriginalResponse().getCustomFieldNames().contains("usage")
&& getOriginalResponse().getCustomField("usage") instanceof Map<?, ?> usage) {
return getOpenAiObjectMapper().convertValue(usage, CompletionUsage.class);
}
return null;
}
}
Loading
Loading