-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
chat_models.ts
377 lines (341 loc) Β· 10.5 KB
/
chat_models.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
import { CohereClient, Cohere } from "cohere-ai";
import {
MessageType,
type BaseMessage,
MessageContent,
AIMessage,
} from "@langchain/core/messages";
import { type BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
type BaseChatModelParams,
BaseChatModel,
} from "@langchain/core/language_models/chat_models";
import {
ChatGeneration,
ChatGenerationChunk,
ChatResult,
} from "@langchain/core/outputs";
import { AIMessageChunk } from "@langchain/core/messages";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { NewTokenIndices } from "@langchain/core/callbacks/base";
/**
* Input interface for ChatCohere
*/
export interface ChatCohereInput extends BaseChatModelParams {
/**
* The API key to use.
* @default {process.env.COHERE_API_KEY}
*/
apiKey?: string;
/**
* The name of the model to use.
* @default {"command"}
*/
model?: string;
/**
* What sampling temperature to use, between 0.0 and 2.0.
* Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
* @default {0.3}
*/
temperature?: number;
/**
* Whether or not to stream the response.
* @default {false}
*/
streaming?: boolean;
}
interface TokenUsage {
completionTokens?: number;
promptTokens?: number;
totalTokens?: number;
}
interface CohereChatCallOptions
extends BaseLanguageModelCallOptions,
Partial<Omit<Cohere.ChatRequest, "message">>,
Partial<Omit<Cohere.ChatStreamRequest, "message">> {}
function convertMessagesToCohereMessages(
messages: Array<BaseMessage>
): Array<Cohere.ChatMessage> {
const getRole = (role: MessageType) => {
switch (role) {
case "system":
return "SYSTEM";
case "human":
return "USER";
case "ai":
return "CHATBOT";
default:
throw new Error(
`Unknown message type: '${role}'. Accepted types: 'human', 'ai', 'system'`
);
}
};
const getContent = (content: MessageContent): string => {
if (typeof content === "string") {
return content;
}
throw new Error(
`ChatCohere does not support non text message content. Received: ${JSON.stringify(
content,
null,
2
)}`
);
};
return messages.map((message) => ({
role: getRole(message._getType()),
message: getContent(message.content),
}));
}
/**
* Integration with ChatCohere
* @example
* ```typescript
* const model = new ChatCohere({
* apiKey: process.env.COHERE_API_KEY, // Default
* model: "command" // Default
* });
* const response = await model.invoke([
* new HumanMessage("How tall are the largest pengiuns?")
* ]);
* ```
*/
export class ChatCohere<
CallOptions extends CohereChatCallOptions = CohereChatCallOptions
>
extends BaseChatModel<CallOptions>
implements ChatCohereInput
{
static lc_name() {
return "ChatCohere";
}
lc_serializable = true;
client: CohereClient;
model = "command";
temperature = 0.3;
streaming = false;
constructor(fields?: ChatCohereInput) {
super(fields ?? {});
const token = fields?.apiKey ?? getEnvironmentVariable("COHERE_API_KEY");
if (!token) {
throw new Error("No API key provided for ChatCohere.");
}
this.client = new CohereClient({
token,
});
this.model = fields?.model ?? this.model;
this.temperature = fields?.temperature ?? this.temperature;
this.streaming = fields?.streaming ?? this.streaming;
}
_llmType() {
return "cohere";
}
invocationParams(options: this["ParsedCallOptions"]) {
const params = {
model: this.model,
preamble: options.preamble,
conversationId: options.conversationId,
promptTruncation: options.promptTruncation,
connectors: options.connectors,
searchQueriesOnly: options.searchQueriesOnly,
documents: options.documents,
temperature: options.temperature ?? this.temperature,
};
// Filter undefined entries
return Object.fromEntries(
Object.entries(params).filter(([, value]) => value !== undefined)
);
}
/** @ignore */
async _generate(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<ChatResult> {
const tokenUsage: TokenUsage = {};
const params = this.invocationParams(options);
const cohereMessages = convertMessagesToCohereMessages(messages);
// The last message in the array is the most recent, all other messages
// are apart of the chat history.
const { message } = cohereMessages[cohereMessages.length - 1];
const chatHistory: Cohere.ChatMessage[] = [];
if (cohereMessages.length > 1) {
chatHistory.push(...cohereMessages.slice(0, -1));
}
const input = {
...params,
message,
chatHistory,
};
// Handle streaming
if (this.streaming) {
const stream = this._streamResponseChunks(messages, options, runManager);
const finalChunks: Record<number, ChatGenerationChunk> = {};
for await (const chunk of stream) {
const index =
(chunk.generationInfo as NewTokenIndices)?.completion ?? 0;
if (finalChunks[index] === undefined) {
finalChunks[index] = chunk;
} else {
finalChunks[index] = finalChunks[index].concat(chunk);
}
}
const generations = Object.entries(finalChunks)
.sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
.map(([_, value]) => value);
return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };
}
// Not streaming, so we can just call the API once.
const response: Cohere.NonStreamedChatResponse =
await this.caller.callWithOptions(
{ signal: options.signal },
async () => {
let response;
try {
response = await this.client.chat(input);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
e.status = e.status ?? e.statusCode;
throw e;
}
return response;
}
);
if ("token_count" in response) {
const {
response_tokens: completionTokens,
prompt_tokens: promptTokens,
total_tokens: totalTokens,
} = response.token_count as Record<string, number>;
if (completionTokens) {
tokenUsage.completionTokens =
(tokenUsage.completionTokens ?? 0) + completionTokens;
}
if (promptTokens) {
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
}
if (totalTokens) {
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
}
}
const generationInfo: Record<string, unknown> = { ...response };
delete generationInfo.text;
const generations: ChatGeneration[] = [
{
text: response.text,
message: new AIMessage({
content: response.text,
additional_kwargs: generationInfo,
}),
generationInfo,
},
];
return {
generations,
llmOutput: { estimatedTokenUsage: tokenUsage },
};
}
async *_streamResponseChunks(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
const params = this.invocationParams(options);
const cohereMessages = convertMessagesToCohereMessages(messages);
// The last message in the array is the most recent, all other messages
// are apart of the chat history.
const { message } = cohereMessages[cohereMessages.length - 1];
const chatHistory: Cohere.ChatMessage[] = [];
if (cohereMessages.length > 1) {
chatHistory.push(...cohereMessages.slice(0, -1));
}
const input = {
...params,
message,
chatHistory,
};
// All models have a built in `this.caller` property for retries
const stream = await this.caller.call(async () => {
let stream;
try {
stream = await this.client.chatStream(input);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
e.status = e.status ?? e.statusCode;
throw e;
}
return stream;
});
for await (const chunk of stream) {
if (chunk.eventType === "text-generation") {
yield new ChatGenerationChunk({
text: chunk.text,
message: new AIMessageChunk({ content: chunk.text }),
});
await runManager?.handleLLMNewToken(chunk.text);
} else if (chunk.eventType !== "stream-end") {
// Used for when the user uses their RAG/Search/other API
// and the stream takes more actions then just text generation.
yield new ChatGenerationChunk({
text: "",
message: new AIMessageChunk({
content: "",
additional_kwargs: {
...chunk,
},
}),
generationInfo: {
...chunk,
},
});
}
}
}
/** @ignore */
_combineLLMOutput(...llmOutputs: CohereLLMOutput[]): CohereLLMOutput {
return llmOutputs.reduce<{
[key in keyof CohereLLMOutput]: Required<CohereLLMOutput[key]>;
}>(
(acc, llmOutput) => {
if (llmOutput && llmOutput.estimatedTokenUsage) {
let completionTokens = acc.estimatedTokenUsage?.completionTokens ?? 0;
let promptTokens = acc.estimatedTokenUsage?.promptTokens ?? 0;
let totalTokens = acc.estimatedTokenUsage?.totalTokens ?? 0;
completionTokens +=
llmOutput.estimatedTokenUsage.completionTokens ?? 0;
promptTokens += llmOutput.estimatedTokenUsage.promptTokens ?? 0;
totalTokens += llmOutput.estimatedTokenUsage.totalTokens ?? 0;
acc.estimatedTokenUsage = {
completionTokens,
promptTokens,
totalTokens,
};
}
return acc;
},
{
estimatedTokenUsage: {
completionTokens: 0,
promptTokens: 0,
totalTokens: 0,
},
}
);
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
apiKey: "COHERE_API_KEY",
api_key: "COHERE_API_KEY",
};
}
get lc_aliases(): { [key: string]: string } | undefined {
return {
apiKey: "cohere_api_key",
api_key: "cohere_api_key",
};
}
}
interface CohereLLMOutput {
estimatedTokenUsage?: TokenUsage;
}