-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
tavily_search.ts
73 lines (63 loc) · 2.15 KB
/
tavily_search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager";
import { Tool, type ToolParams } from "@langchain/core/tools";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
/**
* Options for the TavilySearchResults tool.
*/
export type TavilySearchAPIRetrieverFields = ToolParams & {
maxResults?: number;
kwargs?: Record<string, unknown>;
apiKey?: string;
};
/**
* Tool for the Tavily search API.
*/
export class TavilySearchResults extends Tool {
static lc_name(): string {
return "TavilySearchResults";
}
description =
"A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events. Input should be a search query.";
name = "tavily_search_results_json";
protected maxResults = 5;
protected apiKey?: string;
protected kwargs: Record<string, unknown> = {};
constructor(fields?: TavilySearchAPIRetrieverFields) {
super(fields);
this.maxResults = fields?.maxResults ?? this.maxResults;
this.kwargs = fields?.kwargs ?? this.kwargs;
this.apiKey = fields?.apiKey ?? getEnvironmentVariable("TAVILY_API_KEY");
if (this.apiKey === undefined) {
throw new Error(
`No Tavily API key found. Either set an environment variable named "TAVILY_API_KEY" or pass an API key as "apiKey".`
);
}
}
protected async _call(
input: string,
_runManager?: CallbackManagerForToolRun
): Promise<string> {
const body: Record<string, unknown> = {
query: input,
max_results: this.maxResults,
api_key: this.apiKey,
};
const response = await fetch("https://api.tavily.com/search", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({ ...body, ...this.kwargs }),
});
const json = await response.json();
if (!response.ok) {
throw new Error(
`Request failed with status code ${response.status}: ${json.error}`
);
}
if (!Array.isArray(json.results)) {
throw new Error(`Could not parse Tavily results. Please try again.`);
}
return JSON.stringify(json.results);
}
}