📝 Prompt Management
Quản lý prompts hiệu quả cho AI applications.
Tại Sao Cần Quản Lý Prompts?
Text
1Problems với hardcoded prompts:2- Khó update/modify3- Không A/B test được4- Duplicate code5- Khó maintain nhiều AI featuresPrompt Storage Strategies
Strategy 1: Database Storage
Text
1Data Type: Prompt2- name (text, unique)3- system_prompt (text)4- user_template (text)5- model (text)6- temperature (number)7- max_tokens (number)8- is_active (yes/no)9- version (number)Strategy 2: Options/Settings
Text
1App Settings:2- prompt_summarize3- prompt_translate4- prompt_chat_system5- prompt_content_genStrategy 3: Backend Workflows
Text
1Store prompts in Bubble's Backend:2- Scheduled API calls3- Reusable API workflows4- Centralized managementBuilding a Prompt Manager
Admin UI
Text
1┌────────────────────────────────────────┐2│ Prompt Manager [+ New] │3├────────────────────────────────────────┤4│ ┌──────────────────────────────────┐ │5│ │ content_generator [Edit]│ │6│ │ v2 | gpt-4o-mini | Active │ │7│ └──────────────────────────────────┘ │8│ ┌──────────────────────────────────┐ │9│ │ summarizer [Edit]│ │10│ │ v1 | gpt-4o-mini | Active │ │11│ └──────────────────────────────────┘ │12└────────────────────────────────────────┘Prompt Editor
Text
1Form fields:21. Name (unique identifier)32. System Prompt (multiline)43. User Template (with {variables})54. Model Selection65. Temperature slider76. Max Tokens87. Test PanelVariable System
Template Variables
Text
1User Template:2"Write a {content_type} about {topic} in {tone} tone.3Target audience: {audience}4Length: {length} words"5 6Variables:7- {content_type}: blog, email, ad8- {topic}: user input9- {tone}: professional, casual10- {audience}: dropdown11- {length}: number inputDynamic Replacement
Text
1In Bubble:2 31. Get prompt template from database42. Use "Find & Replace" plugin5 - Find: {topic}6 - Replace with: Input Topic's value73. Repeat for each variable84. Send to APIPrompt Versioning
Version Control
Text
1Each prompt change:21. Keep old version (mark inactive)32. Create new version43. Track performance per version5 6Data structure:7Prompt:8- name9- version (number)10- is_active (yes/no)11- content12- created_date13- performance_scoreA/B Testing
Text
1Setup:2- PromptA (50% traffic)3- PromptB (50% traffic)4 5Random selection:61. Generate random number72. If < 0.5 → PromptA83. Else → PromptB94. Log which was used105. Track resultsContext Injection
User Context
Text
1System prompt enhancement:2 3"You are assisting {user_name}.4Their account type: {account_type}5Previous interactions: {interaction_count}6Preferences: {user_preferences}"App Context
Text
1Add app-specific context:2 3"Current app state: {page_context}4User's recent actions: {recent_actions}5Related content: {relevant_data}"Best Practices
Prompt Structure
Template Structure
Text
1ROLE:2[Who the AI should be]3 4TASK:5[What to do]6 7CONTEXT:8[Background information]9 10FORMAT:11[Expected output format]12 13CONSTRAINTS:14[Rules and limitations]15 16EXAMPLES:17[If needed]Clear Instructions
Text
1❌ Bad:2"Write something about AI"3 4✅ Good:5"Write a 200-word introduction to AI for beginners.6Use simple language, avoid jargon.7Include one real-world example.8End with a question to engage readers."Reusable Prompt Functions
Backend Workflow Setup
Text
1In Bubble - Backend Workflows:2 3Workflow: generate_content4Parameters:5- prompt_name (text)6- variables (text/JSON)7- user_id (User)8 9Steps:101. Get Prompt where name = prompt_name112. Replace variables in template123. Call OpenAI API134. Log usage145. Return responseCalling from Frontend
Text
1Workflow:21. Button clicked32. Schedule API workflow: generate_content4 - prompt_name: "blog_intro"5 - variables: JSON with user inputs63. Wait for response74. Display resultSecurity Considerations
Prompt Security
Text
1Protect prompts:2- Don't expose full system prompts3- Validate user inputs4- Filter output5 6Prevent injection:7- Sanitize user input8- Limit input length9- Use structured data when possiblePerformance Optimization
Caching
Text
1Cache strategy:21. Hash input (prompt + variables)32. Check cache database43. If hit → return cached response54. If miss → call API, save to cache6 7Cache Data Type:8- input_hash9- response10- created_date11- hit_countRate Limiting
Text
1Per-user limits:2- Free: 10 requests/day3- Pro: 100 requests/day4- Enterprise: Unlimited5 6Track in database:7- user_id8- request_count9- last_reset_dateDebugging Prompts
Logging
Text
1Log each AI call:2 3AILog:4- prompt_used5- user_input6- full_request7- response8- response_time9- tokens_used10- error (if any)Testing Panel
Text
1Build admin test interface:21. Select prompt32. Enter test variables43. Run test54. View full request/response65. Adjust and iterateBài Tập
Practice
Xây dựng Prompt Manager:
- Create Prompt data type
- Build admin editor page
- Implement variable replacement
- Add prompt versioning
- Create usage logging
- Test with real AI calls
Tiếp theo: Bài 8 - Handling AI Responses
