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.3k stars 6.36k forks source link

Exception output after the workflow was published as a tool #7505

Closed skywolf123 closed 3 weeks ago

skywolf123 commented 3 weeks ago

Self Checks

Dify version

0.7.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. I did a workflow that was very simple. I started typing a string, passed through an LLM, and the result was the text output by the LLM
  2. Then I published this workflow as a tool
  3. In a new workflow, I started typing a string, connecting an LLM, and then connecting the tool that was just released. The output text of the LLM is given to the tool, and then the output of the tool is given to the result.
  4. However, the output at this time is {"text": "XXXX"} instead of normal XXXXX image

✔️ Expected Behavior

No response

❌ Actual Behavior

No response

dosubot[bot] commented 3 weeks ago

The output of your tool in the new workflow shows {"text": "XXXX"} instead of the expected normal XXXXX because the text is being returned as a JSON object with a key "text" rather than a plain string. This behavior is likely due to the configuration of the tool or the way the output is being handled in the workflow.

To resolve this, you need to adjust the workflow or tool configuration to extract the value of the text key from the JSON object before displaying it. Here is an example of how you might adjust the workflow to extract the text value:

def create_text_message(self, text: str, save_as: str = '') -> ToolInvokeMessage:
    """
        create a text message

        :param text: the text of the message
        :return: the text message
    """

In this example, you would need to parse the JSON object and extract the value of the text key to display the plain text XXXX instead of the JSON object {"text": "XXXX"}. This can be done by modifying the tool's output handling to directly return the text value [1][2].

To continue talking to Dosu, mention @dosu.

skywolf123 commented 3 weeks ago

To be honest, I didn't understand this AI reply

dosubot[bot] commented 3 weeks ago

To adjust the workflow or tool configuration to extract the value of the text key from the JSON object and display the plain text instead, you can use the JSONParseTool class provided in the Dify platform. This class allows you to extract data from JSON content using a JSON filter.

Here is an example of how you can configure and use this tool to extract and display the plain text value of the text key from a JSON object:

import json
from core.tools.provider.builtin.json_process.tools.parse import JSONParseTool

def extract_and_display_text(json_object):
    tool = JSONParseTool()
    content = json.dumps(json_object)
    json_filter = '$.text'
    tool_parameters = {
        'content': content,
        'json_filter': json_filter,
        'ensure_ascii': True
    }
    result = tool._invoke(user_id='example_user', tool_parameters=tool_parameters)
    return result.content

# Example usage
json_object = {
    "text": "This is a sample text."
}

plain_text = extract_and_display_text(json_object)
print(plain_text)  # Output: This is a sample text.

In this example, the extract_and_display_text function initializes the JSONParseTool, sets up the JSON content and filter, and then invokes the tool to extract the value of the text key. The resulting plain text is then returned and can be displayed as needed [1][2].

To continue talking to Dosu, mention @dosu.

crazywoola commented 3 weeks ago

It's expected behavior, if you don't want the text key, you could use a jinja or code node to extract it. It's the same as retrieving things from HTTP. It should answer with a structure.

I noticed you have already submit a bug report but actually it's not and this one. So next time, please make sure search existing issue or use the discussion to ask a question.

skywolf123 commented 3 weeks ago

Leave a record for novice users like me.

Here you need to use a Jinja template converter to take out the required string from the json output part of the tool, note that the json here is an array.

image image image

If you use the text section to directly perform string conversion, it may cause no results to be displayed on the published page!

yuweimian-shy commented 3 weeks ago

Self Checks

  • [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing issues, including closed ones.
  • [x] I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • [x] Please do not modify this template :) and fill in all the required fields.

Dify version

0.7.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. I did a workflow that was very simple. I started typing a string, passed through an LLM, and the result was the text output by the LLM
  2. Then I published this workflow as a tool
  3. In a new workflow, I started typing a string, connecting an LLM, and then connecting the tool that was just released. The output text of the LLM is given to the tool, and then the output of the tool is given to the result.
  4. However, the output at this time is {"text": "XXXX"} instead of normal XXXXX image

✔️ Expected Behavior

No response

❌ Actual Behavior

No response

can you tell me how to publish workflow to tools, Why is the button for my publishing tool grayed out 微信图片_20240822154101

skywolf123 commented 3 weeks ago

I don't know why you're graying out here, I released this workflow first, and it works normally as a publishing page, and then I click on publishing as a tool, and I haven't encountered your situation.

You can open an issue or a discussion. The maintainers of this seem to be quite busy, they don't like us to ask questions in the form of issues, and they don't like to explain to novice users like me, you can ask people to see if they have encountered your situation.

crazywoola commented 3 weeks ago

You can open an issue or a discussion. The maintainers of this seem to be quite busy, they don't like us to ask questions in the form of issues, and they don't like to explain to novice users like me, you can ask people to see if they have encountered your situation.

For questions, it's better to open them in discussions. We already highlighted it in the self checks. But I already answered your questions. :)