-
Notifications
You must be signed in to change notification settings - Fork 337
/
replicate_ai.ts
378 lines (340 loc) · 12.3 KB
/
replicate_ai.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { getEnv } from "@llamaindex/env";
import Replicate from "../internal/deps/replicate.js";
import { BaseLLM } from "./base.js";
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
MessageType,
} from "./types.js";
import {
extractText,
streamCallbacks,
streamConverter,
wrapLLMEvent,
} from "./utils.js";
export class ReplicateSession {
replicateKey: string | null = null;
replicate: Replicate;
constructor(replicateKey: string | null = null) {
if (replicateKey) {
this.replicateKey = replicateKey;
} else if (getEnv("REPLICATE_API_TOKEN")) {
this.replicateKey = getEnv("REPLICATE_API_TOKEN") as string;
} else {
throw new Error(
"Set Replicate token in REPLICATE_API_TOKEN env variable",
);
}
this.replicate = new Replicate({ auth: this.replicateKey });
}
}
export const ALL_AVAILABLE_REPLICATE_MODELS = {
// TODO: add more models from replicate
"Llama-2-70b-chat-old": {
contextWindow: 4096,
replicateApi:
"replicate/llama70b-v2-chat:e951f18578850b652510200860fc4ea62b3b16fac280f83ff32282f87bbd2e48",
//^ Previous 70b model. This is also actually 4 bit, although not exllama.
},
"Llama-2-70b-chat-4bit": {
contextWindow: 4096,
replicateApi:
"meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
//^ Model is based off of exllama 4bit.
},
"Llama-2-13b-chat-old": {
contextWindow: 4096,
replicateApi:
"a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5",
},
//^ Last known good 13b non-quantized model. In future versions they add the SYS and INST tags themselves
"Llama-2-13b-chat-4bit": {
contextWindow: 4096,
replicateApi:
"meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d",
},
"Llama-2-7b-chat-old": {
contextWindow: 4096,
replicateApi:
"a16z-infra/llama7b-v2-chat:4f0a4744c7295c024a1de15e1a63c880d3da035fa1f49bfd344fe076074c8eea",
//^ Last (somewhat) known good 7b non-quantized model. In future versions they add the SYS and INST
// tags themselves
// https://github.com/replicate/cog-llama-template/commit/fa5ce83912cf82fc2b9c01a4e9dc9bff6f2ef137
// Problem is that they fix the max_new_tokens issue in the same commit. :-(
},
"Llama-2-7b-chat-4bit": {
contextWindow: 4096,
replicateApi:
"meta/llama-2-7b-chat:13c3cdee13ee059ab779f0291d29054dab00a47dad8261375654de5540165fb0",
},
"llama-3-70b-instruct": {
contextWindow: 8192,
replicateApi: "meta/meta-llama-3-70b-instruct",
},
"llama-3-8b-instruct": {
contextWindow: 8192,
replicateApi: "meta/meta-llama-3-8b-instruct",
},
};
export enum ReplicateChatStrategy {
A16Z = "a16z",
META = "meta",
METAWBOS = "metawbos",
//^ This is not exactly right because SentencePiece puts the BOS and EOS token IDs in after tokenization
// Unfortunately any string only API won't support these properly.
REPLICATE4BIT = "replicate4bit",
//^ To satisfy Replicate's 4 bit models' requirements where they also insert some INST tags
REPLICATE4BITWNEWLINES = "replicate4bitwnewlines",
//^ Replicate's documentation recommends using newlines: https://replicate.com/blog/how-to-prompt-llama
LLAMA3 = "llama3",
}
export const DeuceChatStrategy = ReplicateChatStrategy;
/**
* Replicate LLM implementation used
*/
export class ReplicateLLM extends BaseLLM {
model: keyof typeof ALL_AVAILABLE_REPLICATE_MODELS;
chatStrategy: ReplicateChatStrategy;
temperature: number;
topP: number;
maxTokens?: number;
replicateSession: ReplicateSession;
constructor(init?: Partial<ReplicateLLM> & { noWarn?: boolean }) {
super();
if (!init?.model && !init?.noWarn) {
console.warn(
"The default model has been changed to llama-3-70b-instruct. Set noWarn to true to suppress this warning.",
);
}
this.model = init?.model ?? "llama-3-70b-instruct";
this.chatStrategy =
init?.chatStrategy ??
(this.model.startsWith("llama-3")
? ReplicateChatStrategy.LLAMA3
: this.model.endsWith("4bit")
? ReplicateChatStrategy.REPLICATE4BITWNEWLINES // With the newer Replicate models they do the system message themselves.
: ReplicateChatStrategy.METAWBOS); // With BOS and EOS seems to work best, although they all have problems past a certain point
this.temperature = init?.temperature ?? 0.1; // minimum temperature is 0.01 for Replicate endpoint
this.topP = init?.topP ?? (this.model.startsWith("llama-3") ? 0.9 : 1); // llama-3 defaults to 0.9 top P
this.maxTokens =
init?.maxTokens ??
ALL_AVAILABLE_REPLICATE_MODELS[this.model].contextWindow; // For Replicate, the default is 500 tokens which is too low.
this.replicateSession = init?.replicateSession ?? new ReplicateSession();
}
get metadata() {
return {
model: this.model,
temperature: this.temperature,
topP: this.topP,
maxTokens: this.maxTokens,
contextWindow: ALL_AVAILABLE_REPLICATE_MODELS[this.model].contextWindow,
tokenizer: undefined,
};
}
mapMessagesToPrompt(messages: ChatMessage[]) {
if (this.chatStrategy === ReplicateChatStrategy.LLAMA3) {
return this.mapMessagesToPromptLlama3(messages);
} else if (this.chatStrategy === ReplicateChatStrategy.A16Z) {
return this.mapMessagesToPromptA16Z(messages);
} else if (this.chatStrategy === ReplicateChatStrategy.META) {
return this.mapMessagesToPromptMeta(messages);
} else if (this.chatStrategy === ReplicateChatStrategy.METAWBOS) {
return this.mapMessagesToPromptMeta(messages, { withBos: true });
} else if (this.chatStrategy === ReplicateChatStrategy.REPLICATE4BIT) {
return this.mapMessagesToPromptMeta(messages, {
replicate4Bit: true,
withNewlines: true,
});
} else if (
this.chatStrategy === ReplicateChatStrategy.REPLICATE4BITWNEWLINES
) {
return this.mapMessagesToPromptMeta(messages, {
replicate4Bit: true,
withNewlines: true,
});
} else {
return this.mapMessagesToPromptMeta(messages);
}
}
mapMessagesToPromptLlama3(messages: ChatMessage[]) {
return {
prompt:
"<|begin_of_text|>" +
messages.reduce((acc, message) => {
let content = "";
if (typeof message.content === "string") {
content = message.content;
} else {
if (message.content[0].type === "text") {
content = message.content[0].text;
} else {
content = "";
}
}
return (
acc +
`<|start_header_id|>${message.role}<|end_header_id|>\n\n${content}<|eot_id|>`
);
}, "") +
"<|start_header_id|>assistant<|end_header_id|>\n\n",
systemPrompt: undefined,
};
}
mapMessagesToPromptA16Z(messages: ChatMessage[]) {
return {
prompt:
messages.reduce((acc, message) => {
return (
(acc && `${acc}\n\n`) +
`${this.mapMessageTypeA16Z(message.role)}${message.content}`
);
}, "") + "\n\nAssistant:",
//^ Here we're differing from A16Z by omitting the space. Generally spaces at the end of prompts decrease performance due to tokenization
systemPrompt: undefined,
};
}
mapMessageTypeA16Z(messageType: MessageType): string {
switch (messageType) {
case "user":
return "User: ";
case "assistant":
return "Assistant: ";
case "system":
return "";
default:
throw new Error("Unsupported ReplicateLLM message type");
}
}
mapMessagesToPromptMeta(
messages: ChatMessage[],
opts?: {
withBos?: boolean;
replicate4Bit?: boolean;
withNewlines?: boolean;
},
) {
const {
withBos = false,
replicate4Bit = false,
withNewlines = false,
} = opts ?? {};
const DEFAULT_SYSTEM_PROMPT = `You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.`;
const B_SYS = "<<SYS>>\n";
const E_SYS = "\n<</SYS>>\n\n";
const B_INST = "[INST]";
const E_INST = "[/INST]";
const BOS = "<s>";
const EOS = "</s>";
if (messages.length === 0) {
return { prompt: "", systemPrompt: undefined };
}
messages = [...messages]; // so we can use shift without mutating the original array
let systemPrompt = undefined;
if (messages[0].role === "system") {
const systemMessage = messages.shift()!;
if (replicate4Bit) {
systemPrompt = systemMessage.content;
} else {
const systemStr = `${B_SYS}${systemMessage.content}${E_SYS}`;
// TS Bug: https://github.com/microsoft/TypeScript/issues/9998
// @ts-ignore
if (messages[0].role !== "user") {
throw new Error(
"ReplicateLLM: if there is a system message, the second message must be a user message.",
);
}
const userContent = messages[0].content;
messages[0].content = `${systemStr}${userContent}`;
}
} else {
if (!replicate4Bit) {
messages[0].content = `${B_SYS}${DEFAULT_SYSTEM_PROMPT}${E_SYS}${messages[0].content}`;
}
}
return {
prompt: messages.reduce((acc, message, index) => {
const content = extractText(message.content);
if (index % 2 === 0) {
return (
`${acc}${withBos ? BOS : ""}${B_INST} ${content.trim()} ${E_INST}` +
(withNewlines ? "\n" : "")
);
} else {
return (
`${acc} ${content.trim()}` +
(withNewlines ? "\n" : " ") +
(withBos ? EOS : "")
); // Yes, the EOS comes after the space. This is not a mistake.
}
}, ""),
systemPrompt,
};
}
chat(
params: LLMChatParamsStreaming,
): Promise<AsyncIterable<ChatResponseChunk>>;
chat(params: LLMChatParamsNonStreaming): Promise<ChatResponse>;
@wrapLLMEvent
async chat(
params: LLMChatParamsNonStreaming | LLMChatParamsStreaming,
): Promise<ChatResponse | AsyncIterable<ChatResponseChunk>> {
const { messages, stream } = params;
const api = ALL_AVAILABLE_REPLICATE_MODELS[this.model]
.replicateApi as `${string}/${string}:${string}`;
const { prompt, systemPrompt } = this.mapMessagesToPrompt(messages);
const replicateOptions: any = {
input: {
prompt,
system_prompt: systemPrompt,
temperature: this.temperature,
top_p: this.topP,
},
};
if (this.model.endsWith("4bit")) {
replicateOptions.input.max_new_tokens = this.maxTokens;
} else {
replicateOptions.input.max_length = this.maxTokens;
}
if (this.model.startsWith("llama-3")) {
replicateOptions.input.prompt_template = "{prompt}";
}
if (stream) {
const controller = new AbortController();
const stream = this.replicateSession.replicate.stream(api, {
...replicateOptions,
signal: controller.signal,
});
// replicate.stream is not closing if used as AsyncIterable, force closing after consumption with the AbortController
return streamCallbacks(
streamConverter(stream, (chunk) => {
if (chunk.event === "done") {
return null;
}
return {
raw: chunk,
delta: chunk.data,
};
}),
{ finished: () => controller.abort() },
);
}
//Non-streaming
const response = await this.replicateSession.replicate.run(
api,
replicateOptions,
);
return {
raw: response,
message: {
content: (response as Array<string>).join("").trimStart(),
//^ We need to do this because Replicate returns a list of strings (for streaming functionality which is not exposed by the run function)
role: "assistant",
},
};
}
}
export const LlamaDeuce = ReplicateLLM;