-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
333 lines (284 loc) · 8.71 KB
/
Copy pathparser.ts
File metadata and controls
333 lines (284 loc) · 8.71 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
import { readFileSync, writeFileSync } from "fs";
interface Message {
role: string;
content: string;
tool_calls?: string;
reasoning?: string;
}
interface CurrentFormat {
messages: Message[];
meta?: string;
available_tools?: string;
}
interface Part {
text?: string;
fileData?: {
mimeType: string;
fileUri: string;
};
functionCall?: {
name: string;
args: Record<string, any>;
};
functionResponse?: {
name: string;
response: Record<string, any>;
};
}
interface Content {
role: string;
parts: Part[];
}
interface FunctionDeclaration {
name: string;
description?: string;
parameters?: any; // OpenAPI Schema
response?: any; // OpenAPI Schema
}
interface Tool {
functionDeclarations: FunctionDeclaration[];
}
interface GeminiFormat {
system_instruction?: {
parts: Part[];
};
contents: Content[];
tools?: Tool[];
}
// Helper function to convert function names to valid format (snake_case, no spaces)
function normalizeToolName(name: string): string {
return name
.toLowerCase()
.replace(/[^a-z0-9_.-]/g, "_")
.replace(/_+/g, "_")
.replace(/^[^a-z_]+/, "")
.substring(0, 64);
}
function normalizeSchemaType(type: any): string {
// Normalize to standard types then uppercase for Vertex AI
if (!type || typeof type !== "string") {
return "STRING"; // Default fallback
}
let normalized = type.toLowerCase();
if (normalized === "dict") normalized = "object";
else if (normalized === "int" || normalized === "float")
normalized = "number";
else if (normalized === "str") normalized = "string";
else if (normalized === "bool") normalized = "boolean";
return normalized.toUpperCase();
}
function convertToolsToFunctionDeclarations(
availableTools: string
): FunctionDeclaration[] {
try {
const tools = JSON.parse(availableTools);
return tools.map((tool: any) => {
const declaration: FunctionDeclaration = {
name: normalizeToolName(tool.name),
};
if (tool.description) {
declaration.description = tool.description;
}
if (tool.parameters) {
// Convert the parameters to Vertex AI Schema format
const params = tool.parameters;
// Build the schema
const schema: any = {
type: normalizeSchemaType(params.type || "object"),
};
if (params.properties) {
// Convert each property to proper Vertex format
const convertedProperties: any = {};
for (const [key, prop] of Object.entries(params.properties)) {
const propValue: any = prop;
const convertedProp: any = {};
// Map type field to UPPERCASE Vertex format
if (propValue.type) {
convertedProp.type = normalizeSchemaType(propValue.type);
}
if (propValue.description) {
convertedProp.description = propValue.description;
}
// NOTE: Remove 'default' field - not supported in Vertex AI Schema
// if (propValue.default !== undefined) {
// convertedProp.default = propValue.default;
// }
if (propValue.properties) {
convertedProp.properties = propValue.properties;
}
convertedProperties[key] = convertedProp;
}
schema.properties = convertedProperties;
}
if (params.required && Array.isArray(params.required)) {
schema.required = params.required;
}
declaration.parameters = schema;
}
return declaration;
});
} catch (error) {
console.error("Error parsing available_tools:", error);
return [];
}
}
function convertToGeminiFormat(input: CurrentFormat): GeminiFormat {
const result: GeminiFormat = {
contents: [],
};
// Add tools/function declarations if available
if (input.available_tools) {
const functionDeclarations = convertToolsToFunctionDeclarations(
input.available_tools
);
if (functionDeclarations.length > 0) {
result.tools = [
{
functionDeclarations,
},
];
}
}
for (const message of input.messages) {
// Handle system message
if (message.role === "system") {
result.system_instruction = {
parts: [
{
text: message.content,
},
],
};
continue;
}
// Convert role names to Gemini format
let geminiRole = message.role;
if (message.role === "assistant") {
geminiRole = "model";
} else if (message.role === "tool") {
// Tool responses should be "user" role in Gemini
geminiRole = "user";
}
// Build parts array
const parts: Part[] = [];
// Handle tool/function responses differently
if (message.role === "tool" && message.content) {
// Tool responses need a function name - try to extract from previous context
// The content could be an ACK or actual response data
let functionName = "unknown";
let responseContent: any = {};
// Try to parse as structured JSON response
try {
const responseData = JSON.parse(message.content);
if (responseData.id) {
functionName = responseData.id;
responseContent = responseData;
} else {
// Plain JSON but no id, use as response
responseContent = responseData;
}
} catch {
// Not JSON, treat as plain text in response
// Need to find the function name from previous tool call
// Look back in the messages to find the last tool call
for (let i = input.messages.indexOf(message) - 1; i >= 0; i--) {
const prevMsg = input.messages[i];
if (prevMsg.tool_calls) {
try {
const toolCalls = JSON.parse(prevMsg.tool_calls);
if (toolCalls.length > 0) {
functionName = toolCalls[0].name;
break;
}
} catch {}
}
}
// Structure the response properly
// Check if it's an ACK or actual data
if (message.content.includes("<tool_ack")) {
// ACK - create minimal response
responseContent = { status: "acknowledged" };
} else {
// Regular text response
responseContent = { result: message.content };
}
}
// Normalize the function name to match declaration format
const normalizedName = normalizeToolName(functionName);
parts.push({
functionResponse: {
name: normalizedName,
response: responseContent,
},
});
} else if (message.content) {
// Add text content for non-tool messages
// NOTE: Remove reasoning text as per Vertex best practices
parts.push({
text: message.content,
});
}
// Parse and add tool_calls as separate functionCall parts
if (message.tool_calls) {
try {
const toolCalls = JSON.parse(message.tool_calls);
for (const toolCall of toolCalls) {
parts.push({
functionCall: {
name: normalizeToolName(toolCall.name),
args: toolCall.arguments || {},
},
});
}
} catch (error) {
console.error("Error parsing tool_calls:", error);
// Fallback: add as text if parsing fails
parts.push({
text: `Tool Calls: ${message.tool_calls}`,
});
}
}
// For functionResponse parts, don't include role (Vertex format)
if (message.role === "tool") {
result.contents.push({
parts: parts,
} as any);
} else {
result.contents.push({
role: geminiRole,
parts: parts,
});
}
}
return result;
}
function main() {
const inputFile = "./train-qwen.jsonl";
const outputFile = "./train-gemini.jsonl";
console.log(`Reading from ${inputFile}...`);
const fileContent = readFileSync(inputFile, "utf-8");
const lines = fileContent.trim().split("\n");
console.log(`Found ${lines.length} lines to process`);
const outputLines: string[] = [];
for (let i = 0; i < lines.length; i++) {
try {
const line = lines[i].trim();
if (!line) continue;
const input: CurrentFormat = JSON.parse(line);
const output = convertToGeminiFormat(input);
outputLines.push(JSON.stringify(output));
if ((i + 1) % 100 === 0) {
console.log(`Processed ${i + 1} lines...`);
}
} catch (error) {
console.error(`Error processing line ${i + 1}:`, error);
}
}
console.log(
`Writing ${outputLines.length} converted lines to ${outputFile}...`
);
writeFileSync(outputFile, outputLines.join("\n") + "\n", "utf-8");
console.log("✓ Conversion complete!");
console.log(`Output saved to: ${outputFile}`);
}
main();