Skip to content

Commit b4d924f

Browse files
authored
feat(community): improve support for Tavily search tool args (#7561)
1 parent 7cf310a commit b4d924f

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

libs/langchain-community/src/tools/tavily_search.ts

+116
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,86 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env";
66
* Options for the TavilySearchResults tool.
77
*/
88
export type TavilySearchAPIRetrieverFields = ToolParams & {
9+
/**
10+
* The maximum number of search results to return.
11+
*
12+
* @default 5
13+
*/
914
maxResults?: number;
15+
16+
/**
17+
* Additional keyword arguments to pass to the API.
18+
*/
1019
kwargs?: Record<string, unknown>;
20+
21+
/**
22+
* The API key used for authentication with the Tavily Search API.
23+
*
24+
*/
1125
apiKey?: string;
26+
27+
/**
28+
* Include a list of query-related images in the response.
29+
*
30+
* @default false
31+
*/
32+
includeImages?: boolean;
33+
34+
/**
35+
* When includeImages is set to True, this option adds descriptive text for each image.
36+
*
37+
* @default false
38+
*/
39+
includeImageDescriptions?: boolean;
40+
41+
/**
42+
* Include a short answer to the original query.
43+
*
44+
* @default false
45+
*/
46+
includeAnswer?: boolean;
47+
48+
/**
49+
* Include the cleaned and parsed HTML content of each search result.
50+
*
51+
* @default false
52+
*/
53+
includeRawContent?: boolean;
54+
55+
/**
56+
* A list of domains to specifically include in the search results.
57+
*
58+
* @default []
59+
*/
60+
includeDomains?: string[];
61+
62+
/**
63+
* A list of domains to specifically exclude from the search results.
64+
*
65+
* @default []
66+
*/
67+
excludeDomains?: string[];
68+
69+
/**
70+
* The depth of the search. It can be "basic" or "deep".
71+
*
72+
* @default "basic"
73+
*/
74+
searchDepth?: "basic" | "deep";
75+
76+
/**
77+
* The category of the search. This will determine which of our agents will be used for the search. Currently, only "general" and "news" are supported. See https://docs.tavily.com/docs/rest-api/api-reference
78+
*
79+
* @default "general"
80+
*/
81+
topic?: string;
82+
83+
/**
84+
* The number of days back from the current date to include in the search results.
85+
*
86+
* @default 3
87+
*/
88+
days?: number;
1289
};
1390

1491
/**
@@ -93,11 +170,41 @@ export class TavilySearchResults extends Tool {
93170

94171
protected kwargs: Record<string, unknown> = {};
95172

173+
protected includeImages?: boolean;
174+
175+
protected includeImageDescriptions?: boolean;
176+
177+
protected includeAnswer?: boolean;
178+
179+
protected includeRawContent?: boolean;
180+
181+
protected includeDomains?: string[];
182+
183+
protected excludeDomains?: string[];
184+
185+
protected searchDepth?: "basic" | "deep";
186+
187+
protected topic?: string;
188+
189+
protected days?: number;
190+
96191
constructor(fields?: TavilySearchAPIRetrieverFields) {
97192
super(fields);
98193
this.maxResults = fields?.maxResults ?? this.maxResults;
99194
this.kwargs = fields?.kwargs ?? this.kwargs;
100195
this.apiKey = fields?.apiKey ?? getEnvironmentVariable("TAVILY_API_KEY");
196+
this.includeImages = fields?.includeImages ?? this.includeImages;
197+
this.includeImageDescriptions =
198+
fields?.includeImageDescriptions ?? this.includeImageDescriptions;
199+
this.includeAnswer = fields?.includeAnswer ?? this.includeAnswer;
200+
this.includeRawContent =
201+
fields?.includeRawContent ?? this.includeRawContent;
202+
this.includeDomains = fields?.includeDomains ?? this.includeDomains;
203+
this.excludeDomains = fields?.excludeDomains ?? this.excludeDomains;
204+
this.searchDepth = fields?.searchDepth ?? this.searchDepth;
205+
this.topic = fields?.topic ?? this.topic;
206+
this.days = fields?.days ?? this.days;
207+
101208
if (this.apiKey === undefined) {
102209
throw new Error(
103210
`No Tavily API key found. Either set an environment variable named "TAVILY_API_KEY" or pass an API key as "apiKey".`
@@ -113,6 +220,15 @@ export class TavilySearchResults extends Tool {
113220
query: input,
114221
max_results: this.maxResults,
115222
api_key: this.apiKey,
223+
include_images: this.includeImages,
224+
include_image_descriptions: this.includeImageDescriptions,
225+
include_answer: this.includeAnswer,
226+
include_raw_content: this.includeRawContent,
227+
include_domains: this.includeDomains,
228+
exclude_domains: this.excludeDomains,
229+
search_depth: this.searchDepth,
230+
topic: this.topic,
231+
days: this.days,
116232
};
117233

118234
const response = await fetch("https://api.tavily.com/search", {

0 commit comments

Comments
 (0)