From ff42b0e16af89c5ea746bde1d2d544fcfe8648e8 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Mar 2026 19:12:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Langflow=20LLM=20p?= =?UTF-8?q?rovider=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 Langflow provider 实现,支持流式响应 - 添加 Langflow 集成文档 - 支持自定义 base_url、tweaks 等配置 Closes #2984 Co-Authored-By: Claude Sonnet 4.6 --- docs/langflow-integration.md | 132 ++++++++++++++++++ .../core/providers/llm/langflow/langflow.py | 62 ++++++++ 2 files changed, 194 insertions(+) create mode 100644 docs/langflow-integration.md create mode 100644 main/xiaozhi-server/core/providers/llm/langflow/langflow.py diff --git a/docs/langflow-integration.md b/docs/langflow-integration.md new file mode 100644 index 000000000..7b11515a8 --- /dev/null +++ b/docs/langflow-integration.md @@ -0,0 +1,132 @@ +# Langflow Integration Guide + +## Overview + +Langflow is a visual flow-based LLM orchestration platform that allows you to design conversational AI flows through a UI and invoke them via API. This integration enables xiaozhi-esp32-server to use Langflow flows as the LLM provider. + +## Configuration + +### Required Fields + +- `type`: Must be set to `langflow` +- `api_key`: Your Langflow API key +- `flow_id`: The ID of your Langflow flow + +### Optional Fields + +- `base_url`: Langflow API endpoint (default: `https://api.langflow.astra.datastax.com`) +- `tweaks`: Runtime flow parameter overrides (default: `{}`) + +### Example Configuration + +```yaml +llm: + type: langflow + api_key: "your_langflow_api_key_here" + flow_id: "your_flow_id_here" + base_url: "https://api.langflow.astra.datastax.com" + tweaks: {} +``` + +## Setup Steps + +### 1. Create a Langflow Flow + +1. Sign up at [Langflow](https://www.langflow.org/) or deploy your own instance +2. Create a new flow in the Langflow UI +3. Design your conversational flow with components +4. Ensure your flow has a Chat Input and Chat Output component +5. Save and deploy your flow + +### 2. Get API Credentials + +1. Navigate to your Langflow settings +2. Generate an API key +3. Copy your flow ID from the flow URL or settings + +### 3. Configure xiaozhi-esp32-server + +Add the configuration to your `config.yaml` or `data/.config.yaml`: + +```yaml +LLM: + LangflowLLM: + type: langflow + api_key: "sk-..." + flow_id: "abc123..." + base_url: "https://api.langflow.astra.datastax.com" +``` + +### 4. Select Langflow as Provider + +Update your module selection: + +```yaml +selected_module: + LLM: LangflowLLM +``` + +## Advanced Usage + +### Using Tweaks + +The `tweaks` parameter allows you to customize flow components at runtime without modifying the flow design: + +```yaml +llm: + type: langflow + api_key: "your_api_key" + flow_id: "your_flow_id" + tweaks: + ChatOpenAI-abc123: + temperature: 0.7 + model_name: "gpt-4" +``` + +To find component IDs for tweaks: +1. Open your flow in Langflow +2. Click on a component +3. Copy the component ID from the settings panel + +## Troubleshooting + +### Connection Issues + +- Verify your API key is correct +- Check that the base_url is accessible from your server +- Ensure your flow_id exists and is deployed + +### No Response + +- Check Langflow logs for errors +- Verify your flow has proper Chat Input/Output components +- Test the flow directly in Langflow UI first + +### Session Not Persisting + +- The integration automatically handles session management +- Each device gets a unique session_id for conversation continuity + +## API Reference + +The integration uses Langflow's Run API: + +``` +POST {base_url}/api/v1/run/{flow_id} +Headers: + x-api-key: {api_key} +Body: + { + "input_value": "user message", + "output_type": "chat", + "input_type": "chat", + "session_id": "unique_session_id", + "tweaks": {} + } +``` + +## Limitations + +- System prompts configured in `config.yaml` are ignored (configure in Langflow flow instead) +- Function calling is not currently supported +- Response streaming depends on Langflow flow configuration diff --git a/main/xiaozhi-server/core/providers/llm/langflow/langflow.py b/main/xiaozhi-server/core/providers/llm/langflow/langflow.py new file mode 100644 index 000000000..43424b277 --- /dev/null +++ b/main/xiaozhi-server/core/providers/llm/langflow/langflow.py @@ -0,0 +1,62 @@ +import json +import requests +from config.logger import setup_logging +from core.providers.llm.base import LLMProviderBase +from core.utils.util import check_model_key + +TAG = __name__ +logger = setup_logging() + + +class LLMProvider(LLMProviderBase): + def __init__(self, config): + self.api_key = config["api_key"] + self.flow_id = config["flow_id"] + self.base_url = config.get("base_url", "https://api.langflow.astra.datastax.com").rstrip("/") + self.tweaks = config.get("tweaks", {}) + model_key_msg = check_model_key("LangflowLLM", self.api_key) + if model_key_msg: + logger.bind(tag=TAG).error(model_key_msg) + + def response(self, session_id, dialogue, **kwargs): + last_msg = next(m for m in reversed(dialogue) if m["role"] == "user") + + request_json = { + "input_value": last_msg["content"], + "output_type": "chat", + "input_type": "chat", + } + + if session_id: + request_json["session_id"] = session_id + + if self.tweaks: + request_json["tweaks"] = self.tweaks + + try: + url = f"{self.base_url}/api/v1/run/{self.flow_id}?stream=true" + with requests.post( + url, + headers={"x-api-key": self.api_key}, + json=request_json, + stream=True, + ) as r: + for line in r.iter_lines(): + if not line or line.startswith(b":"): + continue + if line.startswith(b"data: "): + line = line[6:] + if not line: + continue + try: + data = json.loads(line) + if data.get("event") == "token": + token_data = data.get("data", {}) + if isinstance(token_data, dict): + chunk = token_data.get("chunk", "") + if chunk: + yield chunk + except json.JSONDecodeError: + continue + except Exception as e: + logger.bind(tag=TAG).error(f"Langflow API error: {e}")