@langchain/anthropic 0.1.3 → 0.1.4
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,7 +184,24 @@ 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
|
}
|
|
@@ -258,3 +275,11 @@ function isZodSchema(input) {
|
|
|
258
275
|
// Check for a characteristic method of Zod schemas
|
|
259
276
|
return typeof input?.parse === "function";
|
|
260
277
|
}
|
|
278
|
+
function isStructuredOutputMethodParams(x
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
280
|
+
) {
|
|
281
|
+
return (x !== undefined &&
|
|
282
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
283
|
+
typeof x.schema ===
|
|
284
|
+
"object");
|
|
285
|
+
}
|
|
@@ -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,7 +181,24 @@ 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
|
}
|
|
@@ -254,3 +271,11 @@ function isZodSchema(input) {
|
|
|
254
271
|
// Check for a characteristic method of Zod schemas
|
|
255
272
|
return typeof input?.parse === "function";
|
|
256
273
|
}
|
|
274
|
+
function isStructuredOutputMethodParams(x
|
|
275
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
276
|
+
) {
|
|
277
|
+
return (x !== undefined &&
|
|
278
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
279
|
+
typeof x.schema ===
|
|
280
|
+
"object");
|
|
281
|
+
}
|