Skip to content
This repository has been archived by the owner on Feb 25, 2025. It is now read-only.

Commit

Permalink
增加PATH_PREFIX常量
Browse files Browse the repository at this point in the history
  • Loading branch information
meethuhu committed Dec 30, 2024
1 parent b3d9fab commit d6de247
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PORT=3000
API_KEYS=sk-key1,sk-key2
DEBUG_MODE=false
DEBUG_MODE=false
PATH_PREFIX=hf
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
<img src="https://vercel.com/button" style="height: 30px;"/></a>
<br><br>

[中文](./doc/README_zh.md) | [English](./README_en.md)
[中文](./doc/README_zh.md) | [English](./README_en.md)
</div>


## Supported Interfaces

- `GET - /v1/models`
Expand All @@ -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

Expand All @@ -49,15 +47,15 @@
<a href="https://deploy.workers.cloudflare.com/?url=https://github.com/meethuhu/ddg2api/actions/workflows/workers-deploy.yml">
<img src="https://deploy.workers.cloudflare.com/button" style="height: 30px;"/></a>

*suggest manually setting `API_KEYS`
*suggest manually setting `API_KEYS`
*To deploy manually, go to the `cf` branch

- ### Vercel

<a href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fmeethuhu%2FDDG2API">
<img src="https://vercel.com/button" style="height: 30px;"/></a>

*suggest manually setting `API_KEYS`
*suggest manually setting `API_KEYS`

- ### Docker

Expand Down Expand Up @@ -99,6 +97,7 @@
```

## Usage example

```shell
# Get model list
curl http://localhost:3000/v1/models \
Expand All @@ -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

Expand Down
9 changes: 5 additions & 4 deletions doc/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@

## 环境变量

| 名称 | 是否必需 | 描述 |
| ---------- | -------- | ------------------------------ |
| `PORT` | 可选 | 服务端口,默认为 3000 |
| `API_KEYS` | 可选 | API密钥组,多个值用逗号(,)分隔 |
| 名称 | 是否必需 | 描述 |
| ---------- | -------- |----------------------------------------------------------------------|
| `PORT` | 可选 | 服务端口,默认为 3000 |
| `API_KEYS` | 可选 | API密钥组,多个值用逗号(,)分隔 |
| `PATH_PREFIX` | 可选 | 配置后,实际端点 URL 应以“PATH_PREFIX”为前缀,例如:`/PATH_PREFIX/v1/chat/completions` |

## 部署方式

Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 => ({
Expand All @@ -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) {
Expand Down

0 comments on commit d6de247

Please sign in to comment.