diff --git a/docs-java/ai-core/prompt-registry.mdx b/docs-java/ai-core/prompt-registry.mdx index d5af27f06..f998df2f3 100644 --- a/docs-java/ai-core/prompt-registry.mdx +++ b/docs-java/ai-core/prompt-registry.mdx @@ -43,6 +43,8 @@ See [an example pom in our Spring Boot application](https://github.com/SAP/ai-sd ## Create a Prompt Template +### Tenant Scope Approach + You can create a reusable prompt for a specific use case, including placeholders that are filled later. ```java @@ -69,6 +71,33 @@ PromptTemplatePostResponse response = client.createUpdatePromptTemplate(template Refer to the [PromptRegistryController.java](https://github.com/SAP/ai-sdk-java/tree/main/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/PromptRegistryController.java) in our Spring Boot application for a complete example. +### Resource Group Scope Approach + +```java +PromptClient client = new PromptClient(); + +var spec = + PromptTemplateSpec.create() + .template( + SingleChatTemplate.create() + .role("system") + .content( + "You classify input text into the two following categories: {{?categories}}"), + SingleChatTemplate.create().role("user").content("{{?inputExample}}")) + .defaults(Map.of("categories", "Finance, Tech, Sports")); + +var template = PromptTemplatePostRequest.create() + .name("template-name") + .version("0.0.1") + .scenario("categorization") + .spec(spec); + +var resourceGroupId = "ai-sdk-java-e2e"; // your resource group id +var resourceGroupScope = "true"; + +PromptTemplatePostResponse response = client.createUpdatePromptTemplate(template, resourceGroupId, resourceGroupScope); +``` + ## Update a Prompt Template To update an existing prompt template, you can use the same `createUpdatePromptTemplate` method with the updated template details: @@ -85,6 +114,21 @@ PromptTemplatePostResponse response = client.createUpdatePromptTemplate(updatedT Refer to the [PromptRegistryController.java](https://github.com/SAP/ai-sdk-java/tree/main/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/PromptRegistryController.java) in our Spring Boot application for a complete example. +### Resource Group Scope Approach + +```java +var updatedSpec = spec.defaults(Map.of("categories", "Finance, Tech, Sports, Politics")); + +// using the same version will save the old prompt in the history +// using a new version will create a new prompt with a clean history +var updatedTemplate = template.spec(updatedSpec); + +var resourceGroupId = "ai-sdk-java-e2e"; // your resource group id +var resourceGroupScope = "true"; + +PromptTemplatePostResponse response = client.createUpdatePromptTemplate(updatedTemplate, resourceGroupId, resourceGroupScope); +``` + ## Get a Prompt Template You can retrieve a prompt template by ID, or by the combination of name, scenario, and version. @@ -115,6 +159,8 @@ Refer to the [PromptRegistryController.java](https://github.com/SAP/ai-sdk-java/ You can fill a prompt template by ID, or by the combination of name, scenario, and version. +### Tenant Scope Approach + ```java PromptClient client = new PromptClient(); @@ -127,10 +173,29 @@ PromptTemplateSubstitutionResponse substitution = client.parsePromptTemplateById Refer to the [PromptRegistryController.java](https://github.com/SAP/ai-sdk-java/tree/main/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/PromptRegistryController.java) in our Spring Boot application for a complete example. +### Resource Group Scope Approach + +```java +PromptClient client = new PromptClient(); + +var resourceGroupId = "ai-sdk-java-e2e"; // your resource group id +var resourceGroupScope = "true"; + +PromptTemplateSubstitutionResponse substitution = client.parsePromptTemplateById( + "212a9b9b-a532-4c1c-8852-bf75de853d74", + resourceGroupId, + resourceGroupScope, + false, + PromptTemplateSubstitutionRequest.create() + .inputParams(Map.of("inputExample", "I love football"))); +``` + ## Import a Prompt Template You can import a declarative prompt template as a single file export in yaml format. +### Tenant Scope Approach + ```java PromptClient client = new PromptClient(); @@ -140,6 +205,18 @@ PromptTemplatePostResponse response = client.importPromptTemplate(template.getFi Refer to the [PromptRegistryController.java](https://github.com/SAP/ai-sdk-java/tree/main/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/PromptRegistryController.java) in our Spring Boot application for a complete example. +### Resource Group Scope Approach + +```java +PromptClient client = new PromptClient(); + +var resourceGroupId = "ai-sdk-java-e2e"; // your resource group id +var resourceGroupScope = "true"; + +Resource template = new ClassPathResource("prompt-template.yaml"); +PromptTemplatePostResponse response = client.importPromptTemplate(resourceGroupId, resourceGroupScope, template.getFile()); +``` + ## Export a Prompt Template You can export a prompt template as a single file export in declarative compatible yaml format. @@ -150,6 +227,8 @@ Currently not working ## Delete a Prompt Template +### Tenant Scope Approach + Delete a specific version of the prompt template, for imperatively managed prompt templates only. ```java @@ -160,6 +239,17 @@ PromptTemplateDeleteResponse response = client.deletePromptTemplate(template.get Refer to the [PromptRegistryController.java](https://github.com/SAP/ai-sdk-java/tree/main/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/PromptRegistryController.java) in our Spring Boot application for a complete example. +### Resource Group Scope Approach + +```java +PromptClient client = new PromptClient(); + +var resourceGroupId = "ai-sdk-java-e2e"; // your resource group id +var resourceGroupScope = "true"; + +PromptTemplateDeleteResponse response = client.deletePromptTemplate(template.getId(), resourceGroupId, resourceGroupScope); +``` + ## Using Templates in SpringAI You can use prompt templates with input parameters in your Spring AI application. diff --git a/docs-java/orchestration/chat-completion.mdx b/docs-java/orchestration/chat-completion.mdx index d796de79d..9bdd62cb4 100644 --- a/docs-java/orchestration/chat-completion.mdx +++ b/docs-java/orchestration/chat-completion.mdx @@ -180,6 +180,8 @@ In this case the template is defined with the placeholder `{{?language}}` which Alternatively, you can use already prepared templates from the [**Prompt Registry**](../ai-core/prompt-registry) of SAP AI Core instead of passing a template in the request yourself. +#### Tenant Scope Level Approach + ```java var template = TemplateConfig.reference().byId("21cb1358-0bf1-4f43-870b-00f14d0f9f16"); var configWithTemplate = config.withTemplateConfig(template); @@ -190,6 +192,21 @@ var prompt = new OrchestrationPrompt(inputParams); var result = client.chatCompletion(prompt, configWithTemplate); ``` +#### Resource Group Scope Level Approach + +```java +var destination = new AiCoreService().getInferenceDestination("resource-group-id").forScenario("orchestration"); +var clientWithResourceGroup = new OrchestrationClient(destination); + +var template = TemplateConfig.reference().byId("id").withScope(RESOURCE_GROUP); +var configWithTemplate = config.withTemplateConfig(template); + +var inputParams = Map.of("categories", "Finance, Tech, Sports", "inputExample", "What's the latest news on the stock market?"); +var prompt = new OrchestrationPrompt(inputParams); + +var result = clientWithResourceGroup.chatCompletion(prompt, configWithTemplate); +``` + A prompt template can be referenced either by ID as above, or by using a combination of name, scenario, and version. For details on storing a template in the Prompt Registry, refer to [this guide](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-prompt-template-imperative).