diff --git a/README.md b/README.md index 6d01ce1c6..41930a121 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ | OpenAI 接口调用 | 阿里百炼、火山引擎、DeepSeek、智谱、Gemini、科大讯飞、LM Studio | 智谱、Gemini | | ollama 接口调用 | Ollama | - | | dify 接口调用 | Dify | - | +| langflow 接口调用 | Langflow | - | | fastgpt 接口调用 | Fastgpt | - | | coze 接口调用 | Coze | - | | xinference 接口调用 | Xinference | - | 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/config.yaml b/main/xiaozhi-server/config.yaml index 11c87a5f6..4e949d137 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -679,6 +679,16 @@ LLM: # 使用workflows进行返回的时候输入参数为 query 返回参数的名字要设置为 answer # 文本生成的默认输入参数也是query mode: chat-messages + LangflowLLM: + # 定义LLM API类型 + type: langflow + # Langflow是可视化流程编排平台,支持通过API调用设计好的对话流程 + # 如果使用LangflowLLM,配置文件里prompt(提示词)是无效的,需要在Langflow控制台设计流程 + base_url: https://api.langflow.astra.datastax.com + api_key: 你的Langflow API key + flow_id: 你的flow_id + # tweaks参数用于运行时自定义流程参数(可选) + tweaks: {} GeminiLLM: type: gemini # 谷歌Gemini API,需要先在Google Cloud控制台创建API密钥并获取api_key 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}")