@langchain/core 0.2.7 → 0.2.9
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/caches.cjs +2 -2
- package/dist/caches.d.ts +1 -1
- package/dist/caches.js +1 -1
- package/dist/callbacks/base.d.ts +1 -1
- package/dist/callbacks/manager.cjs +2 -2
- package/dist/callbacks/manager.d.ts +1 -1
- package/dist/callbacks/manager.js +1 -1
- package/dist/language_models/base.cjs +23 -3
- package/dist/language_models/base.d.ts +7 -1
- package/dist/language_models/base.js +20 -1
- package/dist/language_models/chat_models.cjs +81 -0
- package/dist/language_models/chat_models.d.ts +10 -4
- package/dist/language_models/chat_models.js +81 -0
- package/dist/messages/ai.cjs +2 -1
- package/dist/messages/ai.js +2 -1
- package/dist/messages/base.cjs +11 -0
- package/dist/messages/base.d.ts +11 -0
- package/dist/messages/base.js +11 -0
- package/dist/messages/chat.cjs +1 -0
- package/dist/messages/chat.js +1 -0
- package/dist/messages/function.cjs +1 -0
- package/dist/messages/function.js +1 -0
- package/dist/messages/human.cjs +1 -0
- package/dist/messages/human.js +1 -0
- package/dist/messages/index.cjs +1 -0
- package/dist/messages/index.d.ts +1 -0
- package/dist/messages/index.js +1 -0
- package/dist/messages/system.cjs +1 -0
- package/dist/messages/system.js +1 -0
- package/dist/messages/tests/base_message.test.js +16 -0
- package/dist/messages/tests/message_utils.test.d.ts +1 -0
- package/dist/messages/tests/message_utils.test.js +382 -0
- package/dist/messages/tool.cjs +1 -0
- package/dist/messages/tool.js +1 -0
- package/dist/messages/transformers.cjs +421 -0
- package/dist/messages/transformers.d.ts +465 -0
- package/dist/messages/transformers.js +414 -0
- package/dist/messages/utils.cjs +17 -4
- package/dist/messages/utils.d.ts +3 -3
- package/dist/messages/utils.js +17 -4
- package/dist/outputs.d.ts +1 -1
- package/dist/prompt_values.cjs +5 -4
- package/dist/prompt_values.d.ts +2 -1
- package/dist/prompt_values.js +2 -1
- package/dist/prompts/template.cjs +1 -1
- package/dist/prompts/template.js +1 -1
- package/dist/tracers/base.d.ts +1 -1
- package/dist/tracers/log_stream.cjs +2 -2
- package/dist/tracers/log_stream.js +1 -1
- package/dist/tracers/tracer_langchain_v1.cjs +2 -2
- package/dist/tracers/tracer_langchain_v1.js +1 -1
- package/package.json +1 -1
package/dist/caches.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.InMemoryCache = exports.BaseCache = exports.serializeGeneration = exports.deserializeStoredGeneration = exports.getCacheKey = void 0;
|
|
4
4
|
const hash_js_1 = require("./utils/hash.cjs");
|
|
5
|
-
const
|
|
5
|
+
const utils_js_1 = require("./messages/utils.cjs");
|
|
6
6
|
/**
|
|
7
7
|
* This cache key should be consistent across all versions of langchain.
|
|
8
8
|
* It is currently NOT consistent across versions of langchain.
|
|
@@ -19,7 +19,7 @@ function deserializeStoredGeneration(storedGeneration) {
|
|
|
19
19
|
if (storedGeneration.message !== undefined) {
|
|
20
20
|
return {
|
|
21
21
|
text: storedGeneration.text,
|
|
22
|
-
message: (0,
|
|
22
|
+
message: (0, utils_js_1.mapStoredMessageToChatMessage)(storedGeneration.message),
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
else {
|
package/dist/caches.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Generation } from "./outputs.js";
|
|
2
|
-
import { type StoredGeneration } from "./messages/
|
|
2
|
+
import { type StoredGeneration } from "./messages/base.js";
|
|
3
3
|
/**
|
|
4
4
|
* This cache key should be consistent across all versions of langchain.
|
|
5
5
|
* It is currently NOT consistent across versions of langchain.
|
package/dist/caches.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { insecureHash } from "./utils/hash.js";
|
|
2
|
-
import { mapStoredMessageToChatMessage
|
|
2
|
+
import { mapStoredMessageToChatMessage } from "./messages/utils.js";
|
|
3
3
|
/**
|
|
4
4
|
* This cache key should be consistent across all versions of langchain.
|
|
5
5
|
* It is currently NOT consistent across versions of langchain.
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChainValues } from "../utils/types/index.js";
|
|
2
|
-
import type { BaseMessage } from "../messages/
|
|
2
|
+
import type { BaseMessage } from "../messages/base.js";
|
|
3
3
|
import type { AgentAction, AgentFinish } from "../agents.js";
|
|
4
4
|
import type { ChatGenerationChunk, GenerationChunk, LLMResult } from "../outputs.js";
|
|
5
5
|
import { Serializable, Serialized, SerializedNotImplemented } from "../load/serializable.js";
|
|
@@ -5,7 +5,7 @@ const uuid_1 = require("uuid");
|
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
6
|
const console_js_1 = require("../tracers/console.cjs");
|
|
7
7
|
const initialize_js_1 = require("../tracers/initialize.cjs");
|
|
8
|
-
const
|
|
8
|
+
const utils_js_1 = require("../messages/utils.cjs");
|
|
9
9
|
const env_js_1 = require("../utils/env.cjs");
|
|
10
10
|
const tracer_langchain_js_1 = require("../tracers/tracer_langchain.cjs");
|
|
11
11
|
const promises_js_1 = require("./promises.cjs");
|
|
@@ -439,7 +439,7 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
439
439
|
await handler.handleChatModelStart?.(llm, [messageGroup], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
440
440
|
}
|
|
441
441
|
else if (handler.handleLLMStart) {
|
|
442
|
-
const messageString = (0,
|
|
442
|
+
const messageString = (0, utils_js_1.getBufferString)(messageGroup);
|
|
443
443
|
await handler.handleLLMStart?.(llm, [messageString], runId_, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
444
444
|
}
|
|
445
445
|
}
|
|
@@ -2,7 +2,7 @@ import { AgentAction, AgentFinish } from "../agents.js";
|
|
|
2
2
|
import type { ChainValues } from "../utils/types/index.js";
|
|
3
3
|
import { LLMResult } from "../outputs.js";
|
|
4
4
|
import { BaseCallbackHandler, CallbackHandlerMethods, HandleLLMNewTokenCallbackFields, NewTokenIndices } from "./base.js";
|
|
5
|
-
import { type BaseMessage } from "../messages/
|
|
5
|
+
import { type BaseMessage } from "../messages/base.js";
|
|
6
6
|
import { LangChainTracerFields } from "../tracers/tracer_langchain.js";
|
|
7
7
|
import { Serialized } from "../load/serializable.js";
|
|
8
8
|
import type { DocumentInterface } from "../documents/document.js";
|
|
@@ -2,7 +2,7 @@ import { v4 as uuidv4 } from "uuid";
|
|
|
2
2
|
import { BaseCallbackHandler, } from "./base.js";
|
|
3
3
|
import { ConsoleCallbackHandler } from "../tracers/console.js";
|
|
4
4
|
import { getTracingV2CallbackHandler } from "../tracers/initialize.js";
|
|
5
|
-
import { getBufferString } from "../messages/
|
|
5
|
+
import { getBufferString } from "../messages/utils.js";
|
|
6
6
|
import { getEnvironmentVariable } from "../utils/env.js";
|
|
7
7
|
import { LangChainTracer, } from "../tracers/tracer_langchain.js";
|
|
8
8
|
import { consumeCallback } from "./promises.js";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseLanguageModel = exports.BaseLangChain = exports.calculateMaxTokens = exports.getModelContextSize = exports.getEmbeddingContextSize = exports.getModelNameForTiktoken = void 0;
|
|
3
|
+
exports.BaseLanguageModel = exports.BaseLangChain = exports.calculateMaxTokens = exports.isOpenAITool = exports.getModelContextSize = exports.getEmbeddingContextSize = exports.getModelNameForTiktoken = void 0;
|
|
4
4
|
const caches_js_1 = require("../caches.cjs");
|
|
5
5
|
const prompt_values_js_1 = require("../prompt_values.cjs");
|
|
6
|
-
const
|
|
6
|
+
const utils_js_1 = require("../messages/utils.cjs");
|
|
7
7
|
const async_caller_js_1 = require("../utils/async_caller.cjs");
|
|
8
8
|
const tiktoken_js_1 = require("../utils/tiktoken.cjs");
|
|
9
9
|
const base_js_1 = require("../runnables/base.cjs");
|
|
@@ -63,6 +63,26 @@ const getModelContextSize = (modelName) => {
|
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
65
|
exports.getModelContextSize = getModelContextSize;
|
|
66
|
+
/**
|
|
67
|
+
* Whether or not the input matches the OpenAI tool definition.
|
|
68
|
+
* @param {unknown} tool The input to check.
|
|
69
|
+
* @returns {boolean} Whether the input is an OpenAI tool definition.
|
|
70
|
+
*/
|
|
71
|
+
function isOpenAITool(tool) {
|
|
72
|
+
if (typeof tool !== "object" || !tool)
|
|
73
|
+
return false;
|
|
74
|
+
if ("type" in tool &&
|
|
75
|
+
tool.type === "function" &&
|
|
76
|
+
"function" in tool &&
|
|
77
|
+
typeof tool.function === "object" &&
|
|
78
|
+
tool.function &&
|
|
79
|
+
"name" in tool.function &&
|
|
80
|
+
"parameters" in tool.function) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
exports.isOpenAITool = isOpenAITool;
|
|
66
86
|
const calculateMaxTokens = async ({ prompt, modelName, }) => {
|
|
67
87
|
let numTokens;
|
|
68
88
|
try {
|
|
@@ -205,7 +225,7 @@ class BaseLanguageModel extends BaseLangChain {
|
|
|
205
225
|
return new prompt_values_js_1.StringPromptValue(input);
|
|
206
226
|
}
|
|
207
227
|
else if (Array.isArray(input)) {
|
|
208
|
-
return new prompt_values_js_1.ChatPromptValue(input.map(
|
|
228
|
+
return new prompt_values_js_1.ChatPromptValue(input.map(utils_js_1.coerceMessageLikeToMessage));
|
|
209
229
|
}
|
|
210
230
|
else {
|
|
211
231
|
return input;
|
|
@@ -2,7 +2,7 @@ import type { TiktokenModel } from "js-tiktoken/lite";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { type BaseCache } from "../caches.js";
|
|
4
4
|
import { type BasePromptValueInterface } from "../prompt_values.js";
|
|
5
|
-
import { type BaseMessage, type BaseMessageLike, type MessageContent } from "../messages/
|
|
5
|
+
import { type BaseMessage, type BaseMessageLike, type MessageContent } from "../messages/base.js";
|
|
6
6
|
import { type LLMResult } from "../outputs.js";
|
|
7
7
|
import { CallbackManager, Callbacks } from "../callbacks/manager.js";
|
|
8
8
|
import { AsyncCaller, AsyncCallerParams } from "../utils/async_caller.js";
|
|
@@ -11,6 +11,12 @@ import { RunnableConfig } from "../runnables/config.js";
|
|
|
11
11
|
export declare const getModelNameForTiktoken: (modelName: string) => TiktokenModel;
|
|
12
12
|
export declare const getEmbeddingContextSize: (modelName?: string) => number;
|
|
13
13
|
export declare const getModelContextSize: (modelName: string) => number;
|
|
14
|
+
/**
|
|
15
|
+
* Whether or not the input matches the OpenAI tool definition.
|
|
16
|
+
* @param {unknown} tool The input to check.
|
|
17
|
+
* @returns {boolean} Whether the input is an OpenAI tool definition.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isOpenAITool(tool: unknown): tool is ToolDefinition;
|
|
14
20
|
interface CalculateMaxTokenProps {
|
|
15
21
|
prompt: string;
|
|
16
22
|
modelName: TiktokenModel;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InMemoryCache } from "../caches.js";
|
|
2
2
|
import { StringPromptValue, ChatPromptValue, } from "../prompt_values.js";
|
|
3
|
-
import { coerceMessageLikeToMessage
|
|
3
|
+
import { coerceMessageLikeToMessage } from "../messages/utils.js";
|
|
4
4
|
import { AsyncCaller } from "../utils/async_caller.js";
|
|
5
5
|
import { encodingForModel } from "../utils/tiktoken.js";
|
|
6
6
|
import { Runnable } from "../runnables/base.js";
|
|
@@ -57,6 +57,25 @@ export const getModelContextSize = (modelName) => {
|
|
|
57
57
|
return 4097;
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* Whether or not the input matches the OpenAI tool definition.
|
|
62
|
+
* @param {unknown} tool The input to check.
|
|
63
|
+
* @returns {boolean} Whether the input is an OpenAI tool definition.
|
|
64
|
+
*/
|
|
65
|
+
export function isOpenAITool(tool) {
|
|
66
|
+
if (typeof tool !== "object" || !tool)
|
|
67
|
+
return false;
|
|
68
|
+
if ("type" in tool &&
|
|
69
|
+
tool.type === "function" &&
|
|
70
|
+
"function" in tool &&
|
|
71
|
+
typeof tool.function === "object" &&
|
|
72
|
+
tool.function &&
|
|
73
|
+
"name" in tool.function &&
|
|
74
|
+
"parameters" in tool.function) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
60
79
|
export const calculateMaxTokens = async ({ prompt, modelName, }) => {
|
|
61
80
|
let numTokens;
|
|
62
81
|
try {
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SimpleChatModel = exports.BaseChatModel = exports.createChatMessageChunkEncoderStream = void 0;
|
|
4
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
4
5
|
const index_js_1 = require("../messages/index.cjs");
|
|
5
6
|
const outputs_js_1 = require("../outputs.cjs");
|
|
6
7
|
const base_js_1 = require("./base.cjs");
|
|
7
8
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
9
|
+
const base_js_2 = require("../runnables/base.cjs");
|
|
8
10
|
const event_stream_js_1 = require("../tracers/event_stream.cjs");
|
|
9
11
|
const log_stream_js_1 = require("../tracers/log_stream.cjs");
|
|
10
12
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
13
|
+
const passthrough_js_1 = require("../runnables/passthrough.cjs");
|
|
14
|
+
const is_zod_schema_js_1 = require("../utils/types/is_zod_schema.cjs");
|
|
11
15
|
/**
|
|
12
16
|
* Creates a transform stream for encoding chat message chunks.
|
|
13
17
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -415,6 +419,83 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
415
419
|
}
|
|
416
420
|
return result.content;
|
|
417
421
|
}
|
|
422
|
+
withStructuredOutput(outputSchema, config) {
|
|
423
|
+
if (typeof this.bindTools !== "function") {
|
|
424
|
+
throw new Error(`Chat model must implement ".bindTools()" to use withStructuredOutput.`);
|
|
425
|
+
}
|
|
426
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
427
|
+
const schema = outputSchema;
|
|
428
|
+
const name = config?.name;
|
|
429
|
+
const description = schema.description ?? "A function available to call.";
|
|
430
|
+
const method = config?.method;
|
|
431
|
+
const includeRaw = config?.includeRaw;
|
|
432
|
+
if (method === "jsonMode") {
|
|
433
|
+
throw new Error(`Base withStructuredOutput implementation only supports "functionCalling" as a method.`);
|
|
434
|
+
}
|
|
435
|
+
let functionName = name ?? "extract";
|
|
436
|
+
let tools;
|
|
437
|
+
if ((0, is_zod_schema_js_1.isZodSchema)(schema)) {
|
|
438
|
+
tools = [
|
|
439
|
+
{
|
|
440
|
+
type: "function",
|
|
441
|
+
function: {
|
|
442
|
+
name: functionName,
|
|
443
|
+
description,
|
|
444
|
+
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(schema),
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
];
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
if ("name" in schema) {
|
|
451
|
+
functionName = schema.name;
|
|
452
|
+
}
|
|
453
|
+
tools = [
|
|
454
|
+
{
|
|
455
|
+
type: "function",
|
|
456
|
+
function: {
|
|
457
|
+
name: functionName,
|
|
458
|
+
description,
|
|
459
|
+
parameters: schema,
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
];
|
|
463
|
+
}
|
|
464
|
+
const llm = this.bindTools(tools);
|
|
465
|
+
const outputParser = base_js_2.RunnableLambda.from((input) => {
|
|
466
|
+
if (!input.tool_calls || input.tool_calls.length === 0) {
|
|
467
|
+
throw new Error("No tool calls found in the response.");
|
|
468
|
+
}
|
|
469
|
+
const toolCall = input.tool_calls.find((tc) => tc.name === functionName);
|
|
470
|
+
if (!toolCall) {
|
|
471
|
+
throw new Error(`No tool call found with name ${functionName}.`);
|
|
472
|
+
}
|
|
473
|
+
return toolCall.args;
|
|
474
|
+
});
|
|
475
|
+
if (!includeRaw) {
|
|
476
|
+
return llm.pipe(outputParser).withConfig({
|
|
477
|
+
runName: "StructuredOutput",
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
const parserAssign = passthrough_js_1.RunnablePassthrough.assign({
|
|
481
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
482
|
+
parsed: (input, config) => outputParser.invoke(input.raw, config),
|
|
483
|
+
});
|
|
484
|
+
const parserNone = passthrough_js_1.RunnablePassthrough.assign({
|
|
485
|
+
parsed: () => null,
|
|
486
|
+
});
|
|
487
|
+
const parsedWithFallback = parserAssign.withFallbacks({
|
|
488
|
+
fallbacks: [parserNone],
|
|
489
|
+
});
|
|
490
|
+
return base_js_2.RunnableSequence.from([
|
|
491
|
+
{
|
|
492
|
+
raw: llm,
|
|
493
|
+
},
|
|
494
|
+
parsedWithFallback,
|
|
495
|
+
]).withConfig({
|
|
496
|
+
runName: "StructuredOutputRunnable",
|
|
497
|
+
});
|
|
498
|
+
}
|
|
418
499
|
}
|
|
419
500
|
exports.BaseChatModel = BaseChatModel;
|
|
420
501
|
/**
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { type BaseMessage, BaseMessageChunk, type BaseMessageLike } from "../messages/index.js";
|
|
2
3
|
import type { BasePromptValueInterface } from "../prompt_values.js";
|
|
3
4
|
import { LLMResult, ChatGenerationChunk, type ChatResult, type Generation } from "../outputs.js";
|
|
4
|
-
import { BaseLanguageModel, type BaseLanguageModelCallOptions, type BaseLanguageModelInput, type BaseLanguageModelParams } from "./base.js";
|
|
5
|
+
import { BaseLanguageModel, StructuredOutputMethodOptions, ToolDefinition, type BaseLanguageModelCallOptions, type BaseLanguageModelInput, type BaseLanguageModelParams } from "./base.js";
|
|
5
6
|
import { type CallbackManagerForLLMRun, type Callbacks } from "../callbacks/manager.js";
|
|
6
7
|
import type { RunnableConfig } from "../runnables/config.js";
|
|
7
8
|
import type { BaseCache } from "../caches.js";
|
|
@@ -64,11 +65,11 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
|
|
|
64
65
|
* Bind tool-like objects to this chat model.
|
|
65
66
|
*
|
|
66
67
|
* @param tools A list of tool definitions to bind to this chat model.
|
|
67
|
-
* Can be a structured tool or an object
|
|
68
|
-
* specific tool schema.
|
|
68
|
+
* Can be a structured tool, an OpenAI formatted tool, or an object
|
|
69
|
+
* matching the provider's specific tool schema.
|
|
69
70
|
* @param kwargs Any additional parameters to bind.
|
|
70
71
|
*/
|
|
71
|
-
bindTools?(tools: (StructuredToolInterface | Record<string, unknown>)[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>;
|
|
72
|
+
bindTools?(tools: (StructuredToolInterface | Record<string, unknown> | ToolDefinition)[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, OutputMessageType, CallOptions>;
|
|
72
73
|
/**
|
|
73
74
|
* Invokes the chat model with a single input.
|
|
74
75
|
* @param input The input for the language model.
|
|
@@ -152,6 +153,11 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
|
|
|
152
153
|
* @returns A Promise that resolves to a string.
|
|
153
154
|
*/
|
|
154
155
|
predict(text: string, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<string>;
|
|
156
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
157
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
158
|
+
raw: BaseMessage;
|
|
159
|
+
parsed: RunOutput;
|
|
160
|
+
}>;
|
|
155
161
|
}
|
|
156
162
|
/**
|
|
157
163
|
* An abstract class that extends BaseChatModel and provides a simple
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
1
2
|
import { AIMessage, HumanMessage, coerceMessageLikeToMessage, } from "../messages/index.js";
|
|
2
3
|
import { RUN_KEY, } from "../outputs.js";
|
|
3
4
|
import { BaseLanguageModel, } from "./base.js";
|
|
4
5
|
import { CallbackManager, } from "../callbacks/manager.js";
|
|
6
|
+
import { RunnableLambda, RunnableSequence, } from "../runnables/base.js";
|
|
5
7
|
import { isStreamEventsHandler } from "../tracers/event_stream.js";
|
|
6
8
|
import { isLogStreamHandler } from "../tracers/log_stream.js";
|
|
7
9
|
import { concat } from "../utils/stream.js";
|
|
10
|
+
import { RunnablePassthrough } from "../runnables/passthrough.js";
|
|
11
|
+
import { isZodSchema } from "../utils/types/is_zod_schema.js";
|
|
8
12
|
/**
|
|
9
13
|
* Creates a transform stream for encoding chat message chunks.
|
|
10
14
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -411,6 +415,83 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
411
415
|
}
|
|
412
416
|
return result.content;
|
|
413
417
|
}
|
|
418
|
+
withStructuredOutput(outputSchema, config) {
|
|
419
|
+
if (typeof this.bindTools !== "function") {
|
|
420
|
+
throw new Error(`Chat model must implement ".bindTools()" to use withStructuredOutput.`);
|
|
421
|
+
}
|
|
422
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
423
|
+
const schema = outputSchema;
|
|
424
|
+
const name = config?.name;
|
|
425
|
+
const description = schema.description ?? "A function available to call.";
|
|
426
|
+
const method = config?.method;
|
|
427
|
+
const includeRaw = config?.includeRaw;
|
|
428
|
+
if (method === "jsonMode") {
|
|
429
|
+
throw new Error(`Base withStructuredOutput implementation only supports "functionCalling" as a method.`);
|
|
430
|
+
}
|
|
431
|
+
let functionName = name ?? "extract";
|
|
432
|
+
let tools;
|
|
433
|
+
if (isZodSchema(schema)) {
|
|
434
|
+
tools = [
|
|
435
|
+
{
|
|
436
|
+
type: "function",
|
|
437
|
+
function: {
|
|
438
|
+
name: functionName,
|
|
439
|
+
description,
|
|
440
|
+
parameters: zodToJsonSchema(schema),
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
];
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
if ("name" in schema) {
|
|
447
|
+
functionName = schema.name;
|
|
448
|
+
}
|
|
449
|
+
tools = [
|
|
450
|
+
{
|
|
451
|
+
type: "function",
|
|
452
|
+
function: {
|
|
453
|
+
name: functionName,
|
|
454
|
+
description,
|
|
455
|
+
parameters: schema,
|
|
456
|
+
},
|
|
457
|
+
},
|
|
458
|
+
];
|
|
459
|
+
}
|
|
460
|
+
const llm = this.bindTools(tools);
|
|
461
|
+
const outputParser = RunnableLambda.from((input) => {
|
|
462
|
+
if (!input.tool_calls || input.tool_calls.length === 0) {
|
|
463
|
+
throw new Error("No tool calls found in the response.");
|
|
464
|
+
}
|
|
465
|
+
const toolCall = input.tool_calls.find((tc) => tc.name === functionName);
|
|
466
|
+
if (!toolCall) {
|
|
467
|
+
throw new Error(`No tool call found with name ${functionName}.`);
|
|
468
|
+
}
|
|
469
|
+
return toolCall.args;
|
|
470
|
+
});
|
|
471
|
+
if (!includeRaw) {
|
|
472
|
+
return llm.pipe(outputParser).withConfig({
|
|
473
|
+
runName: "StructuredOutput",
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
const parserAssign = RunnablePassthrough.assign({
|
|
477
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
478
|
+
parsed: (input, config) => outputParser.invoke(input.raw, config),
|
|
479
|
+
});
|
|
480
|
+
const parserNone = RunnablePassthrough.assign({
|
|
481
|
+
parsed: () => null,
|
|
482
|
+
});
|
|
483
|
+
const parsedWithFallback = parserAssign.withFallbacks({
|
|
484
|
+
fallbacks: [parserNone],
|
|
485
|
+
});
|
|
486
|
+
return RunnableSequence.from([
|
|
487
|
+
{
|
|
488
|
+
raw: llm,
|
|
489
|
+
},
|
|
490
|
+
parsedWithFallback,
|
|
491
|
+
]).withConfig({
|
|
492
|
+
runName: "StructuredOutputRunnable",
|
|
493
|
+
});
|
|
494
|
+
}
|
|
414
495
|
}
|
|
415
496
|
/**
|
|
416
497
|
* An abstract class that extends BaseChatModel and provides a simple
|
package/dist/messages/ai.cjs
CHANGED
|
@@ -122,7 +122,7 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
122
122
|
else if (fields.tool_call_chunks === undefined) {
|
|
123
123
|
initParams = {
|
|
124
124
|
...fields,
|
|
125
|
-
tool_calls: [],
|
|
125
|
+
tool_calls: fields.tool_calls ?? [],
|
|
126
126
|
invalid_tool_calls: [],
|
|
127
127
|
tool_call_chunks: [],
|
|
128
128
|
};
|
|
@@ -219,6 +219,7 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
219
219
|
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
220
220
|
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
221
221
|
tool_call_chunks: [],
|
|
222
|
+
id: this.id ?? chunk.id,
|
|
222
223
|
};
|
|
223
224
|
if (this.tool_call_chunks !== undefined ||
|
|
224
225
|
chunk.tool_call_chunks !== undefined) {
|
package/dist/messages/ai.js
CHANGED
|
@@ -117,7 +117,7 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
117
117
|
else if (fields.tool_call_chunks === undefined) {
|
|
118
118
|
initParams = {
|
|
119
119
|
...fields,
|
|
120
|
-
tool_calls: [],
|
|
120
|
+
tool_calls: fields.tool_calls ?? [],
|
|
121
121
|
invalid_tool_calls: [],
|
|
122
122
|
tool_call_chunks: [],
|
|
123
123
|
};
|
|
@@ -214,6 +214,7 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
214
214
|
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
215
215
|
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
216
216
|
tool_call_chunks: [],
|
|
217
|
+
id: this.id ?? chunk.id,
|
|
217
218
|
};
|
|
218
219
|
if (this.tool_call_chunks !== undefined ||
|
|
219
220
|
chunk.tool_call_chunks !== undefined) {
|
package/dist/messages/base.cjs
CHANGED
|
@@ -104,10 +104,21 @@ class BaseMessage extends serializable_js_1.Serializable {
|
|
|
104
104
|
writable: true,
|
|
105
105
|
value: void 0
|
|
106
106
|
});
|
|
107
|
+
/**
|
|
108
|
+
* An optional unique identifier for the message. This should ideally be
|
|
109
|
+
* provided by the provider/model which created the message.
|
|
110
|
+
*/
|
|
111
|
+
Object.defineProperty(this, "id", {
|
|
112
|
+
enumerable: true,
|
|
113
|
+
configurable: true,
|
|
114
|
+
writable: true,
|
|
115
|
+
value: void 0
|
|
116
|
+
});
|
|
107
117
|
this.name = fields.name;
|
|
108
118
|
this.content = fields.content;
|
|
109
119
|
this.additional_kwargs = fields.additional_kwargs;
|
|
110
120
|
this.response_metadata = fields.response_metadata;
|
|
121
|
+
this.id = fields.id;
|
|
111
122
|
}
|
|
112
123
|
toDict() {
|
|
113
124
|
return {
|
package/dist/messages/base.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface StoredMessageData {
|
|
|
8
8
|
additional_kwargs?: Record<string, any>;
|
|
9
9
|
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
10
10
|
response_metadata?: Record<string, any>;
|
|
11
|
+
id?: string;
|
|
11
12
|
}
|
|
12
13
|
export interface StoredMessage {
|
|
13
14
|
type: string;
|
|
@@ -82,6 +83,11 @@ export type BaseMessageFields = {
|
|
|
82
83
|
};
|
|
83
84
|
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
84
85
|
response_metadata?: Record<string, any>;
|
|
86
|
+
/**
|
|
87
|
+
* An optional unique identifier for the message. This should ideally be
|
|
88
|
+
* provided by the provider/model which created the message.
|
|
89
|
+
*/
|
|
90
|
+
id?: string;
|
|
85
91
|
};
|
|
86
92
|
export declare function mergeContent(firstContent: MessageContent, secondContent: MessageContent): MessageContent;
|
|
87
93
|
/**
|
|
@@ -106,6 +112,11 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
|
|
|
106
112
|
additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;
|
|
107
113
|
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
108
114
|
response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;
|
|
115
|
+
/**
|
|
116
|
+
* An optional unique identifier for the message. This should ideally be
|
|
117
|
+
* provided by the provider/model which created the message.
|
|
118
|
+
*/
|
|
119
|
+
id?: string;
|
|
109
120
|
/** The type of the message. */
|
|
110
121
|
abstract _getType(): MessageType;
|
|
111
122
|
constructor(fields: string | BaseMessageFields,
|
package/dist/messages/base.js
CHANGED
|
@@ -100,10 +100,21 @@ export class BaseMessage extends Serializable {
|
|
|
100
100
|
writable: true,
|
|
101
101
|
value: void 0
|
|
102
102
|
});
|
|
103
|
+
/**
|
|
104
|
+
* An optional unique identifier for the message. This should ideally be
|
|
105
|
+
* provided by the provider/model which created the message.
|
|
106
|
+
*/
|
|
107
|
+
Object.defineProperty(this, "id", {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: void 0
|
|
112
|
+
});
|
|
103
113
|
this.name = fields.name;
|
|
104
114
|
this.content = fields.content;
|
|
105
115
|
this.additional_kwargs = fields.additional_kwargs;
|
|
106
116
|
this.response_metadata = fields.response_metadata;
|
|
117
|
+
this.id = fields.id;
|
|
107
118
|
}
|
|
108
119
|
toDict() {
|
|
109
120
|
return {
|
package/dist/messages/chat.cjs
CHANGED
|
@@ -65,6 +65,7 @@ class ChatMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
65
65
|
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
66
66
|
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
67
67
|
role: this.role,
|
|
68
|
+
id: this.id ?? chunk.id,
|
|
68
69
|
});
|
|
69
70
|
}
|
|
70
71
|
}
|
package/dist/messages/chat.js
CHANGED
|
@@ -61,6 +61,7 @@ export class ChatMessageChunk extends BaseMessageChunk {
|
|
|
61
61
|
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
62
62
|
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
63
63
|
role: this.role,
|
|
64
|
+
id: this.id ?? chunk.id,
|
|
64
65
|
});
|
|
65
66
|
}
|
|
66
67
|
}
|
|
@@ -40,6 +40,7 @@ class FunctionMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
40
40
|
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
41
41
|
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
42
42
|
name: this.name ?? "",
|
|
43
|
+
id: this.id ?? chunk.id,
|
|
43
44
|
});
|
|
44
45
|
}
|
|
45
46
|
}
|
|
@@ -36,6 +36,7 @@ export class FunctionMessageChunk extends BaseMessageChunk {
|
|
|
36
36
|
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
37
37
|
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
38
38
|
name: this.name ?? "",
|
|
39
|
+
id: this.id ?? chunk.id,
|
|
39
40
|
});
|
|
40
41
|
}
|
|
41
42
|
}
|
package/dist/messages/human.cjs
CHANGED
|
@@ -30,6 +30,7 @@ class HumanMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
30
30
|
content: (0, base_js_1.mergeContent)(this.content, chunk.content),
|
|
31
31
|
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
32
32
|
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
33
|
+
id: this.id ?? chunk.id,
|
|
33
34
|
});
|
|
34
35
|
}
|
|
35
36
|
}
|
package/dist/messages/human.js
CHANGED
|
@@ -26,6 +26,7 @@ export class HumanMessageChunk extends BaseMessageChunk {
|
|
|
26
26
|
content: mergeContent(this.content, chunk.content),
|
|
27
27
|
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
28
28
|
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
29
|
+
id: this.id ?? chunk.id,
|
|
29
30
|
});
|
|
30
31
|
}
|
|
31
32
|
}
|
package/dist/messages/index.cjs
CHANGED
|
@@ -22,6 +22,7 @@ __exportStar(require("./function.cjs"), exports);
|
|
|
22
22
|
__exportStar(require("./human.cjs"), exports);
|
|
23
23
|
__exportStar(require("./system.cjs"), exports);
|
|
24
24
|
__exportStar(require("./utils.cjs"), exports);
|
|
25
|
+
__exportStar(require("./transformers.cjs"), exports);
|
|
25
26
|
// TODO: Use a star export when we deprecate the
|
|
26
27
|
// existing "ToolCall" type in "base.js".
|
|
27
28
|
var tool_js_1 = require("./tool.cjs");
|
package/dist/messages/index.d.ts
CHANGED
package/dist/messages/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./function.js";
|
|
|
5
5
|
export * from "./human.js";
|
|
6
6
|
export * from "./system.js";
|
|
7
7
|
export * from "./utils.js";
|
|
8
|
+
export * from "./transformers.js";
|
|
8
9
|
// TODO: Use a star export when we deprecate the
|
|
9
10
|
// existing "ToolCall" type in "base.js".
|
|
10
11
|
export { ToolMessage, ToolMessageChunk, } from "./tool.js";
|
package/dist/messages/system.cjs
CHANGED
|
@@ -30,6 +30,7 @@ class SystemMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
30
30
|
content: (0, base_js_1.mergeContent)(this.content, chunk.content),
|
|
31
31
|
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
32
32
|
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
33
|
+
id: this.id ?? chunk.id,
|
|
33
34
|
});
|
|
34
35
|
}
|
|
35
36
|
}
|
package/dist/messages/system.js
CHANGED
|
@@ -26,6 +26,7 @@ export class SystemMessageChunk extends BaseMessageChunk {
|
|
|
26
26
|
content: mergeContent(this.content, chunk.content),
|
|
27
27
|
additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
|
|
28
28
|
response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
|
|
29
|
+
id: this.id ?? chunk.id,
|
|
29
30
|
});
|
|
30
31
|
}
|
|
31
32
|
}
|