|
| 1 | +# Mock OpenAI API |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/mock-openai-api) |
| 4 | +[](https://github.com/zerob13/mock-openai-api/blob/main/LICENSE) |
| 5 | +[](https://www.typescriptlang.org/) |
| 6 | +[](https://expressjs.com/) |
| 7 | +[](https://github.com/zerob13/mock-openai-api) |
| 8 | +[](https://github.com/zerob13/mock-openai-api/fork) |
| 9 | + |
| 10 | +*中文说明 | [English](README.md)* |
| 11 | + |
| 12 | +一个完整的 OpenAI API 兼容的模拟服务器,无需调用真实的大模型,返回预定义的测试数据。非常适合开发、测试和调试使用 OpenAI API 的应用程序。 |
| 13 | + |
| 14 | +## 🚀 快速开始 |
| 15 | + |
| 16 | +### 安装 |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install -g mock-openai-api |
| 20 | +``` |
| 21 | + |
| 22 | +### 启动服务器 |
| 23 | + |
| 24 | +```bash |
| 25 | +npx mock-openai-api |
| 26 | +``` |
| 27 | + |
| 28 | +服务器将在 `http://localhost:3000` 启动。 |
| 29 | + |
| 30 | +### 基本使用 |
| 31 | + |
| 32 | +```bash |
| 33 | +# 获取模型列表 |
| 34 | +curl http://localhost:3000/v1/models |
| 35 | + |
| 36 | +# 聊天完成(非流式) |
| 37 | +curl -X POST http://localhost:3000/v1/chat/completions \ |
| 38 | + -H "Content-Type: application/json" \ |
| 39 | + -d '{ |
| 40 | + "model": "mock-gpt-thinking", |
| 41 | + "messages": [{"role": "user", "content": "你好"}] |
| 42 | + }' |
| 43 | + |
| 44 | +# 聊天完成(流式) |
| 45 | +curl -X POST http://localhost:3000/v1/chat/completions \ |
| 46 | + -H "Content-Type: application/json" \ |
| 47 | + -d '{ |
| 48 | + "model": "mock-gpt-thinking", |
| 49 | + "messages": [{"role": "user", "content": "你好"}], |
| 50 | + "stream": true |
| 51 | + }' |
| 52 | + |
| 53 | +# 图像生成 |
| 54 | +curl -X POST http://localhost:3000/v1/images/generations \ |
| 55 | + -H "Content-Type: application/json" \ |
| 56 | + -d '{ |
| 57 | + "model": "gpt-4o-image", |
| 58 | + "prompt": "一只可爱的猫咪", |
| 59 | + "n": 1, |
| 60 | + "size": "1024x1024" |
| 61 | + }' |
| 62 | +``` |
| 63 | + |
| 64 | +## 🎯 特性 |
| 65 | + |
| 66 | +- ✅ **完整的 OpenAI API 兼容性** |
| 67 | +- ✅ **支持流式和非流式聊天完成** |
| 68 | +- ✅ **支持函数调用** |
| 69 | +- ✅ **支持图像生成** |
| 70 | +- ✅ **预定义的测试场景** |
| 71 | +- ✅ **TypeScript 编写** |
| 72 | +- ✅ **易于集成和部署** |
| 73 | +- ✅ **详细的错误处理** |
| 74 | + |
| 75 | +## 📋 支持的 API 端点 |
| 76 | + |
| 77 | +### 模型管理 |
| 78 | +- `GET /v1/models` - 获取可用模型列表 |
| 79 | +- `GET /models` - 兼容端点 |
| 80 | + |
| 81 | +### 聊天完成 |
| 82 | +- `POST /v1/chat/completions` - 创建聊天完成 |
| 83 | +- `POST /chat/completions` - 兼容端点 |
| 84 | + |
| 85 | +### 图像生成 |
| 86 | +- `POST /v1/images/generations` - 生成图像 |
| 87 | +- `POST /images/generations` - 兼容端点 |
| 88 | + |
| 89 | +### 健康检查 |
| 90 | +- `GET /health` - 服务器健康状态 |
| 91 | + |
| 92 | +## 🤖 可用模型 |
| 93 | + |
| 94 | +### 1. mock-gpt-thinking |
| 95 | +**思考型模型** - 显示推理过程,适合调试逻辑 |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "model": "mock-gpt-thinking", |
| 100 | + "messages": [{"role": "user", "content": "计算 2+2"}] |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +响应会包含 `<thinking>` 标签显示思考过程。 |
| 105 | + |
| 106 | +### 2. mock-gpt-function |
| 107 | +**函数调用模型** - 支持工具和函数调用 |
| 108 | + |
| 109 | +```json |
| 110 | +{ |
| 111 | + "model": "mock-gpt-function", |
| 112 | + "messages": [{"role": "user", "content": "今天北京的天气怎么样?"}], |
| 113 | + "functions": [ |
| 114 | + { |
| 115 | + "name": "get_weather", |
| 116 | + "description": "获取天气信息", |
| 117 | + "parameters": { |
| 118 | + "type": "object", |
| 119 | + "properties": { |
| 120 | + "location": {"type": "string"}, |
| 121 | + "date": {"type": "string"} |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### 3. mock-gpt-markdown |
| 130 | +**Markdown 示例模型** - 专门输出标准 Markdown 格式的纯文本模型 |
| 131 | + |
| 132 | +```json |
| 133 | +{ |
| 134 | + "model": "mock-gpt-markdown", |
| 135 | + "messages": [{"role": "user", "content": "任何问题"}] |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +响应将是一个完整的 Markdown 文档,包含各种格式元素,适合前端 UI 调试。 |
| 140 | +**注意:** 此模型专注于内容展示,不支持函数调用功能,保持输出的纯净性。 |
| 141 | + |
| 142 | +### 4. gpt-4o-image |
| 143 | +**图像生成模型** - 专门用于图像生成 |
| 144 | + |
| 145 | +```json |
| 146 | +{ |
| 147 | + "model": "gpt-4o-image", |
| 148 | + "prompt": "一只可爱的橘猫在阳光下玩耍", |
| 149 | + "n": 2, |
| 150 | + "size": "1024x1024", |
| 151 | + "quality": "hd" |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +支持多种尺寸和质量设置,返回高质量的模拟图像。 |
| 156 | + |
| 157 | +## 🛠️ 开发 |
| 158 | + |
| 159 | +### 本地开发 |
| 160 | + |
| 161 | +```bash |
| 162 | +# 克隆项目 |
| 163 | +git clone https://github.com/zerob13/mock-openai-api.git |
| 164 | +cd mock-openai-api |
| 165 | + |
| 166 | +# 安装依赖 |
| 167 | +npm install |
| 168 | + |
| 169 | +# 开发模式运行 |
| 170 | +npm run dev |
| 171 | + |
| 172 | +# 构建项目 |
| 173 | +npm run build |
| 174 | + |
| 175 | +# 生产模式运行 |
| 176 | +npm start |
| 177 | +``` |
| 178 | + |
| 179 | +### 项目结构 |
| 180 | + |
| 181 | +``` |
| 182 | +src/ |
| 183 | +├── types/ # TypeScript 类型定义 |
| 184 | +├── data/ # 预定义的测试数据 |
| 185 | +├── utils/ # 工具函数 |
| 186 | +├── services/ # 业务逻辑服务 |
| 187 | +├── controllers/ # 路由控制器 |
| 188 | +├── routes/ # 路由定义 |
| 189 | +├── app.ts # Express 应用设置 |
| 190 | +├── index.ts # 服务器启动 |
| 191 | +└── cli.ts # CLI 工具入口 |
| 192 | +``` |
| 193 | + |
| 194 | +### 添加新的测试场景 |
| 195 | + |
| 196 | +1. 在 `src/data/mockData.ts` 中添加新的测试用例 |
| 197 | +2. 可以为现有模型添加测试用例,或创建新的模型类型 |
| 198 | +3. 重新构建项目:`npm run build` |
| 199 | + |
| 200 | +示例: |
| 201 | + |
| 202 | +```typescript |
| 203 | +const newTestCase: MockTestCase = { |
| 204 | + name: "新功能测试", |
| 205 | + description: "测试新功能的描述", |
| 206 | + prompt: "触发关键词", |
| 207 | + response: "预期的响应内容", |
| 208 | + streamChunks: ["分段", "流式", "内容"], // 可选 |
| 209 | + functionCall: { // 可选,仅用于 function 类型模型 |
| 210 | + name: "function_name", |
| 211 | + arguments: { param: "value" } |
| 212 | + } |
| 213 | +}; |
| 214 | +``` |
| 215 | + |
| 216 | +## 🌐 部署 |
| 217 | + |
| 218 | +### Docker 部署 |
| 219 | + |
| 220 | +创建 `Dockerfile`: |
| 221 | + |
| 222 | +```dockerfile |
| 223 | +FROM node:18-alpine |
| 224 | +WORKDIR /app |
| 225 | +COPY package*.json ./ |
| 226 | +RUN npm ci --only=production |
| 227 | +COPY dist ./dist |
| 228 | +EXPOSE 3000 |
| 229 | +CMD ["node", "dist/index.js"] |
| 230 | +``` |
| 231 | + |
| 232 | +构建和运行: |
| 233 | + |
| 234 | +```bash |
| 235 | +docker build -t mock-openai-api . |
| 236 | +docker run -p 3000:3000 mock-openai-api |
| 237 | +``` |
| 238 | + |
| 239 | +### 环境变量 |
| 240 | + |
| 241 | +- `PORT` - 服务器端口(默认:3000) |
| 242 | +- `HOST` - 服务器主机(默认:0.0.0.0) |
| 243 | + |
| 244 | +## 🧪 测试 |
| 245 | + |
| 246 | +### 使用 curl 测试 |
| 247 | + |
| 248 | +```bash |
| 249 | +# 测试健康检查 |
| 250 | +curl http://localhost:3000/health |
| 251 | + |
| 252 | +# 测试模型列表 |
| 253 | +curl http://localhost:3000/v1/models |
| 254 | + |
| 255 | +# 测试思考型模型 |
| 256 | +curl -X POST http://localhost:3000/v1/chat/completions \ |
| 257 | + -H "Content-Type: application/json" \ |
| 258 | + -d '{ |
| 259 | + "model": "mock-gpt-thinking", |
| 260 | + "messages": [{"role": "user", "content": "解释一下递归"}] |
| 261 | + }' |
| 262 | + |
| 263 | +# 测试函数调用 |
| 264 | +curl -X POST http://localhost:3000/v1/chat/completions \ |
| 265 | + -H "Content-Type: application/json" \ |
| 266 | + -d '{ |
| 267 | + "model": "mock-gpt-function", |
| 268 | + "messages": [{"role": "user", "content": "现在几点了?"}] |
| 269 | + }' |
| 270 | + |
| 271 | +# 测试 Markdown 输出 |
| 272 | +curl -X POST http://localhost:3000/v1/chat/completions \ |
| 273 | + -H "Content-Type: application/json" \ |
| 274 | + -d '{ |
| 275 | + "model": "mock-gpt-markdown", |
| 276 | + "messages": [{"role": "user", "content": "任何内容"}] |
| 277 | + }' |
| 278 | + |
| 279 | +# 测试流式输出 |
| 280 | +curl -X POST http://localhost:3000/v1/chat/completions \ |
| 281 | + -H "Content-Type: application/json" \ |
| 282 | + -d '{ |
| 283 | + "model": "mock-gpt-thinking", |
| 284 | + "messages": [{"role": "user", "content": "讲个故事"}], |
| 285 | + "stream": true |
| 286 | + }' |
| 287 | + |
| 288 | +# 测试图像生成 |
| 289 | +curl -X POST http://localhost:3000/v1/images/generations \ |
| 290 | + -H "Content-Type: application/json" \ |
| 291 | + -d '{ |
| 292 | + "model": "gpt-4o-image", |
| 293 | + "prompt": "一只可爱的橘猫", |
| 294 | + "n": 2, |
| 295 | + "size": "1024x1024" |
| 296 | + }' |
| 297 | +``` |
| 298 | + |
| 299 | +### 使用 OpenAI SDK 测试 |
| 300 | + |
| 301 | +```javascript |
| 302 | +import OpenAI from 'openai'; |
| 303 | + |
| 304 | +const client = new OpenAI({ |
| 305 | + baseURL: 'http://localhost:3000/v1', |
| 306 | + apiKey: 'mock-key' // 可以是任意值 |
| 307 | +}); |
| 308 | + |
| 309 | +// 测试聊天完成 |
| 310 | +const completion = await client.chat.completions.create({ |
| 311 | + model: 'mock-gpt-thinking', |
| 312 | + messages: [{ role: 'user', content: '你好' }] |
| 313 | +}); |
| 314 | + |
| 315 | +console.log(completion.choices[0].message.content); |
| 316 | + |
| 317 | +// 测试流式聊天 |
| 318 | +const stream = await client.chat.completions.create({ |
| 319 | + model: 'mock-gpt-thinking', |
| 320 | + messages: [{ role: 'user', content: '你好' }], |
| 321 | + stream: true |
| 322 | +}); |
| 323 | + |
| 324 | +for await (const chunk of stream) { |
| 325 | + const content = chunk.choices[0]?.delta?.content || ''; |
| 326 | + process.stdout.write(content); |
| 327 | +} |
| 328 | + |
| 329 | +// 测试图像生成 |
| 330 | +const image = await client.images.generate({ |
| 331 | + model: 'gpt-4o-image', |
| 332 | + prompt: '一只可爱的橘猫', |
| 333 | + n: 1, |
| 334 | + size: '1024x1024' |
| 335 | +}); |
| 336 | + |
| 337 | +console.log(image.data[0].url); |
| 338 | +``` |
| 339 | +
|
| 340 | +## 🤝 贡献 |
| 341 | +
|
| 342 | +欢迎提交 Issue 和 Pull Request! |
| 343 | +
|
| 344 | +1. Fork 本项目 |
| 345 | +2. 创建特性分支:`git checkout -b feature/AmazingFeature` |
| 346 | +3. 提交更改:`git commit -m 'Add some AmazingFeature'` |
| 347 | +4. 推送到分支:`git push origin feature/AmazingFeature` |
| 348 | +5. 提交 Pull Request |
| 349 | +
|
| 350 | +## 📄 许可证 |
| 351 | +
|
| 352 | +本项目使用 MIT 许可证。详见 [LICENSE](LICENSE) 文件。 |
| 353 | +
|
| 354 | +## 🔗 相关链接 |
| 355 | +
|
| 356 | +- [OpenAI API 文档](https://platform.openai.com/docs/api-reference) |
| 357 | +- [Express.js](https://expressjs.com/) |
| 358 | +- [TypeScript](https://www.typescriptlang.org/) |
| 359 | +
|
| 360 | +## 💡 使用场景 |
| 361 | +
|
| 362 | +- **前端开发**: 无需等待后端 API,快速开发和测试 AI 功能 |
| 363 | +- **API 集成测试**: 验证应用程序与 OpenAI API 的集成 |
| 364 | +- **演示和原型**: 创建不依赖真实 AI 服务的演示 |
| 365 | +- **开发调试**: 调试流式响应、函数调用等复杂场景 |
| 366 | +- **成本控制**: 避免开发阶段的 API 调用费用 |
| 367 | +- **离线开发**: 在没有网络的情况下开发 AI 应用 |
| 368 | +
|
| 369 | +## 🎉 结语 |
| 370 | +
|
| 371 | +Mock OpenAI API 让您能够快速、可靠地开发和测试 AI 应用,无需担心 API 配额、网络连接或费用问题。开始您的 AI 应用开发之旅吧! |
0 commit comments