-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
498 lines (420 loc) · 15.4 KB
/
Copy pathexample.py
File metadata and controls
498 lines (420 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python3
"""
Comprehensive examples demonstrating all features of prompt-assemble.
This script shows how to use:
- Variable Sets API (create, update, list, delete)
- Owner-based prompt lookup
- Partial name matching
- Enhanced render method with search and filtering
- Scoped variable sets
- Empty render fallback content
"""
from prompt_assemble import PromptProvider
from prompt_assemble.sources import FileSystemSource, DatabaseSource
import psycopg2
# ============================================================================
# SETUP: Initialize with FileSystemSource or DatabaseSource
# ============================================================================
def setup_filesystem_source():
"""Initialize with filesystem-based prompts."""
source = FileSystemSource("./prompts")
provider = PromptProvider(source)
return provider
def setup_database_source():
"""Initialize with PostgreSQL database."""
conn = psycopg2.connect(
host="localhost",
database="prompts",
user="postgres",
password="secret"
)
source = DatabaseSource(conn, table_prefix="demo_")
provider = PromptProvider(source)
return provider
# ============================================================================
# FEATURE 1: Variable Sets API
# ============================================================================
def example_variable_sets(provider):
"""Demonstrate variable set management."""
print("\n=== VARIABLE SETS API ===\n")
# Create a global variable set
print("1. Create global variable set (visible to all)")
global_set_id = provider.create_variable_set(
name="api_keys",
variables={
"PROD_API_KEY": "sk-prod-12345",
"DEV_API_KEY": "sk-dev-67890",
"TIMEOUT": "30"
}
)
print(f" Created: {global_set_id}")
# Create owner-scoped variable sets
print("\n2. Create owner-scoped variable sets")
john_set_id = provider.create_variable_set(
name="john_config",
variables={
"ENVIRONMENT": "production",
"LOG_LEVEL": "info"
},
owner="john"
)
print(f" John's set: {john_set_id}")
alice_set_id = provider.create_variable_set(
name="alice_config",
variables={
"ENVIRONMENT": "staging",
"LOG_LEVEL": "debug"
},
owner="alice"
)
print(f" Alice's set: {alice_set_id}")
# List all variable sets
print("\n3. List all variable sets")
all_sets = provider.list_variable_sets()
for vs in all_sets:
owner_info = f" (owner: {vs['owner']})" if vs.get('owner') else " (global)"
print(f" - {vs['name']}{owner_info}: {list(vs['variables'].keys())}")
# List global variable sets only
print("\n4. List global (unscoped) variable sets")
global_sets = provider.list_global_variable_sets()
for vs in global_sets:
print(f" - {vs['name']}: {list(vs['variables'].keys())}")
# List variable sets by owner
print("\n5. List variable sets for specific owner")
john_sets = provider.list_variable_sets_by_owner("john")
print(f" John's variable sets:")
for vs in john_sets:
print(f" - {vs['name']}: {list(vs['variables'].keys())}")
# Get available variable sets for an owner (global + owner-scoped)
print("\n6. Get available variable sets for owner")
available = provider.get_available_variable_sets(owner="john")
print(f" Available for John: {[vs['name'] for vs in available]}")
# Update a variable set
print("\n7. Update variable set")
provider.update_variable_set(
john_set_id,
variables={
"ENVIRONMENT": "production",
"LOG_LEVEL": "warning",
"NEW_VAR": "new_value"
}
)
print(f" Updated John's config")
# Get specific variable set
print("\n8. Get specific variable set")
vs = provider.get_variable_set(john_set_id)
print(f" John's config variables: {vs['variables']}")
# Delete a variable set
print("\n9. Delete variable set")
provider.delete_variable_set(alice_set_id)
print(f" Deleted Alice's config")
return global_set_id, john_set_id
# ============================================================================
# FEATURE 2: Owner-Based Prompt Lookup
# ============================================================================
def example_owner_lookup(provider):
"""Demonstrate finding prompts by owner."""
print("\n=== OWNER-BASED LOOKUP ===\n")
# Find prompts by owner
print("1. Find all prompts by owner")
john_prompts = provider.find_by_owner("john")
print(f" Prompts owned by John: {john_prompts}")
alice_prompts = provider.find_by_owner("alice")
print(f" Prompts owned by Alice: {alice_prompts}")
# Render prompt by exact match with owner filter
print("\n2. Render prompt with owner filter")
if john_prompts:
result = provider.render(
john_prompts[0],
owner="john",
variables={},
)
print(f" Rendered: {result[:100]}...")
# ============================================================================
# FEATURE 3: Partial Name Matching
# ============================================================================
def example_partial_name_matching(provider):
"""Demonstrate finding prompts by partial name."""
print("\n=== PARTIAL NAME MATCHING ===\n")
# Find prompts by partial name
print("1. Find prompts by partial name (case-insensitive)")
system_prompts = provider.find_by_name("system")
print(f" Prompts matching 'system': {system_prompts}")
config_prompts = provider.find_by_name("config")
print(f" Prompts matching 'config': {config_prompts}")
# Find with wildcard-like pattern
print("\n2. Find with specific pattern")
api_prompts = provider.find_by_name("api")
print(f" Prompts matching 'api': {api_prompts}")
# ============================================================================
# FEATURE 4: Enhanced Render with Search and Filtering
# ============================================================================
def example_enhanced_render(provider):
"""Demonstrate enhanced render method with search."""
print("\n=== ENHANCED RENDER METHOD ===\n")
# Exact match (original behavior)
print("1. Exact match render")
try:
result = provider.render(
"my_prompt",
variables={"name": "World"}
)
print(f" Result: {result[:100]}...")
except Exception as e:
print(f" Note: {e}")
# Partial name match
print("\n2. Partial name match render")
try:
result = provider.render(
"system",
match_type="partial",
variables={"role": "assistant"}
)
print(f" Result: {result[:100]}...")
except Exception as e:
print(f" Note: {e}")
# Render by owner
print("\n3. Render by owner")
try:
result = provider.render(
"prompt",
owner="john",
variables={}
)
print(f" Result: {result[:100]}...")
except Exception as e:
print(f" Note: {e}")
# Render by tags
print("\n4. Render by tags")
try:
result = provider.render(
"prompt",
tags=["prod", "critical"],
variables={}
)
print(f" Result: {result[:100]}...")
except Exception as e:
print(f" Note: {e}")
# Combined search: partial match + owner + tags
print("\n5. Combined search (partial + owner + tags)")
try:
result = provider.render(
"system",
match_type="partial",
owner="john",
tags=["prod"],
variables={"model": "gpt-4"}
)
print(f" Result: {result[:100]}...")
except Exception as e:
print(f" Note: {e}")
# ============================================================================
# FEATURE 5: Empty Render Fallback
# ============================================================================
def example_empty_render(provider):
"""Demonstrate empty_render fallback parameter."""
print("\n=== EMPTY RENDER FALLBACK ===\n")
# Default behavior (returns empty string)
print("1. Default behavior - empty renders as empty string")
try:
result = provider.render(
"nonexistent",
match_type="partial",
variables={},
empty_render=""
)
print(f" Result is empty: {result == ''}")
except Exception as e:
print(f" Note: {e}")
# With custom fallback
print("\n2. With custom fallback message")
try:
result = provider.render(
"nonexistent",
match_type="partial",
variables={},
empty_render="[No prompt content available]"
)
print(f" Result: {result}")
except Exception as e:
print(f" Note: {e}")
# Multi-line fallback
print("\n3. Multi-line fallback")
try:
result = provider.render(
"missing",
match_type="partial",
variables={},
empty_render="""---
No instructions provided.
Please add content to this prompt.
---"""
)
print(f" Result:\n{result}")
except Exception as e:
print(f" Note: {e}")
# ============================================================================
# FEATURE 6: Scoped Variable Sets with Render
# ============================================================================
def example_scoped_variable_sets(provider):
"""Demonstrate scoped variable sets in action."""
print("\n=== SCOPED VARIABLE SETS ===\n")
# Create global and scoped sets
print("1. Setup global and owner-scoped variable sets")
global_set = provider.create_variable_set(
"shared_vars",
{
"COMPANY": "Acme Corp",
"VERSION": "1.0.0"
}
)
print(f" Global set: {global_set}")
dev_set = provider.create_variable_set(
"dev_vars",
{
"ENV": "development",
"DEBUG": "true"
},
owner="developer"
)
print(f" Developer set: {dev_set}")
# Get available sets for developer
print("\n2. Get available sets for 'developer' owner")
available = provider.get_available_variable_sets(owner="developer")
print(f" Available sets: {[s['name'] for s in available]}")
print(f" Includes: global + owner-scoped")
# Link variable sets to a prompt
print("\n3. Link variable sets to a prompt")
try:
provider.set_active_variable_sets(
"my_prompt",
[global_set, dev_set]
)
print(f" Linked variable sets to 'my_prompt'")
# Get active variable sets for a prompt
active = provider.get_active_variable_sets("my_prompt")
print(f" Active variable sets: {[s['id'] for s in active]}")
except Exception as e:
print(f" Note: {e}")
# Set variable overrides for specific prompt
print("\n4. Set variable overrides for a prompt")
try:
provider.set_variable_overrides(
"my_prompt",
dev_set,
{
"DEBUG": "false", # Override global setting
"LOG_LEVEL": "error"
}
)
print(f" Set overrides for 'my_prompt'")
# Get variable overrides
overrides = provider.get_variable_overrides("my_prompt", dev_set)
print(f" Overrides: {overrides}")
except Exception as e:
print(f" Note: {e}")
# ============================================================================
# ADVANCED EXAMPLE: Complete Workflow
# ============================================================================
def example_complete_workflow(provider):
"""Demonstrate a complete real-world workflow."""
print("\n=== COMPLETE WORKFLOW ===\n")
print("Scenario: Multi-team prompt management with scoped variables")
print("-" * 60)
# Setup teams and their prompts
print("\n1. Setup team-specific variable sets")
# Global variables shared by all teams
shared_vars = provider.create_variable_set(
"company_standards",
{
"COMPANY": "TechCorp",
"MODEL": "gpt-4",
"MAX_TOKENS": "2000"
}
)
print(f" Created global: company_standards")
# Backend team variables
backend_vars = provider.create_variable_set(
"backend_config",
{
"ENVIRONMENT": "production",
"DB_POOL_SIZE": "20",
"CACHE_TTL": "3600"
},
owner="backend_team"
)
print(f" Created backend team: backend_config")
# Frontend team variables
frontend_vars = provider.create_variable_set(
"frontend_config",
{
"ENVIRONMENT": "production",
"API_TIMEOUT": "30000",
"RETRY_ATTEMPTS": "3"
},
owner="frontend_team"
)
print(f" Created frontend team: frontend_config")
# Render prompt for backend team
print("\n2. Render team-specific prompts")
print(" Backend team available variables:")
backend_available = provider.get_available_variable_sets(owner="backend_team")
for vs in backend_available:
print(f" - {vs['name']}: {list(vs['variables'].keys())}")
print(" Frontend team available variables:")
frontend_available = provider.get_available_variable_sets(owner="frontend_team")
for vs in frontend_available:
print(f" - {vs['name']}: {list(vs['variables'].keys())}")
# Render with different owners
print("\n3. Render same prompt with different team contexts")
try:
backend_render = provider.render(
"system",
match_type="partial",
owner="backend_team",
variables={},
empty_render="[Backend system prompt]"
)
print(f" Backend: {backend_render[:50]}...")
except Exception as e:
print(f" Note: {e}")
try:
frontend_render = provider.render(
"system",
match_type="partial",
owner="frontend_team",
variables={},
empty_render="[Frontend system prompt]"
)
print(f" Frontend: {frontend_render[:50]}...")
except Exception as e:
print(f" Note: {e}")
# ============================================================================
# MAIN: Run all examples
# ============================================================================
def main():
"""Run all example demonstrations."""
print("=" * 70)
print("PROMPT-ASSEMBLE COMPREHENSIVE EXAMPLES")
print("=" * 70)
try:
# Use filesystem source (no database required)
provider = setup_filesystem_source()
print("\nInitialized with FileSystemSource")
except Exception as e:
print(f"\nNote: Could not initialize FileSystemSource: {e}")
print("To run database examples, ensure PostgreSQL is running and configured.")
return
# Run all examples
example_variable_sets(provider)
example_owner_lookup(provider)
example_partial_name_matching(provider)
example_enhanced_render(provider)
example_empty_render(provider)
example_scoped_variable_sets(provider)
example_complete_workflow(provider)
print("\n" + "=" * 70)
print("EXAMPLES COMPLETE")
print("=" * 70)
if __name__ == "__main__":
main()