-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdxtag_manager.py
More file actions
381 lines (308 loc) · 12.4 KB
/
dxtag_manager.py
File metadata and controls
381 lines (308 loc) · 12.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
"""
DxTag Pattern Manager
Implements the DxTag architectural pattern for prompt engineering:
- Data structures
- eXecution logic
- Tag/presentation formatting
Includes semantic versioning and dependency management
"""
from typing import Dict, List, Any, Optional, Tuple
from dataclasses import dataclass, field
from datetime import datetime
import json
import hashlib
from enum import Enum
class VersionType(Enum):
"""Semantic version types"""
PATCH = "patch" # Minor wording adjustments
MINOR = "minor" # Structural modifications
MAJOR = "major" # Fundamental redesigns
@dataclass
class PromptVersion:
"""Represents a version of a prompt"""
version: str
prompt_data: Dict[str, Any]
execution_logic: Dict[str, Any]
tags: Dict[str, Any]
timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
change_log: str = ""
parent_version: Optional[str] = None
@dataclass
class PromptComponent:
"""
A component of a prompt following DxTag pattern
"""
# Data: The actual content
data: Dict[str, Any]
# eXecution: Logic and instructions
execution: Dict[str, Any]
# Tags: Metadata and formatting
tags: Dict[str, str]
# Version info
version: str = "1.0.0"
component_id: str = field(default_factory=lambda: hashlib.md5(str(datetime.now()).encode()).hexdigest()[:8])
class DxTagManager:
"""
Manages prompts using the DxTag pattern with version control
"""
def __init__(self):
self.versions: Dict[str, PromptVersion] = {}
self.current_version: Optional[str] = None
self.dependencies: Dict[str, List[str]] = {} # prompt_id -> [dependent_ids]
def create_prompt(
self,
role: str,
task: str,
context: Dict[str, Any],
constraints: List[str],
output_format: Dict[str, Any],
examples: Optional[List[Dict]] = None
) -> PromptComponent:
"""
Creates a structured prompt using DxTag pattern
Args:
role: The role/persona for the AI
task: The main task description
context: Background context and information
constraints: List of constraints and requirements
output_format: Specification of the desired output format
examples: Optional few-shot examples
Returns:
PromptComponent with structured data
"""
# Data structure
data = {
"role": role,
"task": task,
"context": context,
"examples": examples or []
}
# Execution logic
execution = {
"constraints": constraints,
"output_format": output_format,
"processing_instructions": [
"Read and understand the role and context",
"Analyze the task requirements",
"Apply constraints during processing",
"Format output according to specifications"
]
}
# Tags and metadata
tags = {
"created": datetime.now().isoformat(),
"purpose": "prompt_engineering",
"format_version": "dxtag-1.0",
"complexity": self._assess_complexity(task, constraints)
}
component = PromptComponent(
data=data,
execution=execution,
tags=tags
)
# Store as version 1.0.0
self._store_version(component, "1.0.0", "Initial prompt creation")
return component
def refine_prompt(
self,
component: PromptComponent,
refinements: Dict[str, Any],
version_type: VersionType = VersionType.MINOR
) -> PromptComponent:
"""
Refines an existing prompt and creates a new version
Args:
component: The current prompt component
refinements: Dictionary of refinements to apply
version_type: Type of version increment (PATCH, MINOR, MAJOR)
Returns:
New PromptComponent with refinements applied
"""
# Create a copy
new_data = component.data.copy()
new_execution = component.execution.copy()
new_tags = component.tags.copy()
# Apply refinements
if "data" in refinements:
new_data.update(refinements["data"])
if "execution" in refinements:
new_execution.update(refinements["execution"])
if "tags" in refinements:
new_tags.update(refinements["tags"])
# Create new component
refined_component = PromptComponent(
data=new_data,
execution=new_execution,
tags=new_tags,
version=self._increment_version(component.version, version_type),
component_id=component.component_id
)
# Store new version
change_log = self._generate_change_log(component, refined_component, refinements)
self._store_version(refined_component, refined_component.version, change_log)
return refined_component
def to_prompt_string(self, component: PromptComponent, style: str = "structured") -> str:
"""
Converts a PromptComponent to a formatted prompt string
Args:
component: The prompt component to convert
style: Output style ('structured', 'minimal', 'conversational')
Returns:
Formatted prompt string
"""
if style == "structured":
return self._format_structured(component)
elif style == "minimal":
return self._format_minimal(component)
elif style == "conversational":
return self._format_conversational(component)
else:
return self._format_structured(component)
def _format_structured(self, component: PromptComponent) -> str:
"""Formats prompt in structured style"""
sections = []
# Role section
sections.append(f"# ROLE\n{component.data['role']}\n")
# Context section
if component.data.get('context'):
sections.append("# CONTEXT")
for key, value in component.data['context'].items():
sections.append(f"- {key}: {value}")
sections.append("")
# Task section
sections.append(f"# TASK\n{component.data['task']}\n")
# Constraints section
if component.execution.get('constraints'):
sections.append("# CONSTRAINTS")
for i, constraint in enumerate(component.execution['constraints'], 1):
sections.append(f"{i}. {constraint}")
sections.append("")
# Output format section
sections.append("# OUTPUT FORMAT")
sections.append(json.dumps(component.execution['output_format'], indent=2))
sections.append("")
# Examples section
if component.data.get('examples') and component.data['examples']:
sections.append("# EXAMPLES")
for i, example in enumerate(component.data['examples'], 1):
sections.append(f"\n## Example {i}")
sections.append(f"Input: {example.get('input', 'N/A')}")
sections.append(f"Output: {example.get('output', 'N/A')}")
sections.append("")
return "\n".join(sections)
def _format_minimal(self, component: PromptComponent) -> str:
"""Formats prompt in minimal style"""
parts = []
if component.data.get('role'):
parts.append(f"You are {component.data['role']}.")
parts.append(component.data['task'])
if component.execution.get('constraints'):
parts.append(f"Requirements: {'; '.join(component.execution['constraints'])}")
return " ".join(parts)
def _format_conversational(self, component: PromptComponent) -> str:
"""Formats prompt in conversational style"""
parts = []
# Friendly introduction
if component.data.get('role'):
parts.append(f"I'd like you to act as {component.data['role']}.")
# Context as narrative
if component.data.get('context'):
context_str = ", ".join([f"{k}: {v}" for k, v in component.data['context'].items()])
parts.append(f"Here's some background information: {context_str}.")
# Task description
parts.append(f"Your task is to {component.data['task']}.")
# Constraints as requests
if component.execution.get('constraints'):
parts.append("Please make sure to:")
for constraint in component.execution['constraints']:
parts.append(f"- {constraint}")
# Output format
parts.append(f"\nPlease provide your response in the following format: {json.dumps(component.execution['output_format'])}")
return "\n".join(parts)
def _assess_complexity(self, task: str, constraints: List[str]) -> str:
"""Assesses prompt complexity"""
score = 0
# Task length
score += len(task.split()) // 20
# Number of constraints
score += len(constraints)
# Complexity keywords
complex_keywords = ['analyze', 'compare', 'evaluate', 'synthesize', 'optimize']
score += sum(1 for keyword in complex_keywords if keyword in task.lower())
if score <= 3:
return "low"
elif score <= 7:
return "medium"
else:
return "high"
def _increment_version(self, current: str, version_type: VersionType) -> str:
"""Increments version number based on type"""
major, minor, patch = map(int, current.split('.'))
if version_type == VersionType.MAJOR:
return f"{major + 1}.0.0"
elif version_type == VersionType.MINOR:
return f"{major}.{minor + 1}.0"
else: # PATCH
return f"{major}.{minor}.{patch + 1}"
def _store_version(self, component: PromptComponent, version: str, change_log: str):
"""Stores a version in the version history"""
prompt_version = PromptVersion(
version=version,
prompt_data=component.data,
execution_logic=component.execution,
tags=component.tags,
change_log=change_log,
parent_version=self.current_version
)
version_key = f"{component.component_id}_{version}"
self.versions[version_key] = prompt_version
self.current_version = version_key
def _generate_change_log(
self,
old: PromptComponent,
new: PromptComponent,
refinements: Dict[str, Any]
) -> str:
"""Generates a change log describing the differences"""
changes = []
if old.data != new.data:
changes.append("Data modifications")
if old.execution != new.execution:
changes.append("Execution logic updated")
if refinements.get("reason"):
changes.append(f"Reason: {refinements['reason']}")
return "; ".join(changes) if changes else "Minor refinements"
def get_version_history(self, component_id: str) -> List[PromptVersion]:
"""Gets version history for a component"""
return [
version for key, version in self.versions.items()
if key.startswith(component_id)
]
def add_dependency(self, prompt_id: str, depends_on: str):
"""Adds a dependency relationship between prompts"""
if prompt_id not in self.dependencies:
self.dependencies[prompt_id] = []
self.dependencies[prompt_id].append(depends_on)
def check_compatibility(self, prompt1_id: str, prompt2_id: str) -> bool:
"""Checks if two prompts are compatible for chaining"""
# Simple version compatibility check
# In production, this would check output/input format compatibility
return True
def to_dict(self, component: PromptComponent) -> Dict[str, Any]:
"""Converts component to dictionary"""
return {
"data": component.data,
"execution": component.execution,
"tags": component.tags,
"version": component.version,
"component_id": component.component_id
}
def from_dict(self, data: Dict[str, Any]) -> PromptComponent:
"""Creates component from dictionary"""
return PromptComponent(
data=data["data"],
execution=data["execution"],
tags=data["tags"],
version=data.get("version", "1.0.0"),
component_id=data.get("component_id", "")
)