From 2e73069ed2ff7c07f39e11157568afa8025cf791 Mon Sep 17 00:00:00 2001 From: leehuwuj Date: Tue, 18 Jun 2024 10:14:53 +0700 Subject: [PATCH 1/3] add duckduckgo search tool --- .changeset/clever-pens-mix.md | 5 +++++ helpers/tools.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .changeset/clever-pens-mix.md diff --git a/.changeset/clever-pens-mix.md b/.changeset/clever-pens-mix.md new file mode 100644 index 000000000..47ef674a7 --- /dev/null +++ b/.changeset/clever-pens-mix.md @@ -0,0 +1,5 @@ +--- +"create-llama": patch +--- + +Add DuckDuckGo search tool (Python) diff --git a/helpers/tools.ts b/helpers/tools.ts index 67d8f486f..3476a65fc 100644 --- a/helpers/tools.ts +++ b/helpers/tools.ts @@ -54,6 +54,26 @@ export const supportedTools: Tool[] = [ }, ], }, + { + display: "DuckDuckGo Search", + name: "duckduckgo.DuckDuckGoSearchToolSpec", + dependencies: [ + { + name: "llama-index-tools-duckduckgo", + version: "0.1.1", + }, + ], + supportedFrameworks: ["fastapi"], + type: ToolType.LLAMAHUB, + envVars: [ + { + name: TOOL_SYSTEM_PROMPT_ENV_VAR, + description: "System prompt for DuckDuckGo search tool.", + value: + "You are a DuckDuckGo search agent. You can use information from DuckDuckGo search to answer user questions.", + }, + ], + }, { display: "Wikipedia", name: "wikipedia.WikipediaToolSpec", From e3d271de8b5ae84ec7fcd8432995e36a70630409 Mon Sep 17 00:00:00 2001 From: leehuwuj Date: Tue, 18 Jun 2024 14:02:58 +0700 Subject: [PATCH 2/3] add duckduckgo search tool for TS --- .changeset/clever-pens-mix.md | 2 +- helpers/tools.ts | 7 ++- .../typescript/agent/tools/duckduckgo.ts | 61 +++++++++++++++++++ .../engines/typescript/agent/tools/index.ts | 4 ++ .../types/streaming/express/package.json | 1 + templates/types/streaming/nextjs/package.json | 1 + 6 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 templates/components/engines/typescript/agent/tools/duckduckgo.ts diff --git a/.changeset/clever-pens-mix.md b/.changeset/clever-pens-mix.md index 47ef674a7..193d78273 100644 --- a/.changeset/clever-pens-mix.md +++ b/.changeset/clever-pens-mix.md @@ -2,4 +2,4 @@ "create-llama": patch --- -Add DuckDuckGo search tool (Python) +Add DuckDuckGo search tool diff --git a/helpers/tools.ts b/helpers/tools.ts index 3476a65fc..c6c5493a9 100644 --- a/helpers/tools.ts +++ b/helpers/tools.ts @@ -63,14 +63,15 @@ export const supportedTools: Tool[] = [ version: "0.1.1", }, ], - supportedFrameworks: ["fastapi"], + supportedFrameworks: ["fastapi", "nextjs", "express"], type: ToolType.LLAMAHUB, envVars: [ { name: TOOL_SYSTEM_PROMPT_ENV_VAR, description: "System prompt for DuckDuckGo search tool.", - value: - "You are a DuckDuckGo search agent. You can use information from DuckDuckGo search to answer user questions.", + value: `You are a DuckDuckGo search agent. +You can use the duckduckgo search tool to get information from the web to answer user questions. +For better results, you can specify the region parameter to get results from a specific region but it's optional.`, }, ], }, diff --git a/templates/components/engines/typescript/agent/tools/duckduckgo.ts b/templates/components/engines/typescript/agent/tools/duckduckgo.ts new file mode 100644 index 000000000..4bf4c2701 --- /dev/null +++ b/templates/components/engines/typescript/agent/tools/duckduckgo.ts @@ -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>; +}; + +const DEFAULT_META_DATA: ToolMetadata> = { + name: "duckduckgo.DuckDuckGoSearchToolSpec", + 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 { + metadata: ToolMetadata>; + + constructor(params: DuckDuckGoToolParams) { + this.metadata = params.metadata ?? DEFAULT_META_DATA; + } + + async call(input: DuckDuckGoParameter) { + const { query, region } = input; + const options = { region } ?? {}; + const searchResults = await search(query, options); + + return searchResults.results.map((result) => { + return { + title: result.title, + description: result.description, + url: result.url, + } as DuckDuckGoSearchResult; + }); + } +} diff --git a/templates/components/engines/typescript/agent/tools/index.ts b/templates/components/engines/typescript/agent/tools/index.ts index 7f823eca2..197e14583 100644 --- a/templates/components/engines/typescript/agent/tools/index.ts +++ b/templates/components/engines/typescript/agent/tools/index.ts @@ -1,5 +1,6 @@ import { BaseToolWithCall } from "llamaindex"; import { ToolsFactory } from "llamaindex/tools/ToolsFactory"; +import { DuckDuckGoSearchTool, DuckDuckGoToolParams } from "./duckduckgo"; import { InterpreterTool, InterpreterToolParams } from "./interpreter"; import { OpenAPIActionTool } from "./openapi-action"; import { WeatherTool, WeatherToolParams } from "./weather"; @@ -35,6 +36,9 @@ const toolFactory: Record = { ); return await openAPIActionTool.toToolFunctions(); }, + "duckduckgo.DuckDuckGoSearchToolSpec": async (config: unknown) => { + return [new DuckDuckGoSearchTool(config as DuckDuckGoToolParams)]; + }, }; async function createLocalTools( diff --git a/templates/types/streaming/express/package.json b/templates/types/streaming/express/package.json index 312f708b8..a170e2fd0 100644 --- a/templates/types/streaming/express/package.json +++ b/templates/types/streaming/express/package.json @@ -13,6 +13,7 @@ "ai": "^3.0.21", "cors": "^2.8.5", "dotenv": "^16.3.1", + "duck-duck-scrape": "^2.2.5", "express": "^4.18.2", "llamaindex": "0.3.16", "pdf2json": "3.0.5", diff --git a/templates/types/streaming/nextjs/package.json b/templates/types/streaming/nextjs/package.json index 69515e6a7..61a9ec58b 100644 --- a/templates/types/streaming/nextjs/package.json +++ b/templates/types/streaming/nextjs/package.json @@ -18,6 +18,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "dotenv": "^16.3.1", + "duck-duck-scrape": "^2.2.5", "llamaindex": "0.3.16", "lucide-react": "^0.294.0", "next": "^14.0.3", From bdaf2c63d93a031791b7af546f20626559c6f709 Mon Sep 17 00:00:00 2001 From: leehuwuj Date: Wed, 19 Jun 2024 12:52:49 +0700 Subject: [PATCH 3/3] use a local duckduckgo tool for python app --- helpers/tools.ts | 10 +++--- .../engines/python/agent/tools/duckduckgo.py | 36 +++++++++++++++++++ .../typescript/agent/tools/duckduckgo.ts | 4 +-- .../engines/typescript/agent/tools/index.ts | 2 +- 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 templates/components/engines/python/agent/tools/duckduckgo.py diff --git a/helpers/tools.ts b/helpers/tools.ts index c6c5493a9..ffa92640b 100644 --- a/helpers/tools.ts +++ b/helpers/tools.ts @@ -55,16 +55,18 @@ export const supportedTools: Tool[] = [ ], }, { + // For python app, we will use a local DuckDuckGo search tool (instead of DuckDuckGo search tool in LlamaHub) + // to get the same results as the TS app. display: "DuckDuckGo Search", - name: "duckduckgo.DuckDuckGoSearchToolSpec", + name: "duckduckgo", dependencies: [ { - name: "llama-index-tools-duckduckgo", - version: "0.1.1", + name: "duckduckgo-search", + version: "6.1.7", }, ], supportedFrameworks: ["fastapi", "nextjs", "express"], - type: ToolType.LLAMAHUB, + type: ToolType.LOCAL, envVars: [ { name: TOOL_SYSTEM_PROMPT_ENV_VAR, diff --git a/templates/components/engines/python/agent/tools/duckduckgo.py b/templates/components/engines/python/agent/tools/duckduckgo.py new file mode 100644 index 000000000..43e799b46 --- /dev/null +++ b/templates/components/engines/python/agent/tools/duckduckgo.py @@ -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)] diff --git a/templates/components/engines/typescript/agent/tools/duckduckgo.ts b/templates/components/engines/typescript/agent/tools/duckduckgo.ts index 4bf4c2701..19423e353 100644 --- a/templates/components/engines/typescript/agent/tools/duckduckgo.ts +++ b/templates/components/engines/typescript/agent/tools/duckduckgo.ts @@ -12,7 +12,7 @@ export type DuckDuckGoToolParams = { }; const DEFAULT_META_DATA: ToolMetadata> = { - name: "duckduckgo.DuckDuckGoSearchToolSpec", + name: "duckduckgo", description: "Use this function to search for any query in DuckDuckGo.", parameters: { type: "object", @@ -47,7 +47,7 @@ export class DuckDuckGoSearchTool implements BaseTool { async call(input: DuckDuckGoParameter) { const { query, region } = input; - const options = { region } ?? {}; + const options = region ? { region } : {}; const searchResults = await search(query, options); return searchResults.results.map((result) => { diff --git a/templates/components/engines/typescript/agent/tools/index.ts b/templates/components/engines/typescript/agent/tools/index.ts index 197e14583..0d6345c69 100644 --- a/templates/components/engines/typescript/agent/tools/index.ts +++ b/templates/components/engines/typescript/agent/tools/index.ts @@ -36,7 +36,7 @@ const toolFactory: Record = { ); return await openAPIActionTool.toToolFunctions(); }, - "duckduckgo.DuckDuckGoSearchToolSpec": async (config: unknown) => { + duckduckgo: async (config: unknown) => { return [new DuckDuckGoSearchTool(config as DuckDuckGoToolParams)]; }, };