@langchain/anthropic 0.1.12-rc.0 → 0.1.13
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/README.md +3 -3
- package/dist/chat_models.cjs +28 -9
- package/dist/chat_models.d.ts +7 -1
- package/dist/chat_models.js +28 -9
- package/dist/tests/chat_models.int.test.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ Then initialize
|
|
|
52
52
|
import { ChatAnthropicMessages } from "@langchain/anthropic";
|
|
53
53
|
|
|
54
54
|
const model = new ChatAnthropic({
|
|
55
|
-
|
|
55
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
56
56
|
});
|
|
57
57
|
const response = await model.invoke(new HumanMessage("Hello world!"));
|
|
58
58
|
```
|
|
@@ -63,8 +63,8 @@ const response = await model.invoke(new HumanMessage("Hello world!"));
|
|
|
63
63
|
import { ChatAnthropicMessages } from "@langchain/anthropic";
|
|
64
64
|
|
|
65
65
|
const model = new ChatAnthropic({
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
67
|
+
model: "claude-3-sonnet-20240229",
|
|
68
68
|
});
|
|
69
69
|
const response = await model.stream(new HumanMessage("Hello world!"));
|
|
70
70
|
```
|
package/dist/chat_models.cjs
CHANGED
|
@@ -71,7 +71,7 @@ function isAnthropicTool(tool) {
|
|
|
71
71
|
*
|
|
72
72
|
* const model = new ChatAnthropic({
|
|
73
73
|
* temperature: 0.9,
|
|
74
|
-
*
|
|
74
|
+
* apiKey: 'YOUR-API-KEY',
|
|
75
75
|
* });
|
|
76
76
|
* const res = await model.invoke({ input: 'Hello!' });
|
|
77
77
|
* console.log(res);
|
|
@@ -84,6 +84,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
84
84
|
get lc_secrets() {
|
|
85
85
|
return {
|
|
86
86
|
anthropicApiKey: "ANTHROPIC_API_KEY",
|
|
87
|
+
apiKey: "ANTHROPIC_API_KEY",
|
|
87
88
|
};
|
|
88
89
|
}
|
|
89
90
|
get lc_aliases() {
|
|
@@ -105,6 +106,12 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
105
106
|
writable: true,
|
|
106
107
|
value: void 0
|
|
107
108
|
});
|
|
109
|
+
Object.defineProperty(this, "apiKey", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: void 0
|
|
114
|
+
});
|
|
108
115
|
Object.defineProperty(this, "apiUrl", {
|
|
109
116
|
enumerable: true,
|
|
110
117
|
configurable: true,
|
|
@@ -141,6 +148,12 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
141
148
|
writable: true,
|
|
142
149
|
value: "claude-2.1"
|
|
143
150
|
});
|
|
151
|
+
Object.defineProperty(this, "model", {
|
|
152
|
+
enumerable: true,
|
|
153
|
+
configurable: true,
|
|
154
|
+
writable: true,
|
|
155
|
+
value: "claude-2.1"
|
|
156
|
+
});
|
|
144
157
|
Object.defineProperty(this, "invocationKwargs", {
|
|
145
158
|
enumerable: true,
|
|
146
159
|
configurable: true,
|
|
@@ -180,13 +193,19 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
180
193
|
value: void 0
|
|
181
194
|
});
|
|
182
195
|
this.anthropicApiKey =
|
|
183
|
-
fields?.
|
|
196
|
+
fields?.apiKey ??
|
|
197
|
+
fields?.anthropicApiKey ??
|
|
198
|
+
(0, env_1.getEnvironmentVariable)("ANTHROPIC_API_KEY");
|
|
184
199
|
if (!this.anthropicApiKey) {
|
|
185
200
|
throw new Error("Anthropic API key not found");
|
|
186
201
|
}
|
|
202
|
+
/** Keep anthropicApiKey for backwards compatibility */
|
|
203
|
+
this.apiKey = this.anthropicApiKey;
|
|
187
204
|
// Support overriding the default API URL (i.e., https://api.anthropic.com)
|
|
188
205
|
this.apiUrl = fields?.anthropicApiUrl;
|
|
189
|
-
|
|
206
|
+
/** Keep modelName for backwards compatibility */
|
|
207
|
+
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
|
|
208
|
+
this.model = this.modelName;
|
|
190
209
|
this.invocationKwargs = fields?.invocationKwargs ?? {};
|
|
191
210
|
this.temperature = fields?.temperature ?? this.temperature;
|
|
192
211
|
this.topK = fields?.topK ?? this.topK;
|
|
@@ -228,7 +247,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
228
247
|
*/
|
|
229
248
|
invocationParams(options) {
|
|
230
249
|
return {
|
|
231
|
-
model: this.
|
|
250
|
+
model: this.model,
|
|
232
251
|
temperature: this.temperature,
|
|
233
252
|
top_k: this.topK,
|
|
234
253
|
top_p: this.topP,
|
|
@@ -262,7 +281,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
262
281
|
/** @ignore */
|
|
263
282
|
_identifyingParams() {
|
|
264
283
|
return {
|
|
265
|
-
model_name: this.
|
|
284
|
+
model_name: this.model,
|
|
266
285
|
...this.invocationParams(),
|
|
267
286
|
};
|
|
268
287
|
}
|
|
@@ -271,7 +290,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
271
290
|
*/
|
|
272
291
|
identifyingParams() {
|
|
273
292
|
return {
|
|
274
|
-
model_name: this.
|
|
293
|
+
model_name: this.model,
|
|
275
294
|
...this.invocationParams(),
|
|
276
295
|
};
|
|
277
296
|
}
|
|
@@ -508,7 +527,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
508
527
|
this.streamingClient = new sdk_1.Anthropic({
|
|
509
528
|
...this.clientOptions,
|
|
510
529
|
...options_,
|
|
511
|
-
apiKey: this.
|
|
530
|
+
apiKey: this.apiKey,
|
|
512
531
|
// Prefer LangChain built-in retries
|
|
513
532
|
maxRetries: 0,
|
|
514
533
|
});
|
|
@@ -522,7 +541,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
522
541
|
}
|
|
523
542
|
/** @ignore */
|
|
524
543
|
async completionWithRetry(request, options) {
|
|
525
|
-
if (!this.
|
|
544
|
+
if (!this.apiKey) {
|
|
526
545
|
throw new Error("Missing Anthropic API key.");
|
|
527
546
|
}
|
|
528
547
|
if (!this.batchClient) {
|
|
@@ -530,7 +549,7 @@ class ChatAnthropicMessages extends chat_models_1.BaseChatModel {
|
|
|
530
549
|
this.batchClient = new sdk_1.Anthropic({
|
|
531
550
|
...this.clientOptions,
|
|
532
551
|
...options,
|
|
533
|
-
apiKey: this.
|
|
552
|
+
apiKey: this.apiKey,
|
|
534
553
|
maxRetries: 0,
|
|
535
554
|
});
|
|
536
555
|
}
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -64,10 +64,14 @@ export interface AnthropicInput {
|
|
|
64
64
|
streaming?: boolean;
|
|
65
65
|
/** Anthropic API key */
|
|
66
66
|
anthropicApiKey?: string;
|
|
67
|
+
/** Anthropic API key */
|
|
68
|
+
apiKey?: string;
|
|
67
69
|
/** Anthropic API URL */
|
|
68
70
|
anthropicApiUrl?: string;
|
|
69
71
|
/** Model name to use */
|
|
70
72
|
modelName: string;
|
|
73
|
+
/** Model name to use */
|
|
74
|
+
model: string;
|
|
71
75
|
/** Overridable Anthropic ClientOptions */
|
|
72
76
|
clientOptions: ClientOptions;
|
|
73
77
|
/** Holds any additional parameters that are valid to pass to {@link
|
|
@@ -98,7 +102,7 @@ type Kwargs = Record<string, any>;
|
|
|
98
102
|
*
|
|
99
103
|
* const model = new ChatAnthropic({
|
|
100
104
|
* temperature: 0.9,
|
|
101
|
-
*
|
|
105
|
+
* apiKey: 'YOUR-API-KEY',
|
|
102
106
|
* });
|
|
103
107
|
* const res = await model.invoke({ input: 'Hello!' });
|
|
104
108
|
* console.log(res);
|
|
@@ -112,12 +116,14 @@ export declare class ChatAnthropicMessages<CallOptions extends ChatAnthropicCall
|
|
|
112
116
|
get lc_aliases(): Record<string, string>;
|
|
113
117
|
lc_serializable: boolean;
|
|
114
118
|
anthropicApiKey?: string;
|
|
119
|
+
apiKey?: string;
|
|
115
120
|
apiUrl?: string;
|
|
116
121
|
temperature: number;
|
|
117
122
|
topK: number;
|
|
118
123
|
topP: number;
|
|
119
124
|
maxTokens: number;
|
|
120
125
|
modelName: string;
|
|
126
|
+
model: string;
|
|
121
127
|
invocationKwargs?: Kwargs;
|
|
122
128
|
stopSequences?: string[];
|
|
123
129
|
streaming: boolean;
|
package/dist/chat_models.js
CHANGED
|
@@ -68,7 +68,7 @@ function isAnthropicTool(tool) {
|
|
|
68
68
|
*
|
|
69
69
|
* const model = new ChatAnthropic({
|
|
70
70
|
* temperature: 0.9,
|
|
71
|
-
*
|
|
71
|
+
* apiKey: 'YOUR-API-KEY',
|
|
72
72
|
* });
|
|
73
73
|
* const res = await model.invoke({ input: 'Hello!' });
|
|
74
74
|
* console.log(res);
|
|
@@ -81,6 +81,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
81
81
|
get lc_secrets() {
|
|
82
82
|
return {
|
|
83
83
|
anthropicApiKey: "ANTHROPIC_API_KEY",
|
|
84
|
+
apiKey: "ANTHROPIC_API_KEY",
|
|
84
85
|
};
|
|
85
86
|
}
|
|
86
87
|
get lc_aliases() {
|
|
@@ -102,6 +103,12 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
102
103
|
writable: true,
|
|
103
104
|
value: void 0
|
|
104
105
|
});
|
|
106
|
+
Object.defineProperty(this, "apiKey", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true,
|
|
109
|
+
writable: true,
|
|
110
|
+
value: void 0
|
|
111
|
+
});
|
|
105
112
|
Object.defineProperty(this, "apiUrl", {
|
|
106
113
|
enumerable: true,
|
|
107
114
|
configurable: true,
|
|
@@ -138,6 +145,12 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
138
145
|
writable: true,
|
|
139
146
|
value: "claude-2.1"
|
|
140
147
|
});
|
|
148
|
+
Object.defineProperty(this, "model", {
|
|
149
|
+
enumerable: true,
|
|
150
|
+
configurable: true,
|
|
151
|
+
writable: true,
|
|
152
|
+
value: "claude-2.1"
|
|
153
|
+
});
|
|
141
154
|
Object.defineProperty(this, "invocationKwargs", {
|
|
142
155
|
enumerable: true,
|
|
143
156
|
configurable: true,
|
|
@@ -177,13 +190,19 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
177
190
|
value: void 0
|
|
178
191
|
});
|
|
179
192
|
this.anthropicApiKey =
|
|
180
|
-
fields?.
|
|
193
|
+
fields?.apiKey ??
|
|
194
|
+
fields?.anthropicApiKey ??
|
|
195
|
+
getEnvironmentVariable("ANTHROPIC_API_KEY");
|
|
181
196
|
if (!this.anthropicApiKey) {
|
|
182
197
|
throw new Error("Anthropic API key not found");
|
|
183
198
|
}
|
|
199
|
+
/** Keep anthropicApiKey for backwards compatibility */
|
|
200
|
+
this.apiKey = this.anthropicApiKey;
|
|
184
201
|
// Support overriding the default API URL (i.e., https://api.anthropic.com)
|
|
185
202
|
this.apiUrl = fields?.anthropicApiUrl;
|
|
186
|
-
|
|
203
|
+
/** Keep modelName for backwards compatibility */
|
|
204
|
+
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
|
|
205
|
+
this.model = this.modelName;
|
|
187
206
|
this.invocationKwargs = fields?.invocationKwargs ?? {};
|
|
188
207
|
this.temperature = fields?.temperature ?? this.temperature;
|
|
189
208
|
this.topK = fields?.topK ?? this.topK;
|
|
@@ -225,7 +244,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
225
244
|
*/
|
|
226
245
|
invocationParams(options) {
|
|
227
246
|
return {
|
|
228
|
-
model: this.
|
|
247
|
+
model: this.model,
|
|
229
248
|
temperature: this.temperature,
|
|
230
249
|
top_k: this.topK,
|
|
231
250
|
top_p: this.topP,
|
|
@@ -259,7 +278,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
259
278
|
/** @ignore */
|
|
260
279
|
_identifyingParams() {
|
|
261
280
|
return {
|
|
262
|
-
model_name: this.
|
|
281
|
+
model_name: this.model,
|
|
263
282
|
...this.invocationParams(),
|
|
264
283
|
};
|
|
265
284
|
}
|
|
@@ -268,7 +287,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
268
287
|
*/
|
|
269
288
|
identifyingParams() {
|
|
270
289
|
return {
|
|
271
|
-
model_name: this.
|
|
290
|
+
model_name: this.model,
|
|
272
291
|
...this.invocationParams(),
|
|
273
292
|
};
|
|
274
293
|
}
|
|
@@ -505,7 +524,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
505
524
|
this.streamingClient = new Anthropic({
|
|
506
525
|
...this.clientOptions,
|
|
507
526
|
...options_,
|
|
508
|
-
apiKey: this.
|
|
527
|
+
apiKey: this.apiKey,
|
|
509
528
|
// Prefer LangChain built-in retries
|
|
510
529
|
maxRetries: 0,
|
|
511
530
|
});
|
|
@@ -519,7 +538,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
519
538
|
}
|
|
520
539
|
/** @ignore */
|
|
521
540
|
async completionWithRetry(request, options) {
|
|
522
|
-
if (!this.
|
|
541
|
+
if (!this.apiKey) {
|
|
523
542
|
throw new Error("Missing Anthropic API key.");
|
|
524
543
|
}
|
|
525
544
|
if (!this.batchClient) {
|
|
@@ -527,7 +546,7 @@ export class ChatAnthropicMessages extends BaseChatModel {
|
|
|
527
546
|
this.batchClient = new Anthropic({
|
|
528
547
|
...this.clientOptions,
|
|
529
548
|
...options,
|
|
530
|
-
apiKey: this.
|
|
549
|
+
apiKey: this.apiKey,
|
|
531
550
|
maxRetries: 0,
|
|
532
551
|
});
|
|
533
552
|
}
|
|
@@ -244,7 +244,7 @@ test("Test ChatAnthropic headers passed through", async () => {
|
|
|
244
244
|
const chat = new ChatAnthropic({
|
|
245
245
|
modelName: "claude-3-sonnet-20240229",
|
|
246
246
|
maxRetries: 0,
|
|
247
|
-
|
|
247
|
+
apiKey: "NOT_REAL",
|
|
248
248
|
clientOptions: {
|
|
249
249
|
defaultHeaders: {
|
|
250
250
|
"X-Api-Key": process.env.ANTHROPIC_API_KEY,
|