Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@langchain/core

Package Overview
Dependencies
Maintainers
5
Versions
177
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@langchain/core - npm Package Compare versions

Comparing version 0.1.14 to 0.1.15

2

dist/callbacks/manager.d.ts

@@ -53,3 +53,3 @@ import { AgentAction, AgentFinish } from "../agents.js";

readonly runId: string;
protected readonly handlers: BaseCallbackHandler[];
readonly handlers: BaseCallbackHandler[];
protected readonly inheritableHandlers: BaseCallbackHandler[];

@@ -56,0 +56,0 @@ protected readonly tags: string[];

@@ -6,3 +6,3 @@ import type { TiktokenModel } from "js-tiktoken/lite";

import { type LLMResult } from "../outputs.js";
import { BaseCallbackConfig, CallbackManager, Callbacks } from "../callbacks/manager.js";
import { CallbackManager, Callbacks } from "../callbacks/manager.js";
import { AsyncCaller, AsyncCallerParams } from "../utils/async_caller.js";

@@ -57,3 +57,3 @@ import { Runnable, type RunnableInterface } from "../runnables/base.js";

}
export interface BaseLanguageModelCallOptions extends BaseCallbackConfig {
export interface BaseLanguageModelCallOptions extends RunnableConfig {
/**

@@ -60,0 +60,0 @@ * Stop tokens to use for this call.

@@ -1,2 +0,2 @@

import { CallbackManager, CallbackManagerForChainRun } from "../callbacks/manager.js";
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
import { LogStreamCallbackHandlerInput, RunLogPatch } from "../tracers/log_stream.js";

@@ -36,2 +36,3 @@ import { Serializable } from "../load/serializable.js";

export type RunnableBatchOptions = {
/** @deprecated Pass in via the standard runnable config object instead */
maxConcurrency?: number;

@@ -93,3 +94,2 @@ returnExceptions?: boolean;

* @param options Either a single call options object to apply to each batch call or an array for each call.
* @param batchOptions.maxConcurrency Maximum number of calls to run at once.
* @param batchOptions.returnExceptions Whether to return errors rather than throwing on the first one

@@ -145,3 +145,2 @@ * @returns An array of RunOutputs, or mixed RunOutputs and errors if batchOptions.returnExceptions is set

}): AsyncGenerator<O>;
_patchConfig(config?: Partial<CallOptions>, callbackManager?: CallbackManager | undefined, recursionLimit?: number | undefined): Partial<CallOptions>;
/**

@@ -269,3 +268,3 @@ * Create a new runnable sequence that runs each individual runnable in series,

* Binds the runnable with the specified arguments.
* @param args The arguments to bind the runnable with.
* @param kwargs The arguments to bind the runnable with.
* @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.

@@ -272,0 +271,0 @@ */

@@ -6,3 +6,3 @@ import pRetry from "p-retry";

import { IterableReadableStream, concat, atee, pipeGeneratorWithSetup, } from "../utils/stream.js";
import { DEFAULT_RECURSION_LIMIT, getCallbackManagerForConfig, mergeConfigs, } from "./config.js";
import { DEFAULT_RECURSION_LIMIT, getCallbackManagerForConfig, mergeConfigs, patchConfig, } from "./config.js";
import { AsyncCaller } from "../utils/async_caller.js";

@@ -115,4 +115,5 @@ import { RootListenersTracer } from "../tracers/root_listener.js";

const configList = this._getOptionsList(options ?? {}, inputs.length);
const maxConcurrency = configList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
const caller = new AsyncCaller({
maxConcurrency: batchOptions?.maxConcurrency,
maxConcurrency,
onFailedAttempt: (e) => {

@@ -243,3 +244,9 @@ throw e;

runManager = pipe.setup;
for await (const chunk of pipe.output) {
const isLogStreamHandler = (handler) => handler.name === "log_stream_tracer";
const streamLogHandler = runManager?.handlers.find(isLogStreamHandler);
let iterator = pipe.output;
if (streamLogHandler !== undefined && runManager !== undefined) {
iterator = await streamLogHandler.tapOutputIterable(runManager.runId, pipe.output);
}
for await (const chunk of iterator) {
yield chunk;

@@ -271,17 +278,2 @@ if (finalOutputSupported) {

}
_patchConfig(config = {}, callbackManager = undefined, recursionLimit = undefined) {
const newConfig = { ...config };
if (callbackManager !== undefined) {
/**
* If we're replacing callbacks we need to unset runName
* since that should apply only to the same run as the original callbacks
*/
delete newConfig.runName;
return { ...newConfig, callbacks: callbackManager };
}
if (recursionLimit !== undefined) {
newConfig.recursionLimit = recursionLimit;
}
return newConfig;
}
/**

@@ -611,3 +603,3 @@ * Create a new runnable sequence that runs each individual runnable in series,

* Binds the runnable with the specified arguments.
* @param args The arguments to bind the runnable with.
* @param kwargs The arguments to bind the runnable with.
* @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.

@@ -636,3 +628,3 @@ */

async _invoke(inputs, config, runManager) {
return this.bound.batch(inputs, this._patchConfig(config, runManager?.getChild()));
return this.bound.batch(inputs, patchConfig(config, { callbacks: runManager?.getChild() }));
}

@@ -690,3 +682,3 @@ /**

const tag = attempt > 1 ? `retry:attempt:${attempt}` : undefined;
return this._patchConfig(config, runManager?.getChild(tag));
return patchConfig(config, { callbacks: runManager?.getChild(tag) });
}

@@ -828,6 +820,10 @@ async _invoke(input, config, runManager) {

const step = initialSteps[i];
nextStepInput = await step.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${i + 1}`)));
nextStepInput = await step.invoke(nextStepInput, patchConfig(options, {
callbacks: runManager?.getChild(`seq:step:${i + 1}`),
}));
}
// TypeScript can't detect that the last output of the sequence returns RunOutput, so call it out of the loop here
finalOutput = await this.last.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${this.steps.length}`)));
finalOutput = await this.last.invoke(nextStepInput, patchConfig(options, {
callbacks: runManager?.getChild(`seq:step:${this.steps.length}`),
}));
}

@@ -847,10 +843,10 @@ catch (e) {

let nextStepInputs = inputs;
let finalOutputs;
try {
const initialSteps = [this.first, ...this.middle];
for (let i = 0; i < initialSteps.length; i += 1) {
const step = initialSteps[i];
nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild(`seq:step:${i + 1}`))), batchOptions);
for (let i = 0; i < this.steps.length; i += 1) {
const step = this.steps[i];
nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager, j) => {
const childRunManager = runManager?.getChild(`seq:step:${i + 1}`);
return patchConfig(configList[j], { callbacks: childRunManager });
}), batchOptions);
}
finalOutputs = await this.last.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[this.steps.length - 1], runManager?.getChild(`seq:step:${this.steps.length}`))), batchOptions);
}

@@ -861,4 +857,4 @@ catch (e) {

}
await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(finalOutputs[i], "output"))));
return finalOutputs;
await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(nextStepInputs, "output"))));
return nextStepInputs;
}

@@ -875,6 +871,10 @@ async *_streamIterator(input, options) {

try {
let finalGenerator = steps[0].transform(inputGenerator(), this._patchConfig(options, runManager?.getChild(`seq:step:1`)));
let finalGenerator = steps[0].transform(inputGenerator(), patchConfig(options, {
callbacks: runManager?.getChild(`seq:step:1`),
}));
for (let i = 1; i < steps.length; i += 1) {
const step = steps[i];
finalGenerator = await step.transform(finalGenerator, this._patchConfig(options, runManager?.getChild(`seq:step:${i + 1}`)));
finalGenerator = await step.transform(finalGenerator, patchConfig(options, {
callbacks: runManager?.getChild(`seq:step:${i + 1}`),
}));
}

@@ -1002,3 +1002,5 @@ for await (const chunk of finalGenerator) {

await Promise.all(Object.entries(this.steps).map(async ([key, runnable]) => {
output[key] = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild(`map:key:${key}`)));
output[key] = await runnable.invoke(input, patchConfig(options, {
callbacks: runManager?.getChild(`map:key:${key}`),
}));
}));

@@ -1020,3 +1022,5 @@ }

const tasks = new Map(Object.entries(steps).map(([key, runnable], i) => {
const gen = runnable.transform(inputCopies[i], this._patchConfig(options, runManager?.getChild(`map:key:${key}`)));
const gen = runnable.transform(inputCopies[i], patchConfig(options, {
callbacks: runManager?.getChild(`map:key:${key}`),
}));
return [key, gen.next().then((result) => ({ key, gen, result }))];

@@ -1080,3 +1084,6 @@ }));

}
output = await output.invoke(input, this._patchConfig(config, runManager?.getChild(), (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1));
output = await output.invoke(input, patchConfig(config, {
callbacks: runManager?.getChild(),
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
}));
}

@@ -1110,3 +1117,6 @@ return output;

}
const stream = await output.stream(finalChunk, this._patchConfig(config, runManager?.getChild(), (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1));
const stream = await output.stream(finalChunk, patchConfig(config, {
callbacks: runManager?.getChild(),
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
}));
for await (const chunk of stream) {

@@ -1180,3 +1190,3 @@ yield chunk;

try {
const output = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
const output = await runnable.invoke(input, patchConfig(options, { callbacks: runManager?.getChild() }));
await runManager?.handleChainEnd(_coerceToDict(output, "output"));

@@ -1208,3 +1218,5 @@ return output;

try {
const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild())), batchOptions);
const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => patchConfig(configList[j], {
callbacks: runManager?.getChild(),
})), batchOptions);
await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));

@@ -1294,3 +1306,3 @@ return outputs;

// create mapper output gen
const mapperOutput = this.mapper.transform(forMapper, this._patchConfig(options, runManager?.getChild()));
const mapperOutput = this.mapper.transform(forMapper, patchConfig(options, { callbacks: runManager?.getChild() }));
// start the mapper

@@ -1297,0 +1309,0 @@ const firstMapperChunkPromise = mapperOutput.next();

import { Runnable, _coerceToRunnable } from "./base.js";
import { patchConfig } from "./config.js";
/**

@@ -116,5 +117,9 @@ * Class that represents a runnable branch. The RunnableBranch is

const [condition, branchRunnable] = this.branches[i];
const conditionValue = await condition.invoke(input, this._patchConfig(config, runManager?.getChild(`condition:${i + 1}`)));
const conditionValue = await condition.invoke(input, patchConfig(config, {
callbacks: runManager?.getChild(`condition:${i + 1}`),
}));
if (conditionValue) {
result = await branchRunnable.invoke(input, this._patchConfig(config, runManager?.getChild(`branch:${i + 1}`)));
result = await branchRunnable.invoke(input, patchConfig(config, {
callbacks: runManager?.getChild(`branch:${i + 1}`),
}));
break;

@@ -124,3 +129,5 @@ }

if (!result) {
result = await this.default.invoke(input, this._patchConfig(config, runManager?.getChild("default")));
result = await this.default.invoke(input, patchConfig(config, {
callbacks: runManager?.getChild("default"),
}));
}

@@ -127,0 +134,0 @@ return result;

@@ -13,4 +13,14 @@ import { type BaseCallbackConfig, CallbackManager } from "../callbacks/manager.js";

recursionLimit?: number;
/** Maximum number of parallel calls to make. */
maxConcurrency?: number;
}
export declare function getCallbackManagerForConfig(config?: RunnableConfig): Promise<CallbackManager | undefined>;
export declare function mergeConfigs<CallOptions extends RunnableConfig>(config: RunnableConfig, options?: Record<string, any>): Partial<CallOptions>;
/**
* Ensure that a passed config is an object with all required keys present.
*/
export declare function ensureConfig<CallOptions extends RunnableConfig>(config?: CallOptions): Partial<CallOptions>;
/**
* Helper function that patches runnable configs with updated properties.
*/
export declare function patchConfig<CallOptions extends RunnableConfig>(config?: Partial<CallOptions>, { callbacks, maxConcurrency, recursionLimit, runName, configurable, }?: RunnableConfig): Partial<CallOptions>;

@@ -77,1 +77,40 @@ import { CallbackManager, } from "../callbacks/manager.js";

}
/**
* Ensure that a passed config is an object with all required keys present.
*/
export function ensureConfig(config) {
return {
tags: [],
metadata: {},
callbacks: undefined,
recursionLimit: 25,
...config,
};
}
/**
* Helper function that patches runnable configs with updated properties.
*/
export function patchConfig(config = {}, { callbacks, maxConcurrency, recursionLimit, runName, configurable, } = {}) {
const newConfig = ensureConfig(config);
if (callbacks !== undefined) {
/**
* If we're replacing callbacks we need to unset runName
* since that should apply only to the same run as the original callbacks
*/
delete newConfig.runName;
newConfig.callbacks = callbacks;
}
if (recursionLimit !== undefined) {
newConfig.recursionLimit = recursionLimit;
}
if (maxConcurrency !== undefined) {
newConfig.maxConcurrency = maxConcurrency;
}
if (runName !== undefined) {
newConfig.runName = runName;
}
if (configurable !== undefined) {
newConfig.configurable = { ...newConfig.configurable, ...configurable };
}
return newConfig;
}
export { type RunnableFunc, type RunnableLike, type RunnableBatchOptions, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableInterface, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
export { type RunnableConfig, getCallbackManagerForConfig } from "./config.js";
export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, } from "./config.js";
export { RunnablePassthrough } from "./passthrough.js";

@@ -4,0 +4,0 @@ export { type RouterInput, RouterRunnable } from "./router.js";

export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
export { getCallbackManagerForConfig } from "./config.js";
export { getCallbackManagerForConfig, patchConfig, } from "./config.js";
export { RunnablePassthrough } from "./passthrough.js";

@@ -4,0 +4,0 @@ export { RouterRunnable } from "./router.js";

@@ -49,5 +49,4 @@ import { Runnable } from "./base.js";

const optionsList = this._getOptionsList(options ?? {}, inputs.length);
const batchSize = batchOptions?.maxConcurrency && batchOptions.maxConcurrency > 0
? batchOptions?.maxConcurrency
: inputs.length;
const maxConcurrency = optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
const batchSize = maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;
const batchResults = [];

@@ -54,0 +53,0 @@ for (let i = 0; i < actualInputs.length; i += batchSize) {

@@ -47,5 +47,12 @@ import { BaseCallbackHandler, } from "../callbacks/base.js";

else {
console.warn(`Parent run with UUID ${storedRun.parent_run_id} not found.`);
// This can happen naturally for callbacks added within a run
// console.debug(`Parent run with UUID ${storedRun.parent_run_id} has no dotted order.`);
}
}
else {
// This can happen naturally for callbacks added within a run
// console.debug(
// `Parent run with UUID ${storedRun.parent_run_id} not found.`
// );
}
}

@@ -52,0 +59,0 @@ else {

@@ -22,2 +22,4 @@ import { type Operation as JSONPatchOperation } from "../utils/fast-json-patch/index.js";

start_time: string;
/** List of general output chunks streamed by this run. */
streamed_output: any[];
/** List of LLM tokens streamed by this run, if applicable. */

@@ -88,2 +90,3 @@ streamed_output_str: string[];

protected excludeTags?: string[];
protected rootId?: string;
private keyMapByRunId;

@@ -99,2 +102,3 @@ private counterMapByRunName;

_includeRun(run: Run): boolean;
tapOutputIterable<T>(runId: string, output: AsyncGenerator<T>): AsyncGenerator<T>;
onRunCreate(run: Run): Promise<void>;

@@ -101,0 +105,0 @@ onRunUpdate(run: Run): Promise<void>;

@@ -99,2 +99,8 @@ import { applyPatch, } from "../utils/fast-json-patch/index.js";

});
Object.defineProperty(this, "rootId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "keyMapByRunId", {

@@ -155,3 +161,3 @@ enumerable: true,

_includeRun(run) {
if (run.parent_run_id === undefined) {
if (run.id === this.rootId) {
return false;

@@ -186,4 +192,28 @@ }

}
async *tapOutputIterable(runId, output) {
// Tap an output async iterator to stream its values to the log.
for await (const chunk of output) {
// root run is handled in .streamLog()
if (runId !== this.rootId) {
// if we can't find the run silently ignore
// eg. because this run wasn't included in the log
const key = this.keyMapByRunId[runId];
if (key) {
await this.writer.write(new RunLogPatch({
ops: [
{
op: "add",
path: `/logs/${key}/streamed_output/-`,
value: chunk,
},
],
}));
}
}
yield chunk;
}
}
async onRunCreate(run) {
if (run.parent_run_id === undefined) {
if (this.rootId === undefined) {
this.rootId = run.id;
await this.writer.write(new RunLogPatch({

@@ -221,2 +251,3 @@ ops: [

start_time: new Date(run.start_time).toISOString(),
streamed_output: [],
streamed_output_str: [],

@@ -260,3 +291,3 @@ final_output: undefined,

finally {
if (run.parent_run_id === undefined) {
if (run.id === this.rootId) {
const patch = new RunLogPatch({

@@ -263,0 +294,0 @@ ops: [

@@ -43,4 +43,6 @@ import { z } from "zod";

sleep?: number;
responses?: string[];
constructor(fields: {
sleep?: number;
responses?: string[];
} & BaseLLMParams);

@@ -47,0 +49,0 @@ _llmType(): string;

@@ -96,3 +96,10 @@ import { BaseChatMessageHistory, BaseListChatMessageHistory, } from "../../chat_history.js";

});
Object.defineProperty(this, "responses", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.sleep = fields.sleep ?? this.sleep;
this.responses = fields.responses;
}

@@ -103,6 +110,10 @@ _llmType() {

async _call(prompt) {
return prompt;
const response = this.responses?.[0];
this.responses = this.responses?.slice(1);
return response ?? prompt;
}
async *_streamResponseChunks(input) {
for (const c of input) {
const response = this.responses?.[0];
this.responses = this.responses?.slice(1);
for (const c of response ?? input) {
await new Promise((resolve) => setTimeout(resolve, this.sleep));

@@ -109,0 +120,0 @@ yield { text: c, generationInfo: {} };

{
"name": "@langchain/core",
"version": "0.1.14",
"version": "0.1.15",
"description": "Core LangChain.js abstractions and schemas",

@@ -21,3 +21,3 @@ "type": "module",

"build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src",
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",

@@ -24,0 +24,0 @@ "lint": "yarn lint:eslint && yarn lint:dpdm",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc