langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
45.78k stars 6.45k forks source link

When a React agent invokes a tool, the tool response is exceptionally repeated twice. #8168

Open lixiangtiandashen opened 1 week ago

lixiangtiandashen commented 1 week ago

Self Checks

Dify version

0.7.3

Cloud or Self Hosted

Cloud

Steps to reproduce

When I use a React-formatted agent to call a tool, it unusually repeats the returned result twice. 5534619383

✔️ Expected Behavior

No response

❌ Actual Behavior

No response

dosubot[bot] commented 1 week ago

@lixiangtiandashen Hello! I'm here to help you with any bugs, questions, or contributions you have for the project. Let me know how I can assist you!

I found a similar unsolved discussion regarding the issue where the agent tool invocation returns duplicate response content. You can check it out here [1].

In version 0.7.3 of Dify, a React agent might invoke a tool and receive a response that is repeated twice due to the way the ToolEngine.agent_invoke method processes and transforms the tool response. Specifically, the method _convert_tool_response_to_str concatenates the messages from the tool response, which might lead to repeated information if the tool response itself contains repeated messages.

Here is the relevant part of the code:

@staticmethod
def _convert_tool_response_to_str(tool_response: list[ToolInvokeMessage]) -> str:
    """
    Handle tool response
    """
    result = ''
    for response in tool_response:
        if response.type == ToolInvokeMessage.MessageType.TEXT:
            result += response.message
        elif response.type == ToolInvokeMessage.MessageType.LINK:
            result += f"result link: {response.message}. please tell user to check it."
        elif response.type == ToolInvokeMessage.MessageType.IMAGE_LINK or \
             response.type == ToolInvokeMessage.MessageType.IMAGE:
            result += "image has been created and sent to user already, you do not need to create it, just tell the user to check it now."
        elif response.type == ToolInvokeMessage.MessageType.JSON:
            result += f"tool response: {json.dumps(response.message, ensure_ascii=False)}."
        else:
            result += f"tool response: {response.message}."

    return result

This method concatenates all messages in the tool_response list into a single string. If the tool_response list contains repeated messages, the resulting string will also contain those repeated messages [2].

Additionally, the CotAgentRunner class in the api/core/agent/cot_agent_runner.py file shows that the agent's thought process and tool invocation results are managed through a scratchpad mechanism. The scratchpad accumulates responses and observations, which are then formatted and published. If the tool invocation results are not properly managed or deduplicated within this process, it could lead to repeated responses being included in the final output [3].

To continue talking to Dosu, mention @dosu.

crazywoola commented 1 week ago

I suggest you switching to a smarter agent, or if you are using ReAct, change the max_iteration to 1 to ensure there is only one tool call .

yuhp-zts commented 1 week ago

I suggest you switching to a smarter agent, or if you are using ReAct, change the max_iteration to 1 to ensure there is only one tool call .

My agent only calls one tool, the model uses Qwen-32B, the agent iteration count is 1, and I do not receive duplicate replies when running the tool separately, but duplicates occur when called through the agent.

yuhp-zts commented 1 week ago

I suggest you switching to a smarter agent, or if you are using ReAct, change the max_iteration to 1 to ensure there is only one tool call .

I found that this issue occurs when the agent calls a tool converted from a workflow; tools that are not part of a workflow do not exhibit duplication.

yuhp-zts commented 1 week ago

Perhaps the reason is here. image

crazywoola commented 1 week ago

In this case you could try to return the text or json only in the workflow end node to see if you can get two same records. @yuhp-zts