快速入门
通过加载模板流、运行它,然后在 /run API 端点提供服务,开始使用 Langflow。
前置条件
-
创建 Langflow API 密钥
Langflow API 密钥是你可以与 Langflow 配合使用的用户特定令牌。
要创建 Langflow API 密钥,请执行以下操作:
-
在 Langflow 中,点击你的用户图标,然后选择 Settings(设置)。
-
点击 Langflow API Keys,然后点击 Add New。
-
为你的密钥命名,然后点击 Create API Key。
-
复制 API 密钥并安全存储。
-
要在请求中使用你的 Langflow API 密钥,请在终端中设置
LANGFLOW_API_KEY环境变量,然后在请求中包含x-api-key标头或查询参数。 例如:_13# 设置变量_13export LANGFLOW_API_KEY="sk..."_13_13# 发送请求_13curl --request POST \_13--url "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID" \_13--header "Content-Type: application/json" \_13--header "x-api-key: $LANGFLOW_API_KEY" \_13--data '{_13"output_type": "chat",_13"input_type": "chat",_13"input_value": "Hello"_13}'
-
运行 Simple Agent 模板流
- 在 Langflow 中,点击 New Flow,然后选择 Simple Agent 模板。

Simple Agent 模板由一个连接到 Chat Input(聊天输入)和 Chat Output(聊天输出)组件的 Agent(代理)组件、一个 Calculator(计算器)组件以及一个 URL 组件组成。当你运行此流时,你通过 Chat Input 组件向代理提交查询,代理使用 Calculator 和 URL 工具生成响应,然后通过 Chat Output 组件返回响应。
许多组件都可以作为代理的工具,包括 模型上下文协议 (MCP) 服务器。代理根据给定查询的上下文决定调用哪些工具。
-
在 Agent 组件中,直接输入你的 OpenAI API 密钥或使用 全局变量。
此示例使用 Agent 组件内置的 OpenAI 模型。 如果你想使用不同的提供商,请相应地编辑模型提供商、模型名称和凭据。 如果你首选的提供商或模型未列出,请将 Model Provider 设置为 Connect other models,然后连接任何语言模型组件。
-
要运行流,请点击 Playground。
-
要测试 Calculator 工具,请向代理询问一个简单的数学问题,例如
I want to add 4 and 4.(我想把 4 和 4 相加)。 为了帮助你测试和评估流,Playground 会显示代理在分析提示词、选择工具并使用工具生成响应时的推理过程。 在这种情况下,数学问题会导致代理选择 Calculator 工具并使用类似evaluate_expression的操作。

-
要测试 URL 工具,请向代理询问有关时事的信息。 对于此请求,代理会选择 URL 工具的
fetch_content操作,然后返回当前新闻头条的摘要。 -
完成流测试后,点击 Close。
既然你已经运行了第一个流,请尝试以下下一步:
- 通过连接不同的工具或向流中添加更多组件来编辑你的 Simple Agent 流。
- 从头开始或通过修改其他模板流来构建你自己的流。
- 将流集成到你的应用程序中,如从外部应用程序运行你的流中所述。
从外部应用程序运行你的流
Langflow 是一个 IDE,但它也是一个运行时,你可以通过 Langflow API 使用 Python、JavaScript 或 HTTP 进行调用。
当你本地启动 Langflow 时,你可以向本地 Langflow 服务器发送请求 。 对于生产应用程序,你需要部署一个稳定的 Langflow 实例来处理 API 调用。
例如,你可以使用 /run 端端来运行流并获取结果。
Langflow 提供了代码片段来帮助你开始使用 Langflow API。
-
编辑流时,点击 Share(分享),然后点击 API access(API 访问)。
API 访问面板中的默认代码构建了一个包含 Langflow 服务器
url、headers(标头)和请求数据payload(负载)的请求。 代码片段会自动包含流的LANGFLOW_SERVER_ADDRESS和FLOW_ID值,以及一个脚本,用于包含你的LANGFLOW_API_KEY(如果你已在终端会话中将其设置为环境变量)。 如果你将代码用于不同的服务器或流,请替换这些值。 默认的 Langflow 服务器地址是http://localhost:7860。- Python
- JavaScript
- curl
_29import requests_29_29url = "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID" # 此流的完整 API 端点 URL_29_29# 请求负载配置_29payload = {_29"output_type": "chat",_29"input_type": "chat",_29"input_value": "hello world!"_29}_29_29# 请求标头_29headers = {_29"Content-Type": "application/json",_29"x-api-key": "$LANGFLOW_API_KEY"_29}_29_29try:_29# 发送 API 请求_29response = requests.request("POST", url, json=payload, headers=headers)_29response.raise_for_status() # 为错误的响应状态码抛出异常_29_29# 打印响应_29print(response.text)_29_29except requests.exceptions.RequestException as e:_29print(f"调用 API 时出错: {e}")_29except ValueError as e:_29print(f"解析响应时出错: {e}")_20const payload = {_20"output_type": "chat",_20"input_type": "chat",_20"input_value": "hello world!",_20"session_id": "user_1"_20};_20_20const options = {_20method: 'POST',_20headers: {_20'Content-Type': 'application/json',_20'x-api-key': 'LANGFLOW_API_KEY'_20},_20body: JSON.stringify(payload)_20};_20_20fetch('http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID', options)_20.then(response => response.json())_20.then(response => console.log(response))_20.catch(err => console.error(err));_11curl --request POST \_11--url 'http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID?stream=false' \_11--header 'Content-Type: application/json' \_11--header "x-api-key: LANGFLOW_API_KEY" \_11--data '{_11"output_type": "chat",_11"input_type": "chat",_11"input_value": "hello world!"_11}'_11_11# 200 响应确认调用成功。 -
复制该代码片段,将其粘贴到脚本文件中,然后运行脚本以发送请求。 如果你使用的是 curl 代码片段,可以直接在终端中运行该命令。
如果请求成功,响应将包含有关流运行的许多详细信息,包括会话 ID、输入、输出、组件、持续时间等。 以下是运行 Simple Agent 模板流的响应示例:
结果 (Result)
_162{_162 "session_id": "29deb764-af3f-4d7d-94a0-47491ed241d6",_162 "outputs": [_162 {_162 "inputs": {_162 "input_value": "hello world!"_162 },_162 "outputs": [_162 {_162 "results": {_162 "message": {_162 "text_key": "text",_162 "data": {_162 "timestamp": "2025-06-16 19:58:23 UTC",_162 "sender": "Machine",_162 "sender_name": "AI",_162 "session_id": "29deb764-af3f-4d7d-94a0-47491ed241d6",_162 "text": "Hello world! 🌍 How can I assist you today?",_162 "files": [],_162 "error": false,_162 "edit": false,_162 "properties": {_162 "text_color": "",_162 "background_color": "",_162 "edited": false,_162 "source": {_162 "id": "Agent-ZOknz",_162 "display_name": "Agent",_162 "source": "gpt-4o-mini"_162 },_162 "icon": "bot",_162 "allow_markdown": false,_162 "positive_feedback": null,_162 "state": "complete",_162 "targets": []_162 },_162 "category": "message",_162 "content_blocks": [_162 {_162 "title": "Agent Steps",_162 "contents": [_162 {_162 "type": "text",_162 "duration": 2,_162 "header": {_162 "title": "Input",_162 "icon": "MessageSquare"_162 },_162 "text": "**Input**: hello world!"_162 },_162 {_162 "type": "text",_162 "duration": 226,_162 "header": {_162 "title": "Output",_162 "icon": "MessageSquare"_162 },_162 "text": "Hello world! 🌍 How can I assist you today?"_162 }_162 ],_162 "allow_markdown": true,_162 "media_url": null_162 }_162 ],_162 "id": "f3d85d9a-261c-4325-b004-95a1bf5de7ca",_162 "flow_id": "29deb764-af3f-4d7d-94a0-47491ed241d6",_162 "duration": null_162 },_162 "default_value": "",_162 "text": "Hello world! 🌍 How can I assist you today?",_162 "sender": "Machine",_162 "sender_name": "AI",_162 "files": [],_162 "session_id": "29deb764-af3f-4d7d-94a0-47491ed241d6",_162 "timestamp": "2025-06-16T19:58:23+00:00",_162 "flow_id": "29deb764-af3f-4d7d-94a0-47491ed241d6",_162 "error": false,_162 "edit": false,_162 "properties": {_162 "text_color": "",_162 "background_color": "",_162 "edited": false,_162 "source": {_162 "id": "Agent-ZOknz",_162 "display_name": "Agent",_162 "source": "gpt-4o-mini"_162 },_162 "icon": "bot",_162 "allow_markdown": false,_162 "positive_feedback": null,_162 "state": "complete",_162 "targets": []_162 },_162 "category": "message",_162 "content_blocks": [_162 {_162 "title": "Agent Steps",_162 "contents": [_162 {_162 "type": "text",_162 "duration": 2,_162 "header": {_162 "title": "Input",_162 "icon": "MessageSquare"_162 },_162 "text": "**Input**: hello world!"_162 },_162 {_162 "type": "text",_162 "duration": 226,_162 "header": {_162 "title": "Output",_162 "icon": "MessageSquare"_162 },_162 "text": "Hello world! 🌍 How can I assist you today?"_162 }_162 ],_162 "allow_markdown": true,_162 "media_url": null_162 }_162 ],_162 "duration": null_162 }_162 },_162 "artifacts": {_162 "message": "Hello world! 🌍 How can I assist you today?",_162 "sender": "Machine",_162 "sender_name": "AI",_162 "files": [],_162 "type": "object"_162 },_162 "outputs": {_162 "message": {_162 "message": "Hello world! 🌍 How can I assist you today?",_162 "type": "text"_162 }_162 },_162 "logs": {_162 "message": []_162 },_162 "messages": [_162 {_162 "message": "Hello world! 🌍 How can I assist you today?",_162 "sender": "Machine",_162 "sender_name": "AI",_162 "session_id": "29deb764-af3f-4d7d-94a0-47491ed241d6",_162 "stream_url": null,_162 "component_id": "ChatOutput-aF5lw",_162 "files": [],_162 "type": "text"_162 }_162 ],_162 "timedelta": null,_162 "duration": null,_162 "component_display_name": "Chat Output",_162 "component_id": "ChatOutput-aF5lw",_162 "used_frozen_result": false_162 }_162 ]_162 }_162 ]_162}
在生产应用程序中,你可能希望选择此响应的部分内容返回给用户、存储在日志中等等。接下来的步骤将演示如何从 Langflow API 响应中提取数据以在你的应用程序中使用。
从响应中提取数据
以下示例基于 API 面板的示例代码,在终端中创建一个问答聊天,并存储代理之前的回答。
-
将你的 Simple Agent 流的
/run代码片段整合到以下脚本中。 此脚本在你的终端中运行问答聊天,并存储代理之前的回答,以便你可以进行比较。- Python
- JavaScript
_59import requests_59import json_59_59url = "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID"_59_59def ask_agent(question):_59payload = {_59"output_type": "chat",_59"input_type": "chat",_59"input_value": question,_59}_59_59headers = {_59"Content-Type": "application/json",_59"x-api-key": "LANGFLOW_API_KEY"_59}_59_59try:_59response = requests.post(url, json=payload, headers=headers)_59response.raise_for_status()_59_59# 获取响应消息_59data = response.json()_59message = data["outputs"][0]["outputs"][0]["outputs"]["message"]["message"]_59return message_59_59except Exception as e:_59return f"Error: {str(e)}"_59_59def extract_message(data):_59try:_59return data["outputs"][0]["outputs"][0]["outputs"]["message"]["message"]_59except (KeyError, IndexError):_59return None_59_59# 存储来自 ask_agent 响应的上一个答案_59previous_answer = None_59_59# 终端聊天_59while True:_59# 获取用户输入_59print("\n向代理提问任何问题,例如 '15 * 7 等于多少?' 或 '法国的首都是哪里?'")_59print("输入 'quit' 退出或输入 'compare' 查看上一个答案")_59user_question = input("你的问题:")_59_59if user_question.lower() == 'quit':_59break_59elif user_question.lower() == 'compare':_59if previous_answer:_59print(f"\n上一个答案是:{previous_answer}")_59else:_59print("\n没有上一个答案可供比较!")_59continue_59_59# 获取并显示答案_59result = ask_agent(user_question)_59print(f"\n代理的回答:{result}")_59# 存储答案以供比较_59previous_answer = result_74const readline = require('readline');_74_74const rl = readline.createInterface({_74input: process.stdin,_74output: process.stdout_74});_74_74const url = 'http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID';_74_74// 存储来自 askAgent 响应的上一个答案_74let previousAnswer = null;_74_74// 代理流,将问题作为 input_value_74async function askAgent(question) {_74const payload = {_74"output_type": "chat",_74"input_type": "chat",_74"input_value": question_74};_74_74const options = {_74method: 'POST',_74headers: {_74'Content-Type': 'application/json',_74'x-api-key': 'LANGFLOW_API_KEY'_74},_74body: JSON.stringify(payload)_74};_74_74try {_74const response = await fetch(url, options);_74const data = await response.json();_74_74// 从嵌套响应中提取消息_74const message = data.outputs[0].outputs[0].outputs.message.message;_74return message;_74} catch (error) {_74return `Error: ${error.message}`;_74}_74}_74_74// 终端聊天_74async function startChat() {_74console.log("\n向代理提问任何问题,例如 '15 * 7 等于多少?' 或 '法国的首都是哪里?'");_74console.log("输入 'quit' 退出或输入 'compare' 查看上一个答案");_74_74const askQuestion = () => {_74rl.question('\n你的问题:', async (userQuestion) => {_74if (userQuestion.toLowerCase() === 'quit') {_74rl.close();_74return;_74}_74_74if (userQuestion.toLowerCase() === 'compare') {_74if (previousAnswer) {_74console.log(`\n上一个答案是:{previousAnswer}`);_74} else {_74console.log("\n没有上一个答案可供比较!");_74}_74askQuestion();_74return;_74}_74_74const result = await askAgent(userQuestion);_74console.log(`\n代理的回答:{result}`);_74previousAnswer = result;_74askQuestion();_74});_74};_74_74askQuestion();_74}_74_74startChat(); -
要查看代理之前的回答,请输入
compare。要关闭终端聊天,请输入quit。
使用 Tweaks 应用流运行的临时覆盖
你可以在请求中包含 tweaks 来临时修改流参数。 Tweaks 会添加到 API 请求中,并临时更改流中组件的参数。 Tweaks 仅针对单次运行覆盖流组件的设置。 它们不会修改底层的流配置,也不会在运行之间持久化。
Tweaks 被添加到 /run 端点的 payload 中。
为了协助格式化,你可以在复制代码片段之前在 Langflow 的 Input Schema(输入模式)面板中定义 tweaks。
- 要打开 Input Schema 面板,请从 API access 面板中点击 Input Schema。
- 在 Input Schema 面板中,选择你想要在下次请求中修改的参数。 在 Input Schema 面板中启用参数不会永久更改列出的参数。它只会将它们添加到示例代码片段中。
- 例如,要将 LLM 提供商从 OpenAI 更改为 Groq,并在请求中包含你的 Groq API 密钥,请选择 Model Providers、Model 和 Groq API Key 的值。
Langflow 会根据你的输入参数更新代码片段中的
tweaks对象,并包含默认值以指导你。 在你的脚本中使用更新后的代码片段,以使用你的覆盖设置运行流。
_12payload = {_12 "output_type": "chat",_12 "input_type": "chat",_12 "input_value": "hello world!",_12 "tweaks": {_12 "Agent-ZOknz": {_12 "agent_llm": "Groq",_12 "api_key": "GROQ_API_KEY",_12 "model_name": "llama-3.1-8b-instant"_12 }_12 }_12}