-
Notifications
You must be signed in to change notification settings - Fork 192
feat: add DuckDuckGo search tool #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "create-llama": patch | ||
| --- | ||
|
|
||
| Add DuckDuckGo search tool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
templates/components/engines/python/agent/tools/duckduckgo.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from llama_index.core.tools.function_tool import FunctionTool | ||
|
|
||
|
|
||
| def duckduckgo_search( | ||
| query: str, | ||
| region: str = "wt-wt", | ||
| max_results: int = 10, | ||
| ): | ||
| """ | ||
| Use this function to search for any query in DuckDuckGo. | ||
| Args: | ||
| query (str): The query to search in DuckDuckGo. | ||
| region Optional(str): The region to be used for the search in [country-language] convention, ex us-en, uk-en, ru-ru, etc... | ||
| max_results Optional(int): The maximum number of results to be returned. Default is 10. | ||
| """ | ||
| try: | ||
| from duckduckgo_search import DDGS | ||
| except ImportError: | ||
| raise ImportError( | ||
| "duckduckgo_search package is required to use this function." | ||
| "Please install it by running: `poetry add duckduckgo_search` or `pip install duckduckgo_search`" | ||
| ) | ||
|
|
||
| params = { | ||
| "keywords": query, | ||
| "region": region, | ||
| "max_results": max_results, | ||
| } | ||
| results = [] | ||
| with DDGS() as ddg: | ||
| results = list(ddg.text(**params)) | ||
| return results | ||
|
|
||
|
|
||
| def get_tools(): | ||
| return [FunctionTool.from_defaults(duckduckgo_search)] |
61 changes: 61 additions & 0 deletions
61
templates/components/engines/typescript/agent/tools/duckduckgo.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { JSONSchemaType } from "ajv"; | ||
| import { search } from "duck-duck-scrape"; | ||
| import { BaseTool, ToolMetadata } from "llamaindex"; | ||
|
|
||
| export type DuckDuckGoParameter = { | ||
| query: string; | ||
| region?: string; | ||
| }; | ||
|
|
||
| export type DuckDuckGoToolParams = { | ||
| metadata?: ToolMetadata<JSONSchemaType<DuckDuckGoParameter>>; | ||
| }; | ||
|
|
||
| const DEFAULT_META_DATA: ToolMetadata<JSONSchemaType<DuckDuckGoParameter>> = { | ||
| name: "duckduckgo", | ||
| description: "Use this function to search for any query in DuckDuckGo.", | ||
| parameters: { | ||
| type: "object", | ||
| properties: { | ||
| query: { | ||
| type: "string", | ||
| description: "The query to search in DuckDuckGo.", | ||
| }, | ||
| region: { | ||
| type: "string", | ||
| description: | ||
| "Optional, The region to be used for the search in [country-language] convention, ex us-en, uk-en, ru-ru, etc...", | ||
| nullable: true, | ||
| }, | ||
| }, | ||
| required: ["query"], | ||
| }, | ||
| }; | ||
|
|
||
| type DuckDuckGoSearchResult = { | ||
| title: string; | ||
| description: string; | ||
| url: string; | ||
| }; | ||
|
|
||
| export class DuckDuckGoSearchTool implements BaseTool<DuckDuckGoParameter> { | ||
| metadata: ToolMetadata<JSONSchemaType<DuckDuckGoParameter>>; | ||
|
|
||
| constructor(params: DuckDuckGoToolParams) { | ||
| this.metadata = params.metadata ?? DEFAULT_META_DATA; | ||
| } | ||
|
|
||
| async call(input: DuckDuckGoParameter) { | ||
| const { query, region } = input; | ||
| const options = region ? { region } : {}; | ||
| const searchResults = await search(query, options); | ||
|
|
||
| return searchResults.results.map((result) => { | ||
| return { | ||
| title: result.title, | ||
| description: result.description, | ||
| url: result.url, | ||
| } as DuckDuckGoSearchResult; | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.