跳到主要内容

快速开始

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

先决条件​

  • 安装并启动 Langflow

  • 创建 OpenAI API 密钥

  • 创建 Langflow API 密钥

    创建 Langflow API 密钥

    Langflow API 密钥是您可以与 Langflow 一起使用的用户特定 token。

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

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

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

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

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

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


      _13
      # Set variable
      _13
      export LANGFLOW_API_KEY="sk..."
      _13
      _13
      # Send request
      _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 模板 flow​

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

Simple agent 启动 flow

Simple Agent flow 由连接到 Chat I/O 组件、Calculator 组件和 URL 组件 的 Agent 组件组成。当您运行此 flow 时,您通过 Chat Input 组件向 Agent 提交查询,Agent 使用 Calculator 和 URL 工具生成响应,然后通过 Chat Output 组件返回响应。

许多组件都可以作为 Agent 的工具,包括 Model Context Protocol (MCP) 服务器。Agent 根据给定查询的上下文决定调用哪些工具。

  1. 在 Agent 组件的设置中,在 OpenAI API Key 字段中,直接输入您的 OpenAI API 密钥,或点击 Globe 来创建全局变量。

    此指南使用 OpenAI 模型作为演示目的。如果您想使用不同的提供商,请更改 Model Provider 和 Model Name 字段,然后为您选择的提供商提供凭据。

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

  3. 要测试 Calculator 工具,请向 Agent 问一个简单的数学问题,例如 I want to add 4 and 4. 为了帮助您测试和评估您的 flow,Playground 展示了 Agent 在分析提示、选择工具、然后使用工具生成响应时的推理过程。 在这种情况下,数学问题使 Agent 选择 Calculator 工具并使用像 evaluate_expression 这样的操作。

Playground 与 Agent 工具

  1. 要测试 URL 工具,请向 Agent 询问当前事件。 对于此请求,Agent 选择 URL 工具的 fetch_content 操作,然后返回当前新闻头条的摘要。

  2. 当您完成 flow 测试后,点击 Close。

下一步

现在您已经运行了第一个 flow,试试这些下一步:

从外部应用程序运行您的 flow​

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

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

例如,您可以使用 /run 端点运行 flow 并获得结果。

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

  1. 要打开 API 访问面板,在 Playground 中点击 Share,然后点击 API access。

    API 访问面板中的默认代码使用 Langflow 服务器 url、headers 和请求数据的 payload 构造请求。 代码片段自动包含 flow 的 LANGFLOW_SERVER_ADDRESS 和 FLOW_ID 值,以及如果您在终端会话中将其设置为环境变量,则包含您的 LANGFLOW_API_KEY 的脚本。 如果您为不同的服务器或 flow 使用代码,请替换这些值。 默认的 Langflow 服务器地址是 http://localhost:7860。


    _29
    import requests
    _29
    _29
    url = "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID" # The complete API endpoint URL for this flow
    _29
    _29
    # Request payload configuration
    _29
    payload = {
    _29
    "output_type": "chat",
    _29
    "input_type": "chat",
    _29
    "input_value": "hello world!"
    _29
    }
    _29
    _29
    # Request headers
    _29
    headers = {
    _29
    "Content-Type": "application/json",
    _29
    "x-api-key": "$LANGFLOW_API_KEY"
    _29
    }
    _29
    _29
    try:
    _29
    # Send API request
    _29
    response = requests.request("POST", url, json=payload, headers=headers)
    _29
    response.raise_for_status() # Raise exception for bad status codes
    _29
    _29
    # Print response
    _29
    print(response.text)
    _29
    _29
    except requests.exceptions.RequestException as e:
    _29
    print(f"Error making API request: {e}")
    _29
    except ValueError as e:
    _29
    print(f"Error parsing response: {e}")

  2. 复制片段,将其粘贴到脚本文件中,然后运行脚本发送请求。 如果您使用 curl 片段,您可以直接在终端中运行命令。

如果请求成功,响应包含关于 flow 运行的许多详细信息,包括会话 ID、输入、输出、组件、持续时间等。 以下是运行 Simple Agent 模板 flow 的响应示例:

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 面板的示例代码,在您的终端中创建一个存储 Agent 上一个答案的问答聊天。

  1. 将您的 Simple Agent flow 的 /run 片段合并到以下脚本中。 此脚本在您的终端中运行问答聊天,并存储 Agent 的上一个答案,以便您可以比较它们。


    _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
    # Get the response message
    _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
    # Store the previous answer from ask_agent response
    _59
    previous_answer = None
    _59
    _59
    # the terminal chat
    _59
    while True:
    _59
    # Get user input
    _59
    print("\nAsk the agent anything, such as 'What is 15 * 7?' or 'What is the capital of France?')")
    _59
    print("Type 'quit' to exit or 'compare' to see the previous answer")
    _59
    user_question = input("Your question: ")
    _59
    _59
    if user_question.lower() == 'quit':
    _59
    break
    _59
    elif user_question.lower() == 'compare':
    _59
    if previous_answer:
    _59
    print(f"\nPrevious answer was: {previous_answer}")
    _59
    else:
    _59
    print("\nNo previous answer to compare with!")
    _59
    continue
    _59
    _59
    # Get and display the answer
    _59
    result = ask_agent(user_question)
    _59
    print(f"\nAgent's answer: {result}")
    _59
    # Store the answer for comparison
    _59
    previous_answer = result

  2. 要查看 Agent 的上一个答案,请输入 compare。要关闭终端聊天,请输入 exit。

使用 tweaks 对 flow 运行应用临时覆盖​

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

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 对象,并包含默认值来指导您。 在您的脚本中使用更新的代码片段来使用您的覆盖运行 flow。

_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