Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

FEAT: Add arxiv tool for searching scientific papers and articles fro… (#2632)

Yash Parmardbd1d7977097
diff --git a/api/core/tools/provider/builtin/arxiv/_assets/icon.svg b/api/core/tools/provider/builtin/arxiv/_assets/icon.svg
new file mode 100644
index 00000000000..0e60f635739
--- /dev/null
+++ b/api/core/tools/provider/builtin/arxiv/_assets/icon.svg
@@ -0,0 +1 @@
+<svg id="logomark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 17.732 24.269"><g id="tiny"><path d="M573.549,280.916l2.266,2.738,6.674-7.84c.353-.47.52-.717.353-1.117a1.218,1.218,0,0,0-1.061-.748h0a.953.953,0,0,0-.712.262Z" transform="translate(-566.984 -271.548)" fill="#bdb9b4"/><path d="M579.525,282.225l-10.606-10.174a1.413,1.413,0,0,0-.834-.5,1.09,1.09,0,0,0-1.027.66c-.167.4-.047.681.319,1.206l8.44,10.242h0l-6.282,7.716a1.336,1.336,0,0,0-.323,1.3,1.114,1.114,0,0,0,1.04.69A.992.992,0,0,0,571,293l8.519-7.92A1.924,1.924,0,0,0,579.525,282.225Z" transform="translate(-566.984 -271.548)" fill="#b31b1b"/><path d="M584.32,293.912l-8.525-10.275,0,0L573.53,280.9l-1.389,1.254a2.063,2.063,0,0,0,0,2.965l10.812,10.419a.925.925,0,0,0,.742.282,1.039,1.039,0,0,0,.953-.667A1.261,1.261,0,0,0,584.32,293.912Z" transform="translate(-566.984 -271.548)" fill="#bdb9b4"/></g></svg>
\ No newline at end of file
diff --git a/api/core/tools/provider/builtin/arxiv/arxiv.py b/api/core/tools/provider/builtin/arxiv/arxiv.py
new file mode 100644
index 00000000000..998128522ed
--- /dev/null
+++ b/api/core/tools/provider/builtin/arxiv/arxiv.py
@@ -0,0 +1,20 @@
+from core.tools.errors import ToolProviderCredentialValidationError
+from core.tools.provider.builtin.arxiv.tools.arxiv_search import ArxivSearchTool
+from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
+
+
+class ArxivProvider(BuiltinToolProviderController):
+ def _validate_credentials(self, credentials: dict) -> None:
+ try:
+ ArxivSearchTool().fork_tool_runtime(
+ meta={
+ "credentials": credentials,
+ }
+ ).invoke(
+ user_id='',
+ tool_parameters={
+ "query": "John Doe",
+ },
+ )
+ except Exception as e:
+ raise ToolProviderCredentialValidationError(str(e))
\ No newline at end of file
diff --git a/api/core/tools/provider/builtin/arxiv/arxiv.yaml b/api/core/tools/provider/builtin/arxiv/arxiv.yaml
new file mode 100644
index 00000000000..78c5c161af3
--- /dev/null
+++ b/api/core/tools/provider/builtin/arxiv/arxiv.yaml
@@ -0,0 +1,10 @@
+identity:
+ author: Yash Parmar
+ name: arxiv
+ label:
+ en_US: ArXiv
+ zh_Hans: ArXiv
+ description:
+ en_US: Access to a vast repository of scientific papers and articles in various fields of research.
+ zh_Hans: 访问各个研究领域大量科学论文和文章的存储库。
+ icon: icon.svg
diff --git a/api/core/tools/provider/builtin/arxiv/tools/arxiv_search.py b/api/core/tools/provider/builtin/arxiv/tools/arxiv_search.py
new file mode 100644
index 00000000000..033d942f4d4
--- /dev/null
+++ b/api/core/tools/provider/builtin/arxiv/tools/arxiv_search.py
@@ -0,0 +1,37 @@
+from typing import Any
+
+from langchain.utilities import ArxivAPIWrapper
+from pydantic import BaseModel, Field
+
+from core.tools.entities.tool_entities import ToolInvokeMessage
+from core.tools.tool.builtin_tool import BuiltinTool
+
+
+class ArxivSearchInput(BaseModel):
+ query: str = Field(..., description="Search query.")
+
+class ArxivSearchTool(BuiltinTool):
+ """
+ A tool for searching articles on Arxiv.
+ """
+ def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage | list[ToolInvokeMessage]:
+ """
+ Invokes the Arxiv search tool with the given user ID and tool parameters.
+
+ Args:
+ user_id (str): The ID of the user invoking the tool.
+ tool_parameters (dict[str, Any]): The parameters for the tool, including the 'query' parameter.
+
+ Returns:
+ ToolInvokeMessage | list[ToolInvokeMessage]: The result of the tool invocation, which can be a single message or a list of messages.
+ """
+ query = tool_parameters.get('query', '')
+
+ if not query:
+ return self.create_text_message('Please input query')
+
+ arxiv = ArxivAPIWrapper()
+
+ response = arxiv.run(query)
+
+ return self.create_text_message(self.summary(user_id=user_id, content=response))
diff --git a/api/core/tools/provider/builtin/arxiv/tools/arxiv_search.yaml b/api/core/tools/provider/builtin/arxiv/tools/arxiv_search.yaml
new file mode 100644
index 00000000000..7439a48658c
--- /dev/null
+++ b/api/core/tools/provider/builtin/arxiv/tools/arxiv_search.yaml
@@ -0,0 +1,23 @@
+identity:
+ name: arxiv_search
+ author: Yash Parmar
+ label:
+ en_US: Arxiv Search
+ zh_Hans: Arxiv 搜索
+description:
+ human:
+ en_US: A tool for searching scientific papers and articles from the Arxiv repository. Input can be an Arxiv ID or an author's name.
+ zh_Hans: 一个用于从Arxiv存储库搜索科学论文和文章的工具。 输入可以是Arxiv ID或作者姓名。
+ llm: A tool for searching scientific papers and articles from the Arxiv repository. Input can be an Arxiv ID or an author's name.
+parameters:
+ - name: query
+ type: string
+ required: true
+ label:
+ en_US: Query string
+ zh_Hans: 查询字符串
+ human_description:
+ en_US: The Arxiv ID or author's name used for searching.
+ zh_Hans: 用于搜索的Arxiv ID或作者姓名。
+ llm_description: The Arxiv ID or author's name used for searching.
+ form: llm
diff --git a/api/requirements.txt b/api/requirements.txt
index 5881c99903c..fbadcdbf043 100644
--- a/api/requirements.txt
+++ b/api/requirements.txt
@@ -66,4 +66,5 @@ yfinance~=0.2.35
pydub~=0.25.1
gmpy2~=2.1.5
numexpr~=2.9.0
-duckduckgo-search==4.4.3
\ No newline at end of file
+duckduckgo-search==4.4.3
+arxiv==2.1.0
\ No newline at end of file