Skip to content

Commit 740e088

Browse files
committed
fix: tool_call work now
1 parent b208619 commit 740e088

File tree

9 files changed

+395
-62
lines changed

9 files changed

+395
-62
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ curl -X POST http://localhost:3000/v1/images/generations \
103103

104104
Response will include `<thinking>` tags showing the reasoning process.
105105

106-
### 2. mock-gpt-function
106+
### 2. gpt-4-mock
107107
**Function Calling Model** - Supports tools and function calling
108108

109109
```json
110110
{
111-
"model": "mock-gpt-function",
111+
"model": "gpt-4-mock",
112112
"messages": [{"role": "user", "content": "What's the weather like in Beijing today?"}],
113113
"functions": [
114114
{
@@ -264,7 +264,7 @@ curl -X POST http://localhost:3000/v1/chat/completions \
264264
curl -X POST http://localhost:3000/v1/chat/completions \
265265
-H "Content-Type: application/json" \
266266
-d '{
267-
"model": "mock-gpt-function",
267+
"model": "gpt-4-mock",
268268
"messages": [{"role": "user", "content": "What time is it now?"}]
269269
}'
270270

README.zh.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ curl -X POST http://localhost:3000/v1/images/generations \
103103

104104
响应会包含 `<thinking>` 标签显示思考过程。
105105

106-
### 2. mock-gpt-function
106+
### 2. gpt-4-mock
107107
**函数调用模型** - 支持工具和函数调用
108108

109109
```json
110110
{
111-
"model": "mock-gpt-function",
111+
"model": "gpt-4-mock",
112112
"messages": [{"role": "user", "content": "今天北京的天气怎么样?"}],
113113
"functions": [
114114
{
@@ -264,7 +264,7 @@ curl -X POST http://localhost:3000/v1/chat/completions \
264264
curl -X POST http://localhost:3000/v1/chat/completions \
265265
-H "Content-Type: application/json" \
266266
-d '{
267-
"model": "mock-gpt-function",
267+
"model": "gpt-4-mock",
268268
"messages": [{"role": "user", "content": "现在几点了?"}]
269269
}'
270270

TOOL_CALLS_README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
本项目支持两种函数调用格式:
66

7-
- `mock-gpt-function`: 支持新版OpenAI tool calls格式的模型
7+
- `gpt-4-mock`: 支持新版OpenAI tool calls格式的模型
88

99
## 模型特点
1010

11-
### mock-gpt-function
11+
### gpt-4-mock
1212
- 使用新版 tool calls 格式
1313
- 支持两阶段调用流程
1414
- 第一阶段返回 tool_calls,第二阶段返回执行结果
@@ -39,27 +39,27 @@ npx mock-openai-api --help
3939

4040
## 使用示例
4141

42-
### 基本调用 - mock-gpt-function
42+
### 基本调用 - gpt-4-mock
4343

4444
```bash
4545
curl -X POST http://localhost:3000/v1/chat/completions \
4646
-H "Content-Type: application/json" \
4747
-d '{
48-
"model": "mock-gpt-function",
48+
"model": "gpt-4-mock",
4949
"messages": [
5050
{"role": "user", "content": "What time is it now?"}
5151
],
5252
"stream": false
5353
}'
5454
```
5555

56-
### 流式调用 - mock-gpt-function
56+
### 流式调用 - gpt-4-mock
5757

5858
```bash
5959
curl -X POST http://localhost:3000/v1/chat/completions \
6060
-H "Content-Type: application/json" \
6161
-d '{
62-
"model": "mock-gpt-function",
62+
"model": "gpt-4-mock",
6363
"messages": [
6464
{"role": "user", "content": "What time is it now?"}
6565
],
@@ -77,7 +77,7 @@ npx mock-openai-api -v -p 8080
7777
curl -X POST http://localhost:8080/v1/chat/completions \
7878
-H "Content-Type: application/json" \
7979
-d '{
80-
"model": "mock-gpt-function",
80+
"model": "gpt-4-mock",
8181
"messages": [
8282
{"role": "user", "content": "Calculate 123 * 456"}
8383
],
@@ -87,7 +87,7 @@ curl -X POST http://localhost:8080/v1/chat/completions \
8787

8888
## 支持的测试场景
8989

90-
mock-gpt-function 模型包含以下预设测试场景:
90+
gpt-4-mock 模型包含以下预设测试场景:
9191

9292
1. **获取当前时间**: `"What time is it now?"`
9393
2. **天气查询**: `"What's the weather like in Beijing today?"`
@@ -103,7 +103,7 @@ mock-gpt-function 模型包含以下预设测试场景:
103103
"id": "chatcmpl-123",
104104
"object": "chat.completion",
105105
"created": 1677652288,
106-
"model": "mock-gpt-function",
106+
"model": "gpt-4-mock",
107107
"choices": [{
108108
"index": 0,
109109
"message": {
@@ -130,7 +130,7 @@ mock-gpt-function 模型包含以下预设测试场景:
130130

131131
### 流式响应示例
132132

133-
对于流式调用,mock-gpt-function 会先返回 tool_calls,然后在第二阶段返回执行结果的分块内容。
133+
对于流式调用,gpt-4-mock 会先返回 tool_calls,然后在第二阶段返回执行结果的分块内容。
134134

135135
## 测试文件
136136

@@ -141,7 +141,7 @@ mock-gpt-function 模型包含以下预设测试场景:
141141
npm run dev
142142

143143
# 在浏览器中打开 test-sse-client.html 文件
144-
# 选择 mock-gpt-function 模型进行测试
144+
# 选择 gpt-4-mock 模型进行测试
145145
```
146146

147147
## 开发说明
@@ -179,4 +179,4 @@ npm run dev
179179
- `src/types/index.ts`: TypeScript 类型定义
180180
- `src/cli.ts`: 命令行接口
181181

182-
可用模型: mock-gpt-thinking, mock-gpt-thinking-tag, mock-gpt-function, mock-gpt-markdown, gpt-4o-image
182+
可用模型: mock-gpt-thinking, mock-gpt-thinking-tag, gpt-4-mock, mock-gpt-markdown, gpt-4o-image

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ app.listen(PORT, HOST, () => {
3737
console.log(` • POST /v1/images/generations - Image generation`);
3838
console.log(`\n✨ Available models:`);
3939
console.log(` - mock-gpt-thinking: Model supporting thought process`);
40-
console.log(` - mock-gpt-function: Model supporting function calls`);
40+
console.log(` - gpt-4-mock: Model supporting function calls`);
4141
console.log(` - mock-gpt-markdown: Model outputting standard Markdown`);
4242
console.log(` - gpt-4o-image: Model specifically for image generation`);
4343
console.log(`\n🔗 Usage example:`);
4444
console.log(` curl -X POST http://localhost:${PORT}/v1/chat/completions \\`);
4545
console.log(` -H "Content-Type: application/json" \\`);
4646
console.log(` -d '{`);
47-
console.log(` "model": "mock-gpt-function",`);
47+
console.log(` "model": "gpt-4-mock",`);
4848
console.log(` "messages": [{"role": "user", "content": "Hello"}]`);
4949
console.log(` }'`);
5050

src/controllers/openaiController.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
getModels,
55
createChatCompletion,
66
createChatCompletionStream,
7+
createToolCallResponseStream,
78
generateImage
89
} from '../services/openaiService';
10+
import { findModelById } from '../utils/helpers';
911

1012
/**
1113
* Get model list
@@ -54,6 +56,13 @@ export function handleChatCompletion(req: Request, res: Response) {
5456
});
5557
}
5658

59+
// Check if this is a tool-calls model and if we need to simulate the two-phase process
60+
const model = findModelById(request.model);
61+
const isToolCallModel = model && model.type === 'tool-calls';
62+
63+
// Check if this is a request with tool results (second phase)
64+
const hasToolResults = request.messages.some(msg => msg.role === 'tool');
65+
5766
// Streaming response
5867
if (request.stream) {
5968
res.setHeader('Content-Type', 'text/event-stream');
@@ -65,9 +74,52 @@ export function handleChatCompletion(req: Request, res: Response) {
6574
// Flush headers to ensure client receives headers immediately
6675
res.flushHeaders();
6776

68-
const stream = createChatCompletionStream(request);
69-
for (const chunk of stream) {
70-
res.write(chunk);
77+
if (isToolCallModel && hasToolResults) {
78+
// This is the second phase of tool call - generate final response
79+
const toolMessage = request.messages.find(msg => msg.role === 'tool');
80+
const toolCallResponseStream = createToolCallResponseStream(
81+
request,
82+
toolMessage?.tool_call_id || '',
83+
toolMessage?.content || ''
84+
);
85+
86+
for (const chunk of toolCallResponseStream) {
87+
res.write(chunk);
88+
}
89+
} else if (isToolCallModel && !hasToolResults) {
90+
// This is the first phase of tool call - but for demo purposes,
91+
// we'll simulate both phases automatically with a delay
92+
const firstPhaseStream = createChatCompletionStream(request);
93+
for (const chunk of firstPhaseStream) {
94+
res.write(chunk);
95+
}
96+
97+
// Simulate a delay and automatic second phase
98+
setTimeout(() => {
99+
console.log('Loop iteration 2 for event simulated_event_id');
100+
101+
// Create a simulated second phase request
102+
const secondPhaseRequest = { ...request };
103+
104+
const toolCallResponseStream = createToolCallResponseStream(
105+
secondPhaseRequest,
106+
'call_0_8a90fac8-b281-49a0-bcc9-55d7f4603891',
107+
'simulated tool result'
108+
);
109+
110+
for (const chunk of toolCallResponseStream) {
111+
res.write(chunk);
112+
}
113+
res.end();
114+
}, 1000); // 1 second delay to simulate external tool execution
115+
116+
return; // Don't end the response yet
117+
} else {
118+
// Normal streaming for non-tool-call models
119+
const stream = createChatCompletionStream(request);
120+
for (const chunk of stream) {
121+
res.write(chunk);
122+
}
71123
}
72124
res.end();
73125
} else {

src/data/mockData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export const mockModels: MockModel[] = [
471471
testCases: thinkingTestCases
472472
},
473473
{
474-
id: "mock-gpt-function",
474+
id: "gpt-4-mock",
475475
name: "Mock GPT Function Calling",
476476
description: "Model that supports function calling with OpenAI tool calls format, suitable for testing tool integration and two-phase call workflows",
477477
type: "tool-calls",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ app.listen(PORT, () => {
1515
console.log(` • POST /v1/images/generations - Image generation`);
1616
console.log(`\n✨ Available models:`);
1717
console.log(` - mock-gpt-thinking: Model supporting thought process`);
18-
console.log(` - mock-gpt-function: Model supporting function calls with tool calls format`);
18+
console.log(` - gpt-4-mock: Model supporting function calls with tool calls format`);
1919
console.log(` - mock-gpt-markdown: Model outputting standard Markdown`);
2020
console.log(` - gpt-4o-image: Model specifically for image generation`);
2121
console.log(`\n🔗 Usage example:`);
2222
console.log(` curl -X POST http://localhost:${PORT}/v1/chat/completions \\`);
2323
console.log(` -H "Content-Type: application/json" \\`);
2424
console.log(` -d '{`);
25-
console.log(` "model": "mock-gpt-function",`);
25+
console.log(` "model": "gpt-4-mock",`);
2626
console.log(` "messages": [{"role": "user", "content": "Hello"}]`);
2727
console.log(` }'`);
2828
console.log(`\n💡 Use CLI for more options: npm run build && npx mock-openai-api --help`);

0 commit comments

Comments
 (0)