流程触发端点
使用 /run
和 /webhook
端点来运行流程。
要创建、读取、更新和删除流程,请参阅流程管理端点。
运行流程
Langflow 会为所有流程的 /v1/run/$FLOW_ID
端点自动生成 Python、JavaScript 和 curl 代码片段。
有关更多信息,请参阅生成 API 代码片段。
通过 ID 或名称执行指定的流程。 流程 ID 可以在API 访问窗格的代码片段中或流程的 URL 中找到。
以下示例运行一个基础提示流程,流程参数在请求主体中传递。
此流程需要一个聊天输入字符串(input_value
),并对所有其他参数使用默认值。
_12curl -X POST \_12 "$LANGFLOW_SERVER_URL/api/v1/run/$FLOW_ID" \_12 -H "Content-Type: application/json" \_12 -H "x-api-key: $LANGFLOW_API_KEY" \_12 -d '{_12 "input_value": "Tell me about something interesting!",_12 "session_id": "chat-123",_12 "input_type": "chat",_12 "output_type": "chat",_12 "output_component": "",_12 "tweaks": null_12 }'
/v1/run/$FLOW_ID
的响应包括运行的元数据、输入和输出。
Result
以下示例展示了基础提示流程的响应:
_29{_29 "session_id": "chat-123",_29 "outputs": [{_29 "inputs": {_29 "input_value": "Tell me about something interesting!"_29 },_29 "outputs": [{_29 "results": {_29 "message": {_29 "text": "Sure! Have you ever heard of the phenomenon known as \"bioluminescence\"? It's a fascinating natural occurrence where living organisms produce and emit light. This ability is found in various species, including certain types of jellyfish, fireflies, and deep-sea creatures like anglerfish.\n\nBioluminescence occurs through a chemical reaction in which a light-emitting molecule called luciferin reacts with oxygen, catalyzed by an enzyme called luciferase. The result is a beautiful glow that can serve various purposes, such as attracting mates, deterring predators, or luring prey.\n\nOne of the most stunning displays of bioluminescence can be seen in the ocean, where certain plankton emit light when disturbed, creating a mesmerizing blue glow in the water. This phenomenon is often referred to as \"sea sparkle\" and can be seen in coastal areas around the world.\n\nBioluminescence not only captivates our imagination but also has practical applications in science and medicine, including the development of biosensors and imaging techniques. It's a remarkable example of nature's creativity and complexity!",_29 "sender": "Machine",_29 "sender_name": "AI",_29 "session_id": "chat-123",_29 "timestamp": "2025-03-03T17:17:37+00:00",_29 "flow_id": "d2bbd92b-187e-4c84-b2d4-5df365704201",_29 "properties": {_29 "source": {_29 "id": "OpenAIModel-d1wOZ",_29 "display_name": "OpenAI",_29 "source": "gpt-4o-mini"_29 },_29 "icon": "OpenAI"_29 },_29 "component_id": "ChatOutput-ylMzN"_29 }_29 }_29 }]_29 }]_29}
如果您在应用程序中解析响应,您很可能需要从响应中提取相关内容,而不是将整个响应传回给用户。 有关从 Langflow API 响应中提取数据的脚本示例,请参阅快速入门。
流式传输 LLM 令牌响应
使用 /v1/run/$FLOW_ID
,流程作为批处理执行,可选择进行 LLM 令牌响应流式传输。
要流式传输 LLM 令牌响应,请在请求中添加 ?stream=true
查询参 数:
_10curl -X POST \_10 "$LANGFLOW_SERVER_URL/api/v1/run/$FLOW_ID?stream=true" \_10 -H "accept: application/json" \_10 -H "Content-Type: application/json" \_10 -H "x-api-key: $LANGFLOW_API_KEY" \_10 -d '{_10 "message": "Tell me something interesting!",_10 "session_id": "chat-123"_10 }'
LLM 聊天响应以 token
事件的形式流式传输回来,最终以关闭连接的 end
事件结束。
Result
以下示例被截断,用于展示一系列 token
事件以及关闭 LLM 令牌流式响应的最终 end
事件:
_19{"event": "add_message", "data": {"timestamp": "2025-03-03T17:20:18", "sender": "User", "sender_name": "User", "session_id": "chat-123", "text": "Tell me about something interesting!", "files": [], "error": false, "edit": false, "properties": {"text_color": "", "background_color": "", "edited": false, "source": {"id": null, "display_name": null, "source": null}, "icon": "", "allow_markdown": false, "positive_feedback": null, "state": "complete", "targets": []}, "category": "message", "content_blocks": [], "id": "0103a21b-ebf7-4c02-9d72-017fb297f812", "flow_id": "d2bbd92b-187e-4c84-b2d4-5df365704201"}}_19_19{"event": "add_message", "data": {"timestamp": "2025-03-03T17:20:18", "sender": "Machine", "sender_name": "AI", "session_id": "chat-123", "text": "", "files": [], "error": false, "edit": false, "properties": {"text_color": "", "background_color": "", "edited": false, "source": {"id": "OpenAIModel-d1wOZ", "display_name": "OpenAI", "source": "gpt-4o-mini"}, "icon": "OpenAI", "allow_markdown": false, "positive_feedback": null, "state": "complete", "targets": []}, "category": "message", "content_blocks": [], "id": "27b66789-e673-4c65-9e81-021752925161", "flow_id": "d2bbd92b-187e-4c84-b2d4-5df365704201"}}_19_19{"event": "token", "data": {"chunk": " Have", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "token", "data": {"chunk": " you", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "token", "data": {"chunk": " ever", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "token", "data": {"chunk": " heard", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "token", "data": {"chunk": " of", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "token", "data": {"chunk": " the", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "token", "data": {"chunk": " phenomenon", "id": "27b66789-e673-4c65-9e81-021752925161", "timestamp": "2025-03-03 17:20:18 UTC"}}_19_19{"event": "end", "data": {"result": {"session_id": "chat-123", "message": "Sure! Have you ever heard of the phenomenon known as \"bioluminescence\"?..."}}}
运行端点标头
标头 | 信息 | 示例 |
---|---|---|
Content-Type | 必需。指定 JSON 格式。 | "application/json" |
accept | 可选。指定响应格式。 | "application/json" |
x-api-key | 可选。仅在启用身份验证时必需。 | "sk-..." |
运行端点参数
参数 | 类型 | 信息 |
---|---|---|
flow_id | UUID/string | 必需。URL 的一部分:/run/$FLOW_ID |
stream | boolean | 可选。查询参数:/run/$FLOW_ID?stream=true |
input_value | string | 可选。JSON 主体字段。主要输入文本/提示。默认值:null |
input_type | string | 可选。JSON 主体字段。输入类型("chat" 或 "text")。默认值:"chat" |
output_type | string | 可选。JSON 主体字段。输出类型("chat"、"any"、"debug")。默认值:"chat" |
output_component | string | 可选。JSON 主体字段。输出的目标组件。默认值:"" |
tweaks | object | 可选。JSON 主体字段。组件调整。默认值:null |
session_id | string | 可选。JSON 主体字段。对话上下文 ID。请参阅 Session ID。默认值:null |
包含所有标头和参数的请求示例
_17curl -X POST \_17 "$LANGFLOW_SERVER_URL/api/v1/run/$FLOW_ID?stream=true" \_17 -H "Content-Type: application/json" \_17 -H "accept: application/json" \_17 -H "x-api-key: $LANGFLOW_API_KEY" \_17 -d '{_17 "input_value": "Tell me a story",_17 "input_type": "chat",_17 "output_type": "chat",_17 "output_component": "chat_output",_17 "session_id": "chat-123",_17 "tweaks": {_17 "component_id": {_17 "parameter_name": "value"_17 }_17 }_17 }'
Webhook 运行流程
使用 /webhook
端点通过发送 HTTP POST
请求来启动流程。
在将 Webhook 组件添加到流程后,打开API 访问窗格,然后点击 Webhook cURL 选项卡,获取为您的流程自动生成的 POST /webhook
请求。
_10curl -X POST \_10 "$LANGFLOW_SERVER_URL/api/v1/webhook/$FLOW_ID" \_10 -H "Content-Type: application/json" \_10 -H "x-api-key: $LANGFLOW_API_KEY" \_10 -d '{"data": "example-data"}'
Result
_10{_10 "message": "Task started in the background",_10 "status": "in progress"_10}
有关更多信息,请参阅 Webhook 组件和使用 webhooks 触发流程。
已弃用的流程触发端点
以下端点已弃用,由 /run
端点替代:
/process
/predict