Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -41,15 +41,23 @@
import com.sap.ai.sdk.orchestration.model.CompletionPostRequest;
import com.sap.ai.sdk.orchestration.model.CompletionPostResponse;
import com.sap.ai.sdk.orchestration.model.DPIEntities;
import com.sap.ai.sdk.orchestration.model.DataRepositoryType;
import com.sap.ai.sdk.orchestration.model.DocumentGroundingFilter;
import com.sap.ai.sdk.orchestration.model.GenericModuleResult;
import com.sap.ai.sdk.orchestration.model.GroundingFilterSearchConfiguration;
import com.sap.ai.sdk.orchestration.model.GroundingModuleConfig;
import com.sap.ai.sdk.orchestration.model.GroundingModuleConfigConfig;
import com.sap.ai.sdk.orchestration.model.ImageContent;
import com.sap.ai.sdk.orchestration.model.ImageContentImageUrl;
import com.sap.ai.sdk.orchestration.model.KeyValueListPair;
import com.sap.ai.sdk.orchestration.model.LLMModuleConfig;
import com.sap.ai.sdk.orchestration.model.LLMModuleResult;
import com.sap.ai.sdk.orchestration.model.LLMModuleResultSynchronous;
import com.sap.ai.sdk.orchestration.model.ModuleConfigs;
import com.sap.ai.sdk.orchestration.model.MultiChatMessage;
import com.sap.ai.sdk.orchestration.model.OrchestrationConfig;
import com.sap.ai.sdk.orchestration.model.SearchDocumentKeyValueListPair;
import com.sap.ai.sdk.orchestration.model.SearchSelectOptionEnum;
import com.sap.ai.sdk.orchestration.model.Template;
import com.sap.ai.sdk.orchestration.model.TextContent;
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
Expand Down Expand Up @@ -129,34 +137,68 @@ void testCompletion() {
}

@Test
void testGrounding() {
void testGrounding() throws IOException {
stubFor(
post(anyUrl())
post(urlPathEqualTo("/completion"))
.willReturn(
aResponse()
.withBodyFile("groundingResponse.json")
.withHeader("Content-Type", "application/json")));
final var response = client.chatCompletion(prompt, config);
final var result = response.getOriginalResponse();
var llmChoice =
((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices().get(0);

final var groundingData =
(Map<String, Object>) result.getModuleResults().getGrounding().getData();
assertThat(groundingData.get("grounding_query")).isEqualTo("grounding call");
assertThat(groundingData.get("grounding_result").toString())
.startsWith("Joule is the AI copilot that truly understands your business.");
assertThat(result.getModuleResults().getGrounding().getMessage()).isEqualTo("grounding result");
assertThat(((ChatMessage) result.getModuleResults().getTemplating().get(0)).getContent())
.startsWith(
"What does Joule do? Use the following information as additional context: Joule is the AI copilot that truly understands your business.");
assertThat(llmChoice.getMessage().getContent())
.startsWith(
"Joule is an AI copilot that revolutionizes how users interact with their SAP business systems.");
assertThat(llmChoice.getFinishReason()).isEqualTo("stop");
assertThat(llmChoice.getMessage().getContent())
.startsWith(
"Joule is an AI copilot that revolutionizes how users interact with their SAP business systems.");

final var documentMetadata =
SearchDocumentKeyValueListPair.create()
.key("document metadata")
.value("2")
.selectMode(List.of(SearchSelectOptionEnum.IGNORE_IF_KEY_ABSENT));
final var databaseFilter =
DocumentGroundingFilter.create()
.id("arbitrary-user-defined-id")
.dataRepositoryType(DataRepositoryType.VECTOR)
.searchConfig(GroundingFilterSearchConfiguration.create().maxChunkCount(3))
.documentMetadata(List.of(documentMetadata))
.chunkMetadata(List.of(KeyValueListPair.create().key("chunk metadata").value("1")));
final var groundingConfigConfig =
GroundingModuleConfigConfig.create()
.inputParams(List.of("query"))
.outputParam("results")
.addFiltersItem(databaseFilter);
final var groundingConfig =
GroundingModuleConfig.create()
.type(GroundingModuleConfig.TypeEnum.DOCUMENT_GROUNDING_SERVICE)
.config(groundingConfigConfig);
final var configWithGrounding = config.withGroundingConfig(groundingConfig);

final Map<String, String> inputParams =
Map.of("query", "String used for similarity search in database");
final var prompt =
new OrchestrationPrompt(
inputParams,
Message.system("Context message with embedded grounding results. {{?results}}"));

final var response = client.chatCompletion(prompt, configWithGrounding);
Comment on lines +160 to +178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Question)

This is supposed to be the expected convenience API usage, right?
Looks tough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is generated code only.


assertThat(response).isNotNull();
assertThat(response.getOriginalResponse().getRequestId())
.isEqualTo("e5d2add4-408c-4da5-84ca-1d8b0fe350c8");

var moduleResults = response.getOriginalResponse().getModuleResults();
assertThat(moduleResults).isNotNull();

var groundingModule = moduleResults.getGrounding();
assertThat(groundingModule).isNotNull();
assertThat(groundingModule.getMessage()).isEqualTo("grounding result");
assertThat(groundingModule.getData()).isNotNull();
var groundingData =
Map.of(
"grounding_query",
"grounding call",
"grounding_result",
"First chunk```Second chunk```Last found chunk");
assertThat(groundingModule.getData()).isEqualTo(groundingData);

final String requestBody = new String(fileLoader.apply("groundingRequest.json").readAllBytes());
verify(
postRequestedFor(urlPathEqualTo("/completion")).withRequestBody(equalToJson(requestBody)));
}

@Test
Expand Down
40 changes: 20 additions & 20 deletions orchestration/src/test/resources/__files/groundingResponse.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
{
"request_id": "0d9d7ce3-9ded-481f-80c6-977e78e2e905",
"request_id": "e5d2add4-408c-4da5-84ca-1d8b0fe350c8",
"module_results": {
"grounding": {
"message": "grounding result",
"data": {
"grounding_query": "grounding call",
"grounding_result": "Joule is the AI copilot that truly understands your business. Joule revolutionizes how you interact with your SAP business systems, making every touchpoint count and every task simpler.```It enables the companion of the Intelligent Enterprise, guiding you through content discovery within SAP Ecosystem, and giving a transparent role-based access to the relevant processes from everywhere. This is the one assistant experience, a unified and delightful user experience across SAP’s \u01ee solution portfolio."
"grounding_result": "First chunk```Second chunk```Last found chunk"
}
},
"templating": [
{
"role": "user",
"content": "What does Joule do? Use the following information as additional context: Joule is the AI copilot that truly understands your business. Joule revolutionizes how you interact with your SAP business systems, making every touchpoint count and every task simpler.```It enables the companion of the Intelligent Enterprise, guiding you through content discovery within SAP Ecosystem, and giving a transparent role-based access to the relevant processes from everywhere. This is the one assistant experience, a unified and delightful user experience across SAP’s \u01ee solution portfolio."
"role": "system",
"content": "Context message with embedded grounding results. First chunk```Second chunk```Last found chunk"
}
],
"llm": {
"id": "chatcmpl-AbRlNdsyQJfvBINnw3MOTP77WSE4X",
"id": "chatcmpl-Apz5s3CMf99jkOnxvPshH1rGLwvvU",
"object": "chat.completion",
"created": 1733488221,
"model": "gpt-35-turbo",
"system_fingerprint": "fp_808245b034",
"created": 1736952936,
"model": "gpt-4o-2024-08-06",
"system_fingerprint": "fp_f3927aa00d",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Joule is an AI copilot that revolutionizes how users interact with their SAP business systems. It enables the companion of the Intelligent Enterprise, guiding users through content discovery within the SAP Ecosystem and providing transparent role-based access to relevant processes from anywhere. Joule aims to provide a unified and delightful user experience across SAP's solution portfolio."
"content": "Response that makes uses of grounding results in the context message."
},
"finish_reason": "stop"
}
],
"usage": {
"completion_tokens": 68,
"prompt_tokens": 113,
"total_tokens": 181
"completion_tokens": 163,
"prompt_tokens": 217,
"total_tokens": 380
}
}
},
"orchestration_result": {
"id": "chatcmpl-AbRlNdsyQJfvBINnw3MOTP77WSE4X",
"id": "chatcmpl-Apz5s3CMf99jkOnxvPshH1rGLwvvU",
"object": "chat.completion",
"created": 1733488221,
"model": "gpt-35-turbo",
"system_fingerprint": "fp_808245b034",
"created": 1736952936,
"model": "gpt-4o-2024-08-06",
"system_fingerprint": "fp_f3927aa00d",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Joule is an AI copilot that revolutionizes how users interact with their SAP business systems. It enables the companion of the Intelligent Enterprise, guiding users through content discovery within the SAP Ecosystem and providing transparent role-based access to relevant processes from anywhere. Joule aims to provide a unified and delightful user experience across SAP's solution portfolio."
"content": "Response that makes uses of grounding results in the context message."
},
"finish_reason": "stop"
}
],
"usage": {
"completion_tokens": 68,
"prompt_tokens": 113,
"total_tokens": 181
"completion_tokens": 163,
"prompt_tokens": 217,
"total_tokens": 380
}
}
}
54 changes: 54 additions & 0 deletions orchestration/src/test/resources/groundingRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"orchestration_config" : {
"module_configurations" : {
"llm_module_config" : {
"model_name" : "gpt-35-turbo-16k",
"model_params" : {
"max_tokens" : 50,
"temperature" : 0.1,
"frequency_penalty" : 0,
"presence_penalty" : 0,
"top_p" : 1,
"n" : 1
},
"model_version" : "latest"
},
"templating_module_config" : {
"template" : [ {
"role" : "system",
"content" : "Context message with embedded grounding results. {{?results}}"
} ]
},
"grounding_module_config" : {
"type" : "document_grounding_service",
"config" : {
"filters" : [ {
"id" : "arbitrary-user-defined-id",
"search_config" : {
"max_chunk_count" : 3
},
"data_repositories" : [ "*" ],
"data_repository_type" : "vector",
"data_repository_metadata" : [ ],
"document_metadata" : [ {
"key" : "document metadata",
"value" : [ "2" ],
"select_mode" : [ "ignoreIfKeyAbsent" ]
} ],
"chunk_metadata" : [ {
"key" : "chunk metadata",
"value" : [ "1" ]
} ]
} ],
"input_params" : [ "query" ],
"output_param" : "results"
}
}
},
"stream" : false
},
"input_params" : {
"query" : "String used for similarity search in database"
},
"messages_history" : [ ]
}
Loading