dify 当添加代理工具时,duckduckgo_search出现错误,

balp4ylt  于 2个月前  发布在  Go
关注(0)|答案(4)|浏览(21)

自我检查

  • 这仅用于提交错误报告,如果您想提问,请访问 Discussions
  • 我已在现有问题中进行了搜索,包括已关闭的问题。
  • 我确认我使用英文提交此报告(我已阅读并同意 Language Policy )。
  • 请务必使用英文提交问题,否则会被关闭。谢谢!:)
  • 请不要修改此模板 :) 并填写所有必需的字段。

Dify版本

0.6.15

云或自托管

自托管(Docker)

重现步骤

创建应用程序 > 从空白开始创建 > 在工具中点击添加。.
出现内部服务器错误,然后在日志中我们看到了这个。
ERROR [Dummy-32] [module_import_helper.py:32] - 无法从 /app/api/core/tools/provider/builtin/duckduckgo/tools/duckduckgo_search.py 加载模块 core.tools.provider.builtin.duckduckgo.tools.duckduckgo_search:检测到一个未注解的属性:name = 'duckduckgo_search' 。所有模型字段都需要类型注解;如果 name 不是一个字段,您可能可以通过将其注解为 ClassVar 或更新 model_config['ignored_types'] 来解决此错误。

✔️ 预期行为

没有错误,可以像预期的那样添加工具。

❌ 实际行为

出现内部服务器错误,然后添加工具选项卡继续加载。

biswetbf

biswetbf1#

你好!我在这里帮助你解决仓库中的任何错误、问题或贡献。让我们一起解决你正在经历的问题。

要解决 duckduckgo_search 模块中的 "非注解属性" 错误,你需要在 DuckDuckGoSearchTool 类的属性中添加类型注解。以下是带有必要类型注解的更新后的代码:

from typing import Any, Dict, List

from duckduckgo_search import DDGS

from core.model_runtime.entities.message_entities import SystemPromptMessage
from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.tool.builtin_tool import BuiltinTool

SUMMARY_PROMPT = """
User's query: 
{query}

Here is the search engine result:
{content}

Please summarize the result in a few sentences.
"""

class DuckDuckGoSearchTool(BuiltinTool):
    """
Tool for performing a search using DuckDuckGo search engine.
"""

    def _invoke(self, user_id: str, tool_parameters: Dict[str, Any]) -> ToolInvokeMessage:
        query: str = tool_parameters.get('query', '')
        result_type: str = tool_parameters.get('result_type', 'text')
        max_results: int = tool_parameters.get('max_results', 10)
        require_summary: bool = tool_parameters.get('require_summary', False)
        response: List[Dict[str, Any]] = DDGS().text(query, max_results=max_results)

        if result_type == 'link':
            results: str = "\n".join([f"[{res.get('title')}]({res.get('href')})" for res in response])
            return self.create_link_message(link=results)
        results: str = "\n".join([res.get("body") for res in response])
        if require_summary:
            results = self.summary_results(user_id=user_id, content=results, query=query)
        return self.create_text_message(text=results)

    def summary_results(self, user_id: str, content: str, query: str) -> str:
        prompt: str = SUMMARY_PROMPT.format(query=query, content=content)
        summary = self.invoke_model(user_id=user_id, prompt_messages=[
            SystemPromptMessage(content=prompt),
        ], stop=[])
        return summary.message.content

添加的类型注解如下:

  • Dict[str, Any] 用于 tool_parameters
  • str 用于 queryresult_typeprompt
  • int 用于 max_results
  • bool 用于 require_summary
  • List[Dict[str, Any]] 用于 response
  • str 用于 results

这些注解与 ddgo_search.yaml 文件中的参数定义相匹配。

  • 要继续与 1(https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py) 交流,请提及 @dosu。*
lh80um4z

lh80um4z2#


.
我无法在云端版本和最新主分支上重现此问题。您是否有应用程序配置的任何屏幕截图?

bjp0bcyl

bjp0bcyl3#

@crazywoola 我尝试用 @dosu 建议的替换 https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py,但仍然出现错误。

这里还有我在点击 Create App > Create from Blank > Choose Agent > 在 Tools 点击 Add 时出现的错误截图。

ohfgkhjo

ohfgkhjo4#

您遇到的错误是由于在 DuckDuckGoSearchTool 类中的 name 属性没有被注解类型。您需要为类中的所有属性添加类型注解。以下是带有必要类型注解的 ddgo_search.py 文件的更新版本:

from typing import ClassVar, Any

from duckduckgo_search import DDGS

from core.model_runtime.entities.message_entities import SystemPromptMessage
from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.tool.builtin_tool import BuiltinTool

SUMMARY_PROMPT = """
User's query: 
{query}

Here is the search engine result:
{content}

Please summarize the result in a few sentences.
"""

class DuckDuckGoSearchTool(BuiltinTool):
    """
Tool for performing a search using DuckDuckGo search engine.
"""
    name: ClassVar[str] = 'duckduckgo_search'
    author: ClassVar[str] = 'Yash Parmar'
    label: ClassVar[dict] = {
        'en_US': 'DuckDuckGo Search',
        'zh_Hans': 'DuckDuckGo 搜索'
    }
    description: ClassVar[dict] = {
        'human': {
            'en_US': 'Perform searches on DuckDuckGo and get results.',
            'zh_Hans': '在 DuckDuckGo 上进行搜索并获取结果。'
        },
        'llm': 'Perform searches on DuckDuckGo and get results.'
    }
    parameters: ClassVar[list] = [
        {
            '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'
        },
        {
            'name': 'max_results',
            'type': 'number',
            'required': True,
            'default': 5,
            'label': {
                'en_US': 'Max results',
                'zh_Hans': '最大结果数量'
            },
            'human_description': {
                'en_US': 'The max results.',
                'zh_Hans': '最大结果数量。'
            },
            'form': 'form'
        },
        {
            'name': 'result_type',
            'type': 'select',
            'required': True,
            'options': [
                {
                    'value': 'text',
                    'label': {
                        'en_US': 'text',
                        'zh_Hans': '文本'
                    }
                },
                {
                    'value': 'link',
                    'label': {
                        'en_US': 'link',
                        'zh_Hans': '链接'
                    }
                }
            ],
            'default': 'text',
            'label': {
                'en_US': 'Result type',
                'zh_Hans': '结果类型'
            },
            'human_description': {
                'en_US': 'used for selecting the result type, text or link',
                'zh_Hans': '用于选择结果类型,使用文本还是链接进行展示'
            },
            'form': 'form'
        },
        {
            'name': 'require_summary',
            'type': 'boolean',
            'default': False,
            'label': {
                'en_US': 'Require Summary',
                'zh_Hans': '是否总结'
            },
            'human_description': {
                'en_US': 'Whether to pass the search results to llm for summarization.',
                'zh_Hans': '是否需要将搜索结果传给大模型总结'
            },
            'form': 'form'
        }
    ]

    def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage:
        query = tool_parameters.get('query', '')
        result_type = tool_parameters.get('result_type', 'text')
        max_results = tool_parameters.get('max_results', 10)
        require_summary = tool_parameters.get('require_summary', False)
        response = DDGS().text(query, max_results=max_results)

        if result_type == 'link':
            results = [f"[{res.get('title')}]({res.get('href')})" for res in response]
            results = "\n".join(results)
            return self.create_link_message(link=results)
        results = [res.get("body") for res in response]
        results = "\n".join(results)
        if require_summary:
            results = self.summary_results(user_id=user_id, content=results, query=query)
        return self.create_text_message(text=results)

    def summary_results(self, user_id: str, content: str, query: str) -> str:
        prompt = SUMMARY_PROMPT.format(query=query, content=content)
        summary = self.invoke_model(user_id=user_id, prompt_messages=[
            SystemPromptMessage(content=prompt),
        ], stop=[])
        return summary.message.content

通过向类属性添加 ClassVar 类型注解并确保所有属性都正确地进行了类型标注,您应该能够解决“内部服务器错误”问题 1(https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.yaml)2(https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/duckduckgo/tools/ddgo_search.py)。
已经有维护者介入进一步协助。

  • 要继续与 Dosu 对话,请提及 @dosu。*

相关问题