|
const fs = require("fs"); |
|
const data = JSON.parse(fs.readFileSync('two-chatter-only.json', 'utf8')); |
|
let destArray = []; |
|
|
|
|
|
const groupBy = (array, key) => |
|
{ |
|
return array.reduce((result, item) => |
|
{ |
|
(result[item[key]] = result[item[key]] || []).push(item); |
|
return result; |
|
}, {}); |
|
}; |
|
|
|
const grouped = groupBy(data, 'thread_href'); |
|
|
|
|
|
|
|
for (let thread in grouped) |
|
{ |
|
let messages = grouped[thread]; |
|
let destObj = {}; |
|
destObj.id = generateId(); |
|
destObj.conversations = []; |
|
|
|
for (let i = 0; i < messages.length; i++) |
|
{ |
|
let message = messages[i]; |
|
let conversation = {}; |
|
|
|
if (i % 2 == 0) |
|
{ |
|
conversation.from = "human"; |
|
} |
|
else |
|
{ |
|
conversation.from = "gpt"; |
|
} |
|
conversation.value = message.message; |
|
|
|
destObj.conversations.push(conversation); |
|
} |
|
|
|
destArray.push(destObj); |
|
} |
|
|
|
|
|
fs.writeFileSync("vicuna-output.json", JSON.stringify(destArray, null, 2), "utf8"); |
|
|
|
|
|
function generateId() |
|
{ |
|
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
|
|
|
let id = ""; |
|
for (let i = 0; i < 6; i++) |
|
{ |
|
|
|
let index = Math.floor(Math.random() * 62); |
|
|
|
id += chars[index]; |
|
} |
|
|
|
id += "_" + Math.floor(Math.random() * 100); |
|
|
|
return id; |
|
} |
|
|