Skip to content

Commit 431402d

Browse files
authored
Merge branch 'main' into mask-grounding
2 parents 4a30c26 + 248b049 commit 431402d

File tree

9 files changed

+201
-2
lines changed

9 files changed

+201
-2
lines changed

docs/guides/ORCHESTRATION_CHAT_COMPLETION.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ In this example, the input will be masked before the call to the LLM and will re
228228

229229
Use the grounding module to provide additional context to the AI model.
230230

231+
### Vector Data Repository
232+
233+
One way to provide grounding is by using a vector data repository. This can be done as follows.
234+
231235
```java
232236
// optional filter for collections
233237
var documentMetadata =
@@ -251,7 +255,20 @@ var result = client.chatCompletion(prompt, configWithGrounding);
251255

252256
In this example, the AI model is provided with additional context in the form of grounding information.
253257

254-
`Grounding.create()` is by default a document grounding service with a vector data repository.
258+
### Grounding via *help.sap.com*
259+
260+
You can also use grounding based on *help.sap.com* for convenient SAP specific grounding. This can be achieved as follows.
261+
262+
```java
263+
var groundingHelpSapCom =
264+
DocumentGroundingFilter.create()
265+
.dataRepositoryType(DataRepositoryType.HELP_SAP_COM);
266+
var groundingConfig = Grounding.create().filters(groundingHelpSapCom);
267+
var configWithGrounding = config.withGrounding(groundingConfig);
268+
269+
var prompt = groundingConfig.createGroundingPrompt("What is a fuzzy search?");
270+
var response = client.chatCompletion(prompt, configWithGrounding);
271+
```
255272

256273
Please find [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java).
257274

docs/release-notes/release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [Orchestration] [Add Spring AI Chat Memory support](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/SPRING_AI_INTEGRATION.md#chat-memory)
1616
- [Orchestration] [Prompt templates can be consumed from registry.](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md#Chat-completion-with-Templates)
1717
- [Orchestration] [Masking is now available on grounding.](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md#mask-grounding)
18+
- [Orchestration] [Grounding via *help.sap.com* is enabled.](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md#grounding)
1819

1920
### 📈 Improvements
2021

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public GroundingModuleConfig createConfig() {
8585
.outputParam("groundingContext")
8686
.filters(filters);
8787

88+
if (filters.contains(
89+
DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.HELP_SAP_COM))) {
90+
groundingConfigConfig.setMetadataParams(null);
91+
}
92+
8893
return GroundingModuleConfig.create()
8994
.type(documentGroundingService)
9095
.config(groundingConfigConfig);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,34 @@ void testGrounding() throws IOException {
224224
}
225225
}
226226

227+
@Test
228+
void testGroundingWithHelpSapCom() throws IOException {
229+
stubFor(
230+
post(urlPathEqualTo("/completion"))
231+
.willReturn(
232+
aResponse()
233+
.withBodyFile("groundingHelpSapComResponse.json")
234+
.withHeader("Content-Type", "application/json")));
235+
val groundingHelpSapCom =
236+
DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.HELP_SAP_COM);
237+
val groundingConfig = Grounding.create().filters(groundingHelpSapCom);
238+
val configWithGrounding = config.withGrounding(groundingConfig);
239+
240+
val prompt = groundingConfig.createGroundingPrompt("What is a fuzzy search?");
241+
val response = client.chatCompletion(prompt, configWithGrounding);
242+
243+
assertThat(
244+
response.getOriginalResponse().getModuleResults().getGrounding().getData().toString())
245+
.contains(
246+
"A fuzzy search is a search technique that is designed to be fast and tolerant of errors");
247+
assertThat(response.getContent()).startsWith("A fuzzy search is a search technique");
248+
249+
try (var requestInputStream = fileLoader.apply("groundingHelpSapComRequest.json")) {
250+
final String request = new String(requestInputStream.readAllBytes());
251+
verify(postRequestedFor(urlPathEqualTo("/completion")).withRequestBody(equalToJson(request)));
252+
}
253+
}
254+
227255
@Test
228256
void testTemplating() throws IOException {
229257
stubFor(
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"request_id": "47ee9f8c-3c54-4028-aabe-358f89409de9",
3+
"module_results": {
4+
"grounding": {
5+
"message": "grounding result",
6+
"data": {
7+
"grounding_query": "grounding call",
8+
"grounding_result": "A fuzzy search is a search technique that is designed to be fast and tolerant of errors"
9+
}
10+
},
11+
"templating": [
12+
{
13+
"role": "user",
14+
"content": "What is a fuzzy search? Use the following information as additional context: A fuzzy search is a search technique that is designed to be fast and tolerant of errors"
15+
}
16+
],
17+
"llm": {
18+
"id": "chatcmpl-B7fExAgihXpKnBaaJ3uXnLgmaC6Ox",
19+
"object": "chat.completion",
20+
"created": 1741166523,
21+
"model": "gpt-4o-2024-08-06",
22+
"system_fingerprint": "fp_b705f0c291",
23+
"choices": [
24+
{
25+
"index": 0,
26+
"message": {
27+
"role": "assistant",
28+
"content": "A fuzzy search is a search technique that is designed to be fast and tolerant of errors, such as additional or missing characters or other types of spelling mistakes. This feature is particularly useful in scenarios where exact matches are not required or when dealing with imperfect data"
29+
},
30+
"finish_reason": "length"
31+
}
32+
],
33+
"usage": {
34+
"completion_tokens": 50,
35+
"prompt_tokens": 1222,
36+
"total_tokens": 1272
37+
}
38+
}
39+
},
40+
"orchestration_result": {
41+
"id": "chatcmpl-B7fExAgihXpKnBaaJ3uXnLgmaC6Ox",
42+
"object": "chat.completion",
43+
"created": 1741166523,
44+
"model": "gpt-4o-2024-08-06",
45+
"system_fingerprint": "fp_b705f0c291",
46+
"choices": [
47+
{
48+
"index": 0,
49+
"message": {
50+
"role": "assistant",
51+
"content": "A fuzzy search is a search technique that is designed to be fast and tolerant of errors, such as additional or missing characters or other types of spelling mistakes. This feature is particularly useful in scenarios where exact matches are not required or when dealing with imperfect data"
52+
},
53+
"finish_reason": "length"
54+
}
55+
],
56+
"usage": {
57+
"completion_tokens": 50,
58+
"prompt_tokens": 1222,
59+
"total_tokens": 1272
60+
}
61+
}
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"orchestration_config" : {
3+
"module_configurations" : {
4+
"llm_module_config" : {
5+
"model_name" : "gpt-4o",
6+
"model_params" : {
7+
"max_tokens" : 50,
8+
"temperature" : 0.1,
9+
"frequency_penalty" : 0,
10+
"presence_penalty" : 0,
11+
"top_p" : 1,
12+
"n" : 1
13+
},
14+
"model_version" : "latest"
15+
},
16+
"templating_module_config" : {
17+
"template" : [ {
18+
"role" : "user",
19+
"content" : "{{?userMessage}} Use the following information as additional context: {{?groundingContext}}"
20+
} ],
21+
"defaults" : { },
22+
"tools" : [ ]
23+
},
24+
"grounding_module_config" : {
25+
"type" : "document_grounding_service",
26+
"config" : {
27+
"filters" : [ {
28+
"data_repositories" : [ "*" ],
29+
"data_repository_type" : "help.sap.com",
30+
"data_repository_metadata" : [ ],
31+
"document_metadata" : [ ],
32+
"chunk_metadata" : [ ]
33+
} ],
34+
"input_params" : [ "userMessage" ],
35+
"output_param" : "groundingContext"
36+
}
37+
}
38+
},
39+
"stream" : false
40+
},
41+
"input_params" : {
42+
"userMessage" : "What is a fuzzy search?"
43+
},
44+
"messages_history" : [ ]
45+
}

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ Object grounding(
192192
return response.getContent();
193193
}
194194

195+
@GetMapping("/groundingHelpSapCom")
196+
@Nonnull
197+
Object groundingHelpSapCom(
198+
@Nullable @RequestParam(value = "format", required = false) final String format) {
199+
final var response = service.groundingHelpSapCom("What is a fuzzy search?");
200+
if ("json".equals(format)) {
201+
return response;
202+
}
203+
return response.getContent();
204+
}
205+
195206
@GetMapping("/image")
196207
@Nonnull
197208
Object imageInput(@RequestParam(value = "format", required = false) final String format) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,26 @@ public OrchestrationChatResponse grounding(
348348
return client.chatCompletion(prompt, configWithGrounding);
349349
}
350350

351+
/**
352+
* Using grounding via *help.sap.com* to provide additional SAP-specific context to the AI model.
353+
*
354+
* @link <a href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/grounding">SAP
355+
* AI Core: Orchestration - Grounding</a>
356+
* @param userMessage the user message to provide grounding for
357+
* @return the assistant response object
358+
*/
359+
@Nonnull
360+
public OrchestrationChatResponse groundingHelpSapCom(@Nonnull final String userMessage) {
361+
val groundingHelpSapCom =
362+
DocumentGroundingFilter.create().dataRepositoryType(DataRepositoryType.HELP_SAP_COM);
363+
val groundingConfig = Grounding.create().filters(groundingHelpSapCom);
364+
val configWithGrounding = config.withGrounding(groundingConfig);
365+
366+
val prompt = groundingConfig.createGroundingPrompt(userMessage);
367+
368+
return client.chatCompletion(prompt, configWithGrounding);
369+
}
370+
351371
/**
352372
* Chat request to OpenAI through the Orchestration service using response format with JSON
353373
* schema.

sample-code/spring-app/src/main/resources/static/index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,17 @@ <h2>Orchestration</h2>
360360
</div>
361361
</div>
362362
</li>
363-
363+
<li class="list-group-item">
364+
<div class="info-tooltip">
365+
<button type="submit" formaction="/orchestration/groundingHelpSapCom"
366+
class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
367+
<code>/orchestration/groundingHelpSapCom</code>
368+
</button>
369+
<div class="tooltip-content">
370+
Using grounding via *help.sap.com* to provide additional SAP-specific context to the AI model.
371+
</div>
372+
</div>
373+
</li>
364374
<li class="list-group-item">
365375
<div class="info-tooltip">
366376
<button type="submit" formaction="/orchestration/image"

0 commit comments

Comments
 (0)