流式

如果在预测时设置了流式处理,则当标记可用时,它们将以纯数据的形式服务器发送的事件发送。

使用Python/TS库

Flowise提供了两个库:

标签页

python 从flowise导入Flowise和PredictionData

def test_streaming(): 客户端 = Flowise()

# 测试流式预测
完成度 = 客户端.创建预测(
    预测数据(
        chatflowId="<流程ID>",
        问题="给我讲个笑话吧!",,
        流式传输=True
    )
)

# 处理并打印每个流式处理的数据块
打印(“流式响应:”)
对于completion中的每个块:
    # {event: "token", data: "hello"}
    打印(chunk)

如果 name 等于 "main": test_streaming()

{% endtab %}

{% tab title="Typescript" %}
```javascript

/// 译文内容:
---
根据上面的信息,执行如下指令:
缺失译文,请检查输入
从'flowise-sdk'导入{FlowiseClient}

异步函数 test_streaming() {
  const client = new FlowiseClient({ baseUrl: 'http://localhost:3000' });;

  尝试 {
    // 用于流式预测
    const prediction = await client.createPrediction({
      chatflowId: '<流程ID>',
      问题:“法国的首都是什么?”,
      流式传输:是,
    });

    对于预测中的每个数据块,执行以下操作:
        // {事件: "token", 数据: "hello"}
        console.log(chunk);;
    }
    
  } catch (error) {
    console.error('错误:', error);;
  }
}

// 运行流式测试
test_streaming()

{% endtab %}

{% tab title="cURL" %}

使用curl命令访问:https://localhost:3000/api/v1/predictions/{flow-id} \
  -H "Content-Type: application/json" \
d
    "问题":"Hello world!",
    "streaming": true
  }'

{% endtab %} {% endtabs %}

事件:令牌
数据:从前。。。

预测的事件流由以下事件类型组成:

事件
描述

start

流媒体开始

标记

当预测正在输出新标记时发出

错误

当预测返回错误时发出

结束

预测完成时发出

元数据

相关流程的所有元数据,如chatId、messageId等。在所有标记流处理完毕后,且在结束事件之前发出

sourceDocuments

当流程从向量存储中返回源时发出

usedTools

当流程使用工具时发出

Streamlit应用

https://github.com/HenryHengZJ/flowise-streamlit

Last updated