소스 검색

FEAT: Add DuckDuckGo Search Tool for Enhanced Privacy-Focused Search Functionality (#2499)

Yash_1124 1 년 전
부모
커밋
adf2651d1f

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
api/core/tools/provider/builtin/duckduckgo/_assets/icon.svg


+ 20 - 0
api/core/tools/provider/builtin/duckduckgo/duckduckgo.py

@@ -0,0 +1,20 @@
+from core.tools.errors import ToolProviderCredentialValidationError
+from core.tools.provider.builtin.duckduckgo.tools.duckduckgo_search import DuckDuckGoSearchTool
+from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
+
+
+class DuckDuckGoProvider(BuiltinToolProviderController):
+    def _validate_credentials(self, credentials: dict) -> None:
+        try:
+            DuckDuckGoSearchTool().fork_tool_runtime(
+                meta={
+                    "credentials": credentials,
+                }
+            ).invoke(
+                user_id='',
+                tool_parameters={
+                    "query": "John Doe",
+                },
+            )
+        except Exception as e:
+            raise ToolProviderCredentialValidationError(str(e))

+ 10 - 0
api/core/tools/provider/builtin/duckduckgo/duckduckgo.yaml

@@ -0,0 +1,10 @@
+identity:
+  author: Yash Parmar
+  name: duckduckgo
+  label:
+    en_US: DuckDuckGo
+    zh_Hans: DuckDuckGo
+  description:
+    en_US: A privacy-focused search engine.
+    zh_Hans: 一个注重隐私的搜索引擎。
+  icon: icon.svg

+ 40 - 0
api/core/tools/provider/builtin/duckduckgo/tools/duckduckgo_search.py

@@ -0,0 +1,40 @@
+from typing import Any
+
+from langchain.tools import DuckDuckGoSearchRun
+from pydantic import BaseModel, Field
+
+from core.tools.entities.tool_entities import ToolInvokeMessage
+from core.tools.tool.builtin_tool import BuiltinTool
+
+
+class DuckDuckGoInput(BaseModel):
+    query: str = Field(..., description="Search query.")
+
+
+class DuckDuckGoSearchTool(BuiltinTool):
+    """
+    Tool for performing a search using DuckDuckGo search engine.
+    """
+
+    def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage | list[ToolInvokeMessage]:
+        """
+        Invoke the DuckDuckGo search tool.
+
+        Args:
+            user_id (str): The ID of the user invoking the tool.
+            tool_parameters (dict[str, Any]): The parameters for the tool invocation.
+
+        Returns:
+            ToolInvokeMessage | list[ToolInvokeMessage]: The result of the tool invocation.
+        """
+        query = tool_parameters.get('query', '')
+
+        if not query:
+            return self.create_text_message('Please input query')
+
+        tool = DuckDuckGoSearchRun(args_schema=DuckDuckGoInput)
+
+        result = tool.run(query)
+
+        return self.create_text_message(self.summary(user_id=user_id, content=result))
+    

+ 23 - 0
api/core/tools/provider/builtin/duckduckgo/tools/duckduckgo_search.yaml

@@ -0,0 +1,23 @@
+identity:
+  name: duckduckgo_search
+  author: Yash Parmar
+  label:
+    en_US: DuckDuckGo Search
+    zh_Hans: DuckDuckGo 搜索
+description:
+  human:
+    en_US: Perform searches on DuckDuckGo and get results.
+    zh_Hans: 在 DuckDuckGo 上进行搜索并获取结果。
+  llm: Perform searches on DuckDuckGo and get results.
+parameters:
+  - name: query
+    type: string
+    required: true
+    label:
+      en_US: Query string
+      zh_Hans: 查询语句
+    human_description:
+      en_US: The search query.
+      zh_Hans: 搜索查询语句。
+    llm_description: Key words for searching
+    form: llm

+ 2 - 1
api/requirements.txt

@@ -65,4 +65,5 @@ matplotlib~=3.8.2
 yfinance~=0.2.35
 pydub~=0.25.1
 gmpy2~=2.1.5
-numexpr~=2.9.0
+numexpr~=2.9.0
+duckduckgo-search==4.4.3

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.