dify Google Gemini提供程序在同时两次调用相同工具时发生错误,

pftdvrlh  于 2个月前  发布在  Go
关注(0)|答案(1)|浏览(46)

自检

  • 这仅用于报告错误,如果您想提问,请转到 Discussions
  • 我已搜索现有问题 search for existing issues ,包括已关闭的问题。
  • 我确认我使用英语提交此报告(我已阅读并同意 Language Policy )。
  • 请不要修改此模板 :) 并填写所有必需的字段。

Dify版本

0.6.10

云或自托管

云,自托管(Docker),自托管(源代码)

重现步骤

  1. 在Agent模式下使用Google Gemini 1.5模型提供程序。
  2. 观察模型可以同时输出两个相同的工具调用。
    您可以使用以下Gemini 1.5 Pro的DSL轻松复制此过程:
app:
  description: ''
  icon: scream
  icon_background: '#FFEAD5'
  mode: agent-chat
  name: POC
model_config:
  agent_mode:
    enabled: true
    max_iteration: 5
    strategy: function_call
    tools:
    - enabled: true
      isDeleted: false
      notAuthor: false
      provider_id: jina
      provider_name: jina
      provider_type: builtin
      tool_label: JinaReader
      tool_name: jina_reader
      tool_parameters:
        summary: ''
        target_selector: ''
        url: ''
        wait_for_selector: ''
  annotation_reply:
    enabled: false
  chat_prompt_config: {}
  completion_prompt_config: {}
  dataset_configs:
    datasets:
      datasets: []
    retrieval_model: single
  dataset_query_variable: ''
  external_data_tools: []
  file_upload:
    image:
      detail: high
      enabled: false
      number_limits: 3
      transfer_methods:
      - remote_url
      - local_file
  model:
    completion_params:
      stop: []
    mode: chat
    name: gemini-1.5-pro-latest
    provider: google
  more_like_this:
    enabled: false
  opening_statement: What would you like to do?
  pre_prompt: 'Please follow the tasks below:

### Tasks:

1. Use the jina_reader tool to gather relevant information about the latest news
from the url: "https://www.cnn.com/".

2. Use the jina_reader tool to gather relevant information about the latest news
from the url: "https://www.bbc.com/".

3. After the two tool calls are returned, answer the user''s query to the best
of your abilities. '
  prompt_type: simple
  retriever_resource:
    enabled: true
  sensitive_word_avoidance:
    configs: []
    enabled: false
    type: ''
  speech_to_text:
    enabled: false
  suggested_questions:
  - Please perform the tasks.
  suggested_questions_after_answer:
    enabled: true
  text_to_speech:
    enabled: false
    language: ''
    voice: ''
  user_input_form: []

✔️预期行为

  • 工具调用结果应正常执行,与OpenAI提供程序一样(见截图)。

❌实际行为

附加信息:

  • 问题在于 FunctionCallAgentRunner(BaseAgentRunner) 类,具体来说是 run() 函数。
  • 修复此问题需要重大重构、清理和重新组织 run() 函数的逻辑。
    建议:

我很乐意提交PR来解决此问题,但我认为最好由Dify团队成员处理。
请让我知道您需要任何进一步的信息或协助。

tkclm6bt

tkclm6bt1#

要解决Dify版本0.6.10中Google Gemini提供程序的问题,即在同一时间调用同一个工具两次会导致错误,您需要确保工具运行时正确管理以处理并发调用。实现锁定机制或使用异步调用可以帮助管理并发工具调用。
以下是使用线程锁来确保工具不会并发调用的可行方法:

from typing import Any
from threading import Lock

from core.tools.errors import ToolProviderCredentialValidationError
from core.tools.provider.builtin.google.tools.google_search import GoogleSearchTool
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController

class GoogleProvider(BuiltinToolProviderController):
    _lock = Lock()

    def _validate_credentials(self, credentials: dict[str, Any]) -> None:
        with self._lock:
            try:
                GoogleSearchTool().fork_tool_runtime(
                    runtime={
                        "credentials": credentials,
                    }
                ).invoke(
                    user_id='',
                    tool_parameters={
                        "query": "test",
                        "result_type": "link"
                    },
                )
            except Exception as e:
                raise ToolProviderCredentialValidationError(str(e))

此代码引入了一个线程锁( _lock )以确保 GoogleSearchTool 不会被并发调用。 with self._lock 语句确保该代码块一次只由一个线程执行,从而防止并发调用 1(https://github.com/langgenius/dify/blob/main/api/core/tools/provider/builtin/google/google.py) 。

  • 要继续与 Dosu 交谈,请提及 @dosu。*

相关问题