diff --git a/README.md b/README.md index 7a585e3..3d3d67a 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,15 @@ OpenDify 是一个将 Dify API 转换为 OpenAI API 格式的代理服务器。 > 🌟 本项目完全由 Cursor + Claude-3.5 自动生成,未手动编写任何代码(包括此Readme),向 AI 辅助编程的未来致敬! +[English Version](README_EN.md) + ## 功能特点 - 完整支持 OpenAI API 格式转换为 Dify API - 支持流式输出(Streaming) - 智能动态延迟控制,提供流畅的输出体验 +- 支持多种会话记忆模式,包括零宽字符模式和history_message模式 - 支持多个模型配置 -- 完整的错误处理和日志记录 - 兼容标准的 OpenAI API 客户端 - 自动获取 Dify 应用信息 @@ -162,12 +164,6 @@ CONVERSATION_MEMORY_MODE=2 - 动态延迟计算 - 平滑的输出体验 -### 错误处理 - -- 完整的错误捕获和处理 -- 详细的日志记录 -- 友好的错误提示 - ### 配置灵活性 - 自动获取应用信息 diff --git a/README_EN.md b/README_EN.md new file mode 100644 index 0000000..1721fef --- /dev/null +++ b/README_EN.md @@ -0,0 +1,179 @@ +# OpenDify + +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. + +> 🌟 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! + +[中文版本](README.md) + +## Features + +- Full support for converting OpenAI API formats to Dify API +- Streaming output support +- Intelligent dynamic delay control for smooth output experience +- Multiple conversation memory modes, including zero-width character mode and history_message mode +- Support for multiple model configurations +- Compatible with standard OpenAI API clients +- Automatic fetching of Dify application information + +## Supported Models + +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. + +## Quick Start + +### Requirements + +- Python 3.9+ +- pip + +### Installing Dependencies + +```bash +pip install -r requirements.txt +``` + +### Configuration + +1. Copy the `.env.example` file and rename it to `.env`: +```bash +cp .env.example .env +``` + +2. Configure your application on the Dify platform: + - Log in to the Dify platform and enter the workspace + - Click "Create Application" and configure the required models (such as Claude, Gemini, etc.) + - Configure the application prompts and other parameters + - Publish the application + - Go to the "Access API" page and generate an API key + + > **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. + +3. Configure your Dify API Keys in the `.env` file: +```env +# Dify API Keys Configuration +# Format: Comma-separated list of API keys +DIFY_API_KEYS=app-xxxxxxxx,app-yyyyyyyy,app-zzzzzzzz + +# Dify API Base URL +DIFY_API_BASE="https://your-dify-api-base-url/v1" + +# Server Configuration +SERVER_HOST="127.0.0.1" +SERVER_PORT=5000 +``` + +Configuration notes: +- `DIFY_API_KEYS`: A comma-separated list of API Keys, each corresponding to a Dify application +- The system automatically retrieves the name and information of each application from the Dify API +- No need to manually configure model names and mapping relationships + +### Running the Service + +```bash +python openai_to_dify.py +``` + +The service will start at `http://127.0.0.1:5000` + +## API Usage + +### List Models + +Get a list of all available models: + +```python +import openai + +openai.api_base = "http://127.0.0.1:5000/v1" +openai.api_key = "any" # Can use any value + +# Get available models +models = openai.Model.list() +print(models) + +# Example output: +{ + "object": "list", + "data": [ + { + "id": "My Translation App", # Dify application name + "object": "model", + "created": 1704603847, + "owned_by": "dify" + }, + { + "id": "Code Assistant", # Another Dify application name + "object": "model", + "created": 1704603847, + "owned_by": "dify" + } + ] +} +``` + +The system automatically retrieves application names from the Dify API and uses them as model IDs. + +### Chat Completions + +```python +import openai + +openai.api_base = "http://127.0.0.1:5000/v1" +openai.api_key = "any" # Can use any value + +response = openai.ChatCompletion.create( + model="My Translation App", # Use the Dify application name + messages=[ + {"role": "user", "content": "Hello"} + ], + stream=True +) + +for chunk in response: + print(chunk.choices[0].delta.content or "", end="") +``` + +## Features + +### Conversation Memory + +The proxy supports automatic remembering of conversation context without requiring additional processing by the client. It provides three conversation memory modes: + +1. **No conversation memory**: Each conversation is independent, with no context association +2. **history_message mode**: Directly appends historical messages to the current message, supporting client-side editing of historical messages +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 + +This feature can be controlled via environment variable: + +```shell +# Set conversation memory mode in the .env file +# 0: No conversation memory +# 1: Construct history_message attached to the message +# 2: Zero-width character mode (default) +CONVERSATION_MEMORY_MODE=2 +``` + +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. + +> Note: history_message mode will append all historical messages to the current message, which may consume more tokens. + +### Streaming Output Optimization + +- Intelligent buffer management +- Dynamic delay calculation +- Smooth output experience + +### Configuration Flexibility + +- Automatic application information retrieval +- Simplified configuration method +- Dynamic model name mapping + +## Contribution Guidelines + +Issues and Pull Requests are welcome to help improve the project. + +## License + +[MIT License](LICENSE) \ No newline at end of file