forked from aws-samples/aws-genai-llm-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
240 lines (198 loc) · 7.26 KB
/
base.ts
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
import { SageMakerEndpoint, SageMakerLLMContentHandler } from 'langchain/llms/sagemaker_endpoint';
import { ChatMode, CompleteArgs, CompleteParams, ErrorArgs, GetPromptArgs, SessionHistoryItem, StreamArgs } from '../types';
const LARGE_LANGUAGE_MODELS = JSON.parse(process.env.LARGE_LANGUAGE_MODELS || '{}');
const MAX_ITERATIONS = 10000;
const MAX_GENERATION_LENGTH = 4096;
export abstract class ModelAdapterBase {
protected readonly model: SageMakerEndpoint;
protected readonly endpointName: string;
protected readonly modelId: string;
protected readonly mode: string | unknown;
private _onStream: (arg0: StreamArgs) => Promise<void>;
private _onComplete: (arg0: CompleteArgs) => Promise<void>;
private _onError: (arg0: ErrorArgs) => Promise<void>;
private _shouldStopGeneration: () => Promise<boolean>;
private _onStoppedGeneration: () => Promise<void>;
constructor(
protected _config: {
modelId: string;
modelKwargs: Record<string, unknown>;
},
) {
this.modelId = this._config.modelId;
this.mode = this._config.modelKwargs?.mode || ChatMode.Streaming;
this.endpointName = LARGE_LANGUAGE_MODELS[this.modelId];
this.model = new SageMakerEndpoint({
endpointName: this.endpointName,
contentHandler: this.getContentHandler(),
modelKwargs: this._config.modelKwargs,
maxRetries: 2,
clientOptions: {
region: process.env.AWS_REGION,
},
});
}
abstract getPrompt(args0: GetPromptArgs): Promise<string>;
abstract getContentHandler(): SageMakerLLMContentHandler;
abstract getStopWords(): Promise<string[]>;
public async onStream(fn: (arg0: StreamArgs) => Promise<void>) {
this._onStream = fn;
}
public async onComplete(fn: (arg0: CompleteArgs) => Promise<void>) {
this._onComplete = fn;
}
public async onError(onError: (arg0: ErrorArgs) => Promise<void>) {
this._onError = onError;
}
public async shouldStopGeneration(fn: () => Promise<boolean>) {
this._shouldStopGeneration = fn;
}
public async onStoppedGeneration(fn: () => Promise<void>) {
this._onStoppedGeneration = fn;
}
public async complete({ prompt, context, history }: CompleteParams) {
const completion = {
generatedText: '',
};
try {
await this._complete({ prompt, history, context, completion });
} catch (error) {
console.log(`Error: ${error}`);
await this._onError({ error: `${error}` });
} finally {
await this._onComplete({ prompt, history, generatedText: completion.generatedText });
}
}
private async _complete({ prompt, history, context, completion }: GetPromptArgs): Promise<void> {
console.log(`Calling complete with prompt: ${prompt}`);
console.log(`Model: ${this.modelId}`);
console.log(`Endpoint: ${this.endpointName}`);
console.log(`Mode: ${this.mode}`);
const initialPrompt = await this.getPrompt({ prompt, history, context, completion });
let _prompt = initialPrompt;
const timestamp = Date.now();
console.log(`Timestamp: ${timestamp}`);
// Streaming mode
if (this.mode === ChatMode.Streaming) {
console.log('Streaming mode enabled');
let isEndOfText = false;
let residual = '';
const stops = await this.getStopWords();
for (let i = 0; i < MAX_ITERATIONS; i++) {
if (await this._shouldStopGeneration()) {
console.log('Stopping generation');
await this._onStoppedGeneration();
break;
}
console.log(`_prompt: ${_prompt}`);
const message = await this.model.call(_prompt);
if (!message) {
console.log('No message returned');
break;
}
let currentIteration = message.replace(_prompt, '');
if (currentIteration.length === 0) {
break;
}
_prompt += currentIteration;
completion.generatedText += currentIteration;
currentIteration = residual + currentIteration;
residual = '';
let skipCnt = 0;
for (const stopSequence of stops) {
if (completion.generatedText.includes(stopSequence) || currentIteration.includes(stopSequence)) {
isEndOfText = true;
completion.generatedText = completion.generatedText.split(stopSequence)[0];
currentIteration = currentIteration.split(stopSequence)[0];
}
for (let i = 0; i < stopSequence.length; i++) {
if (currentIteration.endsWith(stopSequence.substring(0, i + 1))) {
skipCnt = Math.max(i + 1, skipCnt);
}
}
}
if (skipCnt > 0) {
residual = currentIteration.substring(currentIteration.length - skipCnt);
currentIteration = currentIteration.slice(0, -skipCnt);
}
if (currentIteration.length > 0) {
await this._onStream({
timestamp,
sender: 'system',
message: currentIteration,
});
}
console.log('length: ', completion.generatedText.length);
if (completion.generatedText.length >= MAX_GENERATION_LENGTH || isEndOfText) {
break;
}
}
} else {
// Standard mode
console.log('Standard mode enabled');
console.log('Calling model with context: ', _prompt);
const message = await this.model.call(_prompt);
const response = await this.isEndOfText(message.replace(_prompt, ''));
console.log('message: ', message);
console.log('response: ', response);
completion.generatedText = response.text;
await this._onStream({
timestamp,
sender: 'system',
message: completion.generatedText,
});
}
}
protected async isEndOfText(text: string) {
console.log(`Checking if end of text: ${text}`);
let minIndex = text.length;
let foundStop = false;
const stops = await this.getStopWords();
for (const word of stops) {
console.log(`Checking for word: ${word}`);
const index = text.indexOf(word);
console.log(`Index: ${index}`);
if (index >= 0 && index < minIndex) {
console.log(`Found stop word: ${word} at index: ${index}`);
minIndex = index;
foundStop = true;
}
}
const response = {
endOfText: foundStop,
text: text.slice(0, minIndex),
};
console.log(`Response: ${JSON.stringify(response)}`);
return response;
}
protected truncateArgs({ prompt, history, context }: GetPromptArgs, limit: number) {
const promptLength = prompt.length;
let residualLength = limit - promptLength;
if (residualLength <= 0) {
return {
prompt,
history: [],
contextString: '',
};
}
let contextString = context.length > 0 ? context[0] : '';
contextString = contextString.substring(0, residualLength);
residualLength -= contextString.length;
let truncatedHistoryLenght = 0;
const truncatedHistory: SessionHistoryItem[] = [];
for (let i = history.length - 1; i >= 0; i--) {
if (truncatedHistoryLenght + history[i].content.length > residualLength) {
break;
}
truncatedHistoryLenght += history[i].content.length;
truncatedHistory.push(history[i]);
}
truncatedHistory.reverse();
const retVlaue = {
prompt,
history: truncatedHistory,
contextString,
};
return retVlaue;
}
}