Skip to content

Commit b208619

Browse files
committed
feat: add cli params
1 parent 6a110b5 commit b208619

File tree

10 files changed

+522
-89
lines changed

10 files changed

+522
-89
lines changed

TOOL_CALLS_README.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Mock OpenAI API - Function Calls 功能指南
2+
3+
## 概述
4+
5+
本项目支持两种函数调用格式:
6+
7+
- `mock-gpt-function`: 支持新版OpenAI tool calls格式的模型
8+
9+
## 模型特点
10+
11+
### mock-gpt-function
12+
- 使用新版 tool calls 格式
13+
- 支持两阶段调用流程
14+
- 第一阶段返回 tool_calls,第二阶段返回执行结果
15+
- 完全兼容 OpenAI 最新的函数调用规范
16+
17+
## 命令行启动选项
18+
19+
支持以下命令行参数:
20+
21+
```bash
22+
# 使用默认设置启动
23+
npx mock-openai-api
24+
25+
# 指定端口和主机
26+
npx mock-openai-api -p 8080 -H localhost
27+
28+
# 启用详细日志
29+
npx mock-openai-api -v
30+
31+
# 查看所有选项
32+
npx mock-openai-api --help
33+
```
34+
35+
参数说明:
36+
- `-p, --port <number>`: 服务器端口 (默认: 3000)
37+
- `-H, --host <address>`: 服务器主机地址 (默认: 0.0.0.0)
38+
- `-v, --verbose`: 启用请求日志输出到控制台 (默认: 关闭)
39+
40+
## 使用示例
41+
42+
### 基本调用 - mock-gpt-function
43+
44+
```bash
45+
curl -X POST http://localhost:3000/v1/chat/completions \
46+
-H "Content-Type: application/json" \
47+
-d '{
48+
"model": "mock-gpt-function",
49+
"messages": [
50+
{"role": "user", "content": "What time is it now?"}
51+
],
52+
"stream": false
53+
}'
54+
```
55+
56+
### 流式调用 - mock-gpt-function
57+
58+
```bash
59+
curl -X POST http://localhost:3000/v1/chat/completions \
60+
-H "Content-Type: application/json" \
61+
-d '{
62+
"model": "mock-gpt-function",
63+
"messages": [
64+
{"role": "user", "content": "What time is it now?"}
65+
],
66+
"stream": true
67+
}'
68+
```
69+
70+
### 启用详细日志的调用示例
71+
72+
```bash
73+
# 启动服务器并启用日志
74+
npx mock-openai-api -v -p 8080
75+
76+
# 发送请求 (在另一个终端)
77+
curl -X POST http://localhost:8080/v1/chat/completions \
78+
-H "Content-Type: application/json" \
79+
-d '{
80+
"model": "mock-gpt-function",
81+
"messages": [
82+
{"role": "user", "content": "Calculate 123 * 456"}
83+
],
84+
"stream": false
85+
}'
86+
```
87+
88+
## 支持的测试场景
89+
90+
mock-gpt-function 模型包含以下预设测试场景:
91+
92+
1. **获取当前时间**: `"What time is it now?"`
93+
2. **天气查询**: `"What's the weather like in Beijing today?"`
94+
3. **数学计算**: `"Help me calculate 123 multiplied by 456"`
95+
4. **网络搜索**: `"Search for the latest AI news"`
96+
97+
## 响应格式
98+
99+
### Tool Calls 响应示例 (第一阶段)
100+
101+
```json
102+
{
103+
"id": "chatcmpl-123",
104+
"object": "chat.completion",
105+
"created": 1677652288,
106+
"model": "mock-gpt-function",
107+
"choices": [{
108+
"index": 0,
109+
"message": {
110+
"role": "assistant",
111+
"content": null,
112+
"tool_calls": [{
113+
"id": "call_0_8a90fac8-b281-49a0-bcc9-55d7f4603891",
114+
"type": "function",
115+
"function": {
116+
"name": "get_time",
117+
"arguments": "{}"
118+
}
119+
}]
120+
},
121+
"finish_reason": "tool_calls"
122+
}],
123+
"usage": {
124+
"prompt_tokens": 10,
125+
"completion_tokens": 10,
126+
"total_tokens": 20
127+
}
128+
}
129+
```
130+
131+
### 流式响应示例
132+
133+
对于流式调用,mock-gpt-function 会先返回 tool_calls,然后在第二阶段返回执行结果的分块内容。
134+
135+
## 测试文件
136+
137+
可以使用项目中的 `test-sse-client.html` 文件来测试流式响应:
138+
139+
```bash
140+
# 启动服务器
141+
npm run dev
142+
143+
# 在浏览器中打开 test-sse-client.html 文件
144+
# 选择 mock-gpt-function 模型进行测试
145+
```
146+
147+
## 开发说明
148+
149+
### 添加新的测试用例
150+
151+
如需添加新的 function call 测试用例,请在 `src/data/mockData.ts``functionTestCases` 数组中添加新的测试用例,格式如下:
152+
153+
```typescript
154+
{
155+
name: "Test Case Name",
156+
description: "Test case description",
157+
prompt: "User input that triggers this test case",
158+
response: "", // First phase empty
159+
toolCall: {
160+
name: "function_name",
161+
arguments: {
162+
// function arguments
163+
},
164+
id: "unique_call_id"
165+
},
166+
toolCallResponse: "Function execution result",
167+
toolCallResponseChunks: [
168+
"Chunk 1",
169+
" Chunk 2",
170+
" Chunk 3"
171+
]
172+
}
173+
```
174+
175+
### 文件结构
176+
177+
- `src/data/mockData.ts`: 包含所有测试用例数据
178+
- `src/services/openaiService.ts`: 处理 API 请求逻辑
179+
- `src/types/index.ts`: TypeScript 类型定义
180+
- `src/cli.ts`: 命令行接口
181+
182+
可用模型: mock-gpt-thinking, mock-gpt-thinking-tag, mock-gpt-function, mock-gpt-markdown, gpt-4o-image

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@
3939
"cli": "ts-node src/cli.ts"
4040
},
4141
"dependencies": {
42-
"express": "^4.18.2",
43-
"cors": "^2.8.5"
42+
"commander": "^14.0.0",
43+
"cors": "^2.8.5",
44+
"express": "^4.18.2"
4445
},
4546
"devDependencies": {
46-
"@types/express": "^4.17.21",
47+
"@types/commander": "^2.12.0",
4748
"@types/cors": "^2.8.17",
49+
"@types/express": "^4.17.21",
4850
"@types/node": "^20.0.0",
49-
"typescript": "^5.0.0",
50-
"ts-node": "^10.9.0"
51+
"ts-node": "^10.9.0",
52+
"typescript": "^5.0.0"
5153
}
5254
}

src/app.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@ import express from 'express';
22
import cors from 'cors';
33
import routes from './routes';
44

5+
// 扩展全局对象类型
6+
declare global {
7+
var verboseLogging: boolean;
8+
}
9+
510
const app = express();
611

712
// Middleware
813
app.use(cors());
914
app.use(express.json({ limit: '10mb' }));
1015
app.use(express.urlencoded({ extended: true }));
1116

12-
// Request logging middleware
17+
// Request logging middleware (conditional)
1318
app.use((req, res, next) => {
14-
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
19+
if (global.verboseLogging) {
20+
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
21+
if (req.body && Object.keys(req.body).length > 0) {
22+
console.log('Request body:', JSON.stringify(req.body, null, 2));
23+
}
24+
}
1525
next();
1626
});
1727

src/cli.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,56 @@
11
#!/usr/bin/env node
22

3-
import './index';
3+
import { Command } from 'commander';
4+
import app from './app';
5+
6+
// 扩展全局对象类型
7+
declare global {
8+
var verboseLogging: boolean;
9+
}
10+
11+
const program = new Command();
12+
13+
program
14+
.name('mock-openai-api')
15+
.description('Mock OpenAI Compatible Provider API server')
16+
.version('1.0.0')
17+
.option('-p, --port <number>', 'Server port', '3000')
18+
.option('-H, --host <address>', 'Server host address', '0.0.0.0')
19+
.option('-v, --verbose', 'Enable request logging to console', false)
20+
.parse();
21+
22+
const options = program.opts();
23+
24+
const PORT = parseInt(options.port) || 3000;
25+
const HOST = options.host || '0.0.0.0';
26+
27+
// 设置全局变量控制日志输出
28+
global.verboseLogging = options.verbose;
29+
30+
app.listen(PORT, HOST, () => {
31+
console.log(`🚀 Mock OpenAI API server started successfully!`);
32+
console.log(`📍 Server address: http://${HOST}:${PORT}`);
33+
console.log(`📖 API Documentation:`);
34+
console.log(` • GET /health - Health check`);
35+
console.log(` • GET /v1/models - Get model list`);
36+
console.log(` • POST /v1/chat/completions - Chat completions`);
37+
console.log(` • POST /v1/images/generations - Image generation`);
38+
console.log(`\n✨ Available models:`);
39+
console.log(` - mock-gpt-thinking: Model supporting thought process`);
40+
console.log(` - mock-gpt-function: Model supporting function calls`);
41+
console.log(` - mock-gpt-markdown: Model outputting standard Markdown`);
42+
console.log(` - gpt-4o-image: Model specifically for image generation`);
43+
console.log(`\n🔗 Usage example:`);
44+
console.log(` curl -X POST http://localhost:${PORT}/v1/chat/completions \\`);
45+
console.log(` -H "Content-Type: application/json" \\`);
46+
console.log(` -d '{`);
47+
console.log(` "model": "mock-gpt-function",`);
48+
console.log(` "messages": [{"role": "user", "content": "Hello"}]`);
49+
console.log(` }'`);
50+
51+
if (options.verbose) {
52+
console.log(`\n📝 Request logging: ENABLED`);
53+
} else {
54+
console.log(`\n📝 Request logging: DISABLED (use -v to enable)`);
55+
}
56+
});

0 commit comments

Comments
 (0)