@rws-framework/ai-tools 2.1.1 → 2.2.1
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 +1 -1
- package/src/models/prompts/_prompt.ts +140 -241
- package/src/models/prompts/inc/execution-methods-handler.ts +52 -0
- package/src/models/prompts/inc/index.ts +6 -0
- package/src/models/prompts/inc/input-output-manager.ts +120 -0
- package/src/models/prompts/inc/model-execution-manager.ts +110 -0
- package/src/models/prompts/inc/tool-manager.ts +57 -0
- package/src/models/prompts/inc/types.ts +58 -0
- package/src/models/prompts/inc/variable-storage-manager.ts +27 -0
- package/src/types/IPrompt.ts +6 -2
package/package.json
CHANGED
|
@@ -1,327 +1,226 @@
|
|
|
1
1
|
import { Readable } from 'stream';
|
|
2
|
-
import {
|
|
3
|
-
import { EmbedLoader, IChainCallOutput } from '../convo/EmbedLoader';
|
|
4
|
-
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
5
|
-
import { IToolCall } from '../../types/IPrompt'
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
6
3
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
IRWSSinglePromptRequestExecutor,
|
|
14
|
-
IRWSPromptJSON,
|
|
15
|
-
ChainStreamType,
|
|
4
|
+
IPromptParams,
|
|
5
|
+
IRWSPromptRequestExecutor,
|
|
6
|
+
IRWSPromptStreamExecutor,
|
|
7
|
+
IRWSSinglePromptRequestExecutor,
|
|
8
|
+
IRWSPromptJSON,
|
|
9
|
+
ChainStreamType,
|
|
16
10
|
ILLMChunk,
|
|
17
11
|
IAITool,
|
|
18
12
|
IRWSHistoryMessage,
|
|
19
|
-
InputType,
|
|
20
13
|
CompoundInput,
|
|
21
|
-
ToolHandler
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
14
|
+
ToolHandler,
|
|
15
|
+
IToolCall,
|
|
16
|
+
IPromptEnchantment,
|
|
17
|
+
BaseChatModel,
|
|
18
|
+
PromptTemplate,
|
|
19
|
+
EmbedLoader
|
|
20
|
+
} from './inc/types';
|
|
21
|
+
import { IChainCallOutput } from './inc/types';
|
|
22
|
+
import { ToolManager } from './inc/tool-manager';
|
|
23
|
+
import { InputOutputManager } from './inc/input-output-manager';
|
|
24
|
+
import { ModelExecutionManager } from './inc/model-execution-manager';
|
|
25
|
+
import { VariableStorageManager } from './inc/variable-storage-manager';
|
|
26
|
+
import { ExecutionMethodsHandler, IPromptInstance } from './inc/execution-methods-handler';
|
|
27
|
+
|
|
28
|
+
class RWSPrompt implements IPromptInstance {
|
|
33
29
|
public _stream: ChainStreamType;
|
|
34
|
-
private input: CompoundInput[] = [];
|
|
35
|
-
private enhancedInput: IPromptEnchantment[] = [];
|
|
36
|
-
private sentInput: CompoundInput[] = [];
|
|
37
|
-
private originalInput: CompoundInput[] = [];
|
|
38
|
-
private output: string = '';
|
|
39
|
-
private modelId: string;
|
|
40
|
-
private modelType: string;
|
|
41
|
-
private multiTemplate: PromptTemplate;
|
|
42
|
-
private embedLoader: EmbedLoader<any>;
|
|
43
|
-
private hyperParameters: IPromptHyperParameters;
|
|
44
30
|
private created_at: Date;
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
|
|
32
|
+
// Composition over inheritance - use managers for different concerns
|
|
33
|
+
private toolManager: ToolManager;
|
|
34
|
+
private ioManager: InputOutputManager;
|
|
35
|
+
private modelManager: ModelExecutionManager;
|
|
36
|
+
private varManager: VariableStorageManager;
|
|
37
|
+
private executionHandler: ExecutionMethodsHandler;
|
|
38
|
+
|
|
39
|
+
constructor(params: IPromptParams) {
|
|
40
|
+
this.toolManager = new ToolManager();
|
|
41
|
+
this.ioManager = new InputOutputManager(params.input);
|
|
42
|
+
this.modelManager = new ModelExecutionManager(params.modelId, params.modelType, params.hyperParameters);
|
|
43
|
+
this.varManager = new VariableStorageManager();
|
|
44
|
+
this.executionHandler = new ExecutionMethodsHandler();
|
|
45
|
+
|
|
46
|
+
this.created_at = new Date();
|
|
47
|
+
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
+
// Delegation methods for tool management
|
|
50
|
+
setTools(tools: IAITool[]): RWSPrompt {
|
|
51
|
+
this.toolManager.setTools(tools);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
getTools(): IAITool[] {
|
|
56
|
+
return this.toolManager.getTools();
|
|
57
|
+
}
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
this.
|
|
54
|
-
|
|
55
|
-
this.hyperParameters = params.hyperParameters;
|
|
56
|
-
this.modelId = params.modelId;
|
|
57
|
-
this.modelType = params.modelType;
|
|
59
|
+
getTool(key: string): { definition: IAITool, handler: ToolHandler } | null {
|
|
60
|
+
return this.toolManager.getTool(key);
|
|
61
|
+
}
|
|
58
62
|
|
|
59
|
-
|
|
63
|
+
registerToolHandlers(toolHandlers: { [key: string]: ToolHandler }): void {
|
|
64
|
+
this.toolManager.registerToolHandlers(toolHandlers);
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
67
|
+
async callTools<T = unknown, O = unknown>(tools: IToolCall[], moduleRef: ModuleRef, aiToolOptions?: O): Promise<T[]> {
|
|
68
|
+
return this.toolManager.callTools<T, O>(tools, moduleRef, aiToolOptions);
|
|
69
|
+
}
|
|
65
70
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this.output += source;
|
|
70
|
-
this.onStream(source);
|
|
71
|
-
}
|
|
72
|
-
|
|
71
|
+
// Delegation methods for input/output management
|
|
72
|
+
listen(source: string, stream: boolean = true): RWSPrompt {
|
|
73
|
+
this.ioManager.listen(source, stream);
|
|
73
74
|
return this;
|
|
74
|
-
}
|
|
75
|
+
}
|
|
75
76
|
|
|
76
|
-
setStreamCallback(callback: (chunk: string) => void): void
|
|
77
|
-
|
|
78
|
-
this.onStream = callback;
|
|
77
|
+
setStreamCallback(callback: (chunk: string) => void): void {
|
|
78
|
+
this.ioManager.setStreamCallback(callback);
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
addEnchantment(enchantment: IPromptEnchantment): void
|
|
82
|
-
|
|
83
|
-
this.enhancedInput.push(enchantment);
|
|
81
|
+
addEnchantment(enchantment: IPromptEnchantment): void {
|
|
82
|
+
this.ioManager.addEnchantment(enchantment);
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
getEnchantedInputs(): IPromptEnchantment[]
|
|
87
|
-
|
|
88
|
-
return this.enhancedInput;
|
|
85
|
+
getEnchantedInputs(): IPromptEnchantment[] {
|
|
86
|
+
return this.ioManager.getEnchantedInputs();
|
|
89
87
|
}
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return this.modelId;
|
|
89
|
+
readSentInput(): CompoundInput[] {
|
|
90
|
+
return this.ioManager.readSentInput();
|
|
94
91
|
}
|
|
95
92
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return this.sentInput;
|
|
93
|
+
readInput(): CompoundInput[] {
|
|
94
|
+
return this.ioManager.readInput();
|
|
99
95
|
}
|
|
100
96
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const enchantedInput: CompoundInput[] = this.enhancedInput.map(enchantment => ({ role: 'user', type: enchantment.input.type, text: enchantment.input.text }));
|
|
104
|
-
return [...enchantedInput, ...this.input];
|
|
97
|
+
readBaseInput(): CompoundInput[] {
|
|
98
|
+
return this.ioManager.readBaseInput();
|
|
105
99
|
}
|
|
106
100
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
{
|
|
110
|
-
return this.originalInput;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
setBaseInput(input: CompoundInput[]): RWSPrompt
|
|
114
|
-
{
|
|
115
|
-
this.originalInput = input;
|
|
116
|
-
|
|
101
|
+
setBaseInput(input: CompoundInput[]): RWSPrompt {
|
|
102
|
+
this.ioManager.setBaseInput(input);
|
|
117
103
|
return this;
|
|
118
104
|
}
|
|
119
105
|
|
|
120
|
-
injestOutput(content: string): RWSPrompt
|
|
121
|
-
|
|
122
|
-
this.output = content;
|
|
123
|
-
|
|
106
|
+
injestOutput(content: string): RWSPrompt {
|
|
107
|
+
this.ioManager.injestOutput(content);
|
|
124
108
|
return this;
|
|
125
109
|
}
|
|
126
110
|
|
|
127
|
-
readOutput(): string
|
|
128
|
-
|
|
129
|
-
return this.output;
|
|
111
|
+
readOutput(): string {
|
|
112
|
+
return this.ioManager.readOutput();
|
|
130
113
|
}
|
|
131
114
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.hyperParameters = {...base, ...this.hyperParameters};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return this.hyperParameters as T;
|
|
115
|
+
addInput(content: CompoundInput): RWSPrompt {
|
|
116
|
+
this.ioManager.addInput(content);
|
|
117
|
+
return this;
|
|
139
118
|
}
|
|
140
119
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if(!this.hyperParameters[key]){
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return this.hyperParameters[key] as T;
|
|
120
|
+
addHistory(messages: IRWSHistoryMessage[], historyPrompt: string, callback?: (messages: IRWSHistoryMessage[], prompt: string) => void): void {
|
|
121
|
+
this.ioManager.addHistory(messages, historyPrompt, callback);
|
|
148
122
|
}
|
|
149
123
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
this.hyperParameters[key] = value;
|
|
153
|
-
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
setHyperParameters(value: any): RWSPrompt
|
|
158
|
-
{
|
|
159
|
-
this.hyperParameters = value;
|
|
160
|
-
|
|
161
|
-
return this;
|
|
124
|
+
async readStreamAsText(readableStream: ReadableStream, callback: (txt: string) => void): Promise<void> {
|
|
125
|
+
return this.ioManager.readStreamAsText(readableStream, callback);
|
|
162
126
|
}
|
|
163
127
|
|
|
164
|
-
|
|
165
|
-
{
|
|
166
|
-
this.
|
|
167
|
-
return this;
|
|
128
|
+
// Delegation methods for model management
|
|
129
|
+
getModelId(): string {
|
|
130
|
+
return this.modelManager.getModelId();
|
|
168
131
|
}
|
|
169
132
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return this.multiTemplate;
|
|
133
|
+
getModelMetadata(): [string, string] {
|
|
134
|
+
return this.modelManager.getModelMetadata();
|
|
173
135
|
}
|
|
174
136
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
this.embedLoader = embedLoader;
|
|
178
|
-
|
|
137
|
+
setMultiTemplate(template: PromptTemplate): RWSPrompt {
|
|
138
|
+
this.modelManager.setMultiTemplate(template);
|
|
179
139
|
return this;
|
|
180
140
|
}
|
|
181
141
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return this.embedLoader;
|
|
142
|
+
getMultiTemplate(): PromptTemplate {
|
|
143
|
+
return this.modelManager.getMultiTemplate();
|
|
185
144
|
}
|
|
186
145
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
146
|
+
setEmbedLoader(embedLoader: EmbedLoader<BaseChatModel>): RWSPrompt {
|
|
147
|
+
this.modelManager.setEmbedLoader(embedLoader);
|
|
148
|
+
return this;
|
|
190
149
|
}
|
|
191
150
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
return [this.modelType, this.modelId];
|
|
151
|
+
getEmbedLoader<T extends BaseChatModel>(): EmbedLoader<T> {
|
|
152
|
+
return this.modelManager.getEmbedLoader<T>();
|
|
195
153
|
}
|
|
196
154
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
this.sentInput = this.input;
|
|
200
|
-
const returnedRWS = await executor.promptRequest(this, { intruderPrompt, debugVars, tools });
|
|
201
|
-
this.output = returnedRWS.readOutput();
|
|
155
|
+
getHyperParameters<T extends any>(base: any = null): T {
|
|
156
|
+
return this.modelManager.getHyperParameters<T>(base);
|
|
202
157
|
}
|
|
203
158
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
await executor.singlePromptRequest(this, { intruderPrompt, ensureJson, tools });
|
|
207
|
-
this.sentInput = this.input;
|
|
159
|
+
getHyperParameter<T>(key: string): T {
|
|
160
|
+
return this.modelManager.getHyperParameter<T>(key as any);
|
|
208
161
|
}
|
|
209
162
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
return executor.promptStream(this, read, end, { debugVars, tools });
|
|
163
|
+
setHyperParameter(key: string, value: any): RWSPrompt {
|
|
164
|
+
this.modelManager.setHyperParameter(key, value);
|
|
165
|
+
return this;
|
|
214
166
|
}
|
|
215
167
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
this.input.push(content);
|
|
168
|
+
setHyperParameters(value: any): RWSPrompt {
|
|
169
|
+
this.modelManager.setHyperParameters(value);
|
|
219
170
|
return this;
|
|
220
171
|
}
|
|
221
172
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return Object.keys(this.varStorage).includes(key) ? this.varStorage[key] : null;
|
|
173
|
+
replacePromptVar(key: string, val: string): void {
|
|
174
|
+
this.modelManager.replacePromptVar(key, val);
|
|
225
175
|
}
|
|
226
176
|
|
|
227
|
-
|
|
228
|
-
|
|
177
|
+
// Delegation methods for variable storage
|
|
178
|
+
getVar<T>(key: string): T {
|
|
179
|
+
return this.varManager.getVar<T>(key);
|
|
180
|
+
}
|
|
229
181
|
|
|
182
|
+
setVar<T>(key: string, val: T): RWSPrompt {
|
|
183
|
+
this.varManager.setVar<T>(key, val);
|
|
230
184
|
return this;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
private async isChainStreamType(source: any): Promise<boolean> {
|
|
234
|
-
if (source && typeof source[Symbol.asyncIterator] === 'function') {
|
|
235
|
-
const asyncIterator = source[Symbol.asyncIterator]();
|
|
236
|
-
if (typeof asyncIterator.next === 'function' &&
|
|
237
|
-
typeof asyncIterator.throw === 'function' &&
|
|
238
|
-
typeof asyncIterator.return === 'function') {
|
|
239
|
-
try {
|
|
240
|
-
// Optionally check if the next method yields a value of the expected type
|
|
241
|
-
const { value, done } = await asyncIterator.next();
|
|
242
|
-
return !done && value instanceof ReadableStream; // or whatever check makes sense for IterableReadableStream<ChainValues>
|
|
243
|
-
} catch (error) {
|
|
244
|
-
// Handle or ignore error
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return false;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
async readStreamAsText(readableStream: ReadableStream, callback: (txt: string) => void) {
|
|
252
|
-
const reader = readableStream.getReader();
|
|
253
|
-
|
|
254
|
-
let readResult: any;
|
|
255
|
-
|
|
256
|
-
// Continuously read from the stream
|
|
257
|
-
while (!(readResult = await reader.read()).done) {
|
|
258
|
-
|
|
259
|
-
if (readResult.value && readResult.value.response) {
|
|
260
|
-
// Emit each chunk text as it's read
|
|
261
|
-
callback(readResult.value.response);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
185
|
}
|
|
266
186
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
${message.creator}: ${message.content}
|
|
271
|
-
`).join('\n\n')}
|
|
272
|
-
${historyPrompt}
|
|
273
|
-
` ;
|
|
274
|
-
|
|
275
|
-
if(callback){
|
|
276
|
-
callback(messages, prompt);
|
|
277
|
-
}else{
|
|
278
|
-
this.input = [{type: 'text', text: prompt}, ...this.input];
|
|
279
|
-
}
|
|
187
|
+
// Delegation methods for execution
|
|
188
|
+
async requestWith(executor: IRWSPromptRequestExecutor, intruderPrompt: string = null, debugVars: any = {}, tools?: IAITool[]): Promise<void> {
|
|
189
|
+
return this.executionHandler.requestWith(this, executor, intruderPrompt, debugVars, tools);
|
|
280
190
|
}
|
|
281
191
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
this.toolHandlers.set(key, toolHandlers[key]);
|
|
285
|
-
}
|
|
192
|
+
async singleRequestWith(executor: IRWSSinglePromptRequestExecutor, intruderPrompt: string = null, ensureJson: boolean = false, tools?: IAITool[]): Promise<void> {
|
|
193
|
+
return this.executionHandler.singleRequestWith(this, executor, intruderPrompt, ensureJson, tools);
|
|
286
194
|
}
|
|
287
195
|
|
|
288
|
-
async
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
for(const tool of tools){
|
|
292
|
-
if(this.toolHandlers.has(tool.name)){
|
|
293
|
-
const result = await this.callAiTool<T>(tool);
|
|
294
|
-
if(result){
|
|
295
|
-
results.push(result);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
196
|
+
async streamWith(executor: IRWSPromptStreamExecutor, read: (chunk: ILLMChunk) => void, end: () => void = () => { }, debugVars: any = {}, tools?: IAITool[]): Promise<RWSPrompt> {
|
|
197
|
+
return this.executionHandler.streamWith(this, executor, read, end, debugVars, tools);
|
|
198
|
+
}
|
|
299
199
|
|
|
300
|
-
|
|
200
|
+
// IPromptInstance interface implementation
|
|
201
|
+
getInput(): CompoundInput[] {
|
|
202
|
+
return this.ioManager.getInput();
|
|
301
203
|
}
|
|
302
204
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const handler = this.toolHandlers.get(tool.name);
|
|
306
|
-
return await handler(tool.arguments);
|
|
205
|
+
setSentInput(input: CompoundInput[]): void {
|
|
206
|
+
this.ioManager.setSentInput(input);
|
|
307
207
|
}
|
|
308
208
|
|
|
309
|
-
toJSON(): IRWSPromptJSON
|
|
310
|
-
{
|
|
209
|
+
toJSON(): IRWSPromptJSON {
|
|
311
210
|
return {
|
|
312
|
-
input: this.
|
|
313
|
-
enhancedInput: this.
|
|
314
|
-
sentInput: this.
|
|
315
|
-
originalInput: this.
|
|
316
|
-
output: this.
|
|
317
|
-
modelId: this.
|
|
318
|
-
modelType: this.
|
|
319
|
-
multiTemplate: this.
|
|
211
|
+
input: this.ioManager.getInput(),
|
|
212
|
+
enhancedInput: this.ioManager.getEnhancedInput(),
|
|
213
|
+
sentInput: this.ioManager.readSentInput(),
|
|
214
|
+
originalInput: this.ioManager.getOriginalInput(),
|
|
215
|
+
output: this.ioManager.getOutput(),
|
|
216
|
+
modelId: this.modelManager.getModelId(),
|
|
217
|
+
modelType: this.modelManager.getModelType(),
|
|
218
|
+
multiTemplate: this.modelManager.getMultiTemplate(),
|
|
320
219
|
embed: {
|
|
321
|
-
id: this.
|
|
220
|
+
id: this.modelManager.getEmbedLoaderId()
|
|
322
221
|
},
|
|
323
|
-
hyperParameters: this.
|
|
324
|
-
var_storage: this.
|
|
222
|
+
hyperParameters: this.modelManager.getAllHyperParameters(),
|
|
223
|
+
var_storage: this.varManager.getAllVars(),
|
|
325
224
|
created_at: this.created_at.toISOString()
|
|
326
225
|
};
|
|
327
226
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IRWSPromptRequestExecutor,
|
|
3
|
+
IRWSSinglePromptRequestExecutor,
|
|
4
|
+
IRWSPromptStreamExecutor,
|
|
5
|
+
ILLMChunk,
|
|
6
|
+
IAITool
|
|
7
|
+
} from './types';
|
|
8
|
+
|
|
9
|
+
export interface IPromptInstance {
|
|
10
|
+
readOutput(): string;
|
|
11
|
+
readInput(): any[];
|
|
12
|
+
getInput(): any[];
|
|
13
|
+
setSentInput(input: any[]): void;
|
|
14
|
+
injestOutput(content: string): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class ExecutionMethodsHandler {
|
|
18
|
+
async requestWith(
|
|
19
|
+
promptInstance: IPromptInstance,
|
|
20
|
+
executor: IRWSPromptRequestExecutor,
|
|
21
|
+
intruderPrompt: string = null,
|
|
22
|
+
debugVars: any = {},
|
|
23
|
+
tools?: IAITool[]
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
promptInstance.setSentInput(promptInstance.getInput());
|
|
26
|
+
const returnedRWS = await executor.promptRequest(promptInstance as any, { intruderPrompt, debugVars, tools });
|
|
27
|
+
promptInstance.injestOutput(returnedRWS.readOutput());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async singleRequestWith(
|
|
31
|
+
promptInstance: IPromptInstance,
|
|
32
|
+
executor: IRWSSinglePromptRequestExecutor,
|
|
33
|
+
intruderPrompt: string = null,
|
|
34
|
+
ensureJson: boolean = false,
|
|
35
|
+
tools?: IAITool[]
|
|
36
|
+
): Promise<void> {
|
|
37
|
+
await executor.singlePromptRequest(promptInstance as any, { intruderPrompt, ensureJson, tools });
|
|
38
|
+
promptInstance.setSentInput(promptInstance.getInput());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async streamWith(
|
|
42
|
+
promptInstance: IPromptInstance,
|
|
43
|
+
executor: IRWSPromptStreamExecutor,
|
|
44
|
+
read: (chunk: ILLMChunk) => void,
|
|
45
|
+
end: () => void = () => { },
|
|
46
|
+
debugVars: any = {},
|
|
47
|
+
tools?: IAITool[]
|
|
48
|
+
): Promise<any> {
|
|
49
|
+
promptInstance.setSentInput(promptInstance.getInput());
|
|
50
|
+
return executor.promptStream(promptInstance as any, read, end, { debugVars, tools });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './types';
|
|
2
|
+
export { ToolManager } from './tool-manager';
|
|
3
|
+
export { InputOutputManager } from './input-output-manager';
|
|
4
|
+
export { ModelExecutionManager } from './model-execution-manager';
|
|
5
|
+
export { VariableStorageManager } from './variable-storage-manager';
|
|
6
|
+
export { ExecutionMethodsHandler, IPromptInstance } from './execution-methods-handler';
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { CompoundInput, IPromptEnchantment, IRWSHistoryMessage } from './types';
|
|
2
|
+
|
|
3
|
+
export class InputOutputManager {
|
|
4
|
+
private input: CompoundInput[] = [];
|
|
5
|
+
private enhancedInput: IPromptEnchantment[] = [];
|
|
6
|
+
private sentInput: CompoundInput[] = [];
|
|
7
|
+
private originalInput: CompoundInput[] = [];
|
|
8
|
+
private output: string = '';
|
|
9
|
+
private onStream: (chunk: string) => void = () => {};
|
|
10
|
+
|
|
11
|
+
constructor(input: CompoundInput[]) {
|
|
12
|
+
this.input = input;
|
|
13
|
+
this.originalInput = input;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
addInput(content: CompoundInput): void {
|
|
17
|
+
this.input.push(content);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
addEnchantment(enchantment: IPromptEnchantment): void {
|
|
21
|
+
this.enhancedInput.push(enchantment);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getEnchantedInputs(): IPromptEnchantment[] {
|
|
25
|
+
return this.enhancedInput;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
readSentInput(): CompoundInput[] {
|
|
29
|
+
return this.sentInput;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setSentInput(input: CompoundInput[]): void {
|
|
33
|
+
this.sentInput = input;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
readInput(): CompoundInput[] {
|
|
37
|
+
const enchantedInput: CompoundInput[] = this.enhancedInput.map(enchantment => ({
|
|
38
|
+
role: 'user',
|
|
39
|
+
type: enchantment.input.type,
|
|
40
|
+
text: enchantment.input.text
|
|
41
|
+
}));
|
|
42
|
+
return [...enchantedInput, ...this.input];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
readBaseInput(): CompoundInput[] {
|
|
46
|
+
return this.originalInput;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setBaseInput(input: CompoundInput[]): void {
|
|
50
|
+
this.originalInput = input;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
injestOutput(content: string): void {
|
|
54
|
+
this.output = content;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
readOutput(): string {
|
|
58
|
+
return this.output;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
listen(source: string, stream: boolean = true): void {
|
|
62
|
+
this.output = '';
|
|
63
|
+
|
|
64
|
+
if (!stream) {
|
|
65
|
+
this.output = source;
|
|
66
|
+
} else {
|
|
67
|
+
this.output += source;
|
|
68
|
+
this.onStream(source);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
setStreamCallback(callback: (chunk: string) => void): void {
|
|
73
|
+
this.onStream = callback;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
addHistory(messages: IRWSHistoryMessage[], historyPrompt: string, callback?: (messages: IRWSHistoryMessage[], prompt: string) => void): void {
|
|
77
|
+
const prompt = `
|
|
78
|
+
${messages.map(message => `
|
|
79
|
+
${message.creator}: ${message.content}
|
|
80
|
+
`).join('\n\n')}
|
|
81
|
+
${historyPrompt}
|
|
82
|
+
`;
|
|
83
|
+
|
|
84
|
+
if (callback) {
|
|
85
|
+
callback(messages, prompt);
|
|
86
|
+
} else {
|
|
87
|
+
this.input = [{ type: 'text', text: prompt }, ...this.input];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async readStreamAsText(readableStream: ReadableStream, callback: (txt: string) => void): Promise<void> {
|
|
92
|
+
const reader = readableStream.getReader();
|
|
93
|
+
let readResult: any;
|
|
94
|
+
|
|
95
|
+
// Continuously read from the stream
|
|
96
|
+
while (!(readResult = await reader.read()).done) {
|
|
97
|
+
if (readResult.value && readResult.value.response) {
|
|
98
|
+
// Emit each chunk text as it's read
|
|
99
|
+
callback(readResult.value.response);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Getters for state access
|
|
105
|
+
getInput(): CompoundInput[] {
|
|
106
|
+
return this.input;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getEnhancedInput(): IPromptEnchantment[] {
|
|
110
|
+
return this.enhancedInput;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getOriginalInput(): CompoundInput[] {
|
|
114
|
+
return this.originalInput;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
getOutput(): string {
|
|
118
|
+
return this.output;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PromptTemplate,
|
|
3
|
+
EmbedLoader,
|
|
4
|
+
BaseChatModel,
|
|
5
|
+
IPromptHyperParameters,
|
|
6
|
+
IRWSPromptRequestExecutor,
|
|
7
|
+
IRWSSinglePromptRequestExecutor,
|
|
8
|
+
IRWSPromptStreamExecutor,
|
|
9
|
+
ILLMChunk,
|
|
10
|
+
IAITool,
|
|
11
|
+
ChainStreamType
|
|
12
|
+
} from './types';
|
|
13
|
+
|
|
14
|
+
export class ModelExecutionManager {
|
|
15
|
+
private modelId: string;
|
|
16
|
+
private modelType: string;
|
|
17
|
+
private multiTemplate: PromptTemplate;
|
|
18
|
+
private embedLoader: EmbedLoader<any>;
|
|
19
|
+
private hyperParameters: IPromptHyperParameters;
|
|
20
|
+
public _stream: ChainStreamType;
|
|
21
|
+
|
|
22
|
+
constructor(modelId: string, modelType: string, hyperParameters: IPromptHyperParameters) {
|
|
23
|
+
this.modelId = modelId;
|
|
24
|
+
this.modelType = modelType;
|
|
25
|
+
this.hyperParameters = hyperParameters;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getModelId(): string {
|
|
29
|
+
return this.modelId;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getModelMetadata(): [string, string] {
|
|
33
|
+
return [this.modelType, this.modelId];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setMultiTemplate(template: PromptTemplate): void {
|
|
37
|
+
this.multiTemplate = template;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getMultiTemplate(): PromptTemplate {
|
|
41
|
+
return this.multiTemplate;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setEmbedLoader(embedLoader: EmbedLoader<BaseChatModel>): void {
|
|
45
|
+
this.embedLoader = embedLoader;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getEmbedLoader<T extends BaseChatModel>(): EmbedLoader<T> {
|
|
49
|
+
return this.embedLoader;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getHyperParameters<T = any>(base: any = null): T {
|
|
53
|
+
if (base) {
|
|
54
|
+
this.hyperParameters = { ...base, ...this.hyperParameters };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return this.hyperParameters as T;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
getHyperParameter<T>(key: string): T {
|
|
61
|
+
if (!this.hyperParameters[key]) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return this.hyperParameters[key] as T;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
setHyperParameter(key: string, value: any): void {
|
|
69
|
+
this.hyperParameters[key] = value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
setHyperParameters(value: any): void {
|
|
73
|
+
this.hyperParameters = value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
replacePromptVar(key: string, val: string): void {
|
|
77
|
+
// Implementation placeholder
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private async isChainStreamType(source: any): Promise<boolean> {
|
|
81
|
+
if (source && typeof source[Symbol.asyncIterator] === 'function') {
|
|
82
|
+
const asyncIterator = source[Symbol.asyncIterator]();
|
|
83
|
+
if (typeof asyncIterator.next === 'function' &&
|
|
84
|
+
typeof asyncIterator.throw === 'function' &&
|
|
85
|
+
typeof asyncIterator.return === 'function') {
|
|
86
|
+
try {
|
|
87
|
+
// Optionally check if the next method yields a value of the expected type
|
|
88
|
+
const { value, done } = await asyncIterator.next();
|
|
89
|
+
return !done && value instanceof ReadableStream; // or whatever check makes sense for IterableReadableStream<ChainValues>
|
|
90
|
+
} catch (error) {
|
|
91
|
+
// Handle or ignore error
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Getters for state access
|
|
99
|
+
getModelType(): string {
|
|
100
|
+
return this.modelType;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
getEmbedLoaderId(): string {
|
|
104
|
+
return this.embedLoader?.getId() || null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
getAllHyperParameters(): IPromptHyperParameters {
|
|
108
|
+
return this.hyperParameters;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ModuleRef } from '@nestjs/core';
|
|
2
|
+
import { IAITool, ToolHandler, IToolCall } from './types';
|
|
3
|
+
|
|
4
|
+
export class ToolManager {
|
|
5
|
+
private toolHandlers: Map<string, ToolHandler> = new Map();
|
|
6
|
+
private tools: IAITool[] = [];
|
|
7
|
+
|
|
8
|
+
setTools(tools: IAITool[]): void {
|
|
9
|
+
this.tools = tools;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getTools(): IAITool[] {
|
|
13
|
+
return this.tools;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getTool(key: string): { definition: IAITool, handler: ToolHandler } | null {
|
|
17
|
+
const foundTool = this.tools.find(tool => tool.name === key);
|
|
18
|
+
|
|
19
|
+
if (foundTool) {
|
|
20
|
+
return {
|
|
21
|
+
definition: foundTool,
|
|
22
|
+
handler: this.toolHandlers.get(foundTool.name)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
registerToolHandlers(toolHandlers: { [key: string]: ToolHandler }): void {
|
|
30
|
+
for (const key of Object.keys(toolHandlers)) {
|
|
31
|
+
this.toolHandlers.set(key, toolHandlers[key]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async callTools<T = unknown, O = unknown>(tools: IToolCall[], moduleRef: ModuleRef, aiToolOptions?: O): Promise<T[]> {
|
|
36
|
+
const results: T[] = [];
|
|
37
|
+
for (const tool of tools) {
|
|
38
|
+
if (this.toolHandlers.has(tool.function.name)) {
|
|
39
|
+
const result = await this.callAiTool<T, O>(tool, moduleRef, aiToolOptions);
|
|
40
|
+
if (result) {
|
|
41
|
+
results.push(result);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private async callAiTool<T, O>(tool: IToolCall, moduleRef: ModuleRef, aiToolOptions?: O): Promise<T> {
|
|
50
|
+
const handler = this.toolHandlers.get(tool.function.name);
|
|
51
|
+
return await handler(tool.function.arguments, moduleRef, aiToolOptions);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getToolHandlers(): Map<string, ToolHandler> {
|
|
55
|
+
return this.toolHandlers;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Re-export commonly used types from IPrompt
|
|
2
|
+
export {
|
|
3
|
+
IPromptSender,
|
|
4
|
+
IPromptEnchantment,
|
|
5
|
+
IPromptParams,
|
|
6
|
+
IPromptHyperParameters,
|
|
7
|
+
IRWSPromptRequestExecutor,
|
|
8
|
+
IRWSPromptStreamExecutor,
|
|
9
|
+
IRWSSinglePromptRequestExecutor,
|
|
10
|
+
IRWSPromptJSON,
|
|
11
|
+
ChainStreamType,
|
|
12
|
+
ILLMChunk,
|
|
13
|
+
IAITool,
|
|
14
|
+
IRWSHistoryMessage,
|
|
15
|
+
InputType,
|
|
16
|
+
CompoundInput,
|
|
17
|
+
ToolHandler,
|
|
18
|
+
IToolCall
|
|
19
|
+
} from '../../../types/IPrompt';
|
|
20
|
+
|
|
21
|
+
export { EmbedLoader, IChainCallOutput } from '../../convo/EmbedLoader';
|
|
22
|
+
export { PromptTemplate } from '@langchain/core/prompts';
|
|
23
|
+
export { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
24
|
+
export { ModuleRef } from '@nestjs/core';
|
|
25
|
+
export { IContextToken } from '../../../types/IContextToken';
|
|
26
|
+
|
|
27
|
+
import {
|
|
28
|
+
CompoundInput,
|
|
29
|
+
IPromptEnchantment,
|
|
30
|
+
IPromptHyperParameters,
|
|
31
|
+
ToolHandler,
|
|
32
|
+
IAITool
|
|
33
|
+
} from '../../../types/IPrompt';
|
|
34
|
+
import { PromptTemplate } from '@langchain/core/prompts';
|
|
35
|
+
import { EmbedLoader } from '../../convo/EmbedLoader';
|
|
36
|
+
|
|
37
|
+
export type EntryParams = {
|
|
38
|
+
modelId: string;
|
|
39
|
+
body: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export interface IRWSPromptState {
|
|
43
|
+
input: CompoundInput[];
|
|
44
|
+
enhancedInput: IPromptEnchantment[];
|
|
45
|
+
sentInput: CompoundInput[];
|
|
46
|
+
originalInput: CompoundInput[];
|
|
47
|
+
output: string;
|
|
48
|
+
modelId: string;
|
|
49
|
+
modelType: string;
|
|
50
|
+
multiTemplate: PromptTemplate;
|
|
51
|
+
embedLoader: EmbedLoader<any>;
|
|
52
|
+
hyperParameters: IPromptHyperParameters;
|
|
53
|
+
created_at: Date;
|
|
54
|
+
toolHandlers: Map<string, ToolHandler>;
|
|
55
|
+
varStorage: any;
|
|
56
|
+
tools: IAITool[];
|
|
57
|
+
onStream: (chunk: string) => void;
|
|
58
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class VariableStorageManager {
|
|
2
|
+
private varStorage: any = {};
|
|
3
|
+
|
|
4
|
+
getVar<T>(key: string): T {
|
|
5
|
+
return Object.keys(this.varStorage).includes(key) ? this.varStorage[key] : null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
setVar<T>(key: string, val: T): void {
|
|
9
|
+
this.varStorage[key] = val;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
clearVar(key: string): void {
|
|
13
|
+
delete this.varStorage[key];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
clearAllVars(): void {
|
|
17
|
+
this.varStorage = {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getAllVars(): any {
|
|
21
|
+
return { ...this.varStorage };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
hasVar(key: string): boolean {
|
|
25
|
+
return Object.keys(this.varStorage).includes(key);
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/types/IPrompt.ts
CHANGED
|
@@ -36,8 +36,12 @@ interface IAIToolSchema {
|
|
|
36
36
|
|
|
37
37
|
interface IToolCall {
|
|
38
38
|
id: string;
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
index: number;
|
|
40
|
+
type: string;
|
|
41
|
+
function: {
|
|
42
|
+
name: string;
|
|
43
|
+
arguments: string | Record<string, any>
|
|
44
|
+
};
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
interface IAITool {
|