-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathstructChat.test.ts
More file actions
335 lines (310 loc) · 9.92 KB
/
Copy pathstructChat.test.ts
File metadata and controls
335 lines (310 loc) · 9.92 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import {
transformToChatCompletionRequest,
convertToParsedChatCompletionResponse,
responseFormatFromZodObject,
ParsedChatCompletionRequest,
ParsedChatCompletionResponse,
} from "../../src/extra/structChat.js";
import { ResponseFormat } from "../../src/models/components/responseformat.js";
import { z } from "zod";
import * as components from "../../src/models/components/index.js";
const Explanation = z.object({
explanation: z.string(),
output: z.string(),
});
const MathDemonstration = z.object({
steps: z.array(Explanation),
final_answer: z.string(),
});
const raw_request: ParsedChatCompletionRequest<typeof MathDemonstration> = {
model: "mistral-tiny-latest",
messages: [
{
role: "system",
content:
"You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.",
},
{ role: "user", content: "How can I solve 8x + 7 = -23" },
],
responseFormat: MathDemonstration,
};
const transformedResponseFormat: ResponseFormat = {
type: "json_schema",
jsonSchema: {
name: "placeholderName",
schemaDefinition: {
type: "object",
properties: {
steps: {
type: "array",
items: {
type: "object",
properties: {
explanation: { type: "string" },
output: { type: "string" },
},
required: ["explanation", "output"],
additionalProperties: false,
},
},
final_answer: { type: "string" },
},
required: ["steps", "final_answer"],
additionalProperties: false,
$schema: "http://json-schema.org/draft-07/schema#",
},
strict: true,
},
};
const ccr_request: components.ChatCompletionRequest = {
model: "mistral-tiny-latest",
messages: [
{
role: "system",
content:
"You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.",
},
{ role: "user", content: "How can I solve 8x + 7 = -23" },
],
responseFormat: transformedResponseFormat,
};
const raw_response: components.ChatCompletionResponse = {
id: "df9c5af85d0a4474b2e8392ee52dd883",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 71, completionTokens: 140, totalTokens: 211 },
created: 1737982851,
choices: [
{
index: 0,
message: {
content: `
{
"steps": [
{
"explanation": "Subtract 7 from both sides to isolate the term with x.",
"output": "8x + 7 - 7 = -23 - 7"
},
{
"explanation": "Simplify both sides to get the equation in standard form.",
"output": "8x = -30"
},
{
"explanation": "Divide both sides by 8 to solve for x.",
"output": "8x / 8 = -30 / 8"
}
],
"final_answer": "-3.75"
}
`,
toolCalls: null,
prefix: false,
role: "assistant",
},
finishReason: "stop",
},
],
};
const ccr_response: ParsedChatCompletionResponse<typeof MathDemonstration> = {
id: "df9c5af85d0a4474b2e8392ee52dd883",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 71, completionTokens: 140, totalTokens: 211 },
created: 1737982851,
choices: [
{
index: 0,
message: {
content: `
{
"steps": [
{
"explanation": "Subtract 7 from both sides to isolate the term with x.",
"output": "8x + 7 - 7 = -23 - 7"
},
{
"explanation": "Simplify both sides to get the equation in standard form.",
"output": "8x = -30"
},
{
"explanation": "Divide both sides by 8 to solve for x.",
"output": "8x / 8 = -30 / 8"
}
],
"final_answer": "-3.75"
}
`,
toolCalls: null,
prefix: false,
role: "assistant",
parsed: {
steps: [
{
explanation:
"Subtract 7 from both sides to isolate the term with x.",
output: "8x + 7 - 7 = -23 - 7",
},
{
explanation:
"Simplify both sides to get the equation in standard form.",
output: "8x = -30",
},
{
explanation: "Divide both sides by 8 to solve for x.",
output: "8x / 8 = -30 / 8",
},
],
final_answer: "-3.75",
},
},
finishReason: "stop",
},
],
};
describe("transformToChatCompletionRequest", () => {
it("should return a valid ChatCompletionRequest", () => {
const result = transformToChatCompletionRequest(raw_request);
const expected = structuredClone(ccr_request);
// $schema URL differs between Zod 3 (draft-07) and Zod 4 (draft/2020-12)
const resultSchema = (result.responseFormat as any)?.jsonSchema?.schemaDefinition;
const expectedSchema = (expected.responseFormat as any)?.jsonSchema?.schemaDefinition;
if (resultSchema) delete resultSchema.$schema;
if (expectedSchema) delete expectedSchema.$schema;
expect(result).toStrictEqual(expected);
});
});
describe("convertToParsedChatCompletionResponse", () => {
it("should return a valid ParsedChatCompletionResponse", () => {
expect(
convertToParsedChatCompletionResponse(raw_response, MathDemonstration)
).toStrictEqual(ccr_response);
});
it("should set parsed to undefined when content is malformed JSON", () => {
const truncatedResponse: components.ChatCompletionResponse = {
id: "test-malformed",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
created: 1700000000,
choices: [
{
index: 0,
message: {
content: '{"steps": [{"explanation": "truncated',
toolCalls: null,
prefix: false,
role: "assistant",
},
finishReason: "length",
},
],
};
const result = convertToParsedChatCompletionResponse(
truncatedResponse,
MathDemonstration,
);
expect(result.choices).toHaveLength(1);
expect(result.choices![0].message).toBeDefined();
expect(result.choices![0].message!.parsed).toBeUndefined();
expect(result.choices![0].message!.content).toBe(
'{"steps": [{"explanation": "truncated',
);
});
it("should set parsed to undefined when content is valid JSON but fails schema validation", () => {
const wrongShapeResponse: components.ChatCompletionResponse = {
id: "test-wrong-shape",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
created: 1700000000,
choices: [
{
index: 0,
message: {
content: '{"wrong_field": "not matching schema"}',
toolCalls: null,
prefix: false,
role: "assistant",
},
finishReason: "stop",
},
],
};
const result = convertToParsedChatCompletionResponse(
wrongShapeResponse,
MathDemonstration,
);
expect(result.choices).toHaveLength(1);
expect(result.choices![0].message).toBeDefined();
expect(result.choices![0].message!.parsed).toBeUndefined();
});
it("should preserve choices when content is null", () => {
const nullContentResponse: components.ChatCompletionResponse = {
id: "test-null-content",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
created: 1700000000,
choices: [
{
index: 0,
message: {
content: null,
toolCalls: null,
prefix: false,
role: "assistant",
},
finishReason: "stop",
},
],
};
const result = convertToParsedChatCompletionResponse(
nullContentResponse,
MathDemonstration,
);
expect(result.choices).toHaveLength(1);
expect(result.choices![0].message).toBeDefined();
expect(result.choices![0].message!.parsed).toBeUndefined();
});
it("should return empty choices array for empty choices", () => {
const emptyChoicesResponse: components.ChatCompletionResponse = {
id: "test-empty",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 10, completionTokens: 0, totalTokens: 10 },
created: 1700000000,
choices: [],
};
const result = convertToParsedChatCompletionResponse(
emptyChoicesResponse,
MathDemonstration,
);
expect(result.choices).toEqual([]);
});
it("should return undefined choices when choices is undefined", () => {
const noChoicesResponse: components.ChatCompletionResponse = {
id: "test-undefined",
object: "chat.completion",
model: "mistral-tiny-latest",
usage: { promptTokens: 10, completionTokens: 0, totalTokens: 10 },
created: 1700000000,
};
const result = convertToParsedChatCompletionResponse(
noChoicesResponse,
MathDemonstration,
);
expect(result.choices).toBeUndefined();
});
});
describe("responseFormatFromZodObject", () => {
it("should return a valid response format", () => {
const result = responseFormatFromZodObject(MathDemonstration);
const expected = structuredClone(transformedResponseFormat);
// $schema URL differs between Zod 3 (draft-07) and Zod 4 (draft/2020-12)
const resultSchema = (result as any)?.jsonSchema?.schemaDefinition;
const expectedSchema = (expected as any)?.jsonSchema?.schemaDefinition;
if (resultSchema) delete resultSchema.$schema;
if (expectedSchema) delete expectedSchema.$schema;
expect(result).toStrictEqual(expected);
});
});