@memberjunction/ai-recommendations 2.32.2 → 2.34.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/package.json +5 -5
- package/readme.md +236 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-recommendations",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.34.0",
|
|
4
4
|
"description": "MemberJunction Recommendations Engine",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"typescript": "^5.4.5"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@memberjunction/global": "2.
|
|
23
|
-
"@memberjunction/core": "2.
|
|
24
|
-
"@memberjunction/core-entities": "2.
|
|
25
|
-
"@memberjunction/ai": "2.
|
|
22
|
+
"@memberjunction/global": "2.34.0",
|
|
23
|
+
"@memberjunction/core": "2.34.0",
|
|
24
|
+
"@memberjunction/core-entities": "2.34.0",
|
|
25
|
+
"@memberjunction/ai": "2.34.0"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/readme.md
CHANGED
|
@@ -1,2 +1,237 @@
|
|
|
1
1
|
# @memberjunction/ai-recommendations
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
The MemberJunction Recommendations Engine provides a flexible and extensible framework for integrating with AI-powered recommendation systems. It handles the orchestration of recommendation requests, provider management, and the storage of recommendation results within the MemberJunction ecosystem.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Extensible Provider Framework**: Support for multiple recommendation providers through a unified API
|
|
8
|
+
- **Built-in Tracking**: Automatic tracking of recommendation runs and results
|
|
9
|
+
- **Entity Integration**: Seamless integration with MemberJunction entities and records
|
|
10
|
+
- **List Support**: Generate recommendations for records in a list
|
|
11
|
+
- **Error Handling**: Comprehensive error tracking and reporting
|
|
12
|
+
- **Batch Processing**: Process multiple recommendation requests efficiently
|
|
13
|
+
- **Metadata Management**: Automatic management of recommendation metadata
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @memberjunction/ai-recommendations
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Core Components
|
|
22
|
+
|
|
23
|
+
### RecommendationEngineBase
|
|
24
|
+
|
|
25
|
+
The main engine class that coordinates recommendation requests:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';
|
|
29
|
+
|
|
30
|
+
// Initialize and load the engine
|
|
31
|
+
await RecommendationEngineBase.Instance.Config();
|
|
32
|
+
|
|
33
|
+
// Create a recommendation request
|
|
34
|
+
const request = new RecommendationRequest();
|
|
35
|
+
|
|
36
|
+
// Configure the request
|
|
37
|
+
request.EntityAndRecordsInfo = {
|
|
38
|
+
EntityName: 'Customers',
|
|
39
|
+
RecordIDs: ['CUST001', 'CUST002']
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Execute the recommendation
|
|
43
|
+
const result = await RecommendationEngineBase.Instance.Recommend(request);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### RecommendationProviderBase
|
|
47
|
+
|
|
48
|
+
Abstract base class for implementing recommendation providers:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { RecommendationProviderBase, RecommendationRequest, RecommendationResult } from '@memberjunction/ai-recommendations';
|
|
52
|
+
import { UserInfo } from '@memberjunction/core';
|
|
53
|
+
import { RecommendationItemEntity } from '@memberjunction/core-entities';
|
|
54
|
+
|
|
55
|
+
export class MyRecommendationProvider extends RecommendationProviderBase {
|
|
56
|
+
constructor(contextUser: UserInfo) {
|
|
57
|
+
super(contextUser);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public async Recommend(request: RecommendationRequest): Promise<RecommendationResult> {
|
|
61
|
+
const result = new RecommendationResult(request);
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
// Process each recommendation
|
|
65
|
+
for (const recommendation of request.Recommendations) {
|
|
66
|
+
// Generate items for this recommendation
|
|
67
|
+
const items: RecommendationItemEntity[] = [];
|
|
68
|
+
|
|
69
|
+
// Your recommendation logic here
|
|
70
|
+
// ...
|
|
71
|
+
|
|
72
|
+
// Save the recommendation and its items
|
|
73
|
+
await this.SaveRecommendation(recommendation, request.RunID, items);
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
result.AppendError(error.message);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Usage Examples
|
|
85
|
+
|
|
86
|
+
### Generate Recommendations for Specific Records
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';
|
|
90
|
+
|
|
91
|
+
async function getCustomerRecommendations(customerIds: string[]) {
|
|
92
|
+
// Ensure the engine is configured
|
|
93
|
+
await RecommendationEngineBase.Instance.Config();
|
|
94
|
+
|
|
95
|
+
// Create a request for specific customer records
|
|
96
|
+
const request = new RecommendationRequest();
|
|
97
|
+
request.EntityAndRecordsInfo = {
|
|
98
|
+
EntityName: 'Customers',
|
|
99
|
+
RecordIDs: customerIds
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Optionally specify a provider
|
|
103
|
+
// request.Provider = RecommendationEngineBase.Instance.RecommendationProviders.find(p => p.Name === 'MyProvider');
|
|
104
|
+
|
|
105
|
+
// Execute the recommendation
|
|
106
|
+
const result = await RecommendationEngineBase.Instance.Recommend(request);
|
|
107
|
+
|
|
108
|
+
if (result.Success) {
|
|
109
|
+
console.log('Recommendations generated successfully!');
|
|
110
|
+
return result.RecommendationItems;
|
|
111
|
+
} else {
|
|
112
|
+
console.error('Error generating recommendations:', result.ErrorMessage);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Generate Recommendations from a List
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';
|
|
122
|
+
|
|
123
|
+
async function getRecommendationsFromList(listId: string) {
|
|
124
|
+
await RecommendationEngineBase.Instance.Config();
|
|
125
|
+
|
|
126
|
+
// Create a request for records in a list
|
|
127
|
+
const request = new RecommendationRequest();
|
|
128
|
+
request.ListID = listId;
|
|
129
|
+
request.CreateErrorList = true; // Create a list to track errors
|
|
130
|
+
|
|
131
|
+
// Execute the recommendation
|
|
132
|
+
const result = await RecommendationEngineBase.Instance.Recommend(request);
|
|
133
|
+
|
|
134
|
+
if (result.Success) {
|
|
135
|
+
console.log('Recommendations generated successfully!');
|
|
136
|
+
console.log('Error list ID (if needed):', result.Request.ErrorListID);
|
|
137
|
+
return result.RecommendationItems;
|
|
138
|
+
} else {
|
|
139
|
+
console.error('Error generating recommendations:', result.ErrorMessage);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Using Advanced Options
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { RecommendationEngineBase, RecommendationRequest } from '@memberjunction/ai-recommendations';
|
|
149
|
+
|
|
150
|
+
// Define a custom options interface for your provider
|
|
151
|
+
interface MyProviderOptions {
|
|
152
|
+
similarityThreshold: number;
|
|
153
|
+
maxRecommendations: number;
|
|
154
|
+
includeRatings: boolean;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function getCustomizedRecommendations(customerId: string) {
|
|
158
|
+
await RecommendationEngineBase.Instance.Config();
|
|
159
|
+
|
|
160
|
+
// Create a request with custom options
|
|
161
|
+
const request = new RecommendationRequest<MyProviderOptions>();
|
|
162
|
+
request.EntityAndRecordsInfo = {
|
|
163
|
+
EntityName: 'Customers',
|
|
164
|
+
RecordIDs: [customerId]
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// Add provider-specific options
|
|
168
|
+
request.Options = {
|
|
169
|
+
similarityThreshold: 0.75,
|
|
170
|
+
maxRecommendations: 5,
|
|
171
|
+
includeRatings: true
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// Execute the recommendation
|
|
175
|
+
return await RecommendationEngineBase.Instance.Recommend(request);
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Recommendation Flow
|
|
180
|
+
|
|
181
|
+
The recommendation process follows these steps:
|
|
182
|
+
|
|
183
|
+
1. **Request Creation**: A `RecommendationRequest` is created with entity records or a list
|
|
184
|
+
2. **Run Tracking**: A `RecommendationRun` entity is created to track the process
|
|
185
|
+
3. **Provider Selection**: The appropriate recommendation provider is selected
|
|
186
|
+
4. **Recommendation Generation**: The provider generates recommendations for each requested record
|
|
187
|
+
5. **Result Storage**: Recommendations and items are saved to the database
|
|
188
|
+
6. **Status Update**: The run status is updated (completed or error)
|
|
189
|
+
7. **Result Return**: The `RecommendationResult` is returned to the caller
|
|
190
|
+
|
|
191
|
+
## Data Model
|
|
192
|
+
|
|
193
|
+
The recommendation engine works with these key entities:
|
|
194
|
+
|
|
195
|
+
- **RecommendationProviderEntity**: Configuration for recommendation providers
|
|
196
|
+
- **RecommendationRunEntity**: Tracks each recommendation execution
|
|
197
|
+
- **RecommendationEntity**: Represents a recommendation for a source record
|
|
198
|
+
- **RecommendationItemEntity**: Individual recommendation items (products, content, etc.)
|
|
199
|
+
|
|
200
|
+
## Provider Implementation
|
|
201
|
+
|
|
202
|
+
To create a custom recommendation provider:
|
|
203
|
+
|
|
204
|
+
1. Create a class that extends `RecommendationProviderBase`
|
|
205
|
+
2. Implement the `Recommend` method to generate recommendations
|
|
206
|
+
3. Register your provider implementation:
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
// Register your provider with MemberJunction's class factory
|
|
210
|
+
import { MJGlobal } from '@memberjunction/global';
|
|
211
|
+
import { RecommendationProviderBase } from '@memberjunction/ai-recommendations';
|
|
212
|
+
|
|
213
|
+
MJGlobal.Instance.ClassFactory.RegisterClass(
|
|
214
|
+
RecommendationProviderBase,
|
|
215
|
+
'MyRecommendationProvider',
|
|
216
|
+
MyRecommendationProvider
|
|
217
|
+
);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Integration with MemberJunction
|
|
221
|
+
|
|
222
|
+
This package integrates with the MemberJunction ecosystem:
|
|
223
|
+
|
|
224
|
+
- Uses MemberJunction entities for data storage and retrieval
|
|
225
|
+
- Works with MemberJunction lists for batch processing
|
|
226
|
+
- Leverages MemberJunction's metadata system
|
|
227
|
+
|
|
228
|
+
## Dependencies
|
|
229
|
+
|
|
230
|
+
- `@memberjunction/global`: MemberJunction global utilities
|
|
231
|
+
- `@memberjunction/core`: MemberJunction core library
|
|
232
|
+
- `@memberjunction/core-entities`: MemberJunction entity definitions
|
|
233
|
+
- `@memberjunction/ai`: MemberJunction AI abstractions
|
|
234
|
+
|
|
235
|
+
## License
|
|
236
|
+
|
|
237
|
+
ISC
|