Skip to content

Commit 2217ac0

Browse files
authored
Merge pull request #1246 from tomguluson92/tomguluson92-patch-2
feat: upgrade Tavily API with comprehensive input and constrain the token consumption
2 parents e6de3a5 + 85c3af4 commit 2217ac0

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ LARGE_AKASH_CHAT_API_MODEL= # Default: Meta-Llama-3-1-405B-Instruct-FP8
261261
FAL_API_KEY=
262262
FAL_AI_LORA_PATH=
263263

264+
# Web search API Configuration
265+
TAVILY_API_KEY=
266+
264267
# WhatsApp Cloud API Configuration
265268
WHATSAPP_ACCESS_TOKEN= # Permanent access token from Facebook Developer Console
266269
WHATSAPP_PHONE_NUMBER_ID= # Phone number ID from WhatsApp Business API

packages/core/src/generation.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,10 @@ export const generateWebSearch = async (
12151215
api_key: apiKey,
12161216
query,
12171217
include_answer: true,
1218+
max_results: 3, // 5 (default)
1219+
topic: "general", // "general"(default) "news"
1220+
search_depth: "basic", // "basic"(default) "advanced"
1221+
include_images: false, // false (default) true
12181222
}),
12191223
});
12201224

packages/plugin-web-search/src/index.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,30 @@ import {
88
State,
99
} from "@ai16z/eliza";
1010
import { generateWebSearch } from "@ai16z/eliza";
11-
1211
import { SearchResult } from "@ai16z/eliza";
12+
import { encodingForModel, TiktokenModel } from "js-tiktoken";
13+
14+
const DEFAULT_MAX_WEB_SEARCH_TOKENS = 4000;
15+
const DEFAULT_MODEL_ENCODING = "gpt-3.5-turbo";
16+
17+
function getTotalTokensFromString(
18+
str: string,
19+
encodingName: TiktokenModel = DEFAULT_MODEL_ENCODING
20+
) {
21+
const encoding = encodingForModel(encodingName);
22+
return encoding.encode(str).length;
23+
}
24+
25+
function MaxTokens(
26+
data: string,
27+
maxTokens: number = DEFAULT_MAX_WEB_SEARCH_TOKENS
28+
): string {
29+
30+
if (getTotalTokensFromString(data) >= maxTokens) {
31+
return data.slice(0, maxTokens);
32+
}
33+
return data;
34+
}
1335

1436
const webSearch: Action = {
1537
name: "WEB_SEARCH",
@@ -68,7 +90,7 @@ const webSearch: Action = {
6890
: "";
6991

7092
callback({
71-
text: responseList,
93+
text: MaxTokens(responseList, DEFAULT_MAX_WEB_SEARCH_TOKENS),
7294
});
7395
} else {
7496
elizaLogger.error("search failed or returned no data.");

0 commit comments

Comments
 (0)