dify 当代理内的工具使用secret-input类型参数并自动替换为"***"时,将导致API请求失败,

ubby3x7f  于 2个月前  发布在  其他
关注(0)|答案(2)|浏览(29)

自检

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

Dify 版本

0.6.14

云或自托管

自托管(源代码)

重现步骤

工具中的参数类型为 secret-input :

在 Agnet 中添加此工具并手动填写参数:

启动测试对话:

✔️ 预期行为

有时,API 请求期间参数会正常发送,结果也是正确的:

❌ 实际行为

有时,参数会被替换为 **** ,服务器收到的请求也是遮罩参数,请求结果不正确:

这种情况并非不可避免,也无法找到偶尔的规律。它只是发生了!而且,当它发生时,即使我立即重新填写这些参数并保存它们,再次请求时,参数仍然被替换为 **** 。
顺便说一下,我的 Dify 已拉取了代码的最新版本(0.6.14-2024/07/19, f2004ad )

xdyibdwo

xdyibdwo1#

为了解决Dify版本0.6.14中API请求中secret-input类型参数偶尔被替换为"***"的问题,确保在API请求中使用这些参数之前对其进行正确解密。应在使用参数之前调用decrypt_tool_parameters方法对参数进行解密。以下是处理解密的代码相关部分:

def decrypt_tool_parameters(self, parameters: dict[str, Any]) -> dict[str, Any]:
    """
decrypt tool parameters with tenant id

return a deep copy of parameters with decrypted values
"""
    cache = ToolParameterCache(
        tenant_id=self.tenant_id,
        provider=f'{self.provider_type}.{self.provider_name}',
        tool_name=self.tool_runtime.identity.name,
        cache_type=ToolParameterCacheType.PARAMETER,
        identity_id=self.identity_id
    )
    cached_parameters = cache.get()
    if cached_parameters:
        return cached_parameters

    # override parameters
    current_parameters = self._merge_parameters()
    has_secret_input = False

    for parameter in current_parameters:
        if parameter.form == ToolParameter.ToolParameterForm.FORM and parameter.type == ToolParameter.ToolParameterType.SECRET_INPUT:
            if parameter.name in parameters:
                try:
                    has_secret_input = True
                    parameters[parameter.name] = encrypter.decrypt_token(self.tenant_id, parameters[parameter.name])
                except:
                    pass

    if has_secret_input:
        cache.set(parameters)

    return parameters

确保在准备API请求的参数时在代码的适当位置调用此方法。这将确保secret-input类型参数得到解密,而不是被替换为"***" 1(https://github.com/langgenius/dify/blob/main/api/core/tools/utils/configuration.py)。
此外,当API请求中的secret-input参数被替换为"***"时,会生成特定的日志或错误消息。API响应将包括action字段设置为"overrided",并且inputstext字段将显示被替换的内容 2(https://github.com/langgenius/dify-docs/blob/main/en/guides/extension/api-based-extension/moderation-extension.md)3(https://github.com/langgenius/dify-docs/blob/main/en/guides/extension/api-based-extension/moderation.md)。

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

相关问题