Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- **完全兼容 OpenAI API**: 支持标准的 `/v1/models` 和 `/v1/chat/completions` 端点
- **流式响应**: 通过 Server-Sent Events (SSE) 实时返回增量聊天内容
- **非流式响应**: 等待完整响应后一次性返回 JSON 格式的最终结果
- **可选身份认证**: 支持 Bearer Token 认证保护 API 端点
- **无需外部依赖**: 使用 Deno 原生 WebSocket 实现 Socket.IO 协议

## 快速开始
Expand All @@ -33,6 +34,25 @@ deno task dev

服务器将在 `http://localhost:8000` 上启动。

#### 可选:启用身份认证

如需启用 API 身份认证,设置环境变量 `USER_SET_KEY`:

```bash
# Linux/macOS
export USER_SET_KEY="your-secret-key"
deno task dev

# Windows (PowerShell)
$env:USER_SET_KEY="your-secret-key"
deno task dev
```

启用后,所有 `/v1/chat/completions` 请求需要在 Header 中携带:
```
Authorization: Bearer your-secret-key
```

### Deno Deploy 部署

本项目可以直接部署到 Deno Deploy,无需服务器维护。
Expand All @@ -54,7 +74,9 @@ deno task dev

3. **配置部署设置**
- **入口点**: 选择 `deploy.ts`
- **环境变量**: 无需额外配置(所有配置已在代码中)
- **环境变量**(可选):
- 如需启用身份认证,添加 `USER_SET_KEY` 环境变量
- 不设置则跳过认证
- 点击 "Deploy" 开始部署

4. **获取部署 URL**
Expand Down Expand Up @@ -138,6 +160,23 @@ curl -X POST http://localhost:8000/v1/chat/completions \
}'
```

### 带身份认证的请求

如果启用了 `USER_SET_KEY` 环境变量,需要添加 Authorization header:

```bash
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-secret-key" \
-d '{
"model": "gpt-5",
"messages": [
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
```

### 流式聊天完成

```bash
Expand All @@ -163,11 +202,12 @@ curl -N -X POST http://localhost:8000/v1/chat/completions \

## 工作原理

1. **认证**: 获取 LangFast 的匿名访问令牌
2. **创建 Prompt**: 在 LangFast 中创建一个 prompt 并获取其 ID
3. **启动运行**: 启动 prompt 运行并获取任务 ID
4. **WebSocket 连接**: 通过 WebSocket 连接接收流式响应
5. **格式转换**: 将响应转换为 OpenAI 兼容的格式
1. **身份认证(可选)**: 验证请求的 Bearer Token(如果设置了 `USER_SET_KEY`)
2. **LangFast 认证**: 获取 LangFast 的匿名访问令牌
3. **创建 Prompt**: 在 LangFast 中创建一个 prompt 并获取其 ID
4. **启动运行**: 启动 prompt 运行并获取任务 ID
5. **WebSocket 连接**: 通过 WebSocket 连接接收流式响应
6. **格式转换**: 将响应转换为 OpenAI 兼容的格式

## 开发

Expand Down
15 changes: 15 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ export async function handler(req: Request): Promise<Response> {
}

if (pathname === "/v1/chat/completions" && req.method === "POST") {
const userSetKey = Deno.env.get("USER_SET_KEY");

if (userSetKey) {
const authHeader = req.headers.get("Authorization");

if (!authHeader) {
return json({ error: "Missing Authorization header" }, { status: 401 });
}

const expectedAuth = `Bearer ${userSetKey}`;
if (authHeader !== expectedAuth) {
return json({ error: "Invalid authorization token" }, { status: 401 });
}
}

return handleChatCompletions(req);
}

Expand Down
Loading