From 654bd48849bcf6937791a8c72b244ee28491fc4e Mon Sep 17 00:00:00 2001 From: Hans Date: Mon, 14 Apr 2025 23:21:15 +0800 Subject: [PATCH] Support `getNumTokens` for ChatGoogleGenerativeAI (#4149) --- .../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 c26bf5a27..f8bb71110 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'],