Datasets:
Download scripts/extract-tools.ts from labqoat/AsyncTool: direct link, hf CLI and curl.
- Browser
- Download file 4.88 kB
-
https://huggingface.co/datasets/labqoat/AsyncTool/resolve/main/scripts/extract-tools.ts
- Command line
-
hf download hf://datasets/labqoat/AsyncTool/scripts/extract-tools.ts
-
curl -L -o extract-tools.ts https://huggingface.co/datasets/labqoat/AsyncTool/resolve/main/scripts/extract-tools.ts
4.88 kB
| import fs from "fs"; | |
| interface Tool { | |
| name: string; | |
| description: string; | |
| parameters: { | |
| type: string; | |
| properties: Record<string, any>; | |
| required?: string[]; | |
| }; | |
| } | |
| interface ConversationEntry { | |
| system: string; | |
| conversations: any[]; | |
| } | |
| // Normalize tool name: convert to PascalCase (camelCase with first letter capitalized) | |
| function normalizeToolName(name: string): string { | |
| // Split by spaces, underscores, dots, hyphens, or other non-word characters | |
| const words = name.split(/[\s._-]+/).filter((word) => word.length > 0); | |
| // Capitalize first letter of each word and lowercase the rest | |
| const pascalCase = words | |
| .map((word) => { | |
| if (word.length === 0) return ""; | |
| return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); | |
| }) | |
| .join(""); | |
| return pascalCase; | |
| } | |
| // Map Python types to OpenAI/JSON Schema types | |
| function normalizeType(type: string): string { | |
| const typeMap: Record<string, string> = { | |
| dict: "object", | |
| int: "integer", | |
| float: "number", | |
| bool: "boolean", | |
| list: "array", | |
| str: "string", | |
| }; | |
| return typeMap[type] || type; | |
| } | |
| // Recursively normalize types in the tool structure | |
| function normalizeTypes(obj: any): any { | |
| if (obj === null || obj === undefined) { | |
| return obj; | |
| } | |
| if (Array.isArray(obj)) { | |
| return obj.map(normalizeTypes); | |
| } | |
| if (typeof obj === "object") { | |
| const normalized: any = {}; | |
| for (const [key, value] of Object.entries(obj)) { | |
| if (key === "type" && typeof value === "string") { | |
| normalized[key] = normalizeType(value); | |
| } else { | |
| normalized[key] = normalizeTypes(value); | |
| } | |
| } | |
| return normalized; | |
| } | |
| return obj; | |
| } | |
| function cleanTool(tool: any): Tool { | |
| const { required, ...rest } = tool; | |
| const cleaned = rest as Tool; | |
| // Normalize tool name | |
| if (cleaned.name) { | |
| cleaned.name = normalizeToolName(cleaned.name); | |
| } | |
| // Normalize types recursively | |
| return normalizeTypes(cleaned) as Tool; | |
| } | |
| function extractToolsFromSystem(systemMessage: string): Tool[] { | |
| const marker = | |
| "Here is a list of functions in JSON format that you can invoke:\n"; | |
| const markerIndex = systemMessage.indexOf(marker); | |
| if (markerIndex === -1) { | |
| return []; | |
| } | |
| const startIndex = markerIndex + marker.length; | |
| const jsonStart = systemMessage.indexOf("[", startIndex); | |
| if (jsonStart === -1) { | |
| return []; | |
| } | |
| // Find the closing bracket before ". \nShould you decide" | |
| const endMarker = "]. \nShould you decide"; | |
| const endIndex = systemMessage.indexOf(endMarker, jsonStart); | |
| if (endIndex === -1) { | |
| // Try alternative ending pattern | |
| const altEndMarker = "]. \nShould"; | |
| const altEndIndex = systemMessage.indexOf(altEndMarker, jsonStart); | |
| if (altEndIndex === -1) { | |
| return []; | |
| } | |
| const jsonString = systemMessage.substring(jsonStart, altEndIndex + 1); | |
| try { | |
| const tools = JSON.parse(jsonString); | |
| return tools.map(cleanTool); | |
| } catch (e) { | |
| console.error("Failed to parse tools JSON:", e); | |
| return []; | |
| } | |
| } | |
| const jsonString = systemMessage.substring(jsonStart, endIndex + 1); | |
| try { | |
| const tools = JSON.parse(jsonString); | |
| return tools.map(cleanTool); | |
| } catch (e) { | |
| console.error("Failed to parse tools JSON:", e); | |
| return []; | |
| } | |
| } | |
| function main() { | |
| const inputFile = process.argv[2] || "tools-ace-dataset.json"; | |
| const outputFile = process.argv[3] || "extracted-tools.json"; | |
| console.log(`Reading ${inputFile}...`); | |
| const fileContent = fs.readFileSync(inputFile, "utf-8"); | |
| const conversations: ConversationEntry[] = JSON.parse(fileContent); | |
| console.log(`Processing ${conversations.length} conversations...`); | |
| const toolsMap = new Map<string, Tool>(); | |
| let processedCount = 0; | |
| let errorCount = 0; | |
| for (const entry of conversations) { | |
| if (!entry.system) { | |
| continue; | |
| } | |
| try { | |
| const tools = extractToolsFromSystem(entry.system); | |
| for (const tool of tools) { | |
| if (tool.name) { | |
| // Normalize name for deduplication | |
| const normalizedName = normalizeToolName(tool.name); | |
| // Deduplicate by normalized name - keep the first occurrence | |
| if (!toolsMap.has(normalizedName)) { | |
| toolsMap.set(normalizedName, tool); | |
| } | |
| } | |
| } | |
| processedCount++; | |
| } catch (e) { | |
| errorCount++; | |
| console.error(`Error processing conversation:`, e); | |
| } | |
| } | |
| const uniqueTools = Array.from(toolsMap.values()); | |
| console.log(`\nResults:`); | |
| console.log(`- Processed conversations: ${processedCount}`); | |
| console.log(`- Errors: ${errorCount}`); | |
| console.log(`- Unique tools found: ${uniqueTools.length}`); | |
| console.log(`\nWriting to ${outputFile}...`); | |
| fs.writeFileSync(outputFile, JSON.stringify(uniqueTools, null, 2), "utf-8"); | |
| console.log(`Done!`); | |
| } | |
| main(); | |