@memberjunction/aiengine 2.32.2 → 2.33.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.
Files changed (2) hide show
  1. package/README.md +225 -0
  2. package/package.json +5 -5
package/README.md ADDED
@@ -0,0 +1,225 @@
1
+ # @memberjunction/aiengine
2
+
3
+ The MemberJunction AI Engine package manages and executes AI-powered actions across entities in the MemberJunction ecosystem. It provides a comprehensive framework for defining, configuring, and running AI actions with various models, handling metadata, caching, prompt generation, and integration with entity records.
4
+
5
+ ## Features
6
+
7
+ - **AI Action Framework**: Coordinate AI actions across entity records
8
+ - **Model Management**: Maintain a registry of AI models and their capabilities
9
+ - **Dynamic Prompt Generation**: Build prompts with entity data substitution
10
+ - **Entity Integration**: Pre and post-processing hooks for entity AI operations
11
+ - **Results Caching**: Cache AI operation results for faster responses
12
+ - **Multi-Model Support**: Switch between different AI providers
13
+ - **Agent Infrastructure**: Support for AI agents with specialized capabilities
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @memberjunction/aiengine
19
+ ```
20
+
21
+ ## Requirements
22
+
23
+ - Node.js 16+
24
+ - MemberJunction Core libraries
25
+ - At least one AI model provider (e.g., `@memberjunction/ai-openai`)
26
+
27
+ ## Core Concepts
28
+
29
+ ### AI Models
30
+
31
+ AI Models represent specific model implementations from various providers (OpenAI, Anthropic, etc.). Each model has:
32
+
33
+ - A driver class that implements the actual AI functionality
34
+ - Configuration details (API name, vendor, model type)
35
+ - Power ranking and other attributes
36
+
37
+ ### AI Actions
38
+
39
+ Actions represent different AI operations like:
40
+
41
+ - `chat`: General conversational AI
42
+ - `summarize`: Text summarization
43
+ - `classify`: Text classification
44
+
45
+ ### Entity AI Actions
46
+
47
+ Entity AI Actions connect AI actions to specific entity types, defining:
48
+
49
+ - Input preparation from entity records
50
+ - Output handling (save to fields or create related records)
51
+ - Default prompts and models to use
52
+
53
+ ## Usage
54
+
55
+ ### Initialization
56
+
57
+ Load the AI Engine and its metadata:
58
+
59
+ ```typescript
60
+ import { AIEngine } from '@memberjunction/aiengine';
61
+
62
+ // Initialize the engine
63
+ await AIEngine.Instance.Config(false, currentUser);
64
+ ```
65
+
66
+ ### Simple LLM Completions
67
+
68
+ For quick AI tasks with minimal setup:
69
+
70
+ ```typescript
71
+ // Get a simple completion with the highest power LLM
72
+ const response = await AIEngine.Instance.SimpleLLMCompletion(
73
+ "What can you tell me about TypeScript?",
74
+ currentUser,
75
+ "You are a helpful assistant that specializes in programming languages."
76
+ );
77
+
78
+ console.log("AI Response:", response);
79
+ ```
80
+
81
+ ### Entity AI Actions
82
+
83
+ Execute AI actions on entity records:
84
+
85
+ ```typescript
86
+ import { EntityAIActionParams } from '@memberjunction/aiengine';
87
+ import { Metadata } from '@memberjunction/core';
88
+
89
+ // Load an entity record
90
+ const md = new Metadata();
91
+ const customer = await md.GetEntityObject('Customers');
92
+ await customer.Load(customerId);
93
+
94
+ // Execute an AI action
95
+ const params: EntityAIActionParams = {
96
+ actionId: 'action-id-here',
97
+ modelId: 'model-id-here',
98
+ entityAIActionId: 'entity-action-id-here',
99
+ entityRecord: customer
100
+ };
101
+
102
+ const result = await AIEngine.Instance.ExecuteEntityAIAction(params);
103
+
104
+ if (result && result.success) {
105
+ console.log("AI processing completed successfully");
106
+ // The entity record has been updated if configured that way
107
+ } else {
108
+ console.error("Error:", result.errorMessage);
109
+ }
110
+ ```
111
+
112
+ ### Dynamic Prompt Generation
113
+
114
+ Use entity values in prompts:
115
+
116
+ ```typescript
117
+ // The markup will replace {FieldName} with actual values
118
+ const entityAction = {
119
+ UserMessage: "Please summarize the customer profile for {Name} who works at {Company}."
120
+ };
121
+
122
+ // When executed on a record with Name="John Doe" and Company="Acme Inc"
123
+ // The prompt becomes: "Please summarize the customer profile for John Doe who works at Acme Inc."
124
+ ```
125
+
126
+ ### Manual AI Actions
127
+
128
+ Execute AI actions directly:
129
+
130
+ ```typescript
131
+ import { AIActionParams } from '@memberjunction/aiengine';
132
+
133
+ const params: AIActionParams = {
134
+ actionId: 'summarize-action-id',
135
+ modelId: 'gpt4-model-id',
136
+ systemPrompt: "You are a helpful assistant that creates concise summaries.",
137
+ userPrompt: "Summarize the following document: " + documentText
138
+ };
139
+
140
+ const result = await AIEngine.Instance.ExecuteAIAction(params);
141
+ console.log("Summary:", result.data?.choices[0]?.message?.content);
142
+ ```
143
+
144
+ ### Result Caching
145
+
146
+ Check for cached results before running expensive operations:
147
+
148
+ ```typescript
149
+ // Check if we have a cached result
150
+ const cached = await AIEngine.Instance.CheckResultCache(fullPromptText);
151
+
152
+ if (cached) {
153
+ console.log("Using cached result:", cached.ResultText);
154
+ return cached.ResultText;
155
+ }
156
+
157
+ // Otherwise execute the action and cache the result
158
+ const result = await AIEngine.Instance.ExecuteAIAction(params);
159
+
160
+ // Cache the result for future use
161
+ await AIEngine.Instance.CacheResult(
162
+ model,
163
+ prompt,
164
+ fullPromptText,
165
+ result.data.choices[0].message.content
166
+ );
167
+ ```
168
+
169
+ ## API Reference
170
+
171
+ ### AIEngine Class
172
+
173
+ The central class that manages all AI operations.
174
+
175
+ #### Methods
176
+
177
+ - `Config(forceRefresh?, contextUser?, provider?)`: Load AI configuration metadata
178
+ - `ExecuteEntityAIAction(params: EntityAIActionParams)`: Execute an AI action on an entity
179
+ - `ExecuteAIAction(params: AIActionParams)`: Execute a generic AI action
180
+ - `SimpleLLMCompletion(userPrompt, contextUser, systemPrompt?, model?, apiKey?)`: Quick text completion
181
+ - `GetHighestPowerModel(vendorName, modelType, contextUser?)`: Get the most powerful model of a type
182
+ - `GetHighestPowerLLM(vendorName?, contextUser?)`: Get the most powerful LLM
183
+ - `CheckResultCache(prompt)`: Check if a result is cached
184
+ - `CacheResult(model, prompt, promptText, resultText)`: Cache an AI result
185
+
186
+ #### Properties
187
+
188
+ - `Models`: All registered AI models
189
+ - `LanguageModels`: Just the LLM type models
190
+ - `Actions`: All registered AI actions
191
+ - `EntityAIActions`: All entity-specific AI actions
192
+ - `Prompts`: All registered prompts
193
+ - `PromptCategories`: Organized prompt categories
194
+ - `Agents`: Available AI agents
195
+ - `VectorDatabases`: Vector database configurations
196
+
197
+ ## Entity Integration
198
+
199
+ Entity classes can implement special methods to interact with the AI engine:
200
+
201
+ ```typescript
202
+ // In your entity class
203
+ async BeforeEntityAIAction(params: any): Promise<boolean> {
204
+ // Pre-process before AI action
205
+ // Return false to abort the action
206
+ return true;
207
+ }
208
+
209
+ async AfterEntityAIAction(params: any): Promise<boolean> {
210
+ // Post-process after AI action
211
+ // Access the result via params.result
212
+ return true;
213
+ }
214
+ ```
215
+
216
+ ## Dependencies
217
+
218
+ - `@memberjunction/core`: MemberJunction core library
219
+ - `@memberjunction/global`: MemberJunction global utilities
220
+ - `@memberjunction/core-entities`: MemberJunction entity definitions
221
+ - `@memberjunction/ai`: AI abstractions and interfaces
222
+
223
+ ## License
224
+
225
+ ISC
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/aiengine",
3
- "version": "2.32.2",
3
+ "version": "2.33.0",
4
4
  "description": "MemberJunction: AI Engine Package - handles automatic execution of Entity AI Actions using AI Models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,10 +15,10 @@
15
15
  "author": "MemberJunction.com",
16
16
  "license": "ISC",
17
17
  "dependencies": {
18
- "@memberjunction/core": "2.32.2",
19
- "@memberjunction/global": "2.32.2",
20
- "@memberjunction/core-entities": "2.32.2",
21
- "@memberjunction/ai": "2.32.2",
18
+ "@memberjunction/core": "2.33.0",
19
+ "@memberjunction/global": "2.33.0",
20
+ "@memberjunction/core-entities": "2.33.0",
21
+ "@memberjunction/ai": "2.33.0",
22
22
  "dotenv": "^16.4.1",
23
23
  "rxjs": "^7.8.1"
24
24
  },