langchain 0.0.197-rc.1 → 0.0.197
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/dist/chains/openai_moderation.cjs +2 -2
- package/dist/chains/openai_moderation.d.ts +1 -1
- package/dist/chains/openai_moderation.js +1 -1
- package/dist/chat_models/anthropic.cjs +351 -15
- package/dist/chat_models/anthropic.d.ts +157 -1
- package/dist/chat_models/anthropic.js +348 -1
- package/dist/chat_models/cloudflare_workersai.cjs +5 -0
- package/dist/chat_models/cloudflare_workersai.d.ts +3 -0
- package/dist/chat_models/cloudflare_workersai.js +5 -0
- package/dist/chat_models/fireworks.d.ts +1 -1
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
- package/dist/chat_models/minimax.d.ts +1 -1
- package/dist/chat_models/openai.cjs +698 -4
- package/dist/chat_models/openai.d.ts +137 -4
- package/dist/chat_models/openai.js +695 -2
- package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
- package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
- package/dist/embeddings/openai.cjs +240 -2
- package/dist/embeddings/openai.d.ts +82 -1
- package/dist/embeddings/openai.js +239 -1
- package/dist/experimental/openai_assistant/index.cjs +3 -3
- package/dist/experimental/openai_assistant/index.d.ts +1 -1
- package/dist/experimental/openai_assistant/index.js +1 -1
- package/dist/experimental/openai_assistant/schema.d.ts +1 -1
- package/dist/experimental/openai_files/index.cjs +2 -2
- package/dist/experimental/openai_files/index.d.ts +1 -1
- package/dist/experimental/openai_files/index.js +1 -1
- package/dist/llms/fireworks.d.ts +1 -1
- package/dist/llms/openai-chat.cjs +445 -3
- package/dist/llms/openai-chat.d.ts +123 -4
- package/dist/llms/openai-chat.js +443 -2
- package/dist/llms/openai.cjs +530 -6
- package/dist/llms/openai.d.ts +123 -4
- package/dist/llms/openai.js +525 -2
- package/dist/schema/index.d.ts +1 -1
- package/dist/tools/convert_to_openai.cjs +38 -4
- package/dist/tools/convert_to_openai.d.ts +11 -1
- package/dist/tools/convert_to_openai.js +35 -1
- package/dist/types/openai-types.d.ts +133 -1
- package/dist/util/env.cjs +9 -70
- package/dist/util/env.d.ts +1 -21
- package/dist/util/env.js +1 -62
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openai.cjs +18 -2
- package/dist/util/openai.d.ts +1 -1
- package/dist/util/openai.js +17 -1
- package/dist/util/openapi.d.ts +2 -2
- package/dist/util/prompt-layer.d.ts +1 -1
- package/package.json +3 -6
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenAIModerationChain = void 0;
|
|
4
|
-
const openai_1 = require("
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
6
|
const async_caller_js_1 = require("../util/async_caller.cjs");
|
|
7
7
|
const env_js_1 = require("../util/env.cjs");
|
|
@@ -109,7 +109,7 @@ class OpenAIModerationChain extends base_js_1.BaseChain {
|
|
|
109
109
|
apiKey: this.openAIApiKey,
|
|
110
110
|
organization: this.openAIOrganization,
|
|
111
111
|
};
|
|
112
|
-
this.client = new openai_1.
|
|
112
|
+
this.client = new openai_1.OpenAI(this.clientConfig);
|
|
113
113
|
this.caller = new async_caller_js_1.AsyncCaller(fields ?? {});
|
|
114
114
|
}
|
|
115
115
|
_moderate(text, results) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ClientOptions, OpenAIClient } from "
|
|
1
|
+
import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import { BaseChain, ChainInputs } from "./base.js";
|
|
3
3
|
import { ChainValues } from "../schema/index.js";
|
|
4
4
|
import { AsyncCaller, AsyncCallerParams } from "../util/async_caller.js";
|
|
@@ -1,17 +1,353 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
3
|
+
exports.ChatAnthropic = exports.DEFAULT_STOP_SEQUENCES = exports.HUMAN_PROMPT = exports.AI_PROMPT = void 0;
|
|
4
|
+
const sdk_1 = require("@anthropic-ai/sdk");
|
|
5
|
+
Object.defineProperty(exports, "AI_PROMPT", { enumerable: true, get: function () { return sdk_1.AI_PROMPT; } });
|
|
6
|
+
Object.defineProperty(exports, "HUMAN_PROMPT", { enumerable: true, get: function () { return sdk_1.HUMAN_PROMPT; } });
|
|
7
|
+
const messages_1 = require("@langchain/core/messages");
|
|
8
|
+
const outputs_1 = require("@langchain/core/outputs");
|
|
9
|
+
const env_1 = require("@langchain/core/utils/env");
|
|
10
|
+
const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the custom role of a generic chat message.
|
|
13
|
+
* @param message The chat message from which to extract the custom role.
|
|
14
|
+
* @returns The custom role of the chat message.
|
|
15
|
+
*/
|
|
16
|
+
function extractGenericMessageCustomRole(message) {
|
|
17
|
+
if (message.role !== sdk_1.AI_PROMPT &&
|
|
18
|
+
message.role !== sdk_1.HUMAN_PROMPT &&
|
|
19
|
+
message.role !== "") {
|
|
20
|
+
console.warn(`Unknown message role: ${message.role}`);
|
|
21
|
+
}
|
|
22
|
+
return message.role;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets the Anthropic prompt from a base message.
|
|
26
|
+
* @param message The base message from which to get the Anthropic prompt.
|
|
27
|
+
* @returns The Anthropic prompt from the base message.
|
|
28
|
+
*/
|
|
29
|
+
function getAnthropicPromptFromMessage(message) {
|
|
30
|
+
const type = message._getType();
|
|
31
|
+
switch (type) {
|
|
32
|
+
case "ai":
|
|
33
|
+
return sdk_1.AI_PROMPT;
|
|
34
|
+
case "human":
|
|
35
|
+
return sdk_1.HUMAN_PROMPT;
|
|
36
|
+
case "system":
|
|
37
|
+
return "";
|
|
38
|
+
case "generic": {
|
|
39
|
+
if (!messages_1.ChatMessage.isInstance(message))
|
|
40
|
+
throw new Error("Invalid generic chat message");
|
|
41
|
+
return extractGenericMessageCustomRole(message);
|
|
42
|
+
}
|
|
43
|
+
default:
|
|
44
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.DEFAULT_STOP_SEQUENCES = [sdk_1.HUMAN_PROMPT];
|
|
48
|
+
/**
|
|
49
|
+
* Wrapper around Anthropic large language models.
|
|
50
|
+
*
|
|
51
|
+
* To use you should have the `@anthropic-ai/sdk` package installed, with the
|
|
52
|
+
* `ANTHROPIC_API_KEY` environment variable set.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Any parameters that are valid to be passed to {@link
|
|
56
|
+
* https://console.anthropic.com/docs/api/reference |
|
|
57
|
+
* `anthropic.complete`} can be passed through {@link invocationKwargs},
|
|
58
|
+
* even if not explicitly available on this class.
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const model = new ChatAnthropic({
|
|
62
|
+
* temperature: 0.9,
|
|
63
|
+
* anthropicApiKey: 'YOUR-API-KEY',
|
|
64
|
+
* });
|
|
65
|
+
* const res = await model.invoke({ input: 'Hello!' });
|
|
66
|
+
* console.log(res);
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
class ChatAnthropic extends chat_models_1.BaseChatModel {
|
|
70
|
+
static lc_name() {
|
|
71
|
+
return "ChatAnthropic";
|
|
72
|
+
}
|
|
73
|
+
get lc_secrets() {
|
|
74
|
+
return {
|
|
75
|
+
anthropicApiKey: "ANTHROPIC_API_KEY",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
get lc_aliases() {
|
|
79
|
+
return {
|
|
80
|
+
modelName: "model",
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
constructor(fields) {
|
|
84
|
+
super(fields ?? {});
|
|
85
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: true
|
|
90
|
+
});
|
|
91
|
+
Object.defineProperty(this, "anthropicApiKey", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: void 0
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "apiUrl", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(this, "temperature", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: 1
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(this, "topK", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: -1
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(this, "topP", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
configurable: true,
|
|
118
|
+
writable: true,
|
|
119
|
+
value: -1
|
|
120
|
+
});
|
|
121
|
+
Object.defineProperty(this, "maxTokensToSample", {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
configurable: true,
|
|
124
|
+
writable: true,
|
|
125
|
+
value: 2048
|
|
126
|
+
});
|
|
127
|
+
Object.defineProperty(this, "modelName", {
|
|
128
|
+
enumerable: true,
|
|
129
|
+
configurable: true,
|
|
130
|
+
writable: true,
|
|
131
|
+
value: "claude-2"
|
|
132
|
+
});
|
|
133
|
+
Object.defineProperty(this, "invocationKwargs", {
|
|
134
|
+
enumerable: true,
|
|
135
|
+
configurable: true,
|
|
136
|
+
writable: true,
|
|
137
|
+
value: void 0
|
|
138
|
+
});
|
|
139
|
+
Object.defineProperty(this, "stopSequences", {
|
|
140
|
+
enumerable: true,
|
|
141
|
+
configurable: true,
|
|
142
|
+
writable: true,
|
|
143
|
+
value: void 0
|
|
144
|
+
});
|
|
145
|
+
Object.defineProperty(this, "streaming", {
|
|
146
|
+
enumerable: true,
|
|
147
|
+
configurable: true,
|
|
148
|
+
writable: true,
|
|
149
|
+
value: false
|
|
150
|
+
});
|
|
151
|
+
Object.defineProperty(this, "clientOptions", {
|
|
152
|
+
enumerable: true,
|
|
153
|
+
configurable: true,
|
|
154
|
+
writable: true,
|
|
155
|
+
value: void 0
|
|
156
|
+
});
|
|
157
|
+
// Used for non-streaming requests
|
|
158
|
+
Object.defineProperty(this, "batchClient", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: true,
|
|
162
|
+
value: void 0
|
|
163
|
+
});
|
|
164
|
+
// Used for streaming requests
|
|
165
|
+
Object.defineProperty(this, "streamingClient", {
|
|
166
|
+
enumerable: true,
|
|
167
|
+
configurable: true,
|
|
168
|
+
writable: true,
|
|
169
|
+
value: void 0
|
|
170
|
+
});
|
|
171
|
+
this.anthropicApiKey =
|
|
172
|
+
fields?.anthropicApiKey ?? (0, env_1.getEnvironmentVariable)("ANTHROPIC_API_KEY");
|
|
173
|
+
if (!this.anthropicApiKey) {
|
|
174
|
+
throw new Error("Anthropic API key not found");
|
|
175
|
+
}
|
|
176
|
+
// Support overriding the default API URL (i.e., https://api.anthropic.com)
|
|
177
|
+
this.apiUrl = fields?.anthropicApiUrl;
|
|
178
|
+
this.modelName = fields?.modelName ?? this.modelName;
|
|
179
|
+
this.invocationKwargs = fields?.invocationKwargs ?? {};
|
|
180
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
181
|
+
this.topK = fields?.topK ?? this.topK;
|
|
182
|
+
this.topP = fields?.topP ?? this.topP;
|
|
183
|
+
this.maxTokensToSample =
|
|
184
|
+
fields?.maxTokensToSample ?? this.maxTokensToSample;
|
|
185
|
+
this.stopSequences = fields?.stopSequences ?? this.stopSequences;
|
|
186
|
+
this.streaming = fields?.streaming ?? false;
|
|
187
|
+
this.clientOptions = fields?.clientOptions ?? {};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Get the parameters used to invoke the model
|
|
191
|
+
*/
|
|
192
|
+
invocationParams(options) {
|
|
193
|
+
return {
|
|
194
|
+
model: this.modelName,
|
|
195
|
+
temperature: this.temperature,
|
|
196
|
+
top_k: this.topK,
|
|
197
|
+
top_p: this.topP,
|
|
198
|
+
stop_sequences: options?.stop?.concat(exports.DEFAULT_STOP_SEQUENCES) ??
|
|
199
|
+
this.stopSequences ??
|
|
200
|
+
exports.DEFAULT_STOP_SEQUENCES,
|
|
201
|
+
max_tokens_to_sample: this.maxTokensToSample,
|
|
202
|
+
stream: this.streaming,
|
|
203
|
+
...this.invocationKwargs,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/** @ignore */
|
|
207
|
+
_identifyingParams() {
|
|
208
|
+
return {
|
|
209
|
+
model_name: this.modelName,
|
|
210
|
+
...this.invocationParams(),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Get the identifying parameters for the model
|
|
215
|
+
*/
|
|
216
|
+
identifyingParams() {
|
|
217
|
+
return {
|
|
218
|
+
model_name: this.modelName,
|
|
219
|
+
...this.invocationParams(),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
223
|
+
const params = this.invocationParams(options);
|
|
224
|
+
const stream = await this.createStreamWithRetry({
|
|
225
|
+
...params,
|
|
226
|
+
prompt: this.formatMessagesAsPrompt(messages),
|
|
227
|
+
});
|
|
228
|
+
let modelSent = false;
|
|
229
|
+
let stopReasonSent = false;
|
|
230
|
+
for await (const data of stream) {
|
|
231
|
+
if (options.signal?.aborted) {
|
|
232
|
+
stream.controller.abort();
|
|
233
|
+
throw new Error("AbortError: User aborted the request.");
|
|
234
|
+
}
|
|
235
|
+
const additional_kwargs = {};
|
|
236
|
+
if (data.model && !modelSent) {
|
|
237
|
+
additional_kwargs.model = data.model;
|
|
238
|
+
modelSent = true;
|
|
239
|
+
}
|
|
240
|
+
else if (data.stop_reason && !stopReasonSent) {
|
|
241
|
+
additional_kwargs.stop_reason = data.stop_reason;
|
|
242
|
+
stopReasonSent = true;
|
|
243
|
+
}
|
|
244
|
+
const delta = data.completion ?? "";
|
|
245
|
+
yield new outputs_1.ChatGenerationChunk({
|
|
246
|
+
message: new messages_1.AIMessageChunk({
|
|
247
|
+
content: delta,
|
|
248
|
+
additional_kwargs,
|
|
249
|
+
}),
|
|
250
|
+
text: delta,
|
|
251
|
+
});
|
|
252
|
+
await runManager?.handleLLMNewToken(delta);
|
|
253
|
+
if (data.stop_reason) {
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Formats messages as a prompt for the model.
|
|
260
|
+
* @param messages The base messages to format as a prompt.
|
|
261
|
+
* @returns The formatted prompt.
|
|
262
|
+
*/
|
|
263
|
+
formatMessagesAsPrompt(messages) {
|
|
264
|
+
return (messages
|
|
265
|
+
.map((message) => {
|
|
266
|
+
const messagePrompt = getAnthropicPromptFromMessage(message);
|
|
267
|
+
return `${messagePrompt} ${message.content}`;
|
|
268
|
+
})
|
|
269
|
+
.join("") + sdk_1.AI_PROMPT);
|
|
270
|
+
}
|
|
271
|
+
/** @ignore */
|
|
272
|
+
async _generate(messages, options, runManager) {
|
|
273
|
+
if (this.stopSequences && options.stop) {
|
|
274
|
+
throw new Error(`"stopSequence" parameter found in input and default params`);
|
|
275
|
+
}
|
|
276
|
+
const params = this.invocationParams(options);
|
|
277
|
+
let response;
|
|
278
|
+
if (params.stream) {
|
|
279
|
+
response = {
|
|
280
|
+
completion: "",
|
|
281
|
+
model: "",
|
|
282
|
+
stop_reason: "",
|
|
283
|
+
};
|
|
284
|
+
const stream = await this._streamResponseChunks(messages, options, runManager);
|
|
285
|
+
for await (const chunk of stream) {
|
|
286
|
+
response.completion += chunk.message.content;
|
|
287
|
+
response.model =
|
|
288
|
+
chunk.message.additional_kwargs.model ?? response.model;
|
|
289
|
+
response.stop_reason =
|
|
290
|
+
chunk.message.additional_kwargs.stop_reason ??
|
|
291
|
+
response.stop_reason;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
response = await this.completionWithRetry({
|
|
296
|
+
...params,
|
|
297
|
+
prompt: this.formatMessagesAsPrompt(messages),
|
|
298
|
+
}, { signal: options.signal });
|
|
299
|
+
}
|
|
300
|
+
const generations = (response.completion ?? "")
|
|
301
|
+
.split(sdk_1.AI_PROMPT)
|
|
302
|
+
.map((message) => ({
|
|
303
|
+
text: message,
|
|
304
|
+
message: new messages_1.AIMessage(message),
|
|
305
|
+
}));
|
|
306
|
+
return {
|
|
307
|
+
generations,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Creates a streaming request with retry.
|
|
312
|
+
* @param request The parameters for creating a completion.
|
|
313
|
+
* @returns A streaming request.
|
|
314
|
+
*/
|
|
315
|
+
async createStreamWithRetry(request) {
|
|
316
|
+
if (!this.streamingClient) {
|
|
317
|
+
const options = this.apiUrl ? { baseURL: this.apiUrl } : undefined;
|
|
318
|
+
this.streamingClient = new sdk_1.Anthropic({
|
|
319
|
+
...this.clientOptions,
|
|
320
|
+
...options,
|
|
321
|
+
apiKey: this.anthropicApiKey,
|
|
322
|
+
maxRetries: 0,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
const makeCompletionRequest = async () => this.streamingClient.completions.create({ ...request, stream: true }, { headers: request.headers });
|
|
326
|
+
return this.caller.call(makeCompletionRequest);
|
|
327
|
+
}
|
|
328
|
+
/** @ignore */
|
|
329
|
+
async completionWithRetry(request, options) {
|
|
330
|
+
if (!this.anthropicApiKey) {
|
|
331
|
+
throw new Error("Missing Anthropic API key.");
|
|
332
|
+
}
|
|
333
|
+
if (!this.batchClient) {
|
|
334
|
+
const options = this.apiUrl ? { baseURL: this.apiUrl } : undefined;
|
|
335
|
+
this.batchClient = new sdk_1.Anthropic({
|
|
336
|
+
...this.clientOptions,
|
|
337
|
+
...options,
|
|
338
|
+
apiKey: this.anthropicApiKey,
|
|
339
|
+
maxRetries: 0,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
const makeCompletionRequest = async () => this.batchClient.completions.create({ ...request, stream: false }, { headers: request.headers });
|
|
343
|
+
return this.caller.callWithOptions({ signal: options.signal }, makeCompletionRequest);
|
|
344
|
+
}
|
|
345
|
+
_llmType() {
|
|
346
|
+
return "anthropic";
|
|
347
|
+
}
|
|
348
|
+
/** @ignore */
|
|
349
|
+
_combineLLMOutput() {
|
|
350
|
+
return [];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
exports.ChatAnthropic = ChatAnthropic;
|
|
@@ -1 +1,157 @@
|
|
|
1
|
-
|
|
1
|
+
import { Anthropic, AI_PROMPT, HUMAN_PROMPT, ClientOptions } from "@anthropic-ai/sdk";
|
|
2
|
+
import type { CompletionCreateParams } from "@anthropic-ai/sdk/resources/completions";
|
|
3
|
+
import type { Stream } from "@anthropic-ai/sdk/streaming";
|
|
4
|
+
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
5
|
+
import { type BaseMessage } from "@langchain/core/messages";
|
|
6
|
+
import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
|
|
7
|
+
import { BaseChatModel, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
8
|
+
import { type BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
|
|
9
|
+
export { AI_PROMPT, HUMAN_PROMPT };
|
|
10
|
+
export declare const DEFAULT_STOP_SEQUENCES: string[];
|
|
11
|
+
/**
|
|
12
|
+
* Input to AnthropicChat class.
|
|
13
|
+
*/
|
|
14
|
+
export interface AnthropicInput {
|
|
15
|
+
/** Amount of randomness injected into the response. Ranges
|
|
16
|
+
* from 0 to 1. Use temp closer to 0 for analytical /
|
|
17
|
+
* multiple choice, and temp closer to 1 for creative
|
|
18
|
+
* and generative tasks.
|
|
19
|
+
*/
|
|
20
|
+
temperature?: number;
|
|
21
|
+
/** Only sample from the top K options for each subsequent
|
|
22
|
+
* token. Used to remove "long tail" low probability
|
|
23
|
+
* responses. Defaults to -1, which disables it.
|
|
24
|
+
*/
|
|
25
|
+
topK?: number;
|
|
26
|
+
/** Does nucleus sampling, in which we compute the
|
|
27
|
+
* cumulative distribution over all the options for each
|
|
28
|
+
* subsequent token in decreasing probability order and
|
|
29
|
+
* cut it off once it reaches a particular probability
|
|
30
|
+
* specified by top_p. Defaults to -1, which disables it.
|
|
31
|
+
* Note that you should either alter temperature or top_p,
|
|
32
|
+
* but not both.
|
|
33
|
+
*/
|
|
34
|
+
topP?: number;
|
|
35
|
+
/** A maximum number of tokens to generate before stopping. */
|
|
36
|
+
maxTokensToSample: number;
|
|
37
|
+
/** A list of strings upon which to stop generating.
|
|
38
|
+
* You probably want `["\n\nHuman:"]`, as that's the cue for
|
|
39
|
+
* the next turn in the dialog agent.
|
|
40
|
+
*/
|
|
41
|
+
stopSequences?: string[];
|
|
42
|
+
/** Whether to stream the results or not */
|
|
43
|
+
streaming?: boolean;
|
|
44
|
+
/** Anthropic API key */
|
|
45
|
+
anthropicApiKey?: string;
|
|
46
|
+
/** Anthropic API URL */
|
|
47
|
+
anthropicApiUrl?: string;
|
|
48
|
+
/** Model name to use */
|
|
49
|
+
modelName: string;
|
|
50
|
+
/** Overridable Anthropic ClientOptions */
|
|
51
|
+
clientOptions: ClientOptions;
|
|
52
|
+
/** Holds any additional parameters that are valid to pass to {@link
|
|
53
|
+
* https://console.anthropic.com/docs/api/reference |
|
|
54
|
+
* `anthropic.complete`} that are not explicitly specified on this class.
|
|
55
|
+
*/
|
|
56
|
+
invocationKwargs?: Kwargs;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A type representing additional parameters that can be passed to the
|
|
60
|
+
* Anthropic API.
|
|
61
|
+
*/
|
|
62
|
+
type Kwargs = Record<string, any>;
|
|
63
|
+
/**
|
|
64
|
+
* Wrapper around Anthropic large language models.
|
|
65
|
+
*
|
|
66
|
+
* To use you should have the `@anthropic-ai/sdk` package installed, with the
|
|
67
|
+
* `ANTHROPIC_API_KEY` environment variable set.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Any parameters that are valid to be passed to {@link
|
|
71
|
+
* https://console.anthropic.com/docs/api/reference |
|
|
72
|
+
* `anthropic.complete`} can be passed through {@link invocationKwargs},
|
|
73
|
+
* even if not explicitly available on this class.
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const model = new ChatAnthropic({
|
|
77
|
+
* temperature: 0.9,
|
|
78
|
+
* anthropicApiKey: 'YOUR-API-KEY',
|
|
79
|
+
* });
|
|
80
|
+
* const res = await model.invoke({ input: 'Hello!' });
|
|
81
|
+
* console.log(res);
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare class ChatAnthropic<CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions> extends BaseChatModel<CallOptions> implements AnthropicInput {
|
|
85
|
+
static lc_name(): string;
|
|
86
|
+
get lc_secrets(): {
|
|
87
|
+
[key: string]: string;
|
|
88
|
+
} | undefined;
|
|
89
|
+
get lc_aliases(): Record<string, string>;
|
|
90
|
+
lc_serializable: boolean;
|
|
91
|
+
anthropicApiKey?: string;
|
|
92
|
+
apiUrl?: string;
|
|
93
|
+
temperature: number;
|
|
94
|
+
topK: number;
|
|
95
|
+
topP: number;
|
|
96
|
+
maxTokensToSample: number;
|
|
97
|
+
modelName: string;
|
|
98
|
+
invocationKwargs?: Kwargs;
|
|
99
|
+
stopSequences?: string[];
|
|
100
|
+
streaming: boolean;
|
|
101
|
+
clientOptions: ClientOptions;
|
|
102
|
+
protected batchClient: Anthropic;
|
|
103
|
+
protected streamingClient: Anthropic;
|
|
104
|
+
constructor(fields?: Partial<AnthropicInput> & BaseChatModelParams);
|
|
105
|
+
/**
|
|
106
|
+
* Get the parameters used to invoke the model
|
|
107
|
+
*/
|
|
108
|
+
invocationParams(options?: this["ParsedCallOptions"]): Omit<CompletionCreateParams, "prompt"> & Kwargs;
|
|
109
|
+
/** @ignore */
|
|
110
|
+
_identifyingParams(): {
|
|
111
|
+
metadata?: Anthropic.Completions.CompletionCreateParams.Metadata | undefined;
|
|
112
|
+
stream?: boolean | undefined;
|
|
113
|
+
model: (string & {}) | "claude-2" | "claude-instant-1";
|
|
114
|
+
temperature?: number | undefined;
|
|
115
|
+
top_p?: number | undefined;
|
|
116
|
+
top_k?: number | undefined;
|
|
117
|
+
max_tokens_to_sample: number;
|
|
118
|
+
stop_sequences?: string[] | undefined;
|
|
119
|
+
model_name: string;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Get the identifying parameters for the model
|
|
123
|
+
*/
|
|
124
|
+
identifyingParams(): {
|
|
125
|
+
metadata?: Anthropic.Completions.CompletionCreateParams.Metadata | undefined;
|
|
126
|
+
stream?: boolean | undefined;
|
|
127
|
+
model: (string & {}) | "claude-2" | "claude-instant-1";
|
|
128
|
+
temperature?: number | undefined;
|
|
129
|
+
top_p?: number | undefined;
|
|
130
|
+
top_k?: number | undefined;
|
|
131
|
+
max_tokens_to_sample: number;
|
|
132
|
+
stop_sequences?: string[] | undefined;
|
|
133
|
+
model_name: string;
|
|
134
|
+
};
|
|
135
|
+
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
136
|
+
/**
|
|
137
|
+
* Formats messages as a prompt for the model.
|
|
138
|
+
* @param messages The base messages to format as a prompt.
|
|
139
|
+
* @returns The formatted prompt.
|
|
140
|
+
*/
|
|
141
|
+
protected formatMessagesAsPrompt(messages: BaseMessage[]): string;
|
|
142
|
+
/** @ignore */
|
|
143
|
+
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
144
|
+
/**
|
|
145
|
+
* Creates a streaming request with retry.
|
|
146
|
+
* @param request The parameters for creating a completion.
|
|
147
|
+
* @returns A streaming request.
|
|
148
|
+
*/
|
|
149
|
+
protected createStreamWithRetry(request: CompletionCreateParams & Kwargs): Promise<Stream<Anthropic.Completions.Completion>>;
|
|
150
|
+
/** @ignore */
|
|
151
|
+
protected completionWithRetry(request: CompletionCreateParams & Kwargs, options: {
|
|
152
|
+
signal?: AbortSignal;
|
|
153
|
+
}): Promise<Anthropic.Completions.Completion>;
|
|
154
|
+
_llmType(): string;
|
|
155
|
+
/** @ignore */
|
|
156
|
+
_combineLLMOutput(): never[];
|
|
157
|
+
}
|