From acd944e07b096547055834e8b40556e772ae2b4e Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 10 Mar 2025 02:38:04 +0800 Subject: [PATCH] Support `getNumTokens` for ChatGoogleGenerativeAI --- .../ChatGoogleGenerativeAI/FlowiseChatGoogleGenerativeAI.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/components/nodes/chatmodels/ChatGoogleGenerativeAI/FlowiseChatGoogleGenerativeAI.ts b/packages/components/nodes/chatmodels/ChatGoogleGenerativeAI/FlowiseChatGoogleGenerativeAI.ts index c26bf5a274f..f8bb71110c5 100644 --- a/packages/components/nodes/chatmodels/ChatGoogleGenerativeAI/FlowiseChatGoogleGenerativeAI.ts +++ b/packages/components/nodes/chatmodels/ChatGoogleGenerativeAI/FlowiseChatGoogleGenerativeAI.ts @@ -209,6 +209,12 @@ class LangchainChatGoogleGenerativeAI } } + async getNumTokens(prompt: BaseMessage[]) { + const contents = convertBaseMessagesToContent(prompt, this._isMultimodalModel) + const { totalTokens } = await this.client.countTokens({ contents }) + return totalTokens + } + async _generateNonStreaming( prompt: Content[], options: this['ParsedCallOptions'],