Skip to content

Commit af3bba9

Browse files
Merge branch 'main' into translation-module-convenience
2 parents 3d0d337 + 7760819 commit af3bba9

File tree

9 files changed

+130
-14
lines changed

9 files changed

+130
-14
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+
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<javaparser.version>3.27.1</javaparser.version>
7272
<jsonschema-generator.version>4.38.0</jsonschema-generator.version>
7373
<jackson.version>2.20.1</jackson.version>
74-
<logback.version>1.5.20</logback.version>
74+
<logback.version>1.5.21</logback.version>
7575
<!-- conflicts resolution -->
7676
<micrometer.version>1.16.0</micrometer.version>
7777
<json.version>20250517</json.version>
@@ -289,7 +289,7 @@
289289
<plugin>
290290
<groupId>org.apache.maven.plugins</groupId>
291291
<artifactId>maven-jar-plugin</artifactId>
292-
<version>3.4.2</version>
292+
<version>3.5.0</version>
293293
</plugin>
294294
<plugin>
295295
<groupId>com.sap.cloud.sdk.datamodel</groupId>

sample-code/spring-app/README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
Sample code to demonstrate the usage of the SAP AI SDK.
66
Also used as basis for running E2E tests.
77

8+
## Prerequisites
9+
10+
Before running the application, ensure the following prerequisites are met:
11+
12+
- Java 17 or higher
13+
- Maven 3.8 or higher
14+
- Credentials for [SAP AI Core](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/what-is-sap-ai-core) service configured
15+
- Deployments of the following models in the `default` resource group:
16+
- `gpt-4o-mini`
17+
- `text-embedding-3-small`
18+
19+
### Setting Up Credentials for SAP AI Core
20+
21+
To set up credentials for the AI Core service:
22+
23+
- Follow the instruction at [Connecting to AI Core](https://sap.github.io/ai-sdk/docs/java/guides/connecting-to-ai-core) to create a service key for the AI Core service.
24+
- Add the service key content either to a `.env` file in the sample app directory or directly to the `AI_CORE_SERVICE_KEY` environment variable.
25+
26+
### Deploying Generative AI Models in SAP AI Core
27+
28+
To deploy `gpt-4o-mini` and `text-embedding-3-small` models, follow the instructions at:
29+
[Create a Deployment for a Generative AI Model](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-deployment-for-generative-ai-model-in-sap-ai-core)
30+
831
## Build and Run the Sample App
932

1033
Before you can run the sample app, you need to install the AI SDK into your local Maven repository:
@@ -15,13 +38,7 @@ Before you can run the sample app, you need to install the AI SDK into your loca
1538
> The sample app uses the latest state of the SDK, so make sure to install the SDK after pulling a new version via Git.
1639
> Alternatively, you check out one of the release tags of the repository, e.g. `git fetch --all --tags && git checkout rel/1.1.0`.
1740
18-
Next, you'll need to set up credentials for the AI Core service:
19-
20-
* Follow [these instructions](https://sap.github.io/ai-sdk/docs/java/guides/connecting-to-ai-core) to create a service key for the AI Core service.
21-
22-
⚠️ Put the `.env` file in the sample app directory.
23-
24-
Finally, you can start the sample app:
41+
Start the sample app:
2542

2643
* Run `mvn spring-boot:run` from the sample app directory.
2744

sample-code/spring-app/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
<properties>
3535
<project.rootdir>${project.basedir}/../../</project.rootdir>
3636
<spring-boot.version>3.5.7</spring-boot.version>
37-
<logback.version>1.5.20</logback.version>
38-
<cf-logging.version>3.8.6</cf-logging.version>
37+
<logback.version>1.5.21</logback.version>
38+
<cf-logging.version>4.0.0</cf-logging.version>
3939
<apache-tomcat-embed.version>11.0.13</apache-tomcat-embed.version>
4040
<!-- Skip end-to-end tests by default, can be overridden with -DskipTests=false -->
4141
<skipTests>true</skipTests>

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)