From d6de2479c10ed56611b7609d2d190510957cbc47 Mon Sep 17 00:00:00 2001 From: meethuhu Date: Mon, 30 Dec 2024 20:32:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`PATH=5FPREFIX`=E5=B8=B8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.template | 3 ++- README.md | 27 +++++++++++++-------------- doc/README_zh.md | 9 +++++---- index.js | 7 +++++-- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.env.template b/.env.template index 5a490ba..9e6f278 100644 --- a/.env.template +++ b/.env.template @@ -1,3 +1,4 @@ PORT=3000 API_KEYS=sk-key1,sk-key2 -DEBUG_MODE=false \ No newline at end of file +DEBUG_MODE=false +PATH_PREFIX=hf \ No newline at end of file diff --git a/README.md b/README.md index 29c0348..4aa5459 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,9 @@

- [中文](./doc/README_zh.md) | [English](./README_en.md) +[中文](./doc/README_zh.md) | [English](./README_en.md) - ## Supported Interfaces - `GET - /v1/models` @@ -35,12 +34,11 @@ ## Environment variables -| Name | Option | Description | -| ---------- | -------- | ------------------------------------------------ | -| `PORT` | Optional | Request port, default is 3000 | -| `API_KEYS` | Optional | API key group, separate multiple values with `,` | - - +| Name | Option | Description | +|---------------|----------|--------------------------------------------------------------------------------------------------------------------------------| +| `PORT` | Optional | Request port, default is 3000 | +| `API_KEYS` | Optional | API key group, separate multiple values with `,` | +| `PATH_PREFIX` | Optional | The actual endpoint URL should be prefixed with `PATH_PREFIX` after configuration, example: `/PATH_PREFIX/v1/chat/completions` | ## Deployment @@ -49,7 +47,7 @@ - *suggest manually setting `API_KEYS` + *suggest manually setting `API_KEYS` *To deploy manually, go to the `cf` branch - ### Vercel @@ -57,7 +55,7 @@ - *suggest manually setting `API_KEYS` + *suggest manually setting `API_KEYS` - ### Docker @@ -99,6 +97,7 @@ ``` ## Usage example + ```shell # Get model list curl http://localhost:3000/v1/models \ @@ -119,10 +118,10 @@ curl http://localhost:3000/v1/chat/completions \ ### Notes -1. This project is for learning and research purposes only -2. Please comply with DuckDuckGo's terms of use -3. Recommended for use in local or private environments -4. Please keep your keys secure +1. This project is for learning and research purposes only +2. Please comply with DuckDuckGo's terms of use +3. Recommended for use in local or private environments +4. Please keep your keys secure ### Open Source License diff --git a/doc/README_zh.md b/doc/README_zh.md index aced295..4acc956 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -35,10 +35,11 @@ ## 环境变量 -| 名称 | 是否必需 | 描述 | -| ---------- | -------- | ------------------------------ | -| `PORT` | 可选 | 服务端口,默认为 3000 | -| `API_KEYS` | 可选 | API密钥组,多个值用逗号(,)分隔 | +| 名称 | 是否必需 | 描述 | +| ---------- | -------- |----------------------------------------------------------------------| +| `PORT` | 可选 | 服务端口,默认为 3000 | +| `API_KEYS` | 可选 | API密钥组,多个值用逗号(,)分隔 | +| `PATH_PREFIX` | 可选 | 配置后,实际端点 URL 应以“PATH_PREFIX”为前缀,例如:`/PATH_PREFIX/v1/chat/completions` | ## 部署方式 diff --git a/index.js b/index.js index d824e7d..dddae44 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,9 @@ const PORT = process.env.PORT || 3000; const API_KEYS = new Set(process.env.API_KEYS ? process.env.API_KEYS.split(',') : []); // DEBUG 模式 const DEBUG_MODE = process.env.DEBUG_MODE === 'true'; +// 自定义前缀 +const PATH_PREFIX = process.env.PATH_PREFIX?'/'+process.env.PATH_PREFIX:''; + // 模型映射 const MODELS = { 'gpt-4o-mini': 'gpt-4o-mini', @@ -273,7 +276,7 @@ app.use(cors({ app.options('*', cors()); // v1/models 路由 -app.get('/v1/models', validateApiKey, (req, res) => { +app.get(PATH_PREFIX+'/v1/models', validateApiKey, (req, res) => { res.json({ object: "list", data: Object.keys(MODELS).map(modelName => ({ @@ -285,7 +288,7 @@ app.get('/v1/models', validateApiKey, (req, res) => { }); // v1/chat/completions 路由 -app.post('/v1/chat/completions', validateApiKey, async (req, res, next) => { +app.post(PATH_PREFIX+'/v1/chat/completions', validateApiKey, async (req, res, next) => { const {messages, model, stream = false} = req.body; if (!messages?.length) {