如何在 Dify 中以真实且自动化的方式验证整个对话?

你好,我想知道是否有一种真正的方法可以在 Dify 工作流中验证整个对话,而不仅仅是单个响应或孤立的消息。

我的用例如下:
我有一个 LLM,它会生成一个包含完整故事的 JSON 结构(例如,使用像 [voice:name] 这样的标签的海盗主题叙事)。在模型生成这个故事后,我需要对整个输出进行验证,以确保它符合特定规则,例如:故事必须以海盗开头、Captain Barbanegra Rojo 想要偷国王的黄金、国王的女儿必须出现在故事中、JSON 格式正确,以及所有语音标签都正确使用。

我需要在工作流中,在故事生成后立即自动执行此验证。目标不是验证单个消息,而是完整的生成内容,甚至必要时是整个对话。

我想知道 Dify 目前是否提供任何官方机制来验证完整的 JSON 输出或多轮内容,或者是否有推荐的方法来实现对 LLM 生成结果的结构化验证。

简而言之,我希望 Dify 能够获取前一步骤生成的完整 JSON 输出,分析它,检查它是否符合我定义的所有叙事规则,然后返回类似“有效/无效”以及错误列表的结果。

目前的 Dify 工具是否支持此功能,还是未来计划支持完整的对话验证?

以下是一个示例:

故事必须以一些海盗开头。

:robot:

工作流过程

:open_book: 故事:
{
“content”: “[voice:narrador]: In the heart of the stormy seas, a fearsome pirate crew gathered on the deck of the ship known as The Tempest. [voice:antonio]: Ahoy, me hearties! They call me Blackbeard, captain of this cursed vessel, and I lead you into the depths of adventure! [voice:santiago]: I am Santiago, the cunning first mate, always ready to navigate through treacherous waters. [voice:sofia]: And I, Sofia, the fierce gunner, shall defend our ship with unmatched valor! [voice:jaime]: The treasure awaits, and I, Jaime, the swift hands of the crew, will be the first to claim it! [voice:veronica]: Beware! For the sea is full of dangers, and I, Veronica, the wise sage, can foresee the perils that lie ahead. [voice:leo]: But fear not, for luck is on our side. I, Leo, the charmer, can win over any foe we encounter. [voice:hernan]: Let them come! I am Hernan, the brutish fighter, and I shall crush any who dare oppose us! [voice:melina]: Together we sail, united in our quest for glory and gold! [voice:carla]: But remember, even the fiercest pirates face their reckoning. Let us tread carefully, for the sea remembers! [voice:matias]: And I shall make sure we remain stealthy, for I am Matias, the silent shadow of the crew. [voice:rodrigo]: What say ye, captain? Shall we set sail into the storm? [voice:antonio]: Aye! The winds favor us! Hoist the sails and let the adventure begin!”
}

Santiago 想要从邪恶的国王那里偷黄金。

:robot:

工作流过程

:open_book: 故事:
{
“content”: “[voice:narrador]: As the crew gathered around the captain, Santiago stepped forward with a gleam in his eyes. [voice:santiago]: Aye, mates! I have a cunning plan to steal the gold from the evil king! [voice:antonio]: Speak, Santiago! What do you have in mind? [voice:sofia]: The king’s treasure is heavily guarded, we must be clever! [voice:jaime]: I can slip past their defenses, as silent as the night! [voice:veronica]: And I can sense the king’s movements, guiding us through the shadows. [voice:leo]: With my charm, I can distract the guards while you make your move! [voice:hernan]: And I will be ready to fight if things go awry! [voice:melina]: Together, we can achieve the impossible! [voice:carla]: But we must tread lightly; the king’s fury knows no bounds. [voice:matias]: Stealth is our ally, let us prepare for our daring heist! [voice:rodrigo]: Aye! The king will never see it coming! [voice:antonio]: Then it is settled! Prepare the ship, for we sail at dawn!”
}

Santiago 爱上了邪恶国王的女儿。

:robot:

工作流过程

:open_book: 故事:
:open_book: 故事:
{
“content”: “[voice:narrador]: As the days passed, Santiago found himself drawn to a figure that would change his fate. [voice:santiago]: I cannot help but admire her beauty from afar, the king’s daughter. [voice:sofia]: What madness is this, Santiago? She is the daughter of our enemy! [voice:jaime]: Love can be a dangerous game, my friend. [voice:veronica]: But perhaps she holds the key to our plan. [voice:leo]: If you win her heart, we may gain an ally within the castle! [voice:hernan]: Or a traitor among us! [voice:melina]: Be cautious, Santiago. The heart can lead us astray. [voice:carla]: Love and war are often intertwined; tread lightly! [voice:matias]: I can help you, Santiago. Stealth is my specialty, and I’ll ensure you meet her without alerting the guards. [voice:rodrigo]: Aye, but remember, if her father discovers this, it could mean doom for us all. [voice:santiago]: I know the risks, but I must try. For love is a treasure worth fighting for!”
}

由于女儿的帮助,他们得到了黄金并结婚,这是一个幸福的结局。

:robot:

工作流过程

:open_book: 故事:
:open_book: 故事:
{
“content”: “[voice:narrador]: With the king’s daughter by his side, Santiago hatched a plan that would lead them to victory. [voice:santiago]: Together, we will steal the gold and secure our future! [voice:sofia]: I’ll provide cover fire while you make your move! [voice:jaime]: And I’ll create a diversion to keep the guards occupied! [voice:veronica]: I can sense the path to the treasure; follow my lead! [voice:leo]: With the charm of the princess, we can outsmart the king’s forces! [voice:hernan]: Let’s show them the true strength of our crew! [voice:melina]: Love will guide us through this perilous endeavor! [voice:carla]: And together, we shall claim what is rightfully ours! [voice:matias]: Stealth is our ally, and we will succeed! [voice:rodrigo]: The gold will be ours! [voice:narrador]: With the help of the king’s daughter, they executed their plan flawlessly and emerged victorious with the treasure. [voice:santiago]: With the gold in hand, I ask for your hand in marriage, my love! [voice:sofia]: A wedding for the ages! [voice:jaime]: Aye, let’s celebrate this union! [voice:veronica]: A new chapter begins! [voice:leo]: May their love shine brighter than any treasure! [voice:hernan]: Cheers to the happy couple! [voice:melina]: And to a life filled with adventure! [voice:carla]: Aye, let the celebrations begin! [voice:matias]: The sea has blessed us this day! [voice:rodrigo]: A tale for the ages, indeed!”
}

在这里,我想使用另一个模型来验证所有内容。

使用 迭代 节点怎么样?
你需要将输出构建成一个数组,然后在迭代节点中只需验证你的内容。

您可以使用 LLM 节点的结构化输出功能,在单轮对话中指定 LLM 节点输出的 JSON 模式。

https://docs.dify.ai/en/use-dify/nodes/llm#structured-outputs

尽管 LLM 也可以在之后用于验证实际内容,但如果需要严格验证,建议使用代码节点。

要临时存储多轮对话中的所有交互,最好使用会话变量。如果在每轮对话中向会话变量添加值,您可以在后续步骤中自由地从数组中检索之前对话的内容。

https://docs.dify.ai/versions/3-0-x/en/user-guide/workflow/variables#conversation-variables