Skip to content

Commit f74f0d8

Browse files
committed
Improve formatter
1 parent 6c2c2e7 commit f74f0d8

2 files changed

Lines changed: 57 additions & 27 deletions

File tree

packages/torque/src/formatter.test.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@ import type { IDatasetRow } from "./types";
55
describe("ChatTemplateFormatter", () => {
66
const formatter = new ChatTemplateFormatter();
77

8-
it("should transform tools to OpenAI format", () => {
9-
const row: IDatasetRow = {
8+
const createMockRow = (
9+
overrides: Partial<IDatasetRow> = {}
10+
): IDatasetRow => ({
11+
messages: [],
12+
tools: [],
13+
schema: {
14+
metadata: {},
1015
messages: [],
16+
tools: [],
17+
},
18+
meta: {},
19+
...overrides,
20+
});
21+
22+
it("should transform tools to OpenAI format", () => {
23+
const row = createMockRow({
1124
tools: [
1225
{
1326
name: "calculator",
@@ -20,9 +33,7 @@ describe("ChatTemplateFormatter", () => {
2033
output: {},
2134
},
2235
],
23-
schema: {} as any,
24-
meta: {} as any,
25-
};
36+
});
2637

2738
const result = formatter.format(row);
2839
expect(result.tools).toHaveLength(1);
@@ -41,18 +52,15 @@ describe("ChatTemplateFormatter", () => {
4152
});
4253

4354
it("should transform user messages", () => {
44-
const row: IDatasetRow = {
55+
const row = createMockRow({
4556
messages: [
4657
{
4758
role: "user",
4859
content: "Hello",
4960
generationId: "1",
5061
},
5162
],
52-
tools: [],
53-
schema: {} as any,
54-
meta: {} as any,
55-
};
63+
});
5664

5765
const result = formatter.format(row);
5866
expect(result.messages).toHaveLength(1);
@@ -63,7 +71,7 @@ describe("ChatTemplateFormatter", () => {
6371
});
6472

6573
it("should transform assistant messages with tool calls", () => {
66-
const row: IDatasetRow = {
74+
const row = createMockRow({
6775
messages: [
6876
{
6977
role: "assistant",
@@ -77,12 +85,9 @@ describe("ChatTemplateFormatter", () => {
7785
},
7886
],
7987
generationId: "1",
80-
} as any, // Casting because IDatasetMessage content type is strict in tests
88+
},
8189
],
82-
tools: [],
83-
schema: {} as any,
84-
meta: {} as any,
85-
};
90+
});
8691

8792
const result = formatter.format(row);
8893
expect(result.messages).toHaveLength(1);
@@ -102,8 +107,31 @@ describe("ChatTemplateFormatter", () => {
102107
});
103108
});
104109

110+
it("should transform assistant messages with reasoning", () => {
111+
const row = createMockRow({
112+
messages: [
113+
{
114+
role: "assistant",
115+
content: [
116+
{ type: "reasoning", text: "I should check the weather." },
117+
{ type: "text", text: "Checking weather..." },
118+
],
119+
generationId: "1",
120+
},
121+
],
122+
});
123+
124+
const result = formatter.format(row);
125+
expect(result.messages).toHaveLength(1);
126+
expect(result.messages[0]).toEqual({
127+
role: "assistant",
128+
content: "Checking weather...",
129+
reasoning: "I should check the weather.",
130+
});
131+
});
132+
105133
it("should flatten tool result messages", () => {
106-
const row: IDatasetRow = {
134+
const row = createMockRow({
107135
messages: [
108136
{
109137
role: "tool",
@@ -112,24 +140,19 @@ describe("ChatTemplateFormatter", () => {
112140
type: "tool-result",
113141
toolCallId: "call_1",
114142
toolName: "calc",
115-
result: 2,
116-
output: 2, // dataset.ts populates output
143+
output: { type: "text", value: "2" }, // dataset.ts populates output
117144
},
118145
{
119146
type: "tool-result",
120147
toolCallId: "call_2",
121148
toolName: "calc",
122-
result: 4,
123-
output: 4,
149+
output: { type: "text", value: "4" },
124150
},
125151
],
126152
generationId: "1",
127-
} as any,
153+
},
128154
],
129-
tools: [],
130-
schema: {} as any,
131-
meta: {} as any,
132-
};
155+
});
133156

134157
const result = formatter.format(row);
135158
expect(result.messages).toHaveLength(2);

packages/torque/src/formatter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class ChatTemplateFormatter implements IDatasetFormatter {
5858
if (msg.role === "assistant") {
5959
const toolCalls: any[] = [];
6060
let contentString = "";
61+
let reasoningString = "";
6162

6263
if (Array.isArray(msg.content)) {
6364
for (const part of msg.content) {
@@ -72,8 +73,11 @@ export class ChatTemplateFormatter implements IDatasetFormatter {
7273
});
7374
} else if (part.type === "text") {
7475
contentString += part.text;
76+
} else if (part.type === "reasoning") {
77+
// ai sdk types might vary but usually it's text or reasoning
78+
reasoningString +=
79+
(part as any).text || (part as any).reasoning || "";
7580
}
76-
// Skip reasoning for chat_template
7781
}
7882
} else if (typeof msg.content === "string") {
7983
contentString = msg.content;
@@ -86,6 +90,9 @@ export class ChatTemplateFormatter implements IDatasetFormatter {
8690
if (toolCalls.length > 0) {
8791
newMsg.tool_calls = toolCalls;
8892
}
93+
if (reasoningString) {
94+
newMsg.reasoning = reasoningString;
95+
}
8996
return [newMsg];
9097
}
9198

0 commit comments

Comments
 (0)