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
3 changes: 2 additions & 1 deletion docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

### ✨ New Functionality

-[Orchestration] Added new models for `OrchestrationAiModel`: `SONAR`,`SONAR_PRO`.
- [Orchestration] Added new models for `OrchestrationAiModel`: `SONAR`,`SONAR_PRO`.
- [Orchestration] Convenience for adding the `metadata_params` option to grounding calls.

### 📈 Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.val;
Expand All @@ -31,6 +32,8 @@ public class Grounding implements GroundingProvider {
private List<GroundingModuleConfigConfigFiltersInner> filters =
List.of(DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.VECTOR));

@Nullable private List<String> metadataParams = null;

@Setter(onMethod_ = {@Nonnull})
private TypeEnum documentGroundingService = TypeEnum.DOCUMENT_GROUNDING_SERVICE;

Expand Down Expand Up @@ -60,6 +63,19 @@ public Grounding filters(@Nonnull final GroundingModuleConfigConfigFiltersInner.
return this;
}

/**
* Set which metadataParams are used in the grounding response.
*
* @param metadataParams List of metadataParams to set.
* @return The modified grounding configuration.
* @since 1.13.0
*/
@Nonnull
public Grounding metadataParams(@Nonnull final String... metadataParams) {
this.metadataParams = List.of(metadataParams);
return this;
}

/**
* Create a prompt with grounding parameters included in the message.
*
Expand All @@ -86,8 +102,10 @@ public GroundingModuleConfig createConfig() {
GroundingModuleConfigConfigPlaceholders.create()
.input(List.of("userMessage"))
.output("groundingContext"))
.filters(filters);
.filters(filters)
.metadataParams(metadataParams);

// metadata_params field is not allowed for data repository type: `help.sap.com`
Comment on lines +105 to +108
Copy link
Member

Choose a reason for hiding this comment

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

(minor)

This looks a bit counter intuitive. Instead of setting metadataParams first and then conditionally removing it, we should just conditionally set it.

Suggested change
.filters(filters)
.metadataParams(metadataParams);
// metadata_params field is not allowed for data repository type: `help.sap.com`
.filters(filters);
// metadata_params field is not allowed for data repository type: `help.sap.com`
val hasHepSapCom =
filters.contains(
DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.HELP_SAP_COM));
if (!hasHepSapCom) {
groundingConfigConfig.setMetadataParams(metadataParams);
}

Then, you can also set the default value of field metadataParams to null

Copy link
Member Author

Choose a reason for hiding this comment

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

This approach unfortunately does not work since if you do not set metadata params explicitly, the generated code will use an empty list which already leads to an error form grounding service when using help.sap.com.

I changed the default value of the field to null but unfortunately do not see how to improve the code snippet you marked (I agree that it doesn't look ideal).

if (filters.contains(
DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.HELP_SAP_COM))) {
groundingConfigConfig.setMetadataParams(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,28 @@ void testGrounding() throws IOException {
postRequestedFor(urlPathEqualTo("/v2/completion")).withRequestBody(equalToJson(request)));
}

@Test
void testGroundingWithConvenience() throws IOException {
Copy link
Member Author

Choose a reason for hiding this comment

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

I added this test since the only grounding unit test I saw was only testing low-level code (not our convenience).

stubFor(
post(urlPathEqualTo("/v2/completion"))
.willReturn(
aResponse()
.withBodyFile("groundingResponse.json")
.withHeader("Content-Type", "application/json")));

var dbFilters = DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.VECTOR);

var groundingConfig = Grounding.create().metadataParams("foo", "bar").filters(dbFilters);
var prompt = groundingConfig.createGroundingPrompt("Hello, what do you know?");
var configWithGrounding = config.withGrounding(groundingConfig);

final var response = client.chatCompletion(prompt, configWithGrounding);

final String request = fileLoaderStr.apply("groundingRequestMetadata.json");
verify(
postRequestedFor(urlPathEqualTo("/v2/completion")).withRequestBody(equalToJson(request)));
}

@Test
void testGroundingWithHelpSapCom() throws IOException {
stubFor(
Expand Down
51 changes: 51 additions & 0 deletions orchestration/src/test/resources/groundingRequestMetadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"config" : {
"modules" : {
"prompt_templating" : {
"prompt" : {
"template" : [ {
"content" : "{{?userMessage}} Use the following information as additional context: {{?groundingContext}}",
"role" : "user"
} ],
"defaults" : { },
"tools" : [ ]
},
"model" : {
"name" : "gpt-4o",
"version" : "latest",
"params" : {
"max_tokens" : 50,
"temperature" : 0.1,
"frequency_penalty" : 0,
"presence_penalty" : 0,
"top_p" : 1,
"n" : 1
},
"timeout" : 600,
"max_retries" : 2
}
},
"grounding" : {
"type" : "document_grounding_service",
"config" : {
"filters" : [ {
"data_repositories" : [ "*" ],
"data_repository_type" : "vector",
"data_repository_metadata" : [ ],
"document_metadata" : [ ],
"chunk_metadata" : [ ]
} ],
"placeholders" : {
"input" : [ "userMessage" ],
"output" : "groundingContext"
},
"metadata_params" : [ "foo", "bar" ]
}
}
}
},
"placeholder_values" : {
"userMessage" : "Hello, what do you know?"
},
"messages_history" : [ ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,12 @@ public OrchestrationChatResponse grounding(
.searchConfig(GroundingFilterSearchConfiguration.create().maxChunkCount(1))
.addDocumentMetadataItem(documentMetadata);

val groundingConfig = Grounding.create().filters(databaseFilter);
val prompt = groundingConfig.createGroundingPrompt(userMessage);
val groundingConfig = Grounding.create().filters(databaseFilter).metadataParams("*");
val prompt =
groundingConfig
.createGroundingPrompt(userMessage)
.messageHistory(
List.of(Message.system("Add in the response all metadata from grounding.")));
val maskingConfig = // optional masking configuration
DpiMasking.anonymization()
.withEntities(DPIEntities.SENSITIVE_DATA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ void testGrounding() {
assertThat(result.getIntermediateResults().getGrounding().getData()).isNotNull();
assertThat(result.getIntermediateResults().getGrounding().getMessage())
.isEqualTo("grounding result");
var groundingData =
(Map<String, String>) result.getIntermediateResults().getGrounding().getData();
assertThat(groundingData.get("grounding_result")).contains("metadata");

var maskingResult = result.getIntermediateResults().getInputMasking();
assertThat(maskingResult.getMessage()).isNotEmpty();
Expand Down
Loading