eglManPages = {
let usage = 0;
const promises = [];
for (let j=0, m=eglSimplifiedHtmlPages.length; j<m; ++j) {
promises.push(SIMPLIFY(j, m));
}
return await Promise.all(promises);
async function SIMPLIFY(j, m) {
const { name, html } = eglSimplifiedHtmlPages[j];
const fragments = SPLIT(html, { maxTokens: 2048 });
let man = [];
let prevInput = null;
let prevOutput = null;
for (let i=0, n=fragments.length; i<n; ++i) {
const fragment = fragments[i];
await approval(`Page "${name}" (${j+1}/${m}): Fragment ${i+1}/${n}: ${usage} tokens (Approx. \$${(0.002*usage/1000).toFixed(3)})`);
const messages = [];
messages.push({ role: 'system', content: `You are a document transforming AI bot. The user will provide you with a snippet of HTML markup from some online documentation. You respond with the converted markup using syntactically correct Linux mandoc format.` });
if (i > 0) {
messages.push({ role: 'user', content: TAIL(prevInput, { maxTokens: 512 }) });
messages.push({ role: 'assistant', content: TAIL(prevOutput, { maxTokens: 512 }) });
messages.push({ role: 'system', content: `Continue where you left off. Convert the following documentation from HTML to mandoc format.` });
}
messages.push({ role: 'user', content: fragment });
const request = new Request('https://api.openai.com/v1/chat/completions', {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages,
}),
});
let response = await FETCH(request);
usage += response.usage.total_tokens;
response = response.choices[0].message.content;
man.push(response);
prevOutput = response;
prevInput = fragment;
}
man = man.join('\n');
return { name, html, man };
}
}