@langchain/core 0.3.59-rc.2 → 0.3.59

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.
@@ -194,13 +194,37 @@ class BaseLanguageModel extends BaseLangChain {
194
194
  }
195
195
  this.caller = new async_caller_js_1.AsyncCaller(params ?? {});
196
196
  }
197
+ /**
198
+ * Get the number of tokens in the content.
199
+ * @param content The content to get the number of tokens for.
200
+ * @returns The number of tokens in the content.
201
+ */
197
202
  async getNumTokens(content) {
198
- // TODO: Figure out correct value.
199
- if (typeof content !== "string") {
200
- return 0;
203
+ // Extract text content from MessageContent
204
+ let textContent;
205
+ if (typeof content === "string") {
206
+ textContent = content;
207
+ }
208
+ else {
209
+ /**
210
+ * Content is an array of MessageContentComplex
211
+ *
212
+ * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.
213
+ * We need to find a better way to do this.
214
+ * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116
215
+ */
216
+ textContent = content
217
+ .map((item) => {
218
+ if (typeof item === "string")
219
+ return item;
220
+ if (item.type === "text" && "text" in item)
221
+ return item.text;
222
+ return "";
223
+ })
224
+ .join("");
201
225
  }
202
226
  // fallback to approximate calculation if tiktoken is not available
203
- let numTokens = Math.ceil(content.length / 4);
227
+ let numTokens = Math.ceil(textContent.length / 4);
204
228
  if (!this._encoding) {
205
229
  try {
206
230
  this._encoding = await (0, tiktoken_js_1.encodingForModel)("modelName" in this
@@ -213,7 +237,7 @@ class BaseLanguageModel extends BaseLangChain {
213
237
  }
214
238
  if (this._encoding) {
215
239
  try {
216
- numTokens = this._encoding.encode(content).length;
240
+ numTokens = this._encoding.encode(textContent).length;
217
241
  }
218
242
  catch (error) {
219
243
  console.warn("Failed to calculate number of tokens, falling back to approximate count", error);
@@ -188,6 +188,11 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
188
188
  abstract _modelType(): string;
189
189
  abstract _llmType(): string;
190
190
  private _encoding?;
191
+ /**
192
+ * Get the number of tokens in the content.
193
+ * @param content The content to get the number of tokens for.
194
+ * @returns The number of tokens in the content.
195
+ */
191
196
  getNumTokens(content: MessageContent): Promise<number>;
192
197
  protected static _convertInputToPromptValue(input: BaseLanguageModelInput): BasePromptValueInterface;
193
198
  /**
@@ -185,13 +185,37 @@ export class BaseLanguageModel extends BaseLangChain {
185
185
  }
186
186
  this.caller = new AsyncCaller(params ?? {});
187
187
  }
188
+ /**
189
+ * Get the number of tokens in the content.
190
+ * @param content The content to get the number of tokens for.
191
+ * @returns The number of tokens in the content.
192
+ */
188
193
  async getNumTokens(content) {
189
- // TODO: Figure out correct value.
190
- if (typeof content !== "string") {
191
- return 0;
194
+ // Extract text content from MessageContent
195
+ let textContent;
196
+ if (typeof content === "string") {
197
+ textContent = content;
198
+ }
199
+ else {
200
+ /**
201
+ * Content is an array of MessageContentComplex
202
+ *
203
+ * ToDo(@christian-bromann): This is a temporary fix to get the number of tokens for the content.
204
+ * We need to find a better way to do this.
205
+ * @see https://github.com/langchain-ai/langchainjs/pull/8341#pullrequestreview-2933713116
206
+ */
207
+ textContent = content
208
+ .map((item) => {
209
+ if (typeof item === "string")
210
+ return item;
211
+ if (item.type === "text" && "text" in item)
212
+ return item.text;
213
+ return "";
214
+ })
215
+ .join("");
192
216
  }
193
217
  // fallback to approximate calculation if tiktoken is not available
194
- let numTokens = Math.ceil(content.length / 4);
218
+ let numTokens = Math.ceil(textContent.length / 4);
195
219
  if (!this._encoding) {
196
220
  try {
197
221
  this._encoding = await encodingForModel("modelName" in this
@@ -204,7 +228,7 @@ export class BaseLanguageModel extends BaseLangChain {
204
228
  }
205
229
  if (this._encoding) {
206
230
  try {
207
- numTokens = this._encoding.encode(content).length;
231
+ numTokens = this._encoding.encode(textContent).length;
208
232
  }
209
233
  catch (error) {
210
234
  console.warn("Failed to calculate number of tokens, falling back to approximate count", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.59-rc.2",
3
+ "version": "0.3.59",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {