-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
anthropic.ts
455 lines (401 loc) Β· 12.3 KB
/
anthropic.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// TODO: Deprecate in favor of new Anthropic package once out of beta
import {
Anthropic,
AI_PROMPT,
HUMAN_PROMPT,
ClientOptions,
} from "@anthropic-ai/sdk";
import type { CompletionCreateParams } from "@anthropic-ai/sdk/resources/completions";
import type { Stream } from "@anthropic-ai/sdk/streaming";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
AIMessage,
AIMessageChunk,
type BaseMessage,
ChatMessage,
} from "@langchain/core/messages";
import {
type ChatGeneration,
ChatGenerationChunk,
type ChatResult,
} from "@langchain/core/outputs";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import {
BaseChatModel,
type BaseChatModelParams,
} from "@langchain/core/language_models/chat_models";
import { type BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
export { AI_PROMPT, HUMAN_PROMPT };
/**
* Extracts the custom role of a generic chat message.
* @param message The chat message from which to extract the custom role.
* @returns The custom role of the chat message.
*/
function extractGenericMessageCustomRole(message: ChatMessage) {
if (
message.role !== AI_PROMPT &&
message.role !== HUMAN_PROMPT &&
message.role !== ""
) {
console.warn(`Unknown message role: ${message.role}`);
}
return message.role;
}
/**
* Gets the Anthropic prompt from a base message.
* @param message The base message from which to get the Anthropic prompt.
* @returns The Anthropic prompt from the base message.
*/
function getAnthropicPromptFromMessage(message: BaseMessage): string {
const type = message._getType();
switch (type) {
case "ai":
return AI_PROMPT;
case "human":
return HUMAN_PROMPT;
case "system":
return "";
case "function":
return HUMAN_PROMPT;
case "generic": {
if (!ChatMessage.isInstance(message))
throw new Error("Invalid generic chat message");
return extractGenericMessageCustomRole(message);
}
default:
throw new Error(`Unknown message type: ${type}`);
}
}
export const DEFAULT_STOP_SEQUENCES = [HUMAN_PROMPT];
/**
* Input to AnthropicChat class.
* @deprecated Install and import from the "@langchain/anthropic" integration package instead.
*/
export interface AnthropicInput {
/** Amount of randomness injected into the response. Ranges
* from 0 to 1. Use temp closer to 0 for analytical /
* multiple choice, and temp closer to 1 for creative
* and generative tasks.
*/
temperature?: number;
/** Only sample from the top K options for each subsequent
* token. Used to remove "long tail" low probability
* responses. Defaults to -1, which disables it.
*/
topK?: number;
/** Does nucleus sampling, in which we compute the
* cumulative distribution over all the options for each
* subsequent token in decreasing probability order and
* cut it off once it reaches a particular probability
* specified by top_p. Defaults to -1, which disables it.
* Note that you should either alter temperature or top_p,
* but not both.
*/
topP?: number;
/** A maximum number of tokens to generate before stopping. */
maxTokensToSample: number;
/** A list of strings upon which to stop generating.
* You probably want `["\n\nHuman:"]`, as that's the cue for
* the next turn in the dialog agent.
*/
stopSequences?: string[];
/** Whether to stream the results or not */
streaming?: boolean;
/** Anthropic API key */
anthropicApiKey?: string;
/** Anthropic API URL */
anthropicApiUrl?: string;
/** Model name to use */
modelName: string;
/** Overridable Anthropic ClientOptions */
clientOptions: ClientOptions;
/** Holds any additional parameters that are valid to pass to {@link
* https://console.anthropic.com/docs/api/reference |
* `anthropic.complete`} that are not explicitly specified on this class.
*/
invocationKwargs?: Kwargs;
}
/**
* A type representing additional parameters that can be passed to the
* Anthropic API.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Kwargs = Record<string, any>;
/**
* @deprecated Install and import from the "@langchain/anthropic" integration package instead.
*
* Wrapper around Anthropic large language models.
*
* To use you should have the `@anthropic-ai/sdk` package installed, with the
* `ANTHROPIC_API_KEY` environment variable set.
*
* @remarks
* Any parameters that are valid to be passed to {@link
* https://console.anthropic.com/docs/api/reference |
* `anthropic.complete`} can be passed through {@link invocationKwargs},
* even if not explicitly available on this class.
* @example
* ```typescript
* const model = new ChatAnthropic({
* temperature: 0.9,
* anthropicApiKey: 'YOUR-API-KEY',
* });
* const res = await model.invoke({ input: 'Hello!' });
* console.log(res);
* ```
*/
export class ChatAnthropic<
CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions
>
extends BaseChatModel<CallOptions>
implements AnthropicInput
{
static lc_name() {
return "ChatAnthropic";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
anthropicApiKey: "ANTHROPIC_API_KEY",
};
}
get lc_aliases(): Record<string, string> {
return {
modelName: "model",
};
}
lc_serializable = true;
anthropicApiKey?: string;
apiUrl?: string;
temperature = 1;
topK = -1;
topP = -1;
maxTokensToSample = 2048;
modelName = "claude-2";
invocationKwargs?: Kwargs;
stopSequences?: string[];
streaming = false;
clientOptions: ClientOptions;
// Used for non-streaming requests
protected batchClient: Anthropic;
// Used for streaming requests
protected streamingClient: Anthropic;
constructor(fields?: Partial<AnthropicInput> & BaseChatModelParams) {
super(fields ?? {});
this.anthropicApiKey =
fields?.anthropicApiKey ?? getEnvironmentVariable("ANTHROPIC_API_KEY");
if (!this.anthropicApiKey) {
throw new Error("Anthropic API key not found");
}
// Support overriding the default API URL (i.e., https://api.anthropic.com)
this.apiUrl = fields?.anthropicApiUrl;
this.modelName = fields?.modelName ?? this.modelName;
this.invocationKwargs = fields?.invocationKwargs ?? {};
this.temperature = fields?.temperature ?? this.temperature;
this.topK = fields?.topK ?? this.topK;
this.topP = fields?.topP ?? this.topP;
this.maxTokensToSample =
fields?.maxTokensToSample ?? this.maxTokensToSample;
this.stopSequences = fields?.stopSequences ?? this.stopSequences;
this.streaming = fields?.streaming ?? false;
this.clientOptions = fields?.clientOptions ?? {};
}
/**
* Get the parameters used to invoke the model
*/
invocationParams(
options?: this["ParsedCallOptions"]
): Omit<CompletionCreateParams, "prompt"> & Kwargs {
return {
model: this.modelName,
temperature: this.temperature,
top_k: this.topK,
top_p: this.topP,
stop_sequences:
options?.stop?.concat(DEFAULT_STOP_SEQUENCES) ??
this.stopSequences ??
DEFAULT_STOP_SEQUENCES,
max_tokens_to_sample: this.maxTokensToSample,
stream: this.streaming,
...this.invocationKwargs,
};
}
/** @ignore */
_identifyingParams() {
return {
model_name: this.modelName,
...this.invocationParams(),
};
}
/**
* Get the identifying parameters for the model
*/
identifyingParams() {
return {
model_name: this.modelName,
...this.invocationParams(),
};
}
async *_streamResponseChunks(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
const params = this.invocationParams(options);
const stream = await this.createStreamWithRetry({
...params,
prompt: this.formatMessagesAsPrompt(messages),
});
let modelSent = false;
let stopReasonSent = false;
for await (const data of stream) {
if (options.signal?.aborted) {
stream.controller.abort();
throw new Error("AbortError: User aborted the request.");
}
const additional_kwargs: Record<string, unknown> = {};
if (data.model && !modelSent) {
additional_kwargs.model = data.model;
modelSent = true;
} else if (data.stop_reason && !stopReasonSent) {
additional_kwargs.stop_reason = data.stop_reason;
stopReasonSent = true;
}
const delta = data.completion ?? "";
yield new ChatGenerationChunk({
message: new AIMessageChunk({
content: delta,
additional_kwargs,
}),
text: delta,
});
await runManager?.handleLLMNewToken(delta);
if (data.stop_reason) {
break;
}
}
}
/**
* Formats messages as a prompt for the model.
* @param messages The base messages to format as a prompt.
* @returns The formatted prompt.
*/
protected formatMessagesAsPrompt(messages: BaseMessage[]): string {
return (
messages
.map((message) => {
const messagePrompt = getAnthropicPromptFromMessage(message);
return `${messagePrompt} ${message.content}`;
})
.join("") + AI_PROMPT
);
}
/** @ignore */
async _generate(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<ChatResult> {
if (this.stopSequences && options.stop) {
throw new Error(
`"stopSequence" parameter found in input and default params`
);
}
const params = this.invocationParams(options);
let response;
if (params.stream) {
response = {
completion: "",
model: "",
stop_reason: "",
};
const stream = await this._streamResponseChunks(
messages,
options,
runManager
);
for await (const chunk of stream) {
response.completion += chunk.message.content;
response.model =
(chunk.message.additional_kwargs.model as string) ?? response.model;
response.stop_reason =
(chunk.message.additional_kwargs.stop_reason as string) ??
response.stop_reason;
}
} else {
response = await this.completionWithRetry(
{
...params,
prompt: this.formatMessagesAsPrompt(messages),
},
{ signal: options.signal }
);
}
const generations: ChatGeneration[] = (response.completion ?? "")
.split(AI_PROMPT)
.map((message) => ({
text: message,
message: new AIMessage(message),
}));
return {
generations,
};
}
/**
* Creates a streaming request with retry.
* @param request The parameters for creating a completion.
* @returns A streaming request.
*/
protected async createStreamWithRetry(
request: CompletionCreateParams & Kwargs
): Promise<Stream<Anthropic.Completions.Completion>> {
if (!this.streamingClient) {
const options = this.apiUrl ? { baseURL: this.apiUrl } : undefined;
this.streamingClient = new Anthropic({
...this.clientOptions,
...options,
apiKey: this.anthropicApiKey,
maxRetries: 0,
});
}
const makeCompletionRequest = async () =>
this.streamingClient.completions.create(
{ ...request, stream: true },
{ headers: request.headers }
);
return this.caller.call(makeCompletionRequest);
}
/** @ignore */
protected async completionWithRetry(
request: CompletionCreateParams & Kwargs,
options: { signal?: AbortSignal }
): Promise<Anthropic.Completions.Completion> {
if (!this.anthropicApiKey) {
throw new Error("Missing Anthropic API key.");
}
if (!this.batchClient) {
const options = this.apiUrl ? { baseURL: this.apiUrl } : undefined;
this.batchClient = new Anthropic({
...this.clientOptions,
...options,
apiKey: this.anthropicApiKey,
maxRetries: 0,
});
}
const makeCompletionRequest = async () =>
this.batchClient.completions.create(
{ ...request, stream: false },
{ headers: request.headers }
);
return this.caller.callWithOptions(
{ signal: options.signal },
makeCompletionRequest
);
}
_llmType() {
return "anthropic";
}
/** @ignore */
_combineLLMOutput() {
return [];
}
}