@langchain/core 0.1.39 → 0.1.41

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.
@@ -106,6 +106,13 @@ export interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {
106
106
  functions?: FunctionDefinition[];
107
107
  }
108
108
  export type BaseLanguageModelInput = BasePromptValueInterface | string | BaseMessageLike[];
109
+ export type StructuredOutputType = z.infer<z.ZodObject<any, any, any, any>>;
110
+ export type StructuredOutputMethodParams<RunOutput extends Record<string, any> = Record<string, any>, IncludeRaw extends boolean = false> = {
111
+ schema: z.ZodType<RunOutput> | Record<string, any>;
112
+ name?: string;
113
+ method?: "functionCalling" | "jsonMode";
114
+ includeRaw?: IncludeRaw;
115
+ };
109
116
  export interface BaseLanguageModelInterface<RunOutput = any, CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions> extends RunnableInterface<BaseLanguageModelInput, RunOutput, CallOptions> {
110
117
  get callKeys(): string[];
111
118
  generatePrompt(promptValues: BasePromptValueInterface[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;
@@ -177,26 +184,16 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
177
184
  * Load an LLM from a json-like object describing it.
178
185
  */
179
186
  static deserialize(_data: SerializedLLM): Promise<BaseLanguageModel>;
180
- withStructuredOutput?<RunInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name, method, includeRaw, }: {
181
- schema: z.ZodEffects<RunOutput> | Record<string, any>;
182
- name: string;
183
- method?: "functionCalling" | "jsonMode";
184
- includeRaw: true;
185
- }): Runnable<RunInput, {
187
+ withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>({ schema, name, method, includeRaw, }: StructuredOutputMethodParams<RunOutput, false>): Runnable<BaseLanguageModelInput, RunOutput>;
188
+ withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>({ schema, name, method, includeRaw, }: StructuredOutputMethodParams<RunOutput, true>): Runnable<BaseLanguageModelInput, {
186
189
  raw: BaseMessage;
187
190
  parsed: RunOutput;
188
191
  }>;
189
- withStructuredOutput?<RunInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name, method, includeRaw, }: {
190
- schema: z.ZodEffects<RunOutput> | Record<string, any>;
191
- name: string;
192
- method?: "functionCalling" | "jsonMode";
193
- includeRaw?: false;
194
- }): Runnable<RunInput, RunOutput>;
195
192
  /**
196
193
  * Model wrapper that returns outputs formatted to match the given schema.
197
194
  *
198
195
  * @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.
199
- * @template {z.ZodObject<any, any, any, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
196
+ * @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
200
197
  *
201
198
  * @param {z.ZodEffects<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.
202
199
  * @param {string} name The name of the function to call.
@@ -204,7 +201,7 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
204
201
  * @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.
205
202
  * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
206
203
  */
207
- withStructuredOutput?<RunInput extends BaseLanguageModelInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name,
204
+ withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>({ schema, name,
208
205
  /**
209
206
  * @default functionCalling
210
207
  */
@@ -212,12 +209,7 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
212
209
  /**
213
210
  * @default false
214
211
  */
215
- includeRaw, }: {
216
- schema: z.ZodEffects<RunOutput> | Record<string, any>;
217
- name: string;
218
- method?: "functionCalling" | "jsonMode";
219
- includeRaw?: boolean;
220
- }): Runnable<RunInput, RunOutput> | Runnable<RunInput, {
212
+ includeRaw, }: StructuredOutputMethodParams<RunOutput>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
221
213
  raw: BaseMessage;
222
214
  parsed: RunOutput;
223
215
  }>;
@@ -50,7 +50,7 @@ class BaseRetriever extends base_js_1.Runnable {
50
50
  throw new Error("Not implemented!");
51
51
  }
52
52
  async invoke(input, options) {
53
- return this.getRelevantDocuments(input, options);
53
+ return this.getRelevantDocuments(input, (0, config_js_1.ensureConfig)(options));
54
54
  }
55
55
  /**
56
56
  * Main method used to retrieve relevant documents. It takes a query
@@ -11,15 +11,15 @@ export interface BaseRetrieverInput {
11
11
  metadata?: Record<string, unknown>;
12
12
  verbose?: boolean;
13
13
  }
14
- export interface BaseRetrieverInterface extends RunnableInterface<string, DocumentInterface[]> {
15
- getRelevantDocuments(query: string, config?: Callbacks | BaseCallbackConfig): Promise<DocumentInterface[]>;
14
+ export interface BaseRetrieverInterface<Metadata extends Record<string, any> = Record<string, any>> extends RunnableInterface<string, DocumentInterface<Metadata>[]> {
15
+ getRelevantDocuments(query: string, config?: Callbacks | BaseCallbackConfig): Promise<DocumentInterface<Metadata>[]>;
16
16
  }
17
17
  /**
18
18
  * Abstract base class for a Document retrieval system. A retrieval system
19
19
  * is defined as something that can take string queries and return the
20
20
  * most 'relevant' Documents from some source.
21
21
  */
22
- export declare abstract class BaseRetriever extends Runnable<string, DocumentInterface[]> implements BaseRetrieverInterface {
22
+ export declare abstract class BaseRetriever<Metadata extends Record<string, any> = Record<string, any>> extends Runnable<string, DocumentInterface<Metadata>[]> implements BaseRetrieverInterface {
23
23
  callbacks?: Callbacks;
24
24
  tags?: string[];
25
25
  metadata?: Record<string, unknown>;
@@ -30,8 +30,8 @@ export declare abstract class BaseRetriever extends Runnable<string, DocumentInt
30
30
  * changes to people currently using subclassed custom retrievers.
31
31
  * Change it on next major release.
32
32
  */
33
- _getRelevantDocuments(_query: string, _callbacks?: CallbackManagerForRetrieverRun): Promise<DocumentInterface[]>;
34
- invoke(input: string, options?: RunnableConfig): Promise<DocumentInterface[]>;
33
+ _getRelevantDocuments(_query: string, _callbacks?: CallbackManagerForRetrieverRun): Promise<DocumentInterface<Metadata>[]>;
34
+ invoke(input: string, options?: RunnableConfig): Promise<DocumentInterface<Metadata>[]>;
35
35
  /**
36
36
  * Main method used to retrieve relevant documents. It takes a query
37
37
  * string and an optional configuration object, and returns a promise that
@@ -42,5 +42,5 @@ export declare abstract class BaseRetriever extends Runnable<string, DocumentInt
42
42
  * @param config Optional configuration object for the retrieval process.
43
43
  * @returns A promise that resolves to an array of `Document` objects.
44
44
  */
45
- getRelevantDocuments(query: string, config?: Callbacks | BaseCallbackConfig): Promise<DocumentInterface[]>;
45
+ getRelevantDocuments(query: string, config?: Callbacks | BaseCallbackConfig): Promise<DocumentInterface<Metadata>[]>;
46
46
  }
@@ -47,7 +47,7 @@ export class BaseRetriever extends Runnable {
47
47
  throw new Error("Not implemented!");
48
48
  }
49
49
  async invoke(input, options) {
50
- return this.getRelevantDocuments(input, options);
50
+ return this.getRelevantDocuments(input, ensureConfig(options));
51
51
  }
52
52
  /**
53
53
  * Main method used to retrieve relevant documents. It takes a query
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RouterRunnable = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
+ const config_js_1 = require("./config.cjs");
5
6
  /**
6
7
  * A runnable that routes to a set of runnables based on Input['key'].
7
8
  * Returns the output of the selected runnable.
@@ -38,7 +39,7 @@ class RouterRunnable extends base_js_1.Runnable {
38
39
  if (runnable === undefined) {
39
40
  throw new Error(`No runnable associated with key "${key}".`);
40
41
  }
41
- return runnable.invoke(actualInput, options);
42
+ return runnable.invoke(actualInput, (0, config_js_1.ensureConfig)(options));
42
43
  }
43
44
  async batch(inputs, options, batchOptions) {
44
45
  const keys = inputs.map((input) => input.key);
@@ -1,6 +1,6 @@
1
1
  import { Runnable, type RunnableBatchOptions } from "./base.js";
2
2
  import { IterableReadableStream } from "../utils/stream.js";
3
- import type { RunnableConfig } from "./config.js";
3
+ import { type RunnableConfig } from "./config.js";
4
4
  export type RouterInput = {
5
5
  key: string;
6
6
  input: any;
@@ -1,4 +1,5 @@
1
1
  import { Runnable } from "./base.js";
2
+ import { ensureConfig } from "./config.js";
2
3
  /**
3
4
  * A runnable that routes to a set of runnables based on Input['key'].
4
5
  * Returns the output of the selected runnable.
@@ -35,7 +36,7 @@ export class RouterRunnable extends Runnable {
35
36
  if (runnable === undefined) {
36
37
  throw new Error(`No runnable associated with key "${key}".`);
37
38
  }
38
- return runnable.invoke(actualInput, options);
39
+ return runnable.invoke(actualInput, ensureConfig(options));
39
40
  }
40
41
  async batch(inputs, options, batchOptions) {
41
42
  const keys = inputs.map((input) => input.key);
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.betaWarning = void 0;
4
+ /**
5
+ * Util function for logging a warning when a method is called.
6
+ * @param {string} func The name of the function that is in beta.
7
+ */
8
+ function betaWarning(func) {
9
+ console.warn(`The function '${func}' is in beta. It is actively being worked on, so the API may change.`);
10
+ }
11
+ exports.betaWarning = betaWarning;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Util function for logging a warning when a method is called.
3
+ * @param {string} func The name of the function that is in beta.
4
+ */
5
+ export declare function betaWarning(func: string): void;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Util function for logging a warning when a method is called.
3
+ * @param {string} func The name of the function that is in beta.
4
+ */
5
+ export function betaWarning(func) {
6
+ console.warn(`The function '${func}' is in beta. It is actively being worked on, so the API may change.`);
7
+ }
@@ -311,6 +311,12 @@ class FakeListChatModel extends chat_models_js_1.BaseChatModel {
311
311
  this.i = 0;
312
312
  }
313
313
  }
314
+ withStructuredOutput(_params) {
315
+ return base_js_2.RunnableLambda.from(async (input) => {
316
+ const message = await this.invoke(input);
317
+ return JSON.parse(message.content);
318
+ });
319
+ }
314
320
  }
315
321
  exports.FakeListChatModel = FakeListChatModel;
316
322
  class FakeChatMessageHistory extends chat_history_js_1.BaseChatMessageHistory {
@@ -12,6 +12,7 @@ import { Runnable } from "../../runnables/base.js";
12
12
  import { StructuredTool, ToolParams } from "../../tools.js";
13
13
  import { BaseTracer, Run } from "../../tracers/base.js";
14
14
  import { Embeddings, EmbeddingsParams } from "../../embeddings.js";
15
+ import { StructuredOutputMethodParams, BaseLanguageModelInput } from "../../language_models/base.js";
15
16
  /**
16
17
  * Parser for comma-separated values. It splits the input text by commas
17
18
  * and trims the resulting values.
@@ -113,6 +114,11 @@ export declare class FakeListChatModel extends BaseChatModel {
113
114
  _createResponseChunk(text: string): ChatGenerationChunk;
114
115
  _currentResponse(): string;
115
116
  _incrementResponse(): void;
117
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(_params: StructuredOutputMethodParams<RunOutput, false>): Runnable<BaseLanguageModelInput, RunOutput>;
118
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(_params: StructuredOutputMethodParams<RunOutput, true>): Runnable<BaseLanguageModelInput, {
119
+ raw: BaseMessage;
120
+ parsed: RunOutput;
121
+ }>;
116
122
  }
117
123
  export declare class FakeChatMessageHistory extends BaseChatMessageHistory {
118
124
  lc_namespace: string[];
@@ -6,7 +6,7 @@ import { AIMessage, AIMessageChunk, HumanMessage, } from "../../messages/index.j
6
6
  import { BaseOutputParser } from "../../output_parsers/base.js";
7
7
  import { ChatGenerationChunk, } from "../../outputs.js";
8
8
  import { BaseRetriever } from "../../retrievers.js";
9
- import { Runnable } from "../../runnables/base.js";
9
+ import { Runnable, RunnableLambda } from "../../runnables/base.js";
10
10
  import { StructuredTool } from "../../tools.js";
11
11
  import { BaseTracer } from "../../tracers/base.js";
12
12
  import { Embeddings } from "../../embeddings.js";
@@ -302,6 +302,12 @@ export class FakeListChatModel extends BaseChatModel {
302
302
  this.i = 0;
303
303
  }
304
304
  }
305
+ withStructuredOutput(_params) {
306
+ return RunnableLambda.from(async (input) => {
307
+ const message = await this.invoke(input);
308
+ return JSON.parse(message.content);
309
+ });
310
+ }
305
311
  }
306
312
  export class FakeChatMessageHistory extends BaseChatMessageHistory {
307
313
  constructor() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.39",
3
+ "version": "0.1.41",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -524,6 +524,15 @@
524
524
  "import": "./utils/types.js",
525
525
  "require": "./utils/types.cjs"
526
526
  },
527
+ "./utils/beta_warning": {
528
+ "types": {
529
+ "import": "./utils/beta_warning.d.ts",
530
+ "require": "./utils/beta_warning.d.cts",
531
+ "default": "./utils/beta_warning.d.ts"
532
+ },
533
+ "import": "./utils/beta_warning.js",
534
+ "require": "./utils/beta_warning.cjs"
535
+ },
527
536
  "./vectorstores": {
528
537
  "types": {
529
538
  "import": "./vectorstores.d.ts",
@@ -729,6 +738,10 @@
729
738
  "utils/types.js",
730
739
  "utils/types.d.ts",
731
740
  "utils/types.d.cts",
741
+ "utils/beta_warning.cjs",
742
+ "utils/beta_warning.js",
743
+ "utils/beta_warning.d.ts",
744
+ "utils/beta_warning.d.cts",
732
745
  "vectorstores.cjs",
733
746
  "vectorstores.js",
734
747
  "vectorstores.d.ts",
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/utils/beta_warning.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/utils/beta_warning.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/utils/beta_warning.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/utils/beta_warning.js'