跳到主要内容

快速入门

通过加载模板流、运行它,然后在 /run API 端点提供服务,开始使用 Langflow。

前置条件​

  • 安装并启动 Langflow

  • 创建 OpenAI API 密钥

  • 创建 Langflow API 密钥

    创建 Langflow API 密钥

    Langflow API 密钥是你可以与 Langflow 配合使用的用户特定令牌。

    要创建 Langflow API 密钥,请执行以下操作:

    1. 在 Langflow 中,点击你的用户图标,然后选择 Settings(设置)。

    2. 点击 Langflow API Keys,然后点击 Add New。

    3. 为你的密钥命名,然后点击 Create API Key。

    4. 复制 API 密钥并安全存储。

    5. 要在请求中使用你的 Langflow API 密钥,请在终端中设置 LANGFLOW_API_KEY 环境变量,然后在请求中包含 x-api-key 标头或查询参数。 例如:


      _13
      # 设置变量
      _13
      export LANGFLOW_API_KEY="sk..."
      _13
      _13
      # 发送请求
      _13
      curl --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 模板流​

  1. 在 Langflow 中,点击 New Flow,然后选择 Simple Agent 模板。

Simple Agent 模板

Simple Agent 模板由一个连接到 Chat Input(聊天输入)和 Chat Output(聊天输出)组件的 Agent(代理)组件、一个 Calculator(计算器)组件以及一个 URL 组件组成。当你运行此流时,你通过 Chat Input 组件向代理提交查询,代理使用 Calculator 和 URL 工具生成响应,然后通过 Chat Output 组件返回响应。

许多组件都可以作为代理的工具,包括 模型上下文协议 (MCP) 服务器。代理根据给定查询的上下文决定调用哪些工具。

  1. 在 Agent 组件中,直接输入你的 OpenAI API 密钥或使用 全局变量。

    此示例使用 Agent 组件内置的 OpenAI 模型。 如果你想使用不同的提供商,请相应地编辑模型提供商、模型名称和凭据。 如果你首选的提供商或模型未列出,请将 Model Provider 设置为 Connect other models,然后连接任何语言模型组件。

  2. 要运行流,请点击 Playground。

  3. 要测试 Calculator 工具,请向代理询问一个简单的数学问题,例如 I want to add 4 and 4.(我想把 4 和 4 相加)。 为了帮助你测试和评估流,Playground 会显示代理在分析提示词、选择工具并使用工具生成响应时的推理过程。 在这种情况下,数学问题会导致代理选择 Calculator 工具并使用类似 evaluate_expression 的操作。

带有代理工具的 Playground

  1. 要测试 URL 工具,请向代理询问有关时事的信息。 对于此请求,代理会选择 URL 工具的 fetch_content 操作,然后返回当前新闻头条的摘要。

  2. 完成流测试后,点击 Close。

下一步

既然你已经运行了第一个流,请尝试以下下一步:

从外部应用程序运行你的流​

Langflow 是一个 IDE,但它也是一个运行时,你可以通过 Langflow API 使用 Python、JavaScript 或 HTTP 进行调用。

当你本地启动 Langflow 时,你可以向本地 Langflow 服务器发送请求。 对于生产应用程序,你需要部署一个稳定的 Langflow 实例来处理 API 调用。

例如,你可以使用 /run 端端来运行流并获取结果。

Langflow 提供了代码片段来帮助你开始使用 Langflow API。

  1. 编辑流时,点击 Share(分享),然后点击 API access(API 访问)。

    API 访问面板中的默认代码构建了一个包含 Langflow 服务器 url、headers(标头)和请求数据 payload(负载)的请求。 代码片段会自动包含流的 LANGFLOW_SERVER_ADDRESS 和 FLOW_ID 值,以及一个脚本,用于包含你的 LANGFLOW_API_KEY(如果你已在终端会话中将其设置为环境变量)。 如果你将代码用于不同的服务器或流,请替换这些值。 默认的 Langflow 服务器地址是 http://localhost:7860。


    _29
    import requests
    _29
    _29
    url = "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID" # 此流的完整 API 端点 URL
    _29
    _29
    # 请求负载配置
    _29
    payload = {
    _29
    "output_type": "chat",
    _29
    "input_type": "chat",
    _29
    "input_value": "hello world!"
    _29
    }
    _29
    _29
    # 请求标头
    _29
    headers = {
    _29
    "Content-Type": "application/json",
    _29
    "x-api-key": "$LANGFLOW_API_KEY"
    _29
    }
    _29
    _29
    try:
    _29
    # 发送 API 请求
    _29
    response = requests.request("POST", url, json=payload, headers=headers)
    _29
    response.raise_for_status() # 为错误的响应状态码抛出异常
    _29
    _29
    # 打印响应
    _29
    print(response.text)
    _29
    _29
    except requests.exceptions.RequestException as e:
    _29
    print(f"调用 API 时出错: {e}")
    _29
    except ValueError as e:
    _29
    print(f"解析响应时出错: {e}")

  2. 复制该代码片段,将其粘贴到脚本文件中,然后运行脚本以发送请求。 如果你使用的是 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 面板的示例代码,在终端中创建一个问答聊天,并存储代理之前的回答。

  1. 将你的 Simple Agent 流的 /run 代码片段整合到以下脚本中。 此脚本在你的终端中运行问答聊天,并存储代理之前的回答,以便你可以进行比较。


    _59
    import requests
    _59
    import json
    _59
    _59
    url = "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID"
    _59
    _59
    def ask_agent(question):
    _59
    payload = {
    _59
    "output_type": "chat",
    _59
    "input_type": "chat",
    _59
    "input_value": question,
    _59
    }
    _59
    _59
    headers = {
    _59
    "Content-Type": "application/json",
    _59
    "x-api-key": "LANGFLOW_API_KEY"
    _59
    }
    _59
    _59
    try:
    _59
    response = requests.post(url, json=payload, headers=headers)
    _59
    response.raise_for_status()
    _59
    _59
    # 获取响应消息
    _59
    data = response.json()
    _59
    message = data["outputs"][0]["outputs"][0]["outputs"]["message"]["message"]
    _59
    return message
    _59
    _59
    except Exception as e:
    _59
    return f"Error: {str(e)}"
    _59
    _59
    def extract_message(data):
    _59
    try:
    _59
    return data["outputs"][0]["outputs"][0]["outputs"]["message"]["message"]
    _59
    except (KeyError, IndexError):
    _59
    return None
    _59
    _59
    # 存储来自 ask_agent 响应的上一个答案
    _59
    previous_answer = None
    _59
    _59
    # 终端聊天
    _59
    while True:
    _59
    # 获取用户输入
    _59
    print("\n向代理提问任何问题,例如 '15 * 7 等于多少?' 或 '法国的首都是哪里?'")
    _59
    print("输入 'quit' 退出或输入 'compare' 查看上一个答案")
    _59
    user_question = input("你的问题:")
    _59
    _59
    if user_question.lower() == 'quit':
    _59
    break
    _59
    elif user_question.lower() == 'compare':
    _59
    if previous_answer:
    _59
    print(f"\n上一个答案是:{previous_answer}")
    _59
    else:
    _59
    print("\n没有上一个答案可供比较!")
    _59
    continue
    _59
    _59
    # 获取并显示答案
    _59
    result = ask_agent(user_question)
    _59
    print(f"\n代理的回答:{result}")
    _59
    # 存储答案以供比较
    _59
    previous_answer = result

  2. 要查看代理之前的回答,请输入 compare。要关闭终端聊天,请输入 quit。

使用 Tweaks 应用流运行的临时覆盖​

你可以在请求中包含 tweaks 来临时修改流参数。 Tweaks 会添加到 API 请求中,并临时更改流中组件的参数。 Tweaks 仅针对单次运行覆盖流组件的设置。 它们不会修改底层的流配置,也不会在运行之间持久化。

Tweaks 被添加到 /run 端点的 payload 中。 为了协助格式化,你可以在复制代码片段之前在 Langflow 的 Input Schema(输入模式)面板中定义 tweaks。

  1. 要打开 Input Schema 面板,请从 API access 面板中点击 Input Schema。
  2. 在 Input Schema 面板中,选择你想要在下次请求中修改的参数。 在 Input Schema 面板中启用参数不会永久更改列出的参数。它只会将它们添加到示例代码片段中。
  3. 例如,要将 LLM 提供商从 OpenAI 更改为 Groq,并在请求中包含你的 Groq API 密钥,请选择 Model Providers、Model 和 Groq API Key 的值。 Langflow 会根据你的输入参数更新代码片段中的 tweaks 对象,并包含默认值以指导你。 在你的脚本中使用更新后的代码片段,以使用你的覆盖设置运行流。

_12
payload = {
_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
}

下一步​

Search