From 832a274f3053076952a0ffb1635ca19839fdfff7 Mon Sep 17 00:00:00 2001 From: tomguluson92 <314913739@qq.com> Date: Fri, 20 Dec 2024 03:18:29 +0800 Subject: [PATCH 1/3] Update generation.ts --- packages/core/src/generation.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/core/src/generation.ts b/packages/core/src/generation.ts index 59b620d648..e3513083ff 100644 --- a/packages/core/src/generation.ts +++ b/packages/core/src/generation.ts @@ -1214,6 +1214,10 @@ export const generateWebSearch = async ( api_key: apiKey, query, include_answer: true, + max_results: 3, // 5 (default) + topic: "general", // "general"(default) "news" + search_depth: "basic", // "basic"(default) "advanced" + include_images: false, // false (default) true }), }); From db43e1322a8139dee6a1e6f26dc685df1cb583f2 Mon Sep 17 00:00:00 2001 From: tomguluson92 <314913739@qq.com> Date: Fri, 20 Dec 2024 03:19:54 +0800 Subject: [PATCH 2/3] Update index.ts --- packages/plugin-web-search/src/index.ts | 26 +++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/plugin-web-search/src/index.ts b/packages/plugin-web-search/src/index.ts index 4d7d7be44c..68392a8967 100644 --- a/packages/plugin-web-search/src/index.ts +++ b/packages/plugin-web-search/src/index.ts @@ -8,8 +8,30 @@ import { State, } from "@ai16z/eliza"; import { generateWebSearch } from "@ai16z/eliza"; - import { SearchResult } from "@ai16z/eliza"; +import { encodingForModel, TiktokenModel } from "js-tiktoken"; + +const DEFAULT_MAX_WEB_SEARCH_TOKENS = 4000; +const DEFAULT_MODEL_ENCODING = "gpt-3.5-turbo"; + +function getTotalTokensFromString( + str: string, + encodingName: TiktokenModel = DEFAULT_MODEL_ENCODING + ) { + const encoding = encodingForModel(encodingName); + return encoding.encode(str).length; + } + +function MaxTokens( + data: string, + maxTokens: number = DEFAULT_MAX_WEB_SEARCH_TOKENS + ): string { + + if (getTotalTokensFromString(data) >= maxTokens) { + return data.slice(0, maxTokens); + } + return data; + } const webSearch: Action = { name: "WEB_SEARCH", @@ -68,7 +90,7 @@ const webSearch: Action = { : ""; callback({ - text: responseList, + text: MaxTokens(responseList, DEFAULT_MAX_WEB_SEARCH_TOKENS), }); } else { elizaLogger.error("search failed or returned no data."); From 85c3af4faf90eb7ba973ef70e63f9e187c866e21 Mon Sep 17 00:00:00 2001 From: tomguluson92 <314913739@qq.com> Date: Fri, 20 Dec 2024 03:21:17 +0800 Subject: [PATCH 3/3] Update .env.example --- .env.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.env.example b/.env.example index d2d89eda85..bbf9096a4a 100644 --- a/.env.example +++ b/.env.example @@ -257,6 +257,9 @@ LARGE_AKASH_CHAT_API_MODEL= # Default: Meta-Llama-3-1-405B-Instruct-FP8 FAL_API_KEY= FAL_AI_LORA_PATH= +# Web search API Configuration +TAVILY_API_KEY= + # WhatsApp Cloud API Configuration WHATSAPP_ACCESS_TOKEN= # Permanent access token from Facebook Developer Console WHATSAPP_PHONE_NUMBER_ID= # Phone number ID from WhatsApp Business API