@memberjunction/ai-prompts 2.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +639 -0
- package/dist/AIPromptCategoryExtended.d.ts +6 -0
- package/dist/AIPromptCategoryExtended.d.ts.map +1 -0
- package/dist/AIPromptCategoryExtended.js +26 -0
- package/dist/AIPromptCategoryExtended.js.map +1 -0
- package/dist/AIPromptRunner.d.ts +41 -0
- package/dist/AIPromptRunner.d.ts.map +1 -0
- package/dist/AIPromptRunner.js +414 -0
- package/dist/AIPromptRunner.js.map +1 -0
- package/dist/ExecutionPlanner.d.ts +16 -0
- package/dist/ExecutionPlanner.d.ts.map +1 -0
- package/dist/ExecutionPlanner.js +232 -0
- package/dist/ExecutionPlanner.js.map +1 -0
- package/dist/ParallelExecution.d.ts +63 -0
- package/dist/ParallelExecution.d.ts.map +1 -0
- package/dist/ParallelExecution.js +3 -0
- package/dist/ParallelExecution.js.map +1 -0
- package/dist/ParallelExecutionCoordinator.d.ts +19 -0
- package/dist/ParallelExecutionCoordinator.d.ts.map +1 -0
- package/dist/ParallelExecutionCoordinator.js +262 -0
- package/dist/ParallelExecutionCoordinator.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
# @memberjunction/ai-prompts
|
|
2
|
+
|
|
3
|
+
The MemberJunction AI Prompts package provides sophisticated prompt management, execution, and optimization capabilities within the MemberJunction ecosystem. This package handles advanced prompt features including template rendering, parallel execution, intelligent caching, and result selection strategies.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@memberjunction/ai-prompts)
|
|
6
|
+
[](https://opensource.org/licenses/ISC)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **📝 Advanced Prompt System**: Sophisticated prompt management with template rendering and validation
|
|
11
|
+
- **⚡ Parallel Processing**: Multi-model execution with result selection strategies
|
|
12
|
+
- **💾 Intelligent Caching**: Vector similarity matching and TTL-based result caching
|
|
13
|
+
- **🔄 Template Integration**: Dynamic prompt generation with MemberJunction template system
|
|
14
|
+
- **📊 Execution Analytics**: Comprehensive metrics, token usage tracking, and performance monitoring
|
|
15
|
+
- **🎯 Result Selection**: AI-powered selection of best results from parallel executions
|
|
16
|
+
- **🔧 Output Validation**: Structured output validation with retry logic
|
|
17
|
+
- **⚙️ Configuration-Driven**: Metadata-driven prompt configuration and execution
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @memberjunction/ai-prompts
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> **Note**: This package uses MemberJunction's class registration system. The package automatically registers its classes on import to ensure proper functionality within the MJ ecosystem.
|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
- Node.js 16+
|
|
30
|
+
- MemberJunction Core libraries
|
|
31
|
+
- [@memberjunction/aiengine](../Engine/README.md) for model management and basic AI operations
|
|
32
|
+
- [@memberjunction/templates](../../Templates/README.md) for template rendering
|
|
33
|
+
|
|
34
|
+
## Core Architecture
|
|
35
|
+
|
|
36
|
+
### AIPromptRunner Class
|
|
37
|
+
|
|
38
|
+
The `AIPromptRunner` class is the central component for executing prompts with advanced features:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { AIPromptRunner, AIPromptParams } from '@memberjunction/ai-prompts';
|
|
42
|
+
|
|
43
|
+
// Get a prompt from the system
|
|
44
|
+
const prompts = AIEngine.Instance.Prompts;
|
|
45
|
+
const summaryPrompt = prompts.find(p => p.Name === 'Document Summarization');
|
|
46
|
+
|
|
47
|
+
// Execute the prompt
|
|
48
|
+
const params: AIPromptParams = {
|
|
49
|
+
prompt: summaryPrompt,
|
|
50
|
+
data: {
|
|
51
|
+
documentText: "Long document content here...",
|
|
52
|
+
targetLength: "2 paragraphs"
|
|
53
|
+
},
|
|
54
|
+
contextUser: currentUser
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const runner = new AIPromptRunner();
|
|
58
|
+
const result = await runner.ExecutePrompt(params);
|
|
59
|
+
|
|
60
|
+
if (result.success) {
|
|
61
|
+
console.log("Summary:", result.result);
|
|
62
|
+
console.log(`Execution time: ${result.executionTimeMS}ms`);
|
|
63
|
+
console.log(`Tokens used: ${result.totalTokensUsed}`);
|
|
64
|
+
} else {
|
|
65
|
+
console.error("Error:", result.error);
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Quick Start
|
|
70
|
+
|
|
71
|
+
### 1. Basic Prompt Execution
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { AIPromptRunner } from '@memberjunction/ai-prompts';
|
|
75
|
+
import { AIEngine } from '@memberjunction/aiengine';
|
|
76
|
+
|
|
77
|
+
// Initialize the AI Engine
|
|
78
|
+
await AIEngine.Instance.Config(false, currentUser);
|
|
79
|
+
|
|
80
|
+
// Find a prompt
|
|
81
|
+
const prompt = AIEngine.Instance.Prompts.find(p => p.Name === 'Text Analysis');
|
|
82
|
+
|
|
83
|
+
// Execute with data
|
|
84
|
+
const runner = new AIPromptRunner();
|
|
85
|
+
const result = await runner.ExecutePrompt({
|
|
86
|
+
prompt: prompt,
|
|
87
|
+
data: {
|
|
88
|
+
text: "Analyze this sample text for sentiment and key themes.",
|
|
89
|
+
format: "bullet points"
|
|
90
|
+
},
|
|
91
|
+
contextUser: currentUser
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log("Analysis:", result.result);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 2. Template-Driven Prompts
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
// Prompt templates support dynamic data substitution
|
|
101
|
+
const templatePrompt = {
|
|
102
|
+
UserMessage: `Analyze the {{entity.EntityType}} record for {{entity.Name}}.
|
|
103
|
+
Focus on {{analysisType}} and provide insights about {{entity.Description}}.`
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// Data context provides template variables
|
|
107
|
+
const result = await runner.ExecutePrompt({
|
|
108
|
+
prompt: templatePrompt,
|
|
109
|
+
data: {
|
|
110
|
+
entity: {
|
|
111
|
+
EntityType: "Customer",
|
|
112
|
+
Name: "Acme Corp",
|
|
113
|
+
Description: "Enterprise software company"
|
|
114
|
+
},
|
|
115
|
+
analysisType: "growth opportunities"
|
|
116
|
+
},
|
|
117
|
+
contextUser: currentUser
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 3. Parallel Execution with Multiple Models
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// Execute the same prompt across multiple models in parallel
|
|
125
|
+
const multiModelPrompt = prompts.find(p => p.ParallelizationMode === 'ModelSpecific');
|
|
126
|
+
|
|
127
|
+
const result = await runner.ExecutePrompt({
|
|
128
|
+
prompt: multiModelPrompt,
|
|
129
|
+
data: { query: "Analyze this data pattern" },
|
|
130
|
+
contextUser: currentUser
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// When using parallel execution, the system automatically selects the best result
|
|
134
|
+
console.log(`Final result: ${result.result}`);
|
|
135
|
+
console.log(`Execution time: ${result.executionTimeMS}ms`);
|
|
136
|
+
console.log(`Total tokens used: ${result.tokensUsed}`);
|
|
137
|
+
|
|
138
|
+
// The promptRun entity contains metadata about parallel execution in its Messages field
|
|
139
|
+
if (result.promptRun?.Messages) {
|
|
140
|
+
const metadata = JSON.parse(result.promptRun.Messages);
|
|
141
|
+
if (metadata.parallelExecution) {
|
|
142
|
+
console.log(`Parallelization mode: ${metadata.parallelExecution.parallelizationMode}`);
|
|
143
|
+
console.log(`Total tasks: ${metadata.parallelExecution.totalTasks}`);
|
|
144
|
+
console.log(`Successful tasks: ${metadata.parallelExecution.successfulTasks}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Advanced Features
|
|
150
|
+
|
|
151
|
+
### Intelligent Caching
|
|
152
|
+
|
|
153
|
+
The prompt system provides sophisticated caching with vector similarity matching:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
// Caching is automatically handled based on prompt configuration:
|
|
157
|
+
// - EnableCaching: Whether to use caching for this prompt
|
|
158
|
+
// - CacheMatchType: 'Exact' or 'Vector' similarity matching
|
|
159
|
+
// - CacheTTLSeconds: Time-to-live for cached results
|
|
160
|
+
// - CacheMustMatchModel/Vendor/Agent: Cache constraint options
|
|
161
|
+
|
|
162
|
+
// Vector similarity allows reusing results for semantically similar prompts
|
|
163
|
+
// even if the exact text differs
|
|
164
|
+
|
|
165
|
+
const cachedPrompt = {
|
|
166
|
+
Name: "Smart Summary",
|
|
167
|
+
EnableCaching: true,
|
|
168
|
+
CacheMatchType: "Vector",
|
|
169
|
+
CacheTTLSeconds: 3600,
|
|
170
|
+
CacheSimilarityThreshold: 0.85,
|
|
171
|
+
CacheMustMatchModel: true,
|
|
172
|
+
CacheMustMatchVendor: false
|
|
173
|
+
};
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Parallel Execution Strategies
|
|
177
|
+
|
|
178
|
+
The system supports multiple parallelization modes:
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// Prompts can be configured for parallel execution:
|
|
182
|
+
// - ParallelizationMode: 'None', 'StaticCount', 'ConfigParam', 'ModelSpecific'
|
|
183
|
+
// - ParallelCount: Number of parallel executions
|
|
184
|
+
// - ExecutionGroups: Sequential group execution with parallel tasks within groups
|
|
185
|
+
|
|
186
|
+
// Example configurations:
|
|
187
|
+
|
|
188
|
+
// Static parallel count
|
|
189
|
+
const staticParallelPrompt = {
|
|
190
|
+
ParallelizationMode: "StaticCount",
|
|
191
|
+
ParallelCount: 3
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// Configuration-driven count
|
|
195
|
+
const configParallelPrompt = {
|
|
196
|
+
ParallelizationMode: "ConfigParam",
|
|
197
|
+
ParallelConfigParam: "analysis_parallel_count"
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// Model-specific configuration
|
|
201
|
+
const modelSpecificPrompt = {
|
|
202
|
+
ParallelizationMode: "ModelSpecific",
|
|
203
|
+
// Uses settings from AIPromptModel entries
|
|
204
|
+
};
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Result Selection Strategies
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
// The engine supports multiple result selection methods:
|
|
211
|
+
// - 'First': Use the first successful result
|
|
212
|
+
// - 'Random': Randomly select from successful results
|
|
213
|
+
// - 'PromptSelector': Use AI to select the best result
|
|
214
|
+
// - 'Consensus': Select result with highest agreement
|
|
215
|
+
|
|
216
|
+
// Result selector prompts can be configured to intelligently choose
|
|
217
|
+
// the best result from parallel executions
|
|
218
|
+
|
|
219
|
+
const selectorPrompt = {
|
|
220
|
+
Name: "Best Result Selector",
|
|
221
|
+
PromptText: `
|
|
222
|
+
You are evaluating multiple AI responses to select the best one.
|
|
223
|
+
Original query: {{originalQuery}}
|
|
224
|
+
|
|
225
|
+
Responses:
|
|
226
|
+
{{#each responses}}
|
|
227
|
+
Response {{@index}}: {{this}}
|
|
228
|
+
{{/each}}
|
|
229
|
+
|
|
230
|
+
Select the response number (0-based) that is most accurate, helpful, and well-written.
|
|
231
|
+
Return only the number.
|
|
232
|
+
`,
|
|
233
|
+
OutputType: "number"
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const mainPrompt = {
|
|
237
|
+
ParallelizationMode: "StaticCount",
|
|
238
|
+
ParallelCount: 3,
|
|
239
|
+
ResultSelectorPromptID: selectorPrompt.ID
|
|
240
|
+
};
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Output Validation
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
// Configure structured output validation
|
|
247
|
+
const validatedPrompt = {
|
|
248
|
+
Name: "Structured Analysis",
|
|
249
|
+
OutputType: "object",
|
|
250
|
+
OutputExample: {
|
|
251
|
+
sentiment: "positive|negative|neutral",
|
|
252
|
+
confidence: 0.95,
|
|
253
|
+
keyThemes: ["theme1", "theme2"],
|
|
254
|
+
summary: "Brief summary text"
|
|
255
|
+
},
|
|
256
|
+
ValidationBehavior: "Strict",
|
|
257
|
+
MaxRetries: 3,
|
|
258
|
+
RetryDelayMS: 1000,
|
|
259
|
+
RetryStrategy: "exponential"
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// Validation is automatically applied
|
|
263
|
+
const result = await runner.ExecutePrompt({
|
|
264
|
+
prompt: validatedPrompt,
|
|
265
|
+
data: { text: "Content to analyze" },
|
|
266
|
+
contextUser: currentUser,
|
|
267
|
+
skipValidation: false // Validation enabled
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Result.result will be validated against the expected structure
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Template Integration
|
|
274
|
+
|
|
275
|
+
Advanced template features with the MemberJunction template system:
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
// Complex template with conditionals and loops
|
|
279
|
+
const advancedTemplate = {
|
|
280
|
+
PromptText: `
|
|
281
|
+
Analyze the following {{entityType}} records:
|
|
282
|
+
|
|
283
|
+
{{#each records}}
|
|
284
|
+
{{@index + 1}}. {{this.Name}}
|
|
285
|
+
Status: {{this.Status}}
|
|
286
|
+
{{#if this.Priority}}Priority: {{this.Priority}}{{/if}}
|
|
287
|
+
{{#each this.Tags}}
|
|
288
|
+
- Tag: {{this}}
|
|
289
|
+
{{/each}}
|
|
290
|
+
{{/each}}
|
|
291
|
+
|
|
292
|
+
{{#if includeRecommendations}}
|
|
293
|
+
Please provide recommendations for improvement.
|
|
294
|
+
{{/if}}
|
|
295
|
+
|
|
296
|
+
Focus on: {{analysisAreas.join(", ")}}
|
|
297
|
+
`
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const result = await runner.ExecutePrompt({
|
|
301
|
+
prompt: advancedTemplate,
|
|
302
|
+
data: {
|
|
303
|
+
entityType: "Customer",
|
|
304
|
+
records: customerData,
|
|
305
|
+
includeRecommendations: true,
|
|
306
|
+
analysisAreas: ["revenue potential", "risk factors", "engagement"]
|
|
307
|
+
},
|
|
308
|
+
contextUser: currentUser
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Parallel Execution System
|
|
313
|
+
|
|
314
|
+
The package includes sophisticated parallel execution capabilities through specialized classes that work together to manage complex multi-model executions.
|
|
315
|
+
|
|
316
|
+
> **Note**: The ExecutionPlanner and ParallelExecutionCoordinator are internal components used by AIPromptRunner. They are not directly exposed in the public API but understanding their operation helps in configuring prompts effectively.
|
|
317
|
+
|
|
318
|
+
### ExecutionPlanner (Internal)
|
|
319
|
+
|
|
320
|
+
The `ExecutionPlanner` class analyzes prompt configuration and creates optimal execution strategies:
|
|
321
|
+
|
|
322
|
+
**Key Responsibilities:**
|
|
323
|
+
- Analyzes parallelization modes (None, StaticCount, ConfigParam, ModelSpecific)
|
|
324
|
+
- Creates execution groups for coordinated processing
|
|
325
|
+
- Determines optimal task distribution based on model availability
|
|
326
|
+
- Assigns priorities and manages execution order
|
|
327
|
+
- Handles model selection based on power rankings and configuration
|
|
328
|
+
|
|
329
|
+
**Execution Plan Creation:**
|
|
330
|
+
- For `StaticCount`: Creates N parallel tasks using available models
|
|
331
|
+
- For `ConfigParam`: Uses configuration parameters to determine parallel count
|
|
332
|
+
- For `ModelSpecific`: Uses AIPromptModel entries to define exact model usage
|
|
333
|
+
- Supports execution groups for sequential/parallel hybrid execution
|
|
334
|
+
|
|
335
|
+
### ParallelExecutionCoordinator (Internal)
|
|
336
|
+
|
|
337
|
+
The `ParallelExecutionCoordinator` orchestrates the actual execution of tasks created by the ExecutionPlanner:
|
|
338
|
+
|
|
339
|
+
**Core Features:**
|
|
340
|
+
- Manages concurrency limits (default: 5 concurrent executions)
|
|
341
|
+
- Implements retry logic with exponential backoff
|
|
342
|
+
- Handles partial result collection when some tasks fail
|
|
343
|
+
- Provides comprehensive execution metrics and timing
|
|
344
|
+
- Supports fail-fast mode for critical operations
|
|
345
|
+
|
|
346
|
+
**Execution Flow:**
|
|
347
|
+
1. Groups tasks by execution group number
|
|
348
|
+
2. Executes groups sequentially (group 0, then 1, then 2, etc.)
|
|
349
|
+
3. Within each group, executes tasks in parallel up to concurrency limit
|
|
350
|
+
4. Collects and aggregates results from all executions
|
|
351
|
+
5. Applies result selection strategy if multiple results available
|
|
352
|
+
|
|
353
|
+
### Supported Parallelization Modes
|
|
354
|
+
|
|
355
|
+
- **None**: Traditional single execution
|
|
356
|
+
- **StaticCount**: Fixed number of parallel executions
|
|
357
|
+
- **ConfigParam**: Dynamic parallel count from configuration
|
|
358
|
+
- **ModelSpecific**: Individual model configurations with execution groups
|
|
359
|
+
|
|
360
|
+
```typescript
|
|
361
|
+
// Example of model-specific parallel configuration
|
|
362
|
+
const modelSpecificExecution = {
|
|
363
|
+
prompt: complexPrompt,
|
|
364
|
+
data: analysisData,
|
|
365
|
+
contextUser: currentUser
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// The system will:
|
|
369
|
+
// 1. Query AIPromptModel entries for this prompt
|
|
370
|
+
// 2. Group executions by ExecutionGroup
|
|
371
|
+
// 3. Execute groups sequentially, models within groups in parallel
|
|
372
|
+
// 4. Apply result selection strategy
|
|
373
|
+
const result = await runner.ExecutePrompt(modelSpecificExecution);
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
## Performance Monitoring & Analytics
|
|
377
|
+
|
|
378
|
+
Comprehensive tracking and analytics for prompt executions:
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
// Execution results include detailed metrics
|
|
382
|
+
const result = await runner.ExecutePrompt(params);
|
|
383
|
+
|
|
384
|
+
console.log(`Execution time: ${result.executionTimeMS}ms`);
|
|
385
|
+
console.log(`Tokens used: ${result.tokensUsed}`);
|
|
386
|
+
|
|
387
|
+
// The AIPromptRunResult includes execution tracking
|
|
388
|
+
if (result.promptRun) {
|
|
389
|
+
console.log(`Prompt Run ID: ${result.promptRun.ID}`);
|
|
390
|
+
console.log(`Model used: ${result.promptRun.ModelID}`);
|
|
391
|
+
console.log(`Configuration: ${result.promptRun.ConfigurationID}`);
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## API Reference
|
|
396
|
+
|
|
397
|
+
### Exported Classes and Types
|
|
398
|
+
|
|
399
|
+
The package exports the following public API:
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
export { AIPromptCategoryEntityExtended } from './AIPromptCategoryExtended';
|
|
403
|
+
export { AIPromptRunner, AIPromptParams, AIPromptRunResult } from './AIPromptRunner';
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### AIPromptRunner Class
|
|
407
|
+
|
|
408
|
+
Handles execution of AI prompts with advanced parallel processing, template rendering, and result validation.
|
|
409
|
+
|
|
410
|
+
#### Methods
|
|
411
|
+
|
|
412
|
+
- `ExecutePrompt(params: AIPromptParams): Promise<AIPromptRunResult>`: Execute a prompt with full feature support including template rendering, model selection, parallel execution, and output validation
|
|
413
|
+
|
|
414
|
+
#### AIPromptParams Interface
|
|
415
|
+
|
|
416
|
+
```typescript
|
|
417
|
+
interface AIPromptParams {
|
|
418
|
+
prompt: AIPromptEntity; // The prompt to execute
|
|
419
|
+
data?: any; // Template and context data
|
|
420
|
+
modelId?: string; // Override model selection
|
|
421
|
+
vendorId?: string; // Override vendor selection
|
|
422
|
+
configurationId?: string; // Environment-specific config
|
|
423
|
+
contextUser?: UserInfo; // User context
|
|
424
|
+
skipValidation?: boolean; // Skip output validation
|
|
425
|
+
templateData?: any; // Additional template data that augments the main data context
|
|
426
|
+
}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### Extended Entity Classes
|
|
430
|
+
|
|
431
|
+
#### AIPromptCategoryEntityExtended
|
|
432
|
+
|
|
433
|
+
Extended prompt category with prompt collection:
|
|
434
|
+
|
|
435
|
+
```typescript
|
|
436
|
+
class AIPromptCategoryEntityExtended extends AIPromptCategoryEntity {
|
|
437
|
+
get Prompts(): AIPromptEntity[]; // Prompts in this category
|
|
438
|
+
}
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
### Key Interfaces and Types
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
interface AIPromptRunResult {
|
|
445
|
+
success: boolean; // Whether the execution was successful
|
|
446
|
+
rawResult?: string; // The raw result from the AI model
|
|
447
|
+
result?: any; // The parsed/validated result based on OutputType
|
|
448
|
+
errorMessage?: string; // Error message if execution failed
|
|
449
|
+
promptRun?: AIPromptRunEntity; // The AIPromptRun entity that was created for tracking
|
|
450
|
+
executionTimeMS?: number; // Total execution time in milliseconds
|
|
451
|
+
tokensUsed?: number; // Tokens used in the execution
|
|
452
|
+
validationResult?: ValidationResult; // Validation result if output validation was performed
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Parallelization strategies supported by the system
|
|
456
|
+
type ParallelizationStrategy = 'None' | 'StaticCount' | 'ConfigParam' | 'ModelSpecific';
|
|
457
|
+
|
|
458
|
+
// Result selection methods for choosing best result from parallel executions
|
|
459
|
+
type ResultSelectionMethod = 'First' | 'Random' | 'PromptSelector' | 'Consensus';
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
## Integration with Other Packages
|
|
463
|
+
|
|
464
|
+
### With AI Engine
|
|
465
|
+
|
|
466
|
+
The Prompts package builds on the AI Engine for basic functionality:
|
|
467
|
+
|
|
468
|
+
```typescript
|
|
469
|
+
// AI Engine provides model management and basic operations
|
|
470
|
+
import { AIEngine } from '@memberjunction/aiengine';
|
|
471
|
+
import { AIPromptRunner } from '@memberjunction/ai-prompts';
|
|
472
|
+
|
|
473
|
+
// Initialize AI Engine first
|
|
474
|
+
await AIEngine.Instance.Config(false, currentUser);
|
|
475
|
+
|
|
476
|
+
// Access prompts managed by AI Engine
|
|
477
|
+
const prompts = AIEngine.Instance.Prompts;
|
|
478
|
+
const prompt = prompts.find(p => p.Name === 'Your Prompt');
|
|
479
|
+
|
|
480
|
+
// Use Prompts package for advanced execution
|
|
481
|
+
const runner = new AIPromptRunner();
|
|
482
|
+
const result = await runner.ExecutePrompt({ prompt, data, contextUser });
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### With AI Agents
|
|
486
|
+
|
|
487
|
+
AI Agents can leverage the prompt system for sophisticated operations:
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
// Agents use prompts for their intelligence
|
|
491
|
+
import { BaseAgent } from '@memberjunction/ai-agents';
|
|
492
|
+
import { AIPromptRunner } from '@memberjunction/ai-prompts';
|
|
493
|
+
|
|
494
|
+
class IntelligentAgent extends BaseAgent {
|
|
495
|
+
private promptRunner = new AIPromptRunner();
|
|
496
|
+
|
|
497
|
+
async execute(context: AgentContext): Promise<AgentResult> {
|
|
498
|
+
const prompt = this.getPromptForContext(context);
|
|
499
|
+
|
|
500
|
+
const result = await this.promptRunner.ExecutePrompt({
|
|
501
|
+
prompt: prompt,
|
|
502
|
+
data: context.data,
|
|
503
|
+
contextUser: context.user
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
return this.formatAgentResult(result);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
## Dependencies
|
|
512
|
+
|
|
513
|
+
- `@memberjunction/core` (v2.43.0): MemberJunction core library
|
|
514
|
+
- `@memberjunction/global` (v2.43.0): MemberJunction global utilities
|
|
515
|
+
- `@memberjunction/core-entities` (v2.43.0): MemberJunction entity definitions
|
|
516
|
+
- `@memberjunction/ai` (v2.43.0): AI abstractions and interfaces
|
|
517
|
+
- `@memberjunction/aiengine` (v2.43.0): AI model management and basic operations
|
|
518
|
+
- `@memberjunction/templates` (v2.43.0): Template rendering system
|
|
519
|
+
- `dotenv` (^16.4.1): Environment variable management
|
|
520
|
+
- `rxjs` (^7.8.1): Reactive programming support
|
|
521
|
+
|
|
522
|
+
## Related Packages
|
|
523
|
+
|
|
524
|
+
- `@memberjunction/aiengine`: Core AI engine and model management
|
|
525
|
+
- `@memberjunction/ai-agents`: Advanced agent framework built on prompts
|
|
526
|
+
- `@memberjunction/templates`: Template rendering for dynamic content
|
|
527
|
+
|
|
528
|
+
## Migration Guide
|
|
529
|
+
|
|
530
|
+
### From AI Engine Simple Completions
|
|
531
|
+
|
|
532
|
+
For cases requiring more sophisticated prompt management:
|
|
533
|
+
|
|
534
|
+
```typescript
|
|
535
|
+
// Old: Simple LLM completion (still valid for basic cases)
|
|
536
|
+
const response = await AIEngine.Instance.SimpleLLMCompletion(
|
|
537
|
+
"Analyze this data",
|
|
538
|
+
currentUser,
|
|
539
|
+
"You are a data analyst"
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
// New: Advanced prompt with caching, validation, and parallel execution
|
|
543
|
+
const prompt = {
|
|
544
|
+
Name: "Data Analysis",
|
|
545
|
+
PromptText: "Analyze this data: {{data}}",
|
|
546
|
+
EnableCaching: true,
|
|
547
|
+
ParallelizationMode: "StaticCount",
|
|
548
|
+
ParallelCount: 2,
|
|
549
|
+
OutputType: "object",
|
|
550
|
+
ValidationBehavior: "Strict"
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
const runner = new AIPromptRunner();
|
|
554
|
+
const result = await runner.ExecutePrompt({
|
|
555
|
+
prompt: prompt,
|
|
556
|
+
data: { data: "your data here" },
|
|
557
|
+
contextUser: currentUser
|
|
558
|
+
});
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
## Best Practices
|
|
562
|
+
|
|
563
|
+
1. **Enable Caching**: Use intelligent caching for expensive operations
|
|
564
|
+
2. **Validate Outputs**: Always specify expected output types for critical operations
|
|
565
|
+
3. **Use Templates**: Leverage template system for dynamic prompts
|
|
566
|
+
4. **Monitor Performance**: Track token usage and execution times
|
|
567
|
+
5. **Parallel Wisely**: Use parallel execution for independent tasks, not dependent ones
|
|
568
|
+
6. **Handle Errors**: Implement proper retry logic and error handling
|
|
569
|
+
|
|
570
|
+
## Troubleshooting
|
|
571
|
+
|
|
572
|
+
### Common Issues
|
|
573
|
+
|
|
574
|
+
1. **"No suitable model found" Error**
|
|
575
|
+
- Ensure AIEngine.Instance.Config() is called before using prompts
|
|
576
|
+
- Verify prompt has active AIPromptModel associations or proper model selection configuration
|
|
577
|
+
- Check that models meet MinPowerRank requirements
|
|
578
|
+
|
|
579
|
+
2. **Template Rendering Failures**
|
|
580
|
+
- Verify template exists and is associated with the prompt
|
|
581
|
+
- Ensure template data contains all required variables
|
|
582
|
+
- Check template syntax for Handlebars errors
|
|
583
|
+
|
|
584
|
+
3. **Parallel Execution Not Working**
|
|
585
|
+
- Confirm ParallelizationMode is set to a value other than 'None'
|
|
586
|
+
- For ModelSpecific mode, ensure AIPromptModel entries exist
|
|
587
|
+
- Check that multiple suitable models are available
|
|
588
|
+
|
|
589
|
+
4. **Output Validation Errors**
|
|
590
|
+
- Ensure OutputType matches the expected result format
|
|
591
|
+
- Provide a valid OutputExample for structured data
|
|
592
|
+
- Consider increasing MaxRetries for complex outputs
|
|
593
|
+
|
|
594
|
+
## License
|
|
595
|
+
|
|
596
|
+
ISC
|
|
597
|
+
|
|
598
|
+
---
|
|
599
|
+
|
|
600
|
+
## Advanced Configuration
|
|
601
|
+
|
|
602
|
+
### Cache Configuration
|
|
603
|
+
|
|
604
|
+
```typescript
|
|
605
|
+
const cacheOptimizedPrompt = {
|
|
606
|
+
EnableCaching: true,
|
|
607
|
+
CacheMatchType: "Vector", // Vector similarity matching
|
|
608
|
+
CacheTTLSeconds: 3600, // 1 hour cache
|
|
609
|
+
CacheSimilarityThreshold: 0.9, // High similarity required
|
|
610
|
+
CacheMustMatchModel: true, // Model must match
|
|
611
|
+
CacheMustMatchVendor: false, // Vendor can differ
|
|
612
|
+
CacheMustMatchAgent: false, // Agent can differ
|
|
613
|
+
CacheMustMatchConfig: true // Configuration must match
|
|
614
|
+
};
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
### Model Selection Strategies
|
|
618
|
+
|
|
619
|
+
```typescript
|
|
620
|
+
// By power ranking
|
|
621
|
+
const powerBasedPrompt = {
|
|
622
|
+
SelectionStrategy: "ByPower",
|
|
623
|
+
PowerPreference: "Highest", // or "Lowest"
|
|
624
|
+
MinPowerRank: 80 // Minimum capability required
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// Specific models
|
|
628
|
+
const specificModelsPrompt = {
|
|
629
|
+
SelectionStrategy: "Specific",
|
|
630
|
+
// Models defined in AIPromptModel entries
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
// Default system selection
|
|
634
|
+
const defaultPrompt = {
|
|
635
|
+
SelectionStrategy: "Default"
|
|
636
|
+
};
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
For additional configuration options and advanced use cases, refer to the source code and entity definitions in the MemberJunction core system.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AIPromptCategoryEntity, AIPromptEntity } from "@memberjunction/core-entities";
|
|
2
|
+
export declare class AIPromptCategoryEntityExtended extends AIPromptCategoryEntity {
|
|
3
|
+
private _prompts;
|
|
4
|
+
get Prompts(): AIPromptEntity[];
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=AIPromptCategoryExtended.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AIPromptCategoryExtended.d.ts","sourceRoot":"","sources":["../src/AIPromptCategoryExtended.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAIvF,qBACa,8BAA+B,SAAQ,sBAAsB;IACtE,OAAO,CAAC,QAAQ,CAAwB;IACxC,IAAW,OAAO,IAAI,cAAc,EAAE,CAErC;CACJ"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AIPromptCategoryEntityExtended = void 0;
|
|
10
|
+
const core_entities_1 = require("@memberjunction/core-entities");
|
|
11
|
+
const global_1 = require("@memberjunction/global");
|
|
12
|
+
const typeorm_1 = require("typeorm");
|
|
13
|
+
let AIPromptCategoryEntityExtended = class AIPromptCategoryEntityExtended extends core_entities_1.AIPromptCategoryEntity {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
16
|
+
this._prompts = [];
|
|
17
|
+
}
|
|
18
|
+
get Prompts() {
|
|
19
|
+
return this._prompts;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.AIPromptCategoryEntityExtended = AIPromptCategoryEntityExtended;
|
|
23
|
+
exports.AIPromptCategoryEntityExtended = AIPromptCategoryEntityExtended = __decorate([
|
|
24
|
+
(0, global_1.RegisterClass)(typeorm_1.BaseEntity, "AI Prompt Categories")
|
|
25
|
+
], AIPromptCategoryEntityExtended);
|
|
26
|
+
//# sourceMappingURL=AIPromptCategoryExtended.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AIPromptCategoryExtended.js","sourceRoot":"","sources":["../src/AIPromptCategoryExtended.ts"],"names":[],"mappings":";;;;;;;;;AAAA,iEAAuF;AACvF,mDAAuD;AACvD,qCAAqC;AAG9B,IAAM,8BAA8B,GAApC,MAAM,8BAA+B,SAAQ,sCAAsB;IAAnE;;QACK,aAAQ,GAAqB,EAAE,CAAC;IAI5C,CAAC;IAHG,IAAW,OAAO;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;CACJ,CAAA;AALY,wEAA8B;yCAA9B,8BAA8B;IAD1C,IAAA,sBAAa,EAAC,oBAAU,EAAE,sBAAsB,CAAC;GACrC,8BAA8B,CAK1C"}
|