|
| 1 | +--- |
| 2 | +parser: v2 |
| 3 | +auto_validation: true |
| 4 | +time: 45 |
| 5 | +primary_tag: software-product>sap-business-technology-platform |
| 6 | +tags: [ tutorial>beginner, topic>artificial-intelligence, topic>machine-learning, software-product>sap-business-technology-platform ] |
| 7 | +author_name: Smita Naik |
| 8 | +author_profile: https://github.com/I321506 |
| 9 | +--- |
| 10 | + |
| 11 | +# Leveraging Prompt Registry for Seamless Orchestration |
| 12 | +<!-- description --> In this tutorial, we will explore the Prompt Registry feature within Generative AI Hub, focusing on how to efficiently manage and utilize prompt templates in orchestration. You will learn how to register, sync, and integrate prompt templates into the orchestration workflow, ensuring dynamic and structured interactions with AI models. |
| 13 | + |
| 14 | +## You will learn |
| 15 | +- How to register and sync a prompt template in Generative AI Hub. |
| 16 | +- How to consume the registered template within an orchestration workflow. |
| 17 | +- How to apply Grounding techniques, Data Masking, and Content Filtering to refine responses. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | +- Setup Environment: |
| 21 | +Ensure your instance and AI Core credentials are properly configured according to the steps provided in the initial tutorial |
| 22 | +- Orchestration Deployment: |
| 23 | +Ensure at least one orchestration deployment is ready to be consumed during this process. |
| 24 | +- Refer to [this tutorial understand the basic consumption of GenAI models using orchestration.](https://developers.sap.com/tutorials/ai-core-orchestration-consumption.html) |
| 25 | +- Basic Knowledge: |
| 26 | +Familiarity with the orchestration workflow is recommended |
| 27 | + |
| 28 | +### Prompt Registry |
| 29 | + |
| 30 | +[OPTION BEGIN [AI Launchpad]] |
| 31 | + |
| 32 | +A **Prompt Registry** is a centralized system for storing, managing, and versioning prompt templates used in AI-driven applications. It allows developers and teams to reuse, modify, and track changes in prompts efficiently. This is particularly useful in large-scale AI projects where prompts need to be standardized, refined, and deployed across different models or scenarios. |
| 33 | + |
| 34 | +**Why Use a Prompt Registry?** |
| 35 | + |
| 36 | +- **Consistency** – Ensures uniform prompts across different use cases. |
| 37 | + |
| 38 | +- **Version Control** – Tracks prompt iterations and allows rollback if needed. |
| 39 | + |
| 40 | +- **Collaboration** – Enables teams to work on prompt engineering collaboratively. |
| 41 | + |
| 42 | +- **Automation** – Integrates prompts seamlessly into AI workflows and CI/CD pipelines. |
| 43 | + |
| 44 | +There are two key approaches to managing prompts in a **Prompt Registry**: |
| 45 | + |
| 46 | +1. **Imperative API (Direct API Control for Dynamic Prompt Management)**: The Imperative API allows you to create, update, and manage prompt templates dynamically via API calls. This approach is best suited for interactive design-time use cases, where you need to iteratively refine prompts and track their versions. Each change is explicitly made via CRUD operations, and you can manage versions manually. |
| 47 | +2. **Declarative API (Git-based Sync for Automated Prompt Management)**: The Declarative API, on the other hand, integrates with SAP AI Core applications and is ideal for CI/CD pipelines. Instead of managing templates through direct API interactions, you define them as YAML files in a Git repository. The system automatically syncs these templates, ensuring that updates are seamlessly reflected in the prompt registry without manual intervention. |
| 48 | + |
| 49 | +Next, let's dive into the Declarative approach to creating a prompt template. |
| 50 | + |
| 51 | +[OPTION END] |
| 52 | + |
| 53 | +### Create a Prompt Template (Declarative) |
| 54 | + |
| 55 | +[OPTION BEGIN [AI Launchpad]] |
| 56 | + |
| 57 | +- The declarative approach allows you to manage prompt templates using Git repositories, ensuring automatic synchronization with the Prompt Registry. Instead of making API calls to create and update templates manually, you define them in YAML files, commit them to Git, and let the system handle synchronization. |
| 58 | + |
| 59 | +- Create a prompt template and push it to your git repository. The file must be named in the following format: “**your-template-name**.prompttemplate.ai.sap.yaml”. |
| 60 | + |
| 61 | +- YAML File Structure: Copy the below code |
| 62 | + |
| 63 | +```YAML |
| 64 | + |
| 65 | +name: multi_task |
| 66 | +version: 1.1.1 |
| 67 | +scenario: multi-task-processing |
| 68 | +spec: |
| 69 | + template: |
| 70 | + - role: "system" |
| 71 | + content: "{{ ?instruction }}" |
| 72 | + - role: "user" |
| 73 | + content: "Take {{ ?user_input }} from here" |
| 74 | + defaults: |
| 75 | + instruction: "default instruction" |
| 76 | + user_input: "default user input" |
| 77 | + additionalFields: |
| 78 | + isDev: true |
| 79 | + validations: |
| 80 | + required: true |
| 81 | + blockedModels: |
| 82 | + - name: "gpt-4" |
| 83 | + versions: "gpt-4-vision" |
| 84 | + - name: "gpt-4o" |
| 85 | + versions: "*" |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +**Note** - The defaults and additionalFields fields are optional. The additionalFields field is unstructured and can be used to store metadata or configuration objects. Refer to the screenshot above for reference. |
| 91 | + |
| 92 | +- Once the YAML file is created and pushed to Git, the system automatically syncs it with the Prompt Registry. |
| 93 | + |
| 94 | +[OPTION END] |
| 95 | + |
| 96 | +### Onboarding Github and Application on AI core |
| 97 | + |
| 98 | +[OPTION BEGIN [AI Launchpad]] |
| 99 | + |
| 100 | +- Select on your SAP AI Core connection under Workspaces app in the SAP AI Launchpad. Under the **Git Repositories** section in **AI Core Administration app**, click **Add**. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +**Note:** If you don’t see the AI Core Administration app, check that you had selected your SAP AI Core connection from the Workspaces app. If it is still not visible then ask your SAP AI Launchpad administrator to assign roles to you so that you can access the app. |
| 105 | + |
| 106 | + |
| 107 | +**Enter your GitHub Repository Details** |
| 108 | + |
| 109 | +Use the following information as reference: |
| 110 | + |
| 111 | +- **URL**: Paste the URL of your GitHub repository. |
| 112 | + - Example: **https://github.tools.sap/your-username/your-repository** |
| 113 | + |
| 114 | +- **Username**: Your GitHub username. |
| 115 | + - Example: **johndoe** |
| 116 | + |
| 117 | +- **Password**: Paste your GitHub Personal Access Token, Follow below steps to create the Access Token |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +**Note:** Password does not gets validated at time of Adding Github Repository its just meant to save Github Creds to AI core. Passwords gets validated at time of creating Application or when Application refreshes connection to AI core. |
| 128 | + |
| 129 | +You will see your GitHub onboarding completed in a few seconds. As a next steps we will enable an application on AI core. |
| 130 | + |
| 131 | +- Go to your **SAP AI Launchpad**.In the **AI Core Administration app**, click **Applications > Create**. |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +- Using the reference below as a guide, specify the details of your application. This form will create your application on your SAP AI Launchpad. |
| 136 | + |
| 137 | +- Use the following information for reference: |
| 138 | + - **Application Name**: An identifier of your choice. |
| 139 | + - **Repository URL**: Your GitHub account URL and repository suffix. This helps you select the credentials to access the repository. |
| 140 | + - **Path**: The folder in your GitHub where your workflow is located. For this tutorial it is LearningScenarios. |
| 141 | + - **Revision**: The is the unique ID of your GitHub commit. Set this to **HEAD** to have it automatically refer to the latest commit. |
| 142 | + |
| 143 | +**Click on the application you created, then select 'Sync' to synchronize your changes.** |
| 144 | + |
| 145 | + |
| 146 | +After synchronization, navigate to **ML Operations > Scenarios** in the **SAP AI Core Launchpad** and verify your scenario by checking the name specified in your YAML file. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +[OPTION END] |
| 151 | + |
| 152 | +### Verifying and Consuming the Prompt Template |
| 153 | + |
| 154 | +[OPTION BEGIN [AI Launchpad]] |
| 155 | + |
| 156 | +Once the template is synced to the **AI Core Launchpad**, follow these steps to integrate it into your orchestration: |
| 157 | + |
| 158 | +- Navigate to Generative AI Hub and select the Edit Workflow option. Then, disable the Grounding module. |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +- Click on the Template tab, click on the Select icon, and choose your synced template from the list. |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | +- Configure Data Masking by selecting the sensitive information categories (e.g., Name, Organization) that need to be masked. sensitive information. |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +- Set Input Filtering thresholds for content moderation categories such as Hate, Self-Harm, Sexual, and Violence. Adjust the settings to Allow Safe and Low / Block Medium and High as needed. |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +- Select Model Configuration by choosing the appropriate model for orchestration. |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +- Set Output Filtering using the same threshold settings as input filtering to ensure consistency in moderated responses. |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +- Once all configurations are complete, click Test to validate your orchestration workflow. |
| 183 | + - Instruction: "Provide a brief explanation of SAP AI Core and its key functionalities." |
| 184 | + - User Input: "What are the main capabilities of SAP AI Core?" |
| 185 | + |
| 186 | +- After entering these values, execute the test to verify the response. The system should return relevant details based on your configured prompt template and filtering settings. |
| 187 | + |
| 188 | + |
| 189 | +[OPTION END] |
| 190 | + |
| 191 | +### Prompt Templates for Different Use Cases and Reusability |
| 192 | + |
| 193 | +[OPTION BEGIN [AI Launchpad]] |
| 194 | + |
| 195 | +**In Step 4**, we experimented with a single prompt. Now, let's explore some predefined prompt templates designed for various tasks. |
| 196 | + |
| 197 | +To proceed: |
| 198 | +- **Go to the Git repository** and edit the YAML file. |
| 199 | + |
| 200 | +- Keep only the following three fields **constant** in the YAML file: |
| 201 | + |
| 202 | + -**name** |
| 203 | + |
| 204 | + -**version** (Ensure you increment the version, e.g., from 1.1.1 to 1.1.2 when making updates.) |
| 205 | + |
| 206 | + -**scenario** |
| 207 | + |
| 208 | +- **Copy and paste** the relevant prompt templates from below into the YAML files. Modify only the **spec** section of the **YAML** file while keeping other sections unchanged. |
| 209 | + |
| 210 | + |
| 211 | +- **Save the file and sync** it to the application. |
| 212 | + |
| 213 | +**NOTE:**- Please refer to Step 4 for details on modifying the YAML file in Git, syncing it with the application, and ensuring the changes are reflected correctly. |
| 214 | + |
| 215 | +- **Test the different tasks** using these templates to see how they adapt to different use cases. |
| 216 | + |
| 217 | +**Note:** This section provides reusable prompt templates designed for various use cases in SAP AI Launchpad. Each template follows a structured format to ensure consistent and accurate outputs. Below are the prompt templates for different NLP tasks. |
| 218 | + |
| 219 | +#### The Prompt Template Resource |
| 220 | + |
| 221 | +**Template for Text Expansion** |
| 222 | + |
| 223 | +```YAML |
| 224 | +spec: |
| 225 | + template: |
| 226 | + - role: "system" |
| 227 | + content: | |
| 228 | + Expand the following short text into a detailed explanation. |
| 229 | + Return output as: |
| 230 | + Expanded Text: {{ expanded_output }} |
| 231 | + - role: "user" |
| 232 | + content: "Text: {{ ?short_text }}" |
| 233 | + additionalFields: |
| 234 | + isDev: true |
| 235 | + validations: |
| 236 | + required: true |
| 237 | +``` |
| 238 | +**Template for Multi-Task Processing** |
| 239 | +```YAML |
| 240 | +spec: |
| 241 | + template: |
| 242 | + - role: "system" |
| 243 | + content: | |
| 244 | + Perform multiple tasks at once: Detect language and translate to English. |
| 245 | + Respond in the following format: |
| 246 | + Language: <detected_language> |
| 247 | + Converted to English: <translated_text> |
| 248 | + - role: "user" |
| 249 | + content: "Text: {{ ?input_text }}" |
| 250 | + additionalFields: |
| 251 | + isDev: true |
| 252 | + validations: |
| 253 | + required: true |
| 254 | +``` |
| 255 | +
|
| 256 | +**Template for Spell Check and Correction** |
| 257 | +
|
| 258 | +```YAML |
| 259 | +spec: |
| 260 | + template: |
| 261 | + - role: "system" |
| 262 | + content: "Correct any spelling and grammatical errors in the given text. Corrected Text: {{ corrected_output }}" |
| 263 | + - role: "user" |
| 264 | + content: "{{ ?input_text }}" |
| 265 | + defaults: |
| 266 | + input_text: "default input text" |
| 267 | + |
| 268 | + additionalFields: |
| 269 | + isDev: true |
| 270 | + validations: |
| 271 | + required: true |
| 272 | +``` |
| 273 | +
|
| 274 | +**Template for Sentiment Analysis** |
| 275 | +
|
| 276 | +```YAML |
| 277 | +spec: |
| 278 | + template: |
| 279 | + - role: "system" |
| 280 | + content: | |
| 281 | + Classify the sentiment of the given text. |
| 282 | + Respond in the following format: |
| 283 | + Sentiment: {{ classification_output }} |
| 284 | + - role: "user" |
| 285 | + content: "Text: {{ ?input_text }}" |
| 286 | + additionalFields: |
| 287 | + isDev: true |
| 288 | + validations: |
| 289 | + required: true |
| 290 | +``` |
| 291 | +
|
| 292 | +**Template for Text Summarization** |
| 293 | +
|
| 294 | +```YAML |
| 295 | +spec: |
| 296 | + template: |
| 297 | + - role: "system" |
| 298 | + content: | |
| 299 | + Summarize the following text. |
| 300 | + Respond in the following format: |
| 301 | + Summary: {{ summary_output }} |
| 302 | + - role: "user" |
| 303 | + content: "Text: {{ ?input_text }}" |
| 304 | + additionalFields: |
| 305 | + isDev: true |
| 306 | + validations: |
| 307 | + required: true |
| 308 | +``` |
| 309 | +
|
| 310 | +**Template for Tone Adjustment** |
| 311 | +
|
| 312 | +```YAML |
| 313 | +spec: |
| 314 | + template: |
| 315 | + - role: "system" |
| 316 | + content: | |
| 317 | + Translate the following input to a Corporate language. |
| 318 | + Respond in the following format: |
| 319 | + Corporate Version: {{ corporate_output }} |
| 320 | + - role: "user" |
| 321 | + content: "Text: {{ ?input_text }}" |
| 322 | + additionalFields: |
| 323 | + isDev: true |
| 324 | + validations: |
| 325 | + required: true |
| 326 | +``` |
| 327 | +
|
| 328 | +**Template for Question Answering** |
| 329 | +
|
| 330 | +```YAML |
| 331 | +spec: |
| 332 | + template: |
| 333 | + - role: "system" |
| 334 | + content: | |
| 335 | + Answer the question based on the given context. |
| 336 | + Respond in the following format: |
| 337 | + Answer: {{ answer_output }} |
| 338 | + - role: "user" |
| 339 | + content: | |
| 340 | + Context: {{ ?context }} |
| 341 | + Question: {{ ?question }} |
| 342 | + additionalFields: |
| 343 | + isDev: true |
| 344 | + validations: |
| 345 | + required: true |
| 346 | +``` |
| 347 | +**NOTE:** If required, you can create a new YAML file for different tasks instead of modifying the existing one. This helps maintain clarity and version control. |
| 348 | +
|
| 349 | +[OPTION END] |
| 350 | +
|
0 commit comments