快速上手
通过加载模板工作流 (Template Flow)、运行它,并在 /run API 端点提供服务,开始使用 Langflow。
前提条件
-
创建一个 OpenAI API 密钥
-
创建一个 Langflow API 密钥
创建 Langflow API 密钥
Langflow API 密钥是一个用户专用的令牌,您可以将其与 Langflow 一起使用。
要创建 Langflow API 密钥,请执行以下操作:
-
在 Langflow 中,点击您的用户图标,然后选择 Settings(设置)。
-
点击 Langflow API Keys(Langflow API 密钥),然后点击 Add New(新增).
-
为您的密钥命名,然后点击 Create API Key(创建 API 密钥)。
-
复制 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}'
-
如果您在尝试注册时遇到“An API key must be passed as query or header”(必须通过查询或请求头传递 API 密钥)错误,请参阅 故障排除。
运行 Simple Agent(简单智能体)模板工作流
- 在 Langflow 中,点击 New Flow,然后选择 Simple Agent 模板。

Simple Agent 模板包含一个连接到 Chat Input(聊天输入)和 Chat Output(聊天输出)组件、Calculator(计算器)组件以及 URL 组件的 Agent(智能体)组件。当您运行此工作流时,您通过 Chat Input 组件向智能体提交查询,智能体使用 Calculator 和 URL 工具生成响应,然后通过 Chat Output 组件返回响应。
许多组件都可以作为智能体的工具,包括 模型上下文协议 (MCP) 服务器。智能体根据给定查询的上下文决定调用哪些工具。
-
在 Agent 组件中,点击 Setup Provider(设置提供商) 以选择您的语言模型提供商。
要编辑 Langflow 的全局模型提供商配置,请执行以下操作:
-
要打开模型提供商 (Model Providers) 面板,请点击您的个人资料图标,选择设置 (Settings),然后点击 模型提供商 (Model Providers).
-
在模型提供商面板中,选择一个提供商。
-
在API 密钥 (API Key) 字段中,添加您的提供商 API 密钥。
该密钥必须具备调用您流中所用模型的权限,且您的账户必须拥有足够的额度来执行所需的操作。
每个提供商只能添加一个密钥。请确保该密钥拥有访问 Langflow 中所有您想使用的模型的权限。
-
启用您想在 Langflow 中使用的特定模型。可用模型取决于提供商和您 API 密钥的权限。生成文本的模型列在语言模型 (Language Models) 下。生成嵌入的模型列在嵌入模型 (Embedding Models) 下。
在 Langflow 的全局模型配置中启用模型后,您就可以在流中的任何模型驱动组件中使用该模型。
-
-
在 Agent 组件中,从 Language Model(语言模型) 下拉菜单中选择您配置好的模型。
访问更多模型和提供商
有两种方法可以访问更多模型和提供商:
- 编辑 Langflow 的全局 Models(模型) 配置。这些提供商和模型是 Langflow 核心功能的一部分。使用 Ollama 提供商连接到托管在本地或远程 Ollama 实例上的任何模型。
- 将任何 其他语言模型组件 连接到 Agent 组件的 Language Model(语言模型) 端口。
-
要运行工作流,请点击 演练场.
-
要测试 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 服务器地址是https://: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"Error making API request: {e}")_29except ValueError as e_29print(f"Error parsing response: {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 模板工作流的响应示例:
结果
_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("\nAsk the agent anything, such as 'What is 15 * 7?' or 'What is the capital of France?')")_59print("Type 'quit' to exit or 'compare' to see the previous answer")_59user_question = input("Your question: ")_59_59if user_question.lower() == 'quit'_59break_59elif user_question.lower() == 'compare'_59if previous_answer_59print(f"\nPrevious answer was: {previous_answer}")_59else_59print("\nNo previous answer to compare with!")_59continue_59_59# 获取并显示答案_59result = ask_agent(user_question)_59print(f"\nAgent's answer: {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("\nAsk the agent anything, such as 'What is 15 * 7?' or 'What is the capital of France?'");_74console.log("Type 'quit' to exit or 'compare' to see the previous answer");_74_74const askQuestion = () => {_74rl.question('\nYour question: ', async (userQuestion) => {_74if (userQuestion.toLowerCase() === 'quit') {_74rl.close();_74return;_74}_74_74if (userQuestion.toLowerCase() === 'compare') {_74if (previousAnswer) {_74console.log(`\nPrevious answer was: ${previousAnswer}`);_74} else {_74console.log("\nNo previous answer to compare with!");_74}_74askQuestion();_74return;_74}_74_74const result = await askAgent(userQuestion);_74console.log(`\nAgent's answer: {result}`);_74previousAnswer = result;_74askQuestion();_74});_74};_74_74askQuestion();_74}_74_74startChat(); -
要查看智能体的上一个回答,请输入
compare。要关闭终端聊天,请输入exit。
使用调整项 (Tweaks) 对工作流运行应用临时覆盖
您可以在请求中包含调整项,以临时修改工作流参数。调整项被添加到 API 请求中,并临时更改工作流内的组件参数。调整项仅覆盖单次运行的工作流组件设置。它们不会修改基础工作流配置,也不会在多次运行之间持久化。
调整项被添加到 /run 端点的 payload 中。为了辅助格式化,您可以在复制代码片段之前在 Langflow 的 Input Schema(输入模式) 面板中定义调整项。
- 要打开 Input Schema 面板,请从 API access 面板中点击 Input Schema。
- 在 Input Schema 面板中,选择您想在下一次请求中修改的参数。在 Input Schema 面板中启用参数不会永久更改所列的参数,它只会将它们添加到示例代码片段中。
- 例如,要将智能体的 LLM 模型从 OpenAI 更改为 Anthropic,并在请求中包含您的 Anthropic API 密钥,请在 Input Schema 面板中选择 Agent 组件并启用 Language Model 字段。
Langflow 会根据您的输入参数更新代码片段中的 tweaks 对象,并包含默认值以供参考。在您的脚本中使用更新后的代码片段,以应用您的覆盖项来运行工作流。
_12payload = {_12"output_type": "chat",_12"input_type": "chat",_12"input_value": "hello world!",_12"tweaks": {_12"Agent-ZOknz": {_12"agent_llm": "Anthropic",_12"api_key": "ANTHROPIC_API_KEY",_12"model_name": "claude-opus-4-5-20251101"_12 }_12 }_12}