├── .env.example ├── .gitignore ├── README.md ├── README_EN.md ├── images ├── 1.png ├── 2.png └── 3.png ├── main.py └── requirements.txt /.env.example: -------------------------------------------------------------------------------- 1 | # Dify API Keys Configuration 2 | # Format: Comma-separated list of API keys 3 | DIFY_API_KEYS=app-xxxxxxxxxxxxxxxx,app-yyyyyyyyyyyyyyyy,app-zzzzzzzzzzzzzzzz 4 | 5 | # Dify API Base URL 6 | DIFY_API_BASE="https://api.dify.example.com/v1" 7 | 8 | # 会话记忆功能模式 9 | # 1: 构造history_message附加到消息中的模式(默认) 10 | # 2: 当前的零宽字符模式 11 | CONVERSATION_MEMORY_MODE=1 12 | 13 | # Server Configuration 14 | SERVER_HOST="127.0.0.1" 15 | SERVER_PORT=5000 16 | 17 | # OpenAI compatable API Keys 18 | VALID_API_KEYS="sk-abc123,sk-def456" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Python 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | *.so 6 | .Python 7 | build/ 8 | develop-eggs/ 9 | dist/ 10 | downloads/ 11 | eggs/ 12 | .eggs/ 13 | lib/ 14 | lib64/ 15 | parts/ 16 | sdist/ 17 | var/ 18 | wheels/ 19 | *.egg-info/ 20 | .installed.cfg 21 | *.egg 22 | 23 | # Virtual Environment 24 | venv/ 25 | env/ 26 | ENV/ 27 | .env 28 | 29 | # IDE 30 | .idea/ 31 | .vscode/ 32 | *.swp 33 | *.swo 34 | .DS_Store 35 | 36 | # Logs 37 | *.log 38 | logs/ 39 | 40 | # Local development 41 | .env 42 | .env.local 43 | .env.*.local 44 | 45 | # Testing 46 | .coverage 47 | htmlcov/ 48 | .pytest_cache/ 49 | .tox/ 50 | .nox/ 51 | coverage.xml 52 | *.cover 53 | .hypothesis/ 54 | 55 | # Distribution 56 | *.pyc 57 | *.pyo 58 | *.pyd 59 | .Python 60 | *.so 61 | 62 | # Project specific 63 | *.pid 64 | *.sock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenDify 2 | 3 | OpenDify 是一个将 Dify API 转换为 OpenAI API 格式的代理服务器。它允许使用 OpenAI API 客户端直接与 Dify 服务进行交互。 4 | 5 | > 🌟 本项目完全由 Cursor + Claude-3.5 自动生成,未手动编写任何代码(包括此Readme),向 AI 辅助编程的未来致敬! 6 | 7 | [English Version](README_EN.md) 8 | 9 | ## 功能特点 10 | 11 | - 完整支持 OpenAI API 格式转换为 Dify API 12 | - 支持流式输出(Streaming) 13 | - 智能动态延迟控制,提供流畅的输出体验 14 | - 支持多种会话记忆模式,包括零宽字符模式和history_message模式 15 | - 支持 OpenAI Function Call 和 MCP Server 功能 16 | - 支持多个模型配置 17 | - 支持Dify Agent应用,处理高级工具调用(如生成图片等) 18 | - 兼容标准的 OpenAI API 客户端 19 | - 自动获取 Dify 应用信息 20 | 21 | ## 效果展示 22 | 23 | ### Function Call 和 MCP Server 支持 24 | 25 | 新增对 OpenAI Function Call 和 MCP Server 的支持,即使 Dify 不支持直接设置系统提示词: 26 | 27 | - 自动检测请求中的 `system` 角色消息 28 | - 智能将系统提示词插入到用户查询中 29 | - 防止重复插入系统提示词 30 | - 完美兼容 OpenAI 的 Function Call 格式 31 | 32 | ![Function Call 支持示例](images/3.png) 33 | 34 | *上图展示了 OpenDify 对 Function Call 的支持。即使 Dify 应用不支持直接设置系统提示词,通过 OpenDify 的转换,也能正确处理 MCP Server 及 Function Call 的需求。* 35 | 36 | ### Dify Agent应用支持 37 | 38 | ![Dify Agent应用截图](images/1.png) 39 | 40 | *截图展示了OpenDify代理服务支持的Dify Agent应用界面,可以看到Agent成功地处理了用户的Python多线程用法请求,并返回了相关代码示例。* 41 | 42 | ### 会话记忆功能 43 | 44 | ![Dify会话记忆功能截图](images/2.png) 45 | 46 | *上图展示了OpenDify的会话记忆功能。当用户提问"今天是什么天气?"时,AI能够记住之前对话中提到"今天是晴天"的上下文信息,并给出相应回复。* 47 | 48 | 49 | ### 会话记忆功能 50 | 51 | 该代理支持自动记忆会话上下文,无需客户端进行额外处理。提供了两种会话记忆模式: 52 | 53 | 1. **history_message模式**:将历史消息直接附加到当前消息中,支持客户端编辑历史消息(默认) 54 | 2. **零宽字符模式**:在每个新会话的第一条回复中,会自动嵌入不可见的会话ID,后续消息自动继承上下文 55 | 56 | 可以通过环境变量控制此功能: 57 | 58 | ```shell 59 | # 在 .env 文件中设置会话记忆模式 60 | # 1: 构造history_message附加到消息中的模式(默认) 61 | # 2: 零宽字符模式 62 | CONVERSATION_MEMORY_MODE=1 63 | ``` 64 | 65 | 默认情况下使用history_message模式,这种模式更灵活,支持客户端编辑历史消息,并能更好地处理系统提示词。 66 | 67 | > 注意:history_message模式会将所有历史消息追加到当前消息中,可能会消耗更多的token。 68 | 69 | ### 流式输出优化 70 | 71 | - 智能缓冲区管理 72 | - 动态延迟计算 73 | - 平滑的输出体验 74 | 75 | ### 配置灵活性 76 | 77 | - 自动获取应用信息 78 | - 简化的配置方式 79 | - 动态模型名称映射 80 | 81 | ## 支持的模型 82 | 83 | 支持任意 Dify 应用,系统会自动从 Dify API 获取应用名称和信息。只需在配置文件中添加应用的 API Key 即可。 84 | 85 | ## API 使用 86 | 87 | ### List Models 88 | 89 | 获取所有可用模型列表: 90 | 91 | ```python 92 | import openai 93 | 94 | openai.api_base = "http://127.0.0.1:5000/v1" 95 | openai.api_key = "any" # 可以使用任意值 96 | 97 | # 获取可用模型列表 98 | models = openai.Model.list() 99 | print(models) 100 | 101 | # 输出示例: 102 | { 103 | "object": "list", 104 | "data": [ 105 | { 106 | "id": "My Translation App", # Dify 应用名称 107 | "object": "model", 108 | "created": 1704603847, 109 | "owned_by": "dify" 110 | }, 111 | { 112 | "id": "Code Assistant", # 另一个 Dify 应用名称 113 | "object": "model", 114 | "created": 1704603847, 115 | "owned_by": "dify" 116 | } 117 | ] 118 | } 119 | ``` 120 | 121 | 系统会自动从 Dify API 获取应用名称,并用作模型 ID。 122 | 123 | ### Chat Completions 124 | 125 | ```python 126 | import openai 127 | 128 | openai.api_base = "http://127.0.0.1:5000/v1" 129 | openai.api_key = "any" # 可以使用任意值 130 | 131 | response = openai.ChatCompletion.create( 132 | model="My Translation App", # 使用 Dify 应用的名称 133 | messages=[ 134 | {"role": "user", "content": "你好"} 135 | ], 136 | stream=True 137 | ) 138 | 139 | for chunk in response: 140 | print(chunk.choices[0].delta.content or "", end="") 141 | ``` 142 | 143 | ## 快速开始 144 | 145 | ### 环境要求 146 | 147 | - Python 3.9+ 148 | - pip 149 | 150 | ### 安装依赖 151 | 152 | ```bash 153 | pip install -r requirements.txt 154 | ``` 155 | 156 | ### 配置 157 | 158 | 1. 复制 `.env.example` 文件并重命名为 `.env`: 159 | ```bash 160 | cp .env.example .env 161 | ``` 162 | 163 | 2. 在 Dify 平台配置应用: 164 | - 登录 Dify 平台,进入工作室 165 | - 点击"创建应用",配置好需要的模型(如 Claude、Gemini 等) 166 | - 配置应用的提示语和其他参数 167 | - 发布应用 168 | - 进入"访问 API"页面,生成 API 密钥 169 | 170 | > **重要说明**:Dify 不支持在请求时动态传入提示词、切换模型及其他参数。所有这些配置都需要在创建应用时设置好。Dify 会根据 API 密钥来确定使用哪个应用及其对应的配置。系统会自动从 Dify API 获取应用的名称和描述信息。 171 | 172 | 3. 在 `.env` 文件中配置你的 Dify API Keys: 173 | ```env 174 | # Dify API Keys Configuration 175 | # Format: Comma-separated list of API keys 176 | DIFY_API_KEYS=app-xxxxxxxx,app-yyyyyyyy,app-zzzzzzzz 177 | 178 | # Dify API Base URL 179 | DIFY_API_BASE="https://your-dify-api-base-url/v1" 180 | 181 | # Server Configuration 182 | SERVER_HOST="127.0.0.1" 183 | SERVER_PORT=5000 184 | ``` 185 | 186 | 配置说明: 187 | - `DIFY_API_KEYS`:以逗号分隔的 API Keys 列表,每个 Key 对应一个 Dify 应用 188 | - 系统会自动从 Dify API 获取每个应用的名称和信息 189 | - 无需手动配置模型名称和映射关系 190 | 191 | ### 运行服务 192 | 193 | ```bash 194 | python openai_to_dify.py 195 | ``` 196 | 197 | 服务将在 `http://127.0.0.1:5000` 启动 198 | 199 | ## 贡献指南 200 | 201 | 欢迎提交 Issue 和 Pull Request 来帮助改进项目。 202 | 203 | ## 许可证 204 | 205 | [MIT License](LICENSE) -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # OpenDify 2 | 3 | OpenDify is a proxy server that transforms the Dify API into OpenAI API format. It allows direct interaction with Dify services using any OpenAI API client. 4 | 5 | > 🌟 This project was fully generated by Cursor + Claude-3.5, without any manual coding (including this README), salute to the future of AI-assisted programming! 6 | 7 | [中文版本](README.md) 8 | 9 | ## Features 10 | 11 | - Full support for converting OpenAI API formats to Dify API 12 | - Streaming output support 13 | - Intelligent dynamic delay control for smooth output experience 14 | - Multiple conversation memory modes, including zero-width character mode and history_message mode 15 | - Support for OpenAI Function Call and MCP Server functionality 16 | - Support for multiple model configurations 17 | - Support for Dify Agent applications with advanced tool calls (like image generation) 18 | - Compatible with standard OpenAI API clients 19 | - Automatic fetching of Dify application information 20 | 21 | ## Screenshots 22 | 23 | ### Function Call and MCP Server Support 24 | 25 | ![Function Call Support Example](images/3.png) 26 | 27 | *The above image demonstrates OpenDify's support for Function Call. Even though Dify applications don't support setting system prompts directly, through OpenDify's conversion, it can correctly handle MCP Server and Function Call requirements.* 28 | 29 | ### Dify Agent Application Support 30 | 31 | ![Dify Agent Application Screenshot](images/1.png) 32 | 33 | *The screenshot shows the Dify Agent application interface supported by the OpenDify proxy service. It demonstrates how the Agent successfully processes a user's request about Python multithreading usage and returns relevant code examples.* 34 | 35 | ### Conversation Memory Feature 36 | 37 | ![Dify Conversation Memory Feature](images/2.png) 38 | 39 | *The above image demonstrates the conversation memory feature of OpenDify. When the user asks "What's the weather today?", the AI remembers the context from previous conversations that "today is sunny" and provides an appropriate response.* 40 | 41 | ## Detailed Features 42 | 43 | ### Function Call and MCP Server Support 44 | 45 | Added support for OpenAI Function Call and MCP Server, even though Dify doesn't support setting system prompts directly: 46 | 47 | - Automatically detects `system` role messages in requests 48 | - Intelligently inserts system prompts into user queries 49 | - Prevents duplicate insertion of system prompts 50 | - Perfectly compatible with OpenAI Function Call format 51 | 52 | ### Conversation Memory 53 | 54 | The proxy supports automatic remembering of conversation context without requiring additional processing by the client. It provides three conversation memory modes: 55 | 56 | 1. **No conversation memory**: Each conversation is independent, with no context association 57 | 2. **history_message mode**: Directly appends historical messages to the current message, supporting client-side editing of historical messages 58 | 3. **Zero-width character mode**: Automatically embeds an invisible session ID in the first reply of each new conversation, and subsequent messages automatically inherit the context 59 | 60 | This feature can be controlled via environment variable: 61 | 62 | ```shell 63 | # Set conversation memory mode in the .env file 64 | # 0: No conversation memory 65 | # 1: Construct history_message attached to the message 66 | # 2: Zero-width character mode (default) 67 | CONVERSATION_MEMORY_MODE=2 68 | ``` 69 | 70 | Zero-width character mode is used by default. For scenarios that need to support client-side editing of historical messages, the history_message mode is recommended. 71 | 72 | > Note: history_message mode will append all historical messages to the current message, which may consume more tokens. 73 | 74 | ### Streaming Output Optimization 75 | 76 | - Intelligent buffer management 77 | - Dynamic delay calculation 78 | - Smooth output experience 79 | 80 | ### Configuration Flexibility 81 | 82 | - Automatic application information retrieval 83 | - Simplified configuration method 84 | - Dynamic model name mapping 85 | 86 | ## Supported Models 87 | 88 | Supports any Dify application. The system automatically retrieves application names and information from the Dify API. Simply add the API Key for the application in the configuration file. 89 | 90 | ## API Usage 91 | 92 | ### List Models 93 | 94 | Get a list of all available models: 95 | 96 | ```python 97 | import openai 98 | 99 | openai.api_base = "http://127.0.0.1:5000/v1" 100 | openai.api_key = "any" # Can use any value 101 | 102 | # Get available models 103 | models = openai.Model.list() 104 | print(models) 105 | 106 | # Example output: 107 | { 108 | "object": "list", 109 | "data": [ 110 | { 111 | "id": "My Translation App", # Dify application name 112 | "object": "model", 113 | "created": 1704603847, 114 | "owned_by": "dify" 115 | }, 116 | { 117 | "id": "Code Assistant", # Another Dify application name 118 | "object": "model", 119 | "created": 1704603847, 120 | "owned_by": "dify" 121 | } 122 | ] 123 | } 124 | ``` 125 | 126 | The system automatically retrieves application names from the Dify API and uses them as model IDs. 127 | 128 | ### Chat Completions 129 | 130 | ```python 131 | import openai 132 | 133 | openai.api_base = "http://127.0.0.1:5000/v1" 134 | openai.api_key = "any" # Can use any value 135 | 136 | response = openai.ChatCompletion.create( 137 | model="My Translation App", # Use the Dify application name 138 | messages=[ 139 | {"role": "user", "content": "Hello"} 140 | ], 141 | stream=True 142 | ) 143 | 144 | for chunk in response: 145 | print(chunk.choices[0].delta.content or "", end="") 146 | ``` 147 | 148 | ## Quick Start 149 | 150 | ### Requirements 151 | 152 | - Python 3.9+ 153 | - pip 154 | 155 | ### Installing Dependencies 156 | 157 | ```bash 158 | pip install -r requirements.txt 159 | ``` 160 | 161 | ### Configuration 162 | 163 | 1. Copy the `.env.example` file and rename it to `.env`: 164 | ```bash 165 | cp .env.example .env 166 | ``` 167 | 168 | 2. Configure your application on the Dify platform: 169 | - Log in to the Dify platform and enter the workspace 170 | - Click "Create Application" and configure the required models (such as Claude, Gemini, etc.) 171 | - Configure the application prompts and other parameters 172 | - Publish the application 173 | - Go to the "Access API" page and generate an API key 174 | 175 | > **Important Note**: Dify does not support dynamically passing prompts, switching models, or other parameters in requests. All these configurations need to be set when creating the application. Dify determines which application and its corresponding configuration to use based on the API key. The system will automatically retrieve the application's name and description information from the Dify API. 176 | 177 | 3. Configure your Dify API Keys in the `.env` file: 178 | ```env 179 | # Dify API Keys Configuration 180 | # Format: Comma-separated list of API keys 181 | DIFY_API_KEYS=app-xxxxxxxx,app-yyyyyyyy,app-zzzzzzzz 182 | 183 | # Dify API Base URL 184 | DIFY_API_BASE="https://your-dify-api-base-url/v1" 185 | 186 | # Server Configuration 187 | SERVER_HOST="127.0.0.1" 188 | SERVER_PORT=5000 189 | ``` 190 | 191 | Configuration notes: 192 | - `DIFY_API_KEYS`: A comma-separated list of API Keys, each corresponding to a Dify application 193 | - The system automatically retrieves the name and information of each application from the Dify API 194 | - No need to manually configure model names and mapping relationships 195 | 196 | ### Running the Service 197 | 198 | ```bash 199 | python openai_to_dify.py 200 | ``` 201 | 202 | The service will start at `http://127.0.0.1:5000` 203 | 204 | ## Contribution Guidelines 205 | 206 | Issues and Pull Requests are welcome to help improve the project. 207 | 208 | ## License 209 | 210 | [MIT License](LICENSE) -------------------------------------------------------------------------------- /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzskyline/OpenDify/5350860f787d7ef68e8a89128abe100544037920/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzskyline/OpenDify/5350860f787d7ef68e8a89128abe100544037920/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lzskyline/OpenDify/5350860f787d7ef68e8a89128abe100544037920/images/3.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import json 2 | import logging 3 | import asyncio 4 | from flask import Flask, request, Response, stream_with_context, jsonify 5 | import httpx 6 | import time 7 | from dotenv import load_dotenv 8 | import os 9 | import ast 10 | 11 | # 配置日志 12 | logging.basicConfig( 13 | level=logging.INFO, 14 | format='%(asctime)s - %(levelname)s - %(message)s' 15 | ) 16 | logger = logging.getLogger(__name__) 17 | 18 | # 设置httpx的日志级别为WARNING,减少不必要的输出 19 | logging.getLogger("httpx").setLevel(logging.WARNING) 20 | 21 | # 加载环境变量 22 | load_dotenv() 23 | 24 | # 从环境变量读取有效的API密钥(逗号分隔) 25 | VALID_API_KEYS = [key.strip() for key in os.getenv("VALID_API_KEYS", "").split(",") if key] 26 | 27 | # 获取会话记忆功能模式配置 28 | # 1: 构造history_message附加到消息中的模式(默认) 29 | # 2: 零宽字符模式 30 | CONVERSATION_MEMORY_MODE = int(os.getenv('CONVERSATION_MEMORY_MODE', '1')) 31 | 32 | class DifyModelManager: 33 | def __init__(self): 34 | self.api_keys = [] 35 | self.name_to_api_key = {} # 应用名称到API Key的映射 36 | self.api_key_to_name = {} # API Key到应用名称的映射 37 | self.load_api_keys() 38 | 39 | def load_api_keys(self): 40 | """从环境变量加载API Keys""" 41 | api_keys_str = os.getenv('DIFY_API_KEYS', '') 42 | if api_keys_str: 43 | self.api_keys = [key.strip() for key in api_keys_str.split(',') if key.strip()] 44 | logger.info(f"Loaded {len(self.api_keys)} API keys") 45 | 46 | async def fetch_app_info(self, api_key): 47 | """获取Dify应用信息""" 48 | try: 49 | async with httpx.AsyncClient() as client: 50 | headers = { 51 | "Authorization": f"Bearer {api_key}", 52 | "Content-Type": "application/json" 53 | } 54 | response = await client.get( 55 | f"{DIFY_API_BASE}/info", 56 | headers=headers, 57 | params={"user": "default_user"} 58 | ) 59 | 60 | if response.status_code == 200: 61 | app_info = response.json() 62 | return app_info.get("name", "Unknown App") 63 | else: 64 | logger.error(f"Failed to fetch app info for API key: {api_key[:8]}...") 65 | return None 66 | except Exception as e: 67 | logger.error(f"Error fetching app info: {str(e)}") 68 | return None 69 | 70 | async def refresh_model_info(self): 71 | """刷新所有应用信息""" 72 | self.name_to_api_key.clear() 73 | self.api_key_to_name.clear() 74 | 75 | for api_key in self.api_keys: 76 | app_name = await self.fetch_app_info(api_key) 77 | if app_name: 78 | self.name_to_api_key[app_name] = api_key 79 | self.api_key_to_name[api_key] = app_name 80 | logger.info(f"Mapped app '{app_name}' to API key: {api_key[:8]}...") 81 | 82 | def get_api_key(self, model_name): 83 | """根据模型名称获取API Key""" 84 | return self.name_to_api_key.get(model_name) 85 | 86 | def get_available_models(self): 87 | """获取可用模型列表""" 88 | return [ 89 | { 90 | "id": name, 91 | "object": "model", 92 | "created": int(time.time()), 93 | "owned_by": "dify" 94 | } 95 | for name in self.name_to_api_key.keys() 96 | ] 97 | 98 | # 创建模型管理器实例 99 | model_manager = DifyModelManager() 100 | 101 | # 从环境变量获取API基础URL 102 | DIFY_API_BASE = os.getenv("DIFY_API_BASE", "") 103 | 104 | app = Flask(__name__) 105 | 106 | def get_api_key(model_name): 107 | """根据模型名称获取对应的API密钥""" 108 | api_key = model_manager.get_api_key(model_name) 109 | if not api_key: 110 | logger.warning(f"No API key found for model: {model_name}") 111 | return api_key 112 | 113 | def transform_openai_to_dify(openai_request, endpoint): 114 | """将OpenAI格式的请求转换为Dify格式""" 115 | 116 | if endpoint == "/chat/completions": 117 | messages = openai_request.get("messages", []) 118 | stream = openai_request.get("stream", False) 119 | 120 | # 尝试从历史消息中提取conversation_id 121 | conversation_id = None 122 | 123 | # 提取system消息内容 124 | system_content = "" 125 | system_messages = [msg for msg in messages if msg.get("role") == "system"] 126 | if system_messages: 127 | system_content = system_messages[0].get("content", "") 128 | # 记录找到的system消息 129 | logger.info(f"Found system message: {system_content[:100]}{'...' if len(system_content) > 100 else ''}") 130 | 131 | if CONVERSATION_MEMORY_MODE == 2: # 零宽字符模式 132 | if len(messages) > 1: 133 | # 遍历历史消息,找到最近的assistant消息 134 | for msg in reversed(messages[:-1]): # 除了最后一条消息 135 | if msg.get("role") == "assistant": 136 | content = msg.get("content", "") 137 | # 尝试解码conversation_id 138 | conversation_id = decode_conversation_id(content) 139 | if conversation_id: 140 | break 141 | 142 | # 获取最后一条用户消息 143 | user_query = messages[-1]["content"] if messages and messages[-1].get("role") != "system" else "" 144 | 145 | # 如果有system消息且是首次对话(没有conversation_id),则将system内容添加到用户查询前 146 | if system_content and not conversation_id: 147 | user_query = f"系统指令: {system_content}\n\n用户问题: {user_query}" 148 | logger.info(f"[零宽字符模式] 首次对话,添加system内容到查询前") 149 | 150 | dify_request = { 151 | "inputs": {}, 152 | "query": user_query, 153 | "response_mode": "streaming" if stream else "blocking", 154 | "conversation_id": conversation_id, 155 | "user": openai_request.get("user", "default_user") 156 | } 157 | else: # history_message模式(默认) 158 | # 获取最后一条用户消息 159 | user_query = messages[-1]["content"] if messages and messages[-1].get("role") != "system" else "" 160 | 161 | # 构造历史消息 162 | if len(messages) > 1: 163 | history_messages = [] 164 | has_system_in_history = False 165 | 166 | # 检查历史消息中是否已经包含system消息 167 | for msg in messages[:-1]: # 除了最后一条消息 168 | role = msg.get("role", "") 169 | content = msg.get("content", "") 170 | if role and content: 171 | if role == "system": 172 | has_system_in_history = True 173 | history_messages.append(f"{role}: {content}") 174 | 175 | # 如果历史中没有system消息但现在有system消息,则添加到历史的最前面 176 | if system_content and not has_system_in_history: 177 | history_messages.insert(0, f"system: {system_content}") 178 | logger.info(f"[history_message模式] 添加system内容到历史消息前") 179 | 180 | # 将历史消息添加到查询中 181 | if history_messages: 182 | history_context = "\n\n".join(history_messages) 183 | user_query = f"\n{history_context}\n\n\n用户当前问题: {user_query}" 184 | elif system_content: # 没有历史消息但有system消息 185 | user_query = f"系统指令: {system_content}\n\n用户问题: {user_query}" 186 | logger.info(f"[history_message模式] 首次对话,添加system内容到查询前") 187 | 188 | dify_request = { 189 | "inputs": {}, 190 | "query": user_query, 191 | "response_mode": "streaming" if stream else "blocking", 192 | "user": openai_request.get("user", "default_user") 193 | } 194 | 195 | return dify_request 196 | 197 | return None 198 | 199 | def transform_dify_to_openai(dify_response, model="claude-3-5-sonnet-v2", stream=False): 200 | """将Dify格式的响应转换为OpenAI格式""" 201 | 202 | if not stream: 203 | # 首先获取回答内容,支持不同的响应模式 204 | answer = "" 205 | mode = dify_response.get("mode", "") 206 | 207 | # 普通聊天模式 208 | if "answer" in dify_response: 209 | answer = dify_response.get("answer", "") 210 | 211 | # 如果是Agent模式,需要从agent_thoughts中提取回答 212 | elif "agent_thoughts" in dify_response: 213 | # Agent模式下通常最后一个thought包含最终答案 214 | agent_thoughts = dify_response.get("agent_thoughts", []) 215 | if agent_thoughts: 216 | for thought in agent_thoughts: 217 | if thought.get("thought"): 218 | answer = thought.get("thought", "") 219 | 220 | # 只在零宽字符会话记忆模式时处理conversation_id 221 | if CONVERSATION_MEMORY_MODE == 2: 222 | conversation_id = dify_response.get("conversation_id", "") 223 | history = dify_response.get("conversation_history", []) 224 | 225 | # 检查历史消息中是否已经有会话ID 226 | has_conversation_id = False 227 | if history: 228 | for msg in history: 229 | if msg.get("role") == "assistant": 230 | content = msg.get("content", "") 231 | if decode_conversation_id(content) is not None: 232 | has_conversation_id = True 233 | break 234 | 235 | # 只在新会话且历史消息中没有会话ID时插入 236 | if conversation_id and not has_conversation_id: 237 | logger.info(f"[Debug] Inserting conversation_id: {conversation_id}, history_length: {len(history)}") 238 | encoded = encode_conversation_id(conversation_id) 239 | answer = answer + encoded 240 | logger.info(f"[Debug] Response content after insertion: {repr(answer)}") 241 | 242 | return { 243 | "id": dify_response.get("message_id", ""), 244 | "object": "chat.completion", 245 | "created": dify_response.get("created", int(time.time())), 246 | "model": model, 247 | "choices": [{ 248 | "index": 0, 249 | "message": { 250 | "role": "assistant", 251 | "content": answer 252 | }, 253 | "finish_reason": "stop" 254 | }] 255 | } 256 | else: 257 | # 流式响应的转换在stream_response函数中处理 258 | return dify_response 259 | 260 | def create_openai_stream_response(content, message_id, model="claude-3-5-sonnet-v2"): 261 | """创建OpenAI格式的流式响应""" 262 | return { 263 | "id": message_id, 264 | "object": "chat.completion.chunk", 265 | "created": int(time.time()), 266 | "model": model, 267 | "choices": [{ 268 | "index": 0, 269 | "delta": { 270 | "content": content 271 | }, 272 | "finish_reason": None 273 | }] 274 | } 275 | 276 | def encode_conversation_id(conversation_id): 277 | """将conversation_id编码为不可见的字符序列""" 278 | if not conversation_id: 279 | return "" 280 | 281 | # 使用Base64编码减少长度 282 | import base64 283 | encoded = base64.b64encode(conversation_id.encode()).decode() 284 | 285 | # 使用8种不同的零宽字符表示3位数字 286 | # 这样可以将编码长度进一步减少 287 | char_map = { 288 | '0': '\u200b', # 零宽空格 289 | '1': '\u200c', # 零宽非连接符 290 | '2': '\u200d', # 零宽连接符 291 | '3': '\ufeff', # 零宽非断空格 292 | '4': '\u2060', # 词组连接符 293 | '5': '\u180e', # 蒙古语元音分隔符 294 | '6': '\u2061', # 函数应用 295 | '7': '\u2062', # 不可见乘号 296 | } 297 | 298 | # 将Base64字符串转换为八进制数字 299 | result = [] 300 | for c in encoded: 301 | # 将每个字符转换为8进制数字(0-7) 302 | if c.isalpha(): 303 | if c.isupper(): 304 | val = ord(c) - ord('A') 305 | else: 306 | val = ord(c) - ord('a') + 26 307 | elif c.isdigit(): 308 | val = int(c) + 52 309 | elif c == '+': 310 | val = 62 311 | elif c == '/': 312 | val = 63 313 | else: # '=' 314 | val = 0 315 | 316 | # 每个Base64字符可以产生2个3位数字 317 | first = (val >> 3) & 0x7 318 | second = val & 0x7 319 | result.append(char_map[str(first)]) 320 | if c != '=': # 不编码填充字符的后半部分 321 | result.append(char_map[str(second)]) 322 | 323 | return ''.join(result) 324 | 325 | def decode_conversation_id(content): 326 | """从消息内容中解码conversation_id""" 327 | try: 328 | # 零宽字符到3位数字的映射 329 | char_to_val = { 330 | '\u200b': '0', # 零宽空格 331 | '\u200c': '1', # 零宽非连接符 332 | '\u200d': '2', # 零宽连接符 333 | '\ufeff': '3', # 零宽非断空格 334 | '\u2060': '4', # 词组连接符 335 | '\u180e': '5', # 蒙古语元音分隔符 336 | '\u2061': '6', # 函数应用 337 | '\u2062': '7', # 不可见乘号 338 | } 339 | 340 | # 提取最后一段零宽字符序列 341 | space_chars = [] 342 | for c in reversed(content): 343 | if c not in char_to_val: 344 | break 345 | space_chars.append(c) 346 | 347 | if not space_chars: 348 | return None 349 | 350 | # 将零宽字符转换回Base64字符串 351 | space_chars.reverse() 352 | base64_chars = [] 353 | for i in range(0, len(space_chars), 2): 354 | first = int(char_to_val[space_chars[i]], 8) 355 | if i + 1 < len(space_chars): 356 | second = int(char_to_val[space_chars[i + 1]], 8) 357 | val = (first << 3) | second 358 | else: 359 | val = first << 3 360 | 361 | # 转换回Base64字符 362 | if val < 26: 363 | base64_chars.append(chr(val + ord('A'))) 364 | elif val < 52: 365 | base64_chars.append(chr(val - 26 + ord('a'))) 366 | elif val < 62: 367 | base64_chars.append(str(val - 52)) 368 | elif val == 62: 369 | base64_chars.append('+') 370 | else: 371 | base64_chars.append('/') 372 | 373 | # 添加Base64填充 374 | padding = len(base64_chars) % 4 375 | if padding: 376 | base64_chars.extend(['='] * (4 - padding)) 377 | 378 | # 解码Base64字符串 379 | import base64 380 | base64_str = ''.join(base64_chars) 381 | return base64.b64decode(base64_str).decode() 382 | 383 | except Exception as e: 384 | logger.debug(f"Failed to decode conversation_id: {e}") 385 | return None 386 | 387 | @app.route('/v1/chat/completions', methods=['POST']) 388 | def chat_completions(): 389 | try: 390 | # 新增:验证API密钥 391 | auth_header = request.headers.get('Authorization') 392 | if not auth_header: 393 | return jsonify({ 394 | "error": { 395 | "message": "Missing Authorization header", 396 | "type": "invalid_request_error", 397 | "param": None, 398 | "code": "invalid_api_key" 399 | } 400 | }), 401 401 | 402 | parts = auth_header.split() 403 | if len(parts) != 2 or parts[0].lower() != 'bearer': 404 | return jsonify({ 405 | "error": { 406 | "message": "Invalid Authorization header format. Expected: Bearer ", 407 | "type": "invalid_request_error", 408 | "param": None, 409 | "code": "invalid_api_key" 410 | } 411 | }), 401 412 | 413 | provided_api_key = parts[1] 414 | if provided_api_key not in VALID_API_KEYS: 415 | return jsonify({ 416 | "error": { 417 | "message": "Invalid API key", 418 | "type": "invalid_request_error", 419 | "param": None, 420 | "code": "invalid_api_key" 421 | } 422 | }), 401 423 | 424 | # 继续处理原始逻辑 425 | openai_request = request.get_json() 426 | logger.info(f"Received request: {json.dumps(openai_request, ensure_ascii=False)}") 427 | 428 | model = openai_request.get("model", "claude-3-5-sonnet-v2") 429 | 430 | # 验证模型是否支持 431 | api_key = get_api_key(model) 432 | if not api_key: 433 | error_msg = f"Model {model} is not supported. Available models: {', '.join(model_manager.name_to_api_key.keys())}" 434 | logger.error(error_msg) 435 | return { 436 | "error": { 437 | "message": error_msg, 438 | "type": "invalid_request_error", 439 | "code": "model_not_found" 440 | } 441 | }, 404 442 | 443 | dify_request = transform_openai_to_dify(openai_request, "/chat/completions") 444 | 445 | if not dify_request: 446 | logger.error("Failed to transform request") 447 | return { 448 | "error": { 449 | "message": "Invalid request format", 450 | "type": "invalid_request_error", 451 | } 452 | }, 400 453 | 454 | headers = { 455 | "Authorization": f"Bearer {api_key}", 456 | "Content-Type": "application/json" 457 | } 458 | 459 | stream = openai_request.get("stream", False) 460 | dify_endpoint = f"{DIFY_API_BASE}/chat-messages" 461 | logger.info(f"Sending request to Dify endpoint: {dify_endpoint}, stream={stream}") 462 | 463 | if stream: 464 | def generate(): 465 | client = httpx.Client(timeout=None) 466 | 467 | def flush_chunk(chunk_data): 468 | """Helper function to flush chunks immediately""" 469 | return chunk_data.encode('utf-8') 470 | 471 | def calculate_delay(buffer_size): 472 | """ 473 | 根据缓冲区大小动态计算延迟 474 | buffer_size: 缓冲区中剩余的字符数量 475 | """ 476 | if buffer_size > 30: # 缓冲区内容较多,快速输出 477 | return 0.001 # 5ms延迟 478 | elif buffer_size > 20: # 中等数量,适中速度 479 | return 0.002 # 10ms延迟 480 | elif buffer_size > 10: # 较少内容,稍慢速度 481 | return 0.01 # 20ms延迟 482 | else: # 内容很少,使用较慢的速度 483 | return 0.02 # 30ms延迟 484 | 485 | def send_char(char, message_id): 486 | """Helper function to send single character""" 487 | openai_chunk = { 488 | "id": message_id, 489 | "object": "chat.completion.chunk", 490 | "created": int(time.time()), 491 | "model": model, 492 | "choices": [{ 493 | "index": 0, 494 | "delta": { 495 | "content": char 496 | }, 497 | "finish_reason": None 498 | }] 499 | } 500 | chunk_data = f"data: {json.dumps(openai_chunk)}\n\n" 501 | return flush_chunk(chunk_data) 502 | 503 | # 初始化缓冲区 504 | output_buffer = [] 505 | 506 | try: 507 | with client.stream( 508 | 'POST', 509 | dify_endpoint, 510 | json=dify_request, 511 | headers={ 512 | **headers, 513 | 'Accept': 'text/event-stream', 514 | 'Cache-Control': 'no-cache', 515 | 'Connection': 'keep-alive' 516 | } 517 | ) as response: 518 | generate.message_id = None 519 | buffer = "" 520 | 521 | for raw_bytes in response.iter_raw(): 522 | if not raw_bytes: 523 | continue 524 | 525 | try: 526 | buffer += raw_bytes.decode('utf-8') 527 | 528 | while '\n' in buffer: 529 | line, buffer = buffer.split('\n', 1) 530 | line = line.strip() 531 | 532 | if not line or not line.startswith('data: '): 533 | continue 534 | 535 | try: 536 | json_str = line[6:] 537 | dify_chunk = json.loads(json_str) 538 | 539 | if dify_chunk.get("event") == "message" and "answer" in dify_chunk: 540 | current_answer = dify_chunk["answer"] 541 | if not current_answer: 542 | continue 543 | 544 | message_id = dify_chunk.get("message_id", "") 545 | if not generate.message_id: 546 | generate.message_id = message_id 547 | 548 | # 将当前批次的字符添加到输出缓冲区 549 | for char in current_answer: 550 | output_buffer.append((char, generate.message_id)) 551 | 552 | # 根据缓冲区大小动态调整输出速度 553 | while output_buffer: 554 | char, msg_id = output_buffer.pop(0) 555 | yield send_char(char, msg_id) 556 | # 根据剩余缓冲区大小计算延迟 557 | delay = calculate_delay(len(output_buffer)) 558 | time.sleep(delay) 559 | 560 | # 立即继续处理下一个请求 561 | continue 562 | 563 | # 处理Agent模式的消息事件 564 | elif dify_chunk.get("event") == "agent_message" and "answer" in dify_chunk: 565 | current_answer = dify_chunk["answer"] 566 | if not current_answer: 567 | continue 568 | 569 | message_id = dify_chunk.get("message_id", "") 570 | if not generate.message_id: 571 | generate.message_id = message_id 572 | 573 | # 将当前批次的字符添加到输出缓冲区 574 | for char in current_answer: 575 | output_buffer.append((char, generate.message_id)) 576 | 577 | # 根据缓冲区大小动态调整输出速度 578 | while output_buffer: 579 | char, msg_id = output_buffer.pop(0) 580 | yield send_char(char, msg_id) 581 | # 根据剩余缓冲区大小计算延迟 582 | delay = calculate_delay(len(output_buffer)) 583 | time.sleep(delay) 584 | 585 | # 立即继续处理下一个请求 586 | continue 587 | 588 | # 处理Agent的思考过程,记录日志但不输出给用户 589 | elif dify_chunk.get("event") == "agent_thought": 590 | thought_id = dify_chunk.get("id", "") 591 | thought = dify_chunk.get("thought", "") 592 | tool = dify_chunk.get("tool", "") 593 | tool_input = dify_chunk.get("tool_input", "") 594 | observation = dify_chunk.get("observation", "") 595 | 596 | logger.info(f"[Agent Thought] ID: {thought_id}, Tool: {tool}") 597 | if thought: 598 | logger.info(f"[Agent Thought] Thought: {thought}") 599 | if tool_input: 600 | logger.info(f"[Agent Thought] Tool Input: {tool_input}") 601 | if observation: 602 | logger.info(f"[Agent Thought] Observation: {observation}") 603 | 604 | # 获取message_id以关联思考和最终输出 605 | message_id = dify_chunk.get("message_id", "") 606 | if not generate.message_id and message_id: 607 | generate.message_id = message_id 608 | 609 | continue 610 | 611 | # 处理消息中的文件(如图片),记录日志但不直接输出给用户 612 | elif dify_chunk.get("event") == "message_file": 613 | file_id = dify_chunk.get("id", "") 614 | file_type = dify_chunk.get("type", "") 615 | file_url = dify_chunk.get("url", "") 616 | 617 | logger.info(f"[Message File] ID: {file_id}, Type: {file_type}, URL: {file_url}") 618 | continue 619 | 620 | elif dify_chunk.get("event") == "message_end": 621 | # 快速输出剩余内容 622 | while output_buffer: 623 | char, msg_id = output_buffer.pop(0) 624 | yield send_char(char, msg_id) 625 | time.sleep(0.001) # 固定使用最小延迟快速输出剩余内容 626 | 627 | # 只在零宽字符会话记忆模式时处理conversation_id 628 | if CONVERSATION_MEMORY_MODE == 2: 629 | conversation_id = dify_chunk.get("conversation_id") 630 | history = dify_chunk.get("conversation_history", []) 631 | 632 | has_conversation_id = False 633 | if history: 634 | for msg in history: 635 | if msg.get("role") == "assistant": 636 | content = msg.get("content", "") 637 | if decode_conversation_id(content) is not None: 638 | has_conversation_id = True 639 | break 640 | 641 | # 只在新会话且历史消息中没有会话ID时插入 642 | if conversation_id and not has_conversation_id: 643 | logger.info(f"[Debug] Inserting conversation_id in stream: {conversation_id}") 644 | encoded = encode_conversation_id(conversation_id) 645 | logger.info(f"[Debug] Stream encoded content: {repr(encoded)}") 646 | for char in encoded: 647 | yield send_char(char, generate.message_id) 648 | 649 | final_chunk = { 650 | "id": generate.message_id, 651 | "object": "chat.completion.chunk", 652 | "created": int(time.time()), 653 | "model": model, 654 | "choices": [{ 655 | "index": 0, 656 | "delta": {}, 657 | "finish_reason": "stop" 658 | }] 659 | } 660 | yield flush_chunk(f"data: {json.dumps(final_chunk)}\n\n") 661 | yield flush_chunk("data: [DONE]\n\n") 662 | 663 | except json.JSONDecodeError as e: 664 | logger.error(f"JSON decode error: {str(e)}") 665 | continue 666 | 667 | except Exception as e: 668 | logger.error(f"Error processing chunk: {str(e)}") 669 | continue 670 | 671 | finally: 672 | client.close() 673 | 674 | return Response( 675 | stream_with_context(generate()), 676 | content_type='text/event-stream', 677 | headers={ 678 | 'Cache-Control': 'no-cache, no-transform', 679 | 'Connection': 'keep-alive', 680 | 'Transfer-Encoding': 'chunked', 681 | 'X-Accel-Buffering': 'no', 682 | 'Content-Encoding': 'none' 683 | }, 684 | direct_passthrough=True 685 | ) 686 | else: 687 | async def sync_response(): 688 | try: 689 | async with httpx.AsyncClient() as client: 690 | response = await client.post( 691 | dify_endpoint, 692 | json=dify_request, 693 | headers=headers 694 | ) 695 | 696 | if response.status_code != 200: 697 | error_msg = f"Dify API error: {response.text}" 698 | logger.error(f"Request failed: {error_msg}") 699 | return { 700 | "error": { 701 | "message": error_msg, 702 | "type": "api_error", 703 | "code": response.status_code 704 | } 705 | }, response.status_code 706 | 707 | dify_response = response.json() 708 | logger.info(f"Received response from Dify: {json.dumps(dify_response, ensure_ascii=False)}") 709 | logger.info(f"[Debug] Response content: {repr(dify_response.get('answer', ''))}") 710 | openai_response = transform_dify_to_openai(dify_response, model=model) 711 | conversation_id = dify_response.get("conversation_id") 712 | if conversation_id: 713 | # 在响应头中传递conversation_id 714 | return Response( 715 | json.dumps(openai_response), 716 | content_type='application/json', 717 | headers={ 718 | 'Conversation-Id': conversation_id 719 | } 720 | ) 721 | else: 722 | return openai_response 723 | except httpx.RequestError as e: 724 | error_msg = f"Failed to connect to Dify: {str(e)}" 725 | logger.error(error_msg) 726 | return { 727 | "error": { 728 | "message": error_msg, 729 | "type": "api_error", 730 | "code": "connection_error" 731 | } 732 | }, 503 733 | 734 | return asyncio.run(sync_response()) 735 | 736 | except Exception as e: 737 | logger.exception("Unexpected error occurred") 738 | return { 739 | "error": { 740 | "message": str(e), 741 | "type": "internal_error", 742 | } 743 | }, 500 744 | 745 | @app.route('/v1/models', methods=['GET']) 746 | def list_models(): 747 | """返回可用的模型列表""" 748 | logger.info("Listing available models") 749 | 750 | # 刷新模型信息 751 | asyncio.run(model_manager.refresh_model_info()) 752 | 753 | # 获取可用模型列表 754 | available_models = model_manager.get_available_models() 755 | 756 | response = { 757 | "object": "list", 758 | "data": available_models 759 | } 760 | logger.info(f"Available models: {json.dumps(response, ensure_ascii=False)}") 761 | return response 762 | 763 | # 在main.py的最后初始化时添加环境变量检查: 764 | if __name__ == '__main__': 765 | if not VALID_API_KEYS: 766 | print("Warning: No API keys configured. Set the VALID_API_KEYS environment variable with comma-separated keys.") 767 | 768 | # 启动时初始化模型信息 769 | asyncio.run(model_manager.refresh_model_info()) 770 | 771 | host = os.getenv("SERVER_HOST", "127.0.0.1") 772 | port = int(os.getenv("SERVER_PORT", 5000)) 773 | logger.info(f"Starting server on http://{host}:{port}") 774 | app.run(debug=True, host=host, port=port) 775 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | flask 2 | httpx 3 | python-dotenv --------------------------------------------------------------------------------