-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
duckduckgo_search.ts
65 lines (53 loc) · 1.64 KB
/
duckduckgo_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
import { Tool, ToolParams } from "@langchain/core/tools";
import { search, SearchOptions } from "duck-duck-scrape";
export {
SafeSearchType,
SearchOptions,
SearchTimeType,
} from "duck-duck-scrape";
export interface DuckDuckGoSearchParameters extends ToolParams {
/**
* The search options for the search using the SearchOptions interface
* from the duck-duck-scrape package.
*/
searchOptions?: SearchOptions;
/**
* The maximum number of results to return from the search.
* Limiting to 10 to avoid context overload.
* @default 10
*/
maxResults?: number;
}
const DEFAULT_MAX_RESULTS = 10;
/**
* Class for interacting with the DuckDuckGo search engine
* It extends the base Tool class to perform retrieval.
*/
export class DuckDuckGoSearch extends Tool {
private searchOptions?: SearchOptions;
private maxResults = DEFAULT_MAX_RESULTS;
constructor(params?: DuckDuckGoSearchParameters) {
super(params ?? {});
const { searchOptions, maxResults } = params ?? {};
this.searchOptions = searchOptions;
this.maxResults = maxResults || this.maxResults;
}
static lc_name() {
return "DuckDuckGoSearch";
}
name = "duckduckgo-search";
description =
"A search engine. Useful for when you need to answer questions about current events. Input should be a search query.";
async _call(input: string): Promise<string> {
const { results } = await search(input, this.searchOptions);
return JSON.stringify(
results
.map((result) => ({
title: result.title,
link: result.url,
snippet: result.description,
}))
.slice(0, this.maxResults)
);
}
}