@langchain/core
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -6,4 +6,22 @@ import { BaseMessage, BaseMessageChunk, type MessageType, BaseMessageFields } from "./base.js"; | ||
invalid_tool_calls?: InvalidToolCall[]; | ||
usage_metadata?: UsageMetadata; | ||
}; | ||
/** | ||
* Usage metadata for a message, such as token counts. | ||
*/ | ||
export type UsageMetadata = { | ||
/** | ||
* The count of input (or prompt) tokens. | ||
*/ | ||
input_tokens: number; | ||
/** | ||
* The count of output (or completion) tokens | ||
*/ | ||
output_tokens: number; | ||
/** | ||
* The total token count | ||
*/ | ||
total_tokens: number; | ||
}; | ||
/** | ||
* Represents an AI message in a conversation. | ||
@@ -14,2 +32,6 @@ */ | ||
invalid_tool_calls?: InvalidToolCall[]; | ||
/** | ||
* If provided, token usage information associated with the message. | ||
*/ | ||
usage_metadata?: UsageMetadata; | ||
get lc_aliases(): Record<string, string>; | ||
@@ -34,2 +56,6 @@ constructor(fields: string | AIMessageFields, | ||
tool_call_chunks?: ToolCallChunk[]; | ||
/** | ||
* If provided, token usage information associated with the message. | ||
*/ | ||
usage_metadata?: UsageMetadata; | ||
constructor(fields: string | AIMessageChunkFields); | ||
@@ -36,0 +62,0 @@ get lc_aliases(): Record<string, string>; |
@@ -76,2 +76,11 @@ import { parsePartialJson } from "../utils/json.js"; | ||
}); | ||
/** | ||
* If provided, token usage information associated with the message. | ||
*/ | ||
Object.defineProperty(this, "usage_metadata", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
if (typeof initParams !== "string") { | ||
@@ -82,2 +91,3 @@ this.tool_calls = initParams.tool_calls ?? this.tool_calls; | ||
} | ||
this.usage_metadata = initParams.usage_metadata; | ||
} | ||
@@ -172,7 +182,17 @@ static lc_name() { | ||
}); | ||
/** | ||
* If provided, token usage information associated with the message. | ||
*/ | ||
Object.defineProperty(this, "usage_metadata", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
this.tool_call_chunks = | ||
initParams?.tool_call_chunks ?? this.tool_call_chunks; | ||
this.tool_calls = initParams?.tool_calls ?? this.tool_calls; | ||
initParams.tool_call_chunks ?? this.tool_call_chunks; | ||
this.tool_calls = initParams.tool_calls ?? this.tool_calls; | ||
this.invalid_tool_calls = | ||
initParams?.invalid_tool_calls ?? this.invalid_tool_calls; | ||
initParams.invalid_tool_calls ?? this.invalid_tool_calls; | ||
this.usage_metadata = initParams.usage_metadata; | ||
} | ||
@@ -208,4 +228,23 @@ get lc_aliases() { | ||
} | ||
if (this.usage_metadata !== undefined || | ||
chunk.usage_metadata !== undefined) { | ||
const left = this.usage_metadata ?? { | ||
input_tokens: 0, | ||
output_tokens: 0, | ||
total_tokens: 0, | ||
}; | ||
const right = chunk.usage_metadata ?? { | ||
input_tokens: 0, | ||
output_tokens: 0, | ||
total_tokens: 0, | ||
}; | ||
const usage_metadata = { | ||
input_tokens: left.input_tokens + right.input_tokens, | ||
output_tokens: left.output_tokens + right.output_tokens, | ||
total_tokens: left.total_tokens + right.total_tokens, | ||
}; | ||
combinedFields.usage_metadata = usage_metadata; | ||
} | ||
return new AIMessageChunk(combinedFields); | ||
} | ||
} |
@@ -310,3 +310,4 @@ import { Runnable, _coerceToDict } from "./base.js"; | ||
const callbackManager_ = await getCallbackManagerForConfig(options); | ||
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName); | ||
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), config.runId, undefined, undefined, undefined, config.runName); | ||
delete config.runId; | ||
let finalOutput; | ||
@@ -361,3 +362,4 @@ let finalOutputSupported = true; | ||
const callbackManager_ = await getCallbackManagerForConfig(options); | ||
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName); | ||
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), config.runId, undefined, undefined, undefined, config.runName); | ||
delete config.runId; | ||
// The type is in camelCase but the API only accepts snake_case. | ||
@@ -413,3 +415,4 @@ const camelCaseStreamOptions = { | ||
const callbackManager_ = await getCallbackManagerForConfig(options); | ||
const runManager = await callbackManager_?.handleChainStart(outerThis.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName); | ||
const runManager = await callbackManager_?.handleChainStart(outerThis.toJSON(), _coerceToDict(input, "input"), config.runId, undefined, undefined, undefined, config.runName); | ||
delete config.runId; | ||
// The type is in camelCase but the API only accepts snake_case. | ||
@@ -416,0 +419,0 @@ const camelCaseStreamOptions = { |
@@ -112,3 +112,2 @@ import { BaseTracer, type Run } from "./base.js"; | ||
protected excludeTags?: string[]; | ||
protected rootId?: string; | ||
private runInfoMap; | ||
@@ -138,5 +137,4 @@ private tappedPromises; | ||
onRetrieverEnd(run: Run): Promise<void>; | ||
onRunCreate(run: Run): Promise<void>; | ||
onRunUpdate(run: Run): Promise<void>; | ||
finish(): Promise<void>; | ||
} | ||
export {}; |
@@ -69,8 +69,2 @@ import { BaseTracer } from "./base.js"; | ||
}); | ||
Object.defineProperty(this, "rootId", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "runInfoMap", { | ||
@@ -164,3 +158,6 @@ enumerable: true, | ||
const runInfo = this.runInfoMap.get(runId); | ||
// run has finished, don't issue any stream events | ||
// Run has finished, don't issue any stream events. | ||
// An example of this is for runnables that use the default | ||
// implementation of .stream(), which delegates to .invoke() | ||
// and calls .onChainEnd() before passing it to the iterator. | ||
if (runInfo === undefined) { | ||
@@ -208,3 +205,3 @@ yield firstChunk.value; | ||
tappedPromiseResolver(); | ||
// Don't delete from the map to keep track of which runs have been tapped. | ||
// Don't delete from the promises map to keep track of which runs have been tapped. | ||
} | ||
@@ -494,15 +491,8 @@ } | ||
} | ||
async onRunCreate(run) { | ||
if (this.rootId === undefined) { | ||
this.rootId = run.id; | ||
} | ||
async finish() { | ||
const pendingPromises = [...this.tappedPromises.values()]; | ||
void Promise.all(pendingPromises).finally(() => { | ||
void this.writer.close(); | ||
}); | ||
} | ||
async onRunUpdate(run) { | ||
if (run.id === this.rootId && this.autoClose) { | ||
const pendingPromises = [...this.tappedPromises.values()]; | ||
void Promise.all(pendingPromises).finally(() => { | ||
void this.writer.close(); | ||
}); | ||
} | ||
} | ||
} |
{ | ||
"name": "@langchain/core", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Core LangChain.js abstractions and schemas", | ||
@@ -5,0 +5,0 @@ "type": "module", |
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 too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2220395
56737