Prompt Registry
The Prompt Registry API allows you to create, manage, and retrieve prompt and orchestration config templates for use in SAP AI Core when working with Generative AI Hub models.
See SAP Help for the difference between imperative and declarative prompt templates.
In this page, we walk through the design-time process of creating prompt and config templates using the SDK (this is also known as imperative Prompt Template creation). You can create and modify the Prompt Template and Orchestration Config Template — each is versioned automatically, and the versions can be retrieved by an id.
Prerequisite
Provide the credentials to authenticate the client and establish a connection with the Prompt Registry API. See options for providing credentials here.
Prompt Template Management
Initialize Client
from gen_ai_hub.proxy import get_proxy_client
from gen_ai_hub.prompt_registry import PromptTemplateClient
proxy_client = get_proxy_client(proxy_version="gen-ai-hub")
prompt_registry_client = PromptTemplateClient(proxy_client=proxy_client)
Create a Prompt Template
Define the Prompt Template configuration and post it to the Prompt Registry.
from gen_ai_hub.prompt_registry import PromptTemplateSpec, PromptTemplate
prompt_template_spec = PromptTemplateSpec(
template=[PromptTemplate(role='system', content='You are a helpful assistant.')]
)
# PromptTemplateSpec can also include response_format, tools, defaults, and additional_fields.
template_id = prompt_registry_client.create_prompt_template(
scenario='MyScenario',
name='prompt_template_name',
version='1.0.0',
prompt_template_spec=prompt_template_spec
).id
print(f"Created Prompt Template with ID: {template_id}")
Retrieve a Prompt Template
Retrieve the Prompt Template by ID.
response = prompt_registry_client.get_prompt_template_by_id(template_id)
print(response.spec.template)
Modify a Prompt Template
We will add an input variable to the existing Prompt Template.
prompt_template_spec = PromptTemplateSpec(
template=[PromptTemplate(role='system', content='You are a helpful assistant for {{ ?topic }}.')]
)
response = prompt_registry_client.create_prompt_template(
scenario='MyScenario',
name='prompt_template_name',
version='1.0.0',
prompt_template_spec=prompt_template_spec
)
input_template_id = response.id
print(response.message)
Prompt Template History
Retrieve the history of Prompt Templates by scenario, name, and version.
response = prompt_registry_client.get_prompt_template_history(
scenario='MyScenario', name='prompt_template_name', version='1.0.0'
)
print(response.model_dump_json())
Fill a Prompt Template
Fill the variables in the Prompt Template.
response = prompt_registry_client.fill_prompt_template_by_id(
template_id=input_template_id, input_params={"topic": "chemistry"}
)
print(response.parsed_prompt)
Orchestration Config Management
Initialize Client
from gen_ai_hub.proxy import get_proxy_client
from gen_ai_hub.prompt_registry import OrchestrationConfigClient
proxy_client = get_proxy_client(proxy_version="gen-ai-hub")
prompt_registry_client = OrchestrationConfigClient(proxy_client=proxy_client)
Create an Orchestration Config
Define the Orchestration Config configuration and post it to the Prompt Registry.
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, ModuleConfig, LLMModelDetails, UserMessage, Template, PromptTemplatingModuleConfig
)
config_spec = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(content="Hello, World!")]),
model=LLMModelDetails(name="gpt-4o-mini")
)
)
)
template_id = prompt_registry_client.create_orchestration_config(
scenario='MyScenario',
name='prompt_template_name',
version='1.0.0',
spec=config_spec
).id
print(f"Created Orchestration Config Template with ID: {template_id}")
Retrieve an Orchestration Config
Retrieve the Orchestration Config by ID.
response = prompt_registry_client.get_orchestration_config_by_id(template_id)
print(response.spec)
By scenario, name, and version:
response = prompt_registry_client.get_orchestration_configs(
scenario='MyScenario', name='prompt_template_name', version='1.0.0'
)
print(response.resources)
Export an Orchestration Config
Export a design orchestration config to a declarative-compatible YAML file.
response = prompt_registry_client.export_orchestration_config(config_id=template_id)
print(response)