Flow 触发端点
使用 /run
和 /webhook
端点来运行流程。
要创建、读取、更新和删除流程,请参阅 Flow 管理端点。
运行流程
Langflow 会为所有流程自动生成 /v1/run/$FLOW_ID
端点的 Python、JavaScript 和 curl 代码片段。
有关更多信息,请参阅 生成 API 代码片段。
通过 ID 或名称执行指定的流程。 流程 ID 可以在 API 访问面板 的代码片段中或在流程的 URL 中找到。
以下示例运行 Basic Prompting 模板流程,并在请求正文中传递流程参数。
此流程需要一个聊天输入字符串 (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
的响应包含运行的元数据、输入和输出。
结果
以下示例展示了来自 Basic Prompting 流程的响应:
_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": "当然!你听说过被称为 \"生物发光\" 的现象吗?这是一个迷人的自然现象,生物体在其中产生并发出光。这种能力存在于各种物种中,包括某些类型的 jellyfish、fireflies 和深海生物如 anglerfish。\n\n生物发光通过化学反应发生,其中一种称为荧光素的光分子与氧气反应,由一种称为荧光素酶的酶催化。结果是一种美丽的光芒,可以用于各种目的,如吸引配偶、威慑捕食者或诱捕猎物。\n\n生物发光最令人惊叹的展示之一可以在海洋中看到,某些浮游生物在被扰动时会发出光,在水中创造出迷人的蓝色光芒。这种现象通常被称为 \"海火花\",可以在世界各地的沿海地区看到。\n\n生物发光不仅吸引我们的想象力,而且在科学和医学中也有实际应用,包括生物传感器和成像技术的发展。这是自然创造力和复杂性的一个显著例子!",_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 token响应
通过 /v1/run/$FLOW_ID
,流程将以批处理方式执行,并可选择性地流式传输LLM token响应。
要流式传输LLM token响应,请在请求中添加 ?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
事件结束。
结果
以下示例被截断以说明一系列 token
事件以及关闭LLM token流式响应的最终 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\"?..."}}}
运行端点头部
Header | Info | Example |
---|---|---|
Content-Type | Required. Specifies the JSON format. | "application/json" |
accept | Optional. Specifies the response format. | "application/json" |
x-api-key | Optional. Required only if authentication is enabled. | "sk-..." |
运行端点参数
Parameter | Type | Info |
---|---|---|
flow_id | UUID/string | Required. Part of URL: /run/$FLOW_ID |
stream | Boolean | Optional. Query parameter: /run/$FLOW_ID?stream=true |
input_value | string | Optional. JSON body field. Main input text/prompt. Default: null |
input_type | string | Optional. JSON body field. Input type ("chat" or "text"). Default: "chat" |
output_type | string | Optional. JSON body field. Output type ("chat", "any", "debug"). Default: "chat" |
output_component | string | Optional. JSON body field. Target component for output. Default: "" |
tweaks | object | Optional. JSON body field. Component adjustments. Default: null |
session_id | string | Optional. JSON body field. Conversation context ID. See Session ID. Default: 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
请求。
有关更多信息,请参阅 使用 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"}'
结果
_10{_10 "message": "任务已在后台启动",_10 "status": "进行中"_10}
已弃用的流程触发端点
以下端点已被弃用,并由 /run
端点替代:
/process
/predict