Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs-java/ai-core/prompt-registry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions docs-java/orchestration/chat-completion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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).

Expand Down