@langchain/anthropic 0.1.3 → 0.1.5
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.
|
@@ -190,12 +190,11 @@ test("Test ChatAnthropic withStructuredOutput", async () => {
|
|
|
190
190
|
const runnable = new ChatAnthropicTools({
|
|
191
191
|
modelName: "claude-3-sonnet-20240229",
|
|
192
192
|
maxRetries: 0,
|
|
193
|
-
}).withStructuredOutput({
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}),
|
|
193
|
+
}).withStructuredOutput(z.object({
|
|
194
|
+
name: z.string().describe("The name of a person"),
|
|
195
|
+
height: z.number().describe("The person's height"),
|
|
196
|
+
hairColor: z.optional(z.string()).describe("The person's hair color"),
|
|
197
|
+
}), {
|
|
199
198
|
name: "person",
|
|
200
199
|
});
|
|
201
200
|
const message = new HumanMessage("Alex is 5 feet tall. Alex is blonde.");
|
|
@@ -207,15 +206,13 @@ test("Test ChatAnthropic withStructuredOutput on a single array item", async ()
|
|
|
207
206
|
const runnable = new ChatAnthropicTools({
|
|
208
207
|
modelName: "claude-3-sonnet-20240229",
|
|
209
208
|
maxRetries: 0,
|
|
210
|
-
}).withStructuredOutput({
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}),
|
|
218
|
-
});
|
|
209
|
+
}).withStructuredOutput(z.object({
|
|
210
|
+
people: z.array(z.object({
|
|
211
|
+
name: z.string().describe("The name of a person"),
|
|
212
|
+
height: z.number().describe("The person's height"),
|
|
213
|
+
hairColor: z.optional(z.string()).describe("The person's hair color"),
|
|
214
|
+
})),
|
|
215
|
+
}));
|
|
219
216
|
const message = new HumanMessage("Alex is 5 feet tall. Alex is blonde.");
|
|
220
217
|
const res = await runnable.invoke([message]);
|
|
221
218
|
console.log(JSON.stringify(res, null, 2));
|
|
@@ -227,25 +224,24 @@ test("Test ChatAnthropic withStructuredOutput on a single array item", async ()
|
|
|
227
224
|
const runnable = new ChatAnthropicTools({
|
|
228
225
|
modelName: "claude-3-sonnet-20240229",
|
|
229
226
|
maxRetries: 0,
|
|
230
|
-
}).withStructuredOutput({
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}),
|
|
227
|
+
}).withStructuredOutput(z.object({
|
|
228
|
+
sender: z
|
|
229
|
+
.optional(z.string())
|
|
230
|
+
.describe("The sender's name, if available"),
|
|
231
|
+
sender_phone_number: z
|
|
232
|
+
.optional(z.string())
|
|
233
|
+
.describe("The sender's phone number, if available"),
|
|
234
|
+
sender_address: z
|
|
235
|
+
.optional(z.string())
|
|
236
|
+
.describe("The sender's address, if available"),
|
|
237
|
+
action_items: z
|
|
238
|
+
.array(z.string())
|
|
239
|
+
.describe("A list of action items requested by the email"),
|
|
240
|
+
topic: z
|
|
241
|
+
.string()
|
|
242
|
+
.describe("High level description of what the email is about"),
|
|
243
|
+
tone: z.enum(["positive", "negative"]).describe("The tone of the email."),
|
|
244
|
+
}), {
|
|
249
245
|
name: "Email",
|
|
250
246
|
});
|
|
251
247
|
const prompt = ChatPromptTemplate.fromMessages([
|
|
@@ -184,15 +184,29 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
184
184
|
_llmType() {
|
|
185
185
|
return "anthropic_tool_calling";
|
|
186
186
|
}
|
|
187
|
-
withStructuredOutput(
|
|
187
|
+
withStructuredOutput(outputSchema, config) {
|
|
188
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
|
+
let schema;
|
|
190
|
+
let name;
|
|
191
|
+
let method;
|
|
192
|
+
let includeRaw;
|
|
193
|
+
if (isStructuredOutputMethodParams(outputSchema)) {
|
|
194
|
+
schema = outputSchema.schema;
|
|
195
|
+
name = outputSchema.name;
|
|
196
|
+
method = outputSchema.method;
|
|
197
|
+
includeRaw = outputSchema.includeRaw;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
schema = outputSchema;
|
|
201
|
+
name = config?.name;
|
|
202
|
+
method = config?.method;
|
|
203
|
+
includeRaw = config?.includeRaw;
|
|
204
|
+
}
|
|
188
205
|
if (method === "jsonMode") {
|
|
189
206
|
throw new Error(`Anthropic only supports "functionCalling" as a method.`);
|
|
190
207
|
}
|
|
191
208
|
const functionName = name ?? "extract";
|
|
192
|
-
|
|
193
|
-
returnSingle: true,
|
|
194
|
-
keyName: functionName,
|
|
195
|
-
});
|
|
209
|
+
let outputParser;
|
|
196
210
|
let tools;
|
|
197
211
|
if (isZodSchema(schema)) {
|
|
198
212
|
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(schema);
|
|
@@ -206,6 +220,11 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
206
220
|
},
|
|
207
221
|
},
|
|
208
222
|
];
|
|
223
|
+
outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
|
|
224
|
+
returnSingle: true,
|
|
225
|
+
keyName: functionName,
|
|
226
|
+
zodSchema: schema,
|
|
227
|
+
});
|
|
209
228
|
}
|
|
210
229
|
else {
|
|
211
230
|
tools = [
|
|
@@ -218,6 +237,10 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
218
237
|
},
|
|
219
238
|
},
|
|
220
239
|
];
|
|
240
|
+
outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
|
|
241
|
+
returnSingle: true,
|
|
242
|
+
keyName: functionName,
|
|
243
|
+
});
|
|
221
244
|
}
|
|
222
245
|
const llm = this.bind({
|
|
223
246
|
tools,
|
|
@@ -254,7 +277,17 @@ class ChatAnthropicTools extends chat_models_1.BaseChatModel {
|
|
|
254
277
|
}
|
|
255
278
|
}
|
|
256
279
|
exports.ChatAnthropicTools = ChatAnthropicTools;
|
|
257
|
-
function isZodSchema(
|
|
280
|
+
function isZodSchema(
|
|
281
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
282
|
+
input) {
|
|
258
283
|
// Check for a characteristic method of Zod schemas
|
|
259
284
|
return typeof input?.parse === "function";
|
|
260
285
|
}
|
|
286
|
+
function isStructuredOutputMethodParams(x
|
|
287
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
288
|
+
) {
|
|
289
|
+
return (x !== undefined &&
|
|
290
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
291
|
+
typeof x.schema ===
|
|
292
|
+
"object");
|
|
293
|
+
}
|
|
@@ -3,8 +3,9 @@ import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
|
|
|
3
3
|
import { BaseChatModel, BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
4
4
|
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
5
5
|
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
6
|
-
import { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, ToolDefinition } from "@langchain/core/language_models/base";
|
|
6
|
+
import { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
|
|
7
7
|
import { Runnable } from "@langchain/core/runnables";
|
|
8
|
+
import { z } from "zod";
|
|
8
9
|
import { type AnthropicInput } from "../chat_models.js";
|
|
9
10
|
export interface ChatAnthropicToolsCallOptions extends BaseLanguageModelCallOptions {
|
|
10
11
|
tools?: ToolDefinition[];
|
|
@@ -43,8 +44,8 @@ export declare class ChatAnthropicTools extends BaseChatModel<ChatAnthropicTools
|
|
|
43
44
|
}): Promise<ChatResult>;
|
|
44
45
|
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun | undefined): Promise<ChatResult>;
|
|
45
46
|
_llmType(): string;
|
|
46
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(
|
|
47
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(
|
|
47
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> | z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
48
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> | z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
48
49
|
raw: BaseMessage;
|
|
49
50
|
parsed: RunOutput;
|
|
50
51
|
}>;
|
|
@@ -181,15 +181,29 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
181
181
|
_llmType() {
|
|
182
182
|
return "anthropic_tool_calling";
|
|
183
183
|
}
|
|
184
|
-
withStructuredOutput(
|
|
184
|
+
withStructuredOutput(outputSchema, config) {
|
|
185
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
186
|
+
let schema;
|
|
187
|
+
let name;
|
|
188
|
+
let method;
|
|
189
|
+
let includeRaw;
|
|
190
|
+
if (isStructuredOutputMethodParams(outputSchema)) {
|
|
191
|
+
schema = outputSchema.schema;
|
|
192
|
+
name = outputSchema.name;
|
|
193
|
+
method = outputSchema.method;
|
|
194
|
+
includeRaw = outputSchema.includeRaw;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
schema = outputSchema;
|
|
198
|
+
name = config?.name;
|
|
199
|
+
method = config?.method;
|
|
200
|
+
includeRaw = config?.includeRaw;
|
|
201
|
+
}
|
|
185
202
|
if (method === "jsonMode") {
|
|
186
203
|
throw new Error(`Anthropic only supports "functionCalling" as a method.`);
|
|
187
204
|
}
|
|
188
205
|
const functionName = name ?? "extract";
|
|
189
|
-
|
|
190
|
-
returnSingle: true,
|
|
191
|
-
keyName: functionName,
|
|
192
|
-
});
|
|
206
|
+
let outputParser;
|
|
193
207
|
let tools;
|
|
194
208
|
if (isZodSchema(schema)) {
|
|
195
209
|
const jsonSchema = zodToJsonSchema(schema);
|
|
@@ -203,6 +217,11 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
203
217
|
},
|
|
204
218
|
},
|
|
205
219
|
];
|
|
220
|
+
outputParser = new JsonOutputKeyToolsParser({
|
|
221
|
+
returnSingle: true,
|
|
222
|
+
keyName: functionName,
|
|
223
|
+
zodSchema: schema,
|
|
224
|
+
});
|
|
206
225
|
}
|
|
207
226
|
else {
|
|
208
227
|
tools = [
|
|
@@ -215,6 +234,10 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
215
234
|
},
|
|
216
235
|
},
|
|
217
236
|
];
|
|
237
|
+
outputParser = new JsonOutputKeyToolsParser({
|
|
238
|
+
returnSingle: true,
|
|
239
|
+
keyName: functionName,
|
|
240
|
+
});
|
|
218
241
|
}
|
|
219
242
|
const llm = this.bind({
|
|
220
243
|
tools,
|
|
@@ -250,7 +273,17 @@ export class ChatAnthropicTools extends BaseChatModel {
|
|
|
250
273
|
});
|
|
251
274
|
}
|
|
252
275
|
}
|
|
253
|
-
function isZodSchema(
|
|
276
|
+
function isZodSchema(
|
|
277
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
278
|
+
input) {
|
|
254
279
|
// Check for a characteristic method of Zod schemas
|
|
255
280
|
return typeof input?.parse === "function";
|
|
256
281
|
}
|
|
282
|
+
function isStructuredOutputMethodParams(x
|
|
283
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
284
|
+
) {
|
|
285
|
+
return (x !== undefined &&
|
|
286
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
287
|
+
typeof x.schema ===
|
|
288
|
+
"object");
|
|
289
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/anthropic",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Anthropic integrations for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"license": "MIT",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@anthropic-ai/sdk": "^0.15.0",
|
|
42
|
-
"@langchain/core": "~0.1",
|
|
42
|
+
"@langchain/core": "~0.1.44",
|
|
43
43
|
"fast-xml-parser": "^4.3.5",
|
|
44
44
|
"zod": "^3.22.4",
|
|
45
45
|
"zod-to-json-schema": "^3.22.4"
|