Skip to content

Commit e9e2294

Browse files
committed
fix sample app
1 parent da3f72d commit e9e2294

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import com.sap.ai.sdk.orchestration.model.DPIEntities;
1010
import com.sap.cloud.sdk.cloudplatform.thread.ThreadContextExecutors;
1111
import java.io.IOException;
12+
import java.nio.charset.StandardCharsets;
1213
import java.util.List;
1314
import javax.annotation.Nonnull;
1415
import javax.annotation.Nullable;
1516
import lombok.extern.slf4j.Slf4j;
1617
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.core.io.ResourceLoader;
1719
import org.springframework.http.MediaType;
1820
import org.springframework.http.ResponseEntity;
1921
import org.springframework.web.bind.annotation.GetMapping;
@@ -30,6 +32,7 @@
3032
@RequestMapping("/orchestration")
3133
class OrchestrationController {
3234
@Autowired private OrchestrationService service;
35+
@Autowired private ResourceLoader resourceLoader;
3336

3437
@GetMapping("/completion")
3538
Object completion(
@@ -276,8 +279,10 @@ Object templateFromPromptRegistryByScenario(
276279
@Nonnull
277280
Object localPromptTemplate(@RequestParam(value = "format", required = false) final String format)
278281
throws IOException {
279-
final var response =
280-
service.localPromptTemplate("src/main/resources/promptTemplateExample.yaml");
282+
final var resource = resourceLoader.getResource("classpath:promptTemplateExample.yaml");
283+
final var promptTemplate =
284+
new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
285+
final var response = service.localPromptTemplate(promptTemplate);
281286
if ("json".equals(format)) {
282287
return response;
283288
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.sap.ai.sdk.orchestration.model.SearchSelectOptionEnum;
3131
import com.sap.ai.sdk.orchestration.model.Template;
3232
import java.io.IOException;
33-
import java.nio.file.Files;
34-
import java.nio.file.Path;
3533
import java.util.List;
3634
import java.util.Map;
3735
import java.util.stream.Stream;
@@ -518,15 +516,14 @@ public OrchestrationChatResponse templateFromPromptRegistryByScenario(
518516
*
519517
* @link <a href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/templating">SAP
520518
* AI Core: Orchestration - Templating</a>
521-
* @param filePath the filepath to the YAML file
519+
* @param promptTemplate the YAML prompt template to use
522520
* @throws IOException if the YAML cannot be parsed
523521
* @return the assistant response object
524522
*/
525523
@Nonnull
526-
public OrchestrationChatResponse localPromptTemplate(@Nonnull final String filePath)
524+
public OrchestrationChatResponse localPromptTemplate(@Nonnull final String promptTemplate)
527525
throws IOException {
528-
val promptTemplateYaml = Files.readString(Path.of(filePath));
529-
val template = TemplateConfig.create().fromYaml(promptTemplateYaml);
526+
val template = TemplateConfig.create().fromYaml(promptTemplate);
530527
val configWithTemplate = template != null ? config.withTemplateConfig(template) : config;
531528

532529
val inputParams = Map.of("language", "German");

sample-code/spring-app/src/main/resources/promptTemplateExample.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@ spec:
1212
defaults:
1313
word: "apple"
1414
response_format:
15-
type: "json_object"
15+
type: json_schema
16+
json_schema:
17+
name: translation-schema
18+
description: Translate the given word into the provided language.
19+
strict: true
20+
schema:
21+
type: object
22+
additionalProperties: False
23+
required:
24+
- language
25+
- translation
26+
properties:
27+
language:
28+
type: string
29+
translation:
30+
type: string

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.io.InputStream;
1818
import java.net.URL;
1919
import java.nio.charset.StandardCharsets;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
2022
import java.util.Base64;
2123
import java.util.List;
2224
import java.util.Map;
@@ -343,7 +345,8 @@ void testTemplateFromPromptRegistryByScenario() {
343345
void testLocalPromptTemplate() throws IOException {
344346
final var result =
345347
service
346-
.localPromptTemplate("src/main/resources/promptTemplateExample.yaml")
348+
.localPromptTemplate(
349+
Files.readString(Path.of("src/main/resources/promptTemplateExample.yaml")))
347350
.getOriginalResponse();
348351
final var choices = ((LLMModuleResultSynchronous) result.getOrchestrationResult()).getChoices();
349352
assertThat(choices.get(0).getMessage().getContent()).isNotEmpty();

0 commit comments

Comments
 (0)