Repository navigation
Expand file tree
/
Copy pathtest-client.js
More file actions
171 lines (157 loc) · 4.75 KB
/
Copy pathtest-client.js
File metadata and controls
171 lines (157 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Test client for the mock OpenAI API
const API_BASE = 'http://localhost:3333/v1';
async function testChatCompletion(messages, tools = null) {
const body = {
model: 'gpt-4-turbo-preview',
messages: messages
};
if (tools) {
body.tools = tools;
}
const response = await fetch(`${API_BASE}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
return await response.json();
}
// Define some example tools
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather in a given location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g. San Francisco, CA'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit']
}
},
required: ['location']
}
}
},
{
type: 'function',
function: {
name: 'send_email',
description: 'Send an email to a recipient',
parameters: {
type: 'object',
properties: {
to: {
type: 'string',
description: 'Email address of recipient'
},
subject: {
type: 'string',
description: 'Email subject'
},
body: {
type: 'string',
description: 'Email body content'
}
},
required: ['to', 'subject', 'body']
}
}
},
{
type: 'function',
function: {
name: 'calculate',
description: 'Evaluate a mathematical expression',
parameters: {
type: 'object',
properties: {
expression: {
type: 'string',
description: 'Mathematical expression to evaluate'
}
},
required: ['expression']
}
}
},
{
type: 'function',
function: {
name: 'get_time',
description: 'Get current time in a timezone',
parameters: {
type: 'object',
properties: {
timezone: {
type: 'string',
description: 'Timezone name'
}
},
required: ['timezone']
}
}
}
];
async function runTests() {
console.log('Testing Mock OpenAI API\n');
console.log('='.repeat(60));
// Test 1: Weather query (should trigger function call)
console.log('\n1. Testing weather query...');
const weatherResponse = await testChatCompletion([
{ role: 'user', content: 'What is the weather in San Francisco?' }
], tools);
console.log('Response:', JSON.stringify(weatherResponse.choices[0].message, null, 2));
// Test 2: Email request (should trigger function call)
console.log('\n2. Testing email request...');
const emailResponse = await testChatCompletion([
{ role: 'user', content: 'Please send email to john@example.com' }
], tools);
console.log('Response:', JSON.stringify(emailResponse.choices[0].message, null, 2));
// Test 3: Calculate request
console.log('\n3. Testing calculate request...');
const calcResponse = await testChatCompletion([
{ role: 'user', content: 'Can you calculate 5 * 8?' }
], tools);
console.log('Response:', JSON.stringify(calcResponse.choices[0].message, null, 2));
// Test 4: Multiple tools
console.log('\n4. Testing multiple tools request...');
const multiResponse = await testChatCompletion([
{ role: 'user', content: 'Get me multiple tools please' }
], tools);
console.log('Response:', JSON.stringify(multiResponse.choices[0].message, null, 2));
// Test 5: Default response (no function call)
console.log('\n5. Testing default text response...');
const defaultResponse = await testChatCompletion([
{ role: 'user', content: 'Hello, how are you?' }
], tools);
console.log('Response:', JSON.stringify(defaultResponse.choices[0].message, null, 2));
// Test 6: Conversation with function result
console.log('\n6. Testing conversation with function result...');
const conversationResponse = await testChatCompletion([
{ role: 'user', content: 'What is the weather?' },
{ role: 'assistant', content: null, tool_calls: [
{
id: 'call_123',
type: 'function',
function: {
name: 'get_weather',
arguments: JSON.stringify({ location: 'San Francisco', unit: 'celsius' })
}
}
]},
{ role: 'tool', tool_call_id: 'call_123', content: JSON.stringify({ temperature: 22, condition: 'sunny' }) }
], tools);
console.log('Response:', JSON.stringify(conversationResponse.choices[0].message, null, 2));
console.log('\n' + '='.repeat(60));
console.log('\nAll tests completed!');
}
// Run tests
runTests().catch(console.error);