I am using dify's API for enterprise applications, but its streaming output is a string. How can I change it to a reasonable object?
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.15.2
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
I am using dify's API for enterprise applications, but its streaming output is a string. How can I change it to a reasonable object?类似如下: { "type": "cover", "data": { "title": "各类传感器工作原理", "text": "探索传感器奥秘与应用前景" } }
{ "type": "contents", "data": { "items": [ "传感器基础知识", "常见传感器原理", "智能设备中应用", "发展趋势与挑战" ] } }
{ "type": "transition", "data": { "title": "了解传感器基础", "text": "先掌握传感器的基本概念和构成" } }
{ "type": "content", "data": { "title": "传感器基础知识", "items": [ { "title": "定义与分类", "text": "传感器是能感受规定的被测量并按照一定规律转换成可用信号的器件或装置。可分为物理、化学、生物等类型,分类有助于深入研究。" }, { "title": "基本组成", "text": "一般由敏感元件、转换元件和信号调理电路组成。敏感元件感受被测量,转换元件将其转换为电信号,调理电路处理信号。" }, { "title": "应用领域", "text": "广泛应用于工业、农业、交通、医疗等领域。如工业中监测生产参数,交通中辅助自动驾驶,医疗中进行疾病诊断。" } ] } }
{ "type": "transition", "data": { "title": "常见传感器原理", "text": "深入探究不同类型传感器工作机制" } }
{ "type": "content", "data": { "title": "电阻式传感器", "items": [ { "title": "工作原理", "text": "基于电阻值随被测量变化的原理工作。当被测量作用于电阻元件时,其电阻值发生改变,通过测量电阻变化来确定被测量大小。例如应变片传感器。" } ] } }
{ "type": "content", "data": { "title": "电容式传感器", "items": [ { "title": "工作原理", "text": "利用电容的变化来检测被测量。电容值与极板面积、极板间距和介质介电常数有关,被测量改变这些参数时,电容变化被检测出来。常用于液位测量等。" } ] } }
{ "type": "content", "data": { "title": "电感式传感器", "items": [ { "title": "工作原理", "text": "基于电磁感应原理。当被测量引起线圈电感量变化时,通过检测电路将电感变化转换为电信号输出。可用于位移、振动等测量。" } ] } }
{ "type": "content", "data": { "title": "光电传感器", "items": [ { "title": "工作原理", "text": "利用光的发射和接收来检测物体。当物体遮挡光线或反射光线时,接收器接收到的光强发生变化,转换为电信号,用于计数、检测有无物体等。" } ] } }
{ "type": "content", "data": { "title": "超声波传感器", "items": [ { "title": "工作原理", "text": "通过发射超声波,遇到物体反射回来,测量超声波传播时间来计算距离。广泛应用于倒车雷达、液位检测等领域。" } ] } }
{ "type": "content", "data": { "title": "毫米波雷达", "items": [ { "title": "工作原理", "text": "发射毫米波信号,根据回波信号的频率、相位等信息来确定目标的距离、速度和角度。在汽车自动驾驶等领域有重要应用。" } ] } }
{ "type": "transition", "data": { "title": "智能设备中应用", "text": "看看传感器在智能设备中的作用" } }
{ "type": "content", "data": { "title": "汽车中的应用", "items": [ { "title": "安全辅助", "text": "多种传感器用于汽车安全系统,如碰撞传感器在碰撞时触发安全气囊,毫米波雷达用于自适应巡航和自动紧急制动,保障行车安全。" }, { "title": "环境感知", "text": "摄像头、超声波传感器等帮助汽车感知周围环境,实现自动泊车、车道保持等功能,提升驾驶便利性和舒适性。" } ] } }
{ "type": "content", "data": { "title": "工业自动化中应用", "items": [ { "title": "生产监测", "text": "传感器实时监测生产过程中的温度、压力、流量等参数,确保生产稳定进行。出现异常时及时报警,提高生产效率和产品质量。" }, { "title": "机器人控制", "text": "机器人通过传感器获取周围环境信息,实现精确的运动控制和操作。如视觉传感器使机器人能识别物体,触觉传感器辅助抓取。" } ] } }
{ "type": "content", "data": { "title": "智能家居中应用", "items": [ { "title": "环境监测", "text": "温湿度传感器、空气质量传感器实时监测室内环境,自动调节空调、净化器等设备,营造舒适健康的居住环境。" }, { "title": "智能安防", "motion": "门窗传感器、红外传感器用于安防监测,当检测到异常时及时通知用户,保障家庭安全。" } ] } }
{ "type": "transition", "data": { "title": "发展趋势与挑战", "text": "展望传感器未来及面临的问题" } }
{ "type": "content", "data": { "title": "发展方向", "items": [ { "title": "微型化", "text": "传感器不断向微型化发展,体积更小、功耗更低,便于集成到各种设备中,满足不同应用场景需求。" }, { "title": "智能化", "text": "具备智能处理能力,能对数据进行分析和判断,实现自我诊断和自适应调整,提高传感器的性能和可靠性。" }, { "title": "多功能化", "text": "一个传感器可同时测量多种物理量,减少设备数量和成本,提高系统的集成度和效率。" } ] } }
{ "type": "content", "data": { "title": "物联网中的角色", "items": [ { "title": "数据采集", "text": "作为物联网的前端设备,负责采集各种环境和设备数据,为物联网系统提供基础信息,是物联网运行的关键环节。" }, { "title": "连接纽带", "text": "将物理世界和数字世界连接起来,使设备之间能够实现信息交互和远程控制,推动物联网的广泛应用。" } ] } }
{ "type": "content", "data": { "title": "面临的挑战", "items": [ { "title": "精度提升", "text": "随着应用需求的提高,对传感器精度要求越来越高,如何进一步提升精度是面临的重要挑战之一。" }, { "title": "可靠性增强", "text": "在复杂环境下保证传感器的可靠性和稳定性,防止数据误差和故障,是需要解决的问题。" }, { "title": "成本降低", "text": "降低传感器的生产成本,提高性价比,才能促进其更广泛的应用和推广。" } ] } }
{ "type": "end" }
但是dify的SSE却是: "outputs": {"好好学习然后天天向上!!"}
✔️ Expected Behavior
No response
❌ Actual Behavior
No response
To convert the streaming output from Dify's API, which is currently a string, into a structured object for enterprise applications, you can use JSON Schema. Here's how you can do it:
-
Enable JSON Schema in Dify: Switch the LLM in your application to a model that supports JSON Schema output, such as
gpt-4o-mini-2024-07-18or later. In the settings, enableJSON Schemaand fill in the JSON Schema template. Also, enable theresponse_formatcolumn and set it tojson_schema. -
Define a JSON Schema Template: Create a JSON Schema that describes the structure of the data you expect. Here's an example:
{ "name": "template_schema", "description": "A generic template for JSON Schema", "strict": true, "schema": { "type": "object", "properties": { "type": { "type": "string", "description": "Type of the content" }, "data": { "type": "object", "description": "Data content", "properties": { "title": { "type": "string", "description": "Title of the content" }, "text": { "type": "string", "description": "Text of the content" }, "items": { "type": "array", "description": "List of items", "items": { "type": "object", "properties": { "title": { "type": "string" }, "text": { "type": "string" } } } } } } }, "required": ["type", "data"], "additionalProperties": false } } -
Process the Output: Once the JSON Schema is set up, the LLM will output content in a structured format according to the schema. You can then parse this structured JSON output in your application to convert it into a structured object suitable for enterprise applications.
By following these steps, you can ensure that the streaming output from Dify's API is converted into a structured object using JSON Schema [1][2].
To continue talking to Dosu, mention @dosu.
Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other