|
| 1 | +import type { IDatasetRow, DatasetExportFormat } from "./types"; |
| 2 | + |
| 3 | +export interface IDatasetFormatter { |
| 4 | + format(row: IDatasetRow): Record<string, any>; |
| 5 | + parquetSchema: Record<string, any>; |
| 6 | +} |
| 7 | + |
| 8 | +export class AiSdkFormatter implements IDatasetFormatter { |
| 9 | + parquetSchema = { |
| 10 | + messages: { type: "UTF8" }, // JSON string |
| 11 | + tools: { type: "UTF8" }, // JSON string |
| 12 | + schema: { type: "UTF8" }, // JSON string |
| 13 | + meta: { type: "UTF8" }, // JSON string |
| 14 | + }; |
| 15 | + |
| 16 | + format(row: IDatasetRow): Record<string, any> { |
| 17 | + return row as unknown as Record<string, any>; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export class ChatTemplateFormatter implements IDatasetFormatter { |
| 22 | + parquetSchema = { |
| 23 | + tools: { type: "UTF8" }, // JSON string |
| 24 | + messages: { type: "UTF8" }, // JSON string |
| 25 | + }; |
| 26 | + |
| 27 | + format(row: IDatasetRow): Record<string, any> { |
| 28 | + const tools = row.tools.map((tool) => ({ |
| 29 | + type: "function", |
| 30 | + function: { |
| 31 | + name: tool.name, |
| 32 | + description: tool.description, |
| 33 | + parameters: tool.parameters, |
| 34 | + }, |
| 35 | + })); |
| 36 | + |
| 37 | + const messages = row.messages.flatMap((msg) => { |
| 38 | + if (msg.role === "tool") { |
| 39 | + // Flatten tool results |
| 40 | + if (Array.isArray(msg.content)) { |
| 41 | + return msg.content |
| 42 | + .map((part: any) => { |
| 43 | + if (part.type === "tool-result") { |
| 44 | + return { |
| 45 | + role: "tool", |
| 46 | + tool_call_id: part.toolCallId, |
| 47 | + name: part.toolName, |
| 48 | + content: JSON.stringify(part.output), |
| 49 | + }; |
| 50 | + } |
| 51 | + return null; |
| 52 | + }) |
| 53 | + .filter(Boolean); |
| 54 | + } |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + if (msg.role === "assistant") { |
| 59 | + const toolCalls: any[] = []; |
| 60 | + let contentString = ""; |
| 61 | + |
| 62 | + if (Array.isArray(msg.content)) { |
| 63 | + for (const part of msg.content) { |
| 64 | + if (part.type === "tool-call") { |
| 65 | + toolCalls.push({ |
| 66 | + id: part.toolCallId, |
| 67 | + type: "function", |
| 68 | + function: { |
| 69 | + name: part.toolName, |
| 70 | + arguments: part.input, |
| 71 | + }, |
| 72 | + }); |
| 73 | + } else if (part.type === "text") { |
| 74 | + contentString += part.text; |
| 75 | + } |
| 76 | + // Skip reasoning for chat_template |
| 77 | + } |
| 78 | + } else if (typeof msg.content === "string") { |
| 79 | + contentString = msg.content; |
| 80 | + } |
| 81 | + |
| 82 | + const newMsg: any = { |
| 83 | + role: "assistant", |
| 84 | + content: contentString || null, |
| 85 | + }; |
| 86 | + if (toolCalls.length > 0) { |
| 87 | + newMsg.tool_calls = toolCalls; |
| 88 | + } |
| 89 | + return [newMsg]; |
| 90 | + } |
| 91 | + |
| 92 | + // User / System |
| 93 | + let content = msg.content; |
| 94 | + if (Array.isArray(content)) { |
| 95 | + // Ensure content parts are compatible |
| 96 | + // OpenAI accepts array of text/image parts. |
| 97 | + // We'll assume they are compatible or simplify if needed. |
| 98 | + // For now, pass through. |
| 99 | + } |
| 100 | + |
| 101 | + return [ |
| 102 | + { |
| 103 | + role: msg.role, |
| 104 | + content, |
| 105 | + }, |
| 106 | + ]; |
| 107 | + }); |
| 108 | + |
| 109 | + return { tools, messages }; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export function createFormatter( |
| 114 | + format: DatasetExportFormat |
| 115 | +): IDatasetFormatter { |
| 116 | + switch (format) { |
| 117 | + case "ai-sdk": |
| 118 | + return new AiSdkFormatter(); |
| 119 | + case "chat_template": |
| 120 | + return new ChatTemplateFormatter(); |
| 121 | + default: |
| 122 | + throw new Error(`Unsupported export format: ${format}`); |
| 123 | + } |
| 124 | +} |
0 commit comments