Prompt Registry
Introduction
This guide provides examples on how to manage the life cycle of your prompts, from design to runtime in Prompt Registry.
All classes under any of the ...model
packages are generated from an OpenAPI specification.
This means that these model classes are not guaranteed to be stable and may change with future releases.
They are safe to use, but may require updates even in minor releases.
Prerequisites
Before using the Prompt Registry module, ensure that you have met all the general requirements outlined in the overview. Additionally, include the necessary Maven dependency in your project.
Maven Dependencies
Add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.sap.ai.sdk</groupId>
<artifactId>prompt-registry</artifactId>
<version>${ai-sdk.version}</version>
</dependency>
See an example pom in our Spring Boot application
Create a Prompt Template
You can create a reusable prompt for a specific use case, including placeholders that are filled later.
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);
PromptTemplatePostResponse response = client.createUpdatePromptTemplate(template);
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Update a Prompt Template
To update an existing prompt template, you can use the same createUpdatePromptTemplate
method with the updated template details:
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);
PromptTemplatePostResponse response = client.createUpdatePromptTemplate(updatedTemplate);
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Get a Prompt Template
You can retrieve a prompt template by ID, or by the combination of name, scenario, and version.
Prompt templates can also be retrieved and consumed in orchestration. For more information, see Templating.
PromptClient client = new PromptClient();
PromptTemplateListResponse templates = client.listPromptTemplates();
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Get a Prompt Template History
You can list the history of edits to prompt templates, for imperatively managed prompt templates only.
PromptClient client = new PromptClient();
PromptTemplateListResponse history = client.listPromptTemplateHistory("categorization", "0.0.1", NAME);
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Use a Prompt Template
You can fill a prompt template by ID, or by the combination of name, scenario, and version.
PromptClient client = new PromptClient();
PromptTemplateSubstitutionResponse substitution = client.parsePromptTemplateById(
"212a9b9b-a532-4c1c-8852-bf75de853d74",
false,
PromptTemplateSubstitutionRequest.create()
.inputParams(Map.of("inputExample", "I love football")));
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Import a Prompt Template
You can import a declarative prompt template as a single file export in yaml format.
PromptClient client = new PromptClient();
Resource template = new ClassPathResource("prompt-template.yaml");
PromptTemplatePostResponse response = client.importPromptTemplate(template.getFile());
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Export a Prompt Template
You can export a prompt template as a single file export in declarative compatible yaml format.
Currently not working
Delete a Prompt Template
Delete a specific version of the prompt template, for imperatively managed prompt templates only.
PromptClient client = new PromptClient();
PromptTemplateDeleteResponse response = client.deletePromptTemplate(template.getId())
Refer to the PromptRegistryController.java in our Spring Boot application for a complete example.
Locally Test a Prompt Template
Note that you can also locally test a prompt, without needing it to be deployed in the Prompt Registry. For more information, see the Orchestration documentation.