Skip to content

Commit 5b2a54d

Browse files
committed
Merge remote-tracking branch 'origin/translation-module-convenience' into translation-module-convenience
2 parents 0173603 + af3bba9 commit 5b2a54d

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

docs/release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
### ✨ New Functionality
1414

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

1718
### 📈 Improvements
1819

orchestration/src/main/java/com/sap/ai/sdk/orchestration/Grounding.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Map;
1313
import javax.annotation.Nonnull;
14+
import javax.annotation.Nullable;
1415
import lombok.Setter;
1516
import lombok.experimental.Accessors;
1617
import lombok.val;
@@ -31,6 +32,8 @@ public class Grounding implements GroundingProvider {
3132
private List<GroundingModuleConfigConfigFiltersInner> filters =
3233
List.of(DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.VECTOR));
3334

35+
@Nullable private List<String> metadataParams = null;
36+
3437
@Setter(onMethod_ = {@Nonnull})
3538
private TypeEnum documentGroundingService = TypeEnum.DOCUMENT_GROUNDING_SERVICE;
3639

@@ -60,6 +63,19 @@ public Grounding filters(@Nonnull final GroundingModuleConfigConfigFiltersInner.
6063
return this;
6164
}
6265

66+
/**
67+
* Set which metadataParams are used in the grounding response.
68+
*
69+
* @param metadataParams List of metadataParams to set.
70+
* @return The modified grounding configuration.
71+
* @since 1.13.0
72+
*/
73+
@Nonnull
74+
public Grounding metadataParams(@Nonnull final String... metadataParams) {
75+
this.metadataParams = List.of(metadataParams);
76+
return this;
77+
}
78+
6379
/**
6480
* Create a prompt with grounding parameters included in the message.
6581
*
@@ -86,8 +102,10 @@ public GroundingModuleConfig createConfig() {
86102
GroundingModuleConfigConfigPlaceholders.create()
87103
.input(List.of("userMessage"))
88104
.output("groundingContext"))
89-
.filters(filters);
105+
.filters(filters)
106+
.metadataParams(metadataParams);
90107

108+
// metadata_params field is not allowed for data repository type: `help.sap.com`
91109
if (filters.contains(
92110
DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.HELP_SAP_COM))) {
93111
groundingConfigConfig.setMetadataParams(null);

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ void testGrounding() throws IOException {
268268
postRequestedFor(urlPathEqualTo("/v2/completion")).withRequestBody(equalToJson(request)));
269269
}
270270

271+
@Test
272+
void testGroundingWithConvenience() throws IOException {
273+
stubFor(
274+
post(urlPathEqualTo("/v2/completion"))
275+
.willReturn(
276+
aResponse()
277+
.withBodyFile("groundingResponse.json")
278+
.withHeader("Content-Type", "application/json")));
279+
280+
var dbFilters = DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.VECTOR);
281+
282+
var groundingConfig = Grounding.create().metadataParams("foo", "bar").filters(dbFilters);
283+
var prompt = groundingConfig.createGroundingPrompt("Hello, what do you know?");
284+
var configWithGrounding = config.withGrounding(groundingConfig);
285+
286+
final var response = client.chatCompletion(prompt, configWithGrounding);
287+
288+
final String request = fileLoaderStr.apply("groundingRequestMetadata.json");
289+
verify(
290+
postRequestedFor(urlPathEqualTo("/v2/completion")).withRequestBody(equalToJson(request)));
291+
}
292+
271293
@Test
272294
void testGroundingWithHelpSapCom() throws IOException {
273295
stubFor(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"config" : {
3+
"modules" : {
4+
"prompt_templating" : {
5+
"prompt" : {
6+
"template" : [ {
7+
"content" : "{{?userMessage}} Use the following information as additional context: {{?groundingContext}}",
8+
"role" : "user"
9+
} ],
10+
"defaults" : { },
11+
"tools" : [ ]
12+
},
13+
"model" : {
14+
"name" : "gpt-4o",
15+
"version" : "latest",
16+
"params" : {
17+
"max_tokens" : 50,
18+
"temperature" : 0.1,
19+
"frequency_penalty" : 0,
20+
"presence_penalty" : 0,
21+
"top_p" : 1,
22+
"n" : 1
23+
},
24+
"timeout" : 600,
25+
"max_retries" : 2
26+
}
27+
},
28+
"grounding" : {
29+
"type" : "document_grounding_service",
30+
"config" : {
31+
"filters" : [ {
32+
"data_repositories" : [ "*" ],
33+
"data_repository_type" : "vector",
34+
"data_repository_metadata" : [ ],
35+
"document_metadata" : [ ],
36+
"chunk_metadata" : [ ]
37+
} ],
38+
"placeholders" : {
39+
"input" : [ "userMessage" ],
40+
"output" : "groundingContext"
41+
},
42+
"metadata_params" : [ "foo", "bar" ]
43+
}
44+
}
45+
}
46+
},
47+
"placeholder_values" : {
48+
"userMessage" : "Hello, what do you know?"
49+
},
50+
"messages_history" : [ ]
51+
}

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,12 @@ public OrchestrationChatResponse grounding(
376376
.searchConfig(GroundingFilterSearchConfiguration.create().maxChunkCount(1))
377377
.addDocumentMetadataItem(documentMetadata);
378378

379-
val groundingConfig = Grounding.create().filters(databaseFilter);
380-
val prompt = groundingConfig.createGroundingPrompt(userMessage);
379+
val groundingConfig = Grounding.create().filters(databaseFilter).metadataParams("*");
380+
val prompt =
381+
groundingConfig
382+
.createGroundingPrompt(userMessage)
383+
.messageHistory(
384+
List.of(Message.system("Add in the response all metadata from grounding.")));
381385
val maskingConfig = // optional masking configuration
382386
DpiMasking.anonymization()
383387
.withEntities(DPIEntities.SENSITIVE_DATA)

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/OrchestrationTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ void testGrounding() {
209209
assertThat(result.getIntermediateResults().getGrounding().getData()).isNotNull();
210210
assertThat(result.getIntermediateResults().getGrounding().getMessage())
211211
.isEqualTo("grounding result");
212+
var groundingData =
213+
(Map<String, String>) result.getIntermediateResults().getGrounding().getData();
214+
assertThat(groundingData.get("grounding_result")).contains("metadata");
212215

213216
var maskingResult = result.getIntermediateResults().getInputMasking();
214217
assertThat(maskingResult.getMessage()).isNotEmpty();

0 commit comments

Comments
 (0)