Lý thuyết
30 phút
Bài 7/15

Prompt Management

Quản lý và tối ưu prompts trong no-code applications

📝 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/modify
3- Không A/B test được
4- Duplicate code
5- Khó maintain nhiều AI features

Prompt Storage Strategies

Strategy 1: Database Storage

Text
1Data Type: Prompt
2- 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_summarize
3- prompt_translate
4- prompt_chat_system
5- prompt_content_gen

Strategy 3: Backend Workflows

Text
1Store prompts in Bubble's Backend:
2- Scheduled API calls
3- Reusable API workflows
4- Centralized management

Building 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 Selection
65. Temperature slider
76. Max Tokens
87. Test Panel

Variable 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, ad
8- {topic}: user input
9- {tone}: professional, casual
10- {audience}: dropdown
11- {length}: number input

Dynamic Replacement

Text
1In Bubble:
2
31. Get prompt template from database
42. Use "Find & Replace" plugin
5 - Find: {topic}
6 - Replace with: Input Topic's value
73. Repeat for each variable
84. Send to API

Prompt Versioning

Version Control

Text
1Each prompt change:
21. Keep old version (mark inactive)
32. Create new version
43. Track performance per version
5
6Data structure:
7Prompt:
8- name
9- version (number)
10- is_active (yes/no)
11- content
12- created_date
13- performance_score

A/B Testing

Text
1Setup:
2- PromptA (50% traffic)
3- PromptB (50% traffic)
4
5Random selection:
61. Generate random number
72. If < 0.5 → PromptA
83. Else → PromptB
94. Log which was used
105. Track results

Context 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_content
4Parameters:
5- prompt_name (text)
6- variables (text/JSON)
7- user_id (User)
8
9Steps:
101. Get Prompt where name = prompt_name
112. Replace variables in template
123. Call OpenAI API
134. Log usage
145. Return response

Calling from Frontend

Text
1Workflow:
21. Button clicked
32. Schedule API workflow: generate_content
4 - prompt_name: "blog_intro"
5 - variables: JSON with user inputs
63. Wait for response
74. Display result

Security Considerations

Prompt Security
Text
1Protect prompts:
2- Don't expose full system prompts
3- Validate user inputs
4- Filter output
5
6Prevent injection:
7- Sanitize user input
8- Limit input length
9- Use structured data when possible

Performance Optimization

Caching

Text
1Cache strategy:
21. Hash input (prompt + variables)
32. Check cache database
43. If hit → return cached response
54. If miss → call API, save to cache
6
7Cache Data Type:
8- input_hash
9- response
10- created_date
11- hit_count

Rate Limiting

Text
1Per-user limits:
2- Free: 10 requests/day
3- Pro: 100 requests/day
4- Enterprise: Unlimited
5
6Track in database:
7- user_id
8- request_count
9- last_reset_date

Debugging Prompts

Logging

Text
1Log each AI call:
2
3AILog:
4- prompt_used
5- user_input
6- full_request
7- response
8- response_time
9- tokens_used
10- error (if any)

Testing Panel

Text
1Build admin test interface:
21. Select prompt
32. Enter test variables
43. Run test
54. View full request/response
65. Adjust and iterate

Bài Tập

Practice

Xây dựng Prompt Manager:

  1. Create Prompt data type
  2. Build admin editor page
  3. Implement variable replacement
  4. Add prompt versioning
  5. Create usage logging
  6. Test with real AI calls

Tiếp theo: Bài 8 - Handling AI Responses