Skip to content

Commit

Permalink
prompt looks good
Browse files Browse the repository at this point in the history
  • Loading branch information
khmyznikov committed Feb 15, 2023
1 parent 14ea593 commit 7d5dae3
Show file tree
Hide file tree
Showing 6 changed files with 664 additions and 59 deletions.
53 changes: 32 additions & 21 deletions dist/api.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
import express from 'express';
import fetch from 'node-fetch';
import { JSDOM } from 'jsdom';
import dotenv from 'dotenv';
// import { askGPT, initGPT } from './gpt.js';
import { initOpenAI, askOpenAI } from './openai.js';
import { initOpenAI, askOpenAI, manifestPrompt } from './openai.js';
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
let GPTSesion = null;
let OpenAISesion = null;
app.get('/initAPI', async (req, res) => {
GPTSesion = await initOpenAI();
if (GPTSesion) {
res.status(200).send({ message: 'GPT initialized' });
OpenAISesion = await initOpenAI();
if (OpenAISesion) {
res.status(200).send({ message: 'LLM initialized' });
}
else {
GPTSesion = null;
res.status(500).send({ error: 'GPT unavailable' });
OpenAISesion = null;
res.status(500).send({ error: 'LLM unavailable' });
}
});
app.get('/generateManifest', async (req, res) => {
if (!req.query.url) {
res.status(400).send({ error: 'URL not specified' });
return;
}
const request = await fetch(req.query.url.toString());
if (!OpenAISesion) {
res.status(500).send({ error: 'API not initialized' });
return;
}
const request = await fetch(req.query.url.toString(), {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
}
});
let rawHTML = await request.text();
let headerHTML;
if (typeof rawHTML === 'string') {
rawHTML = rawHTML.replace(/\r|\n/g, '').replace(/\s{2,}/g, '');
headerHTML = /<head>(.*)<\/head>/.test(rawHTML) ? rawHTML.match(/<head>(.*)<\/head>/)[0] : 'Head not found';
headerHTML = /<head>(.*)<\/head>/.test(rawHTML) ? rawHTML.match(/<head>(.*)<\/head>/)[0] : null;
}
if (!GPTSesion) {
res.status(500).send({ error: 'API not initialized' });
if (!headerHTML) {
res.status(400).send({ error: '<head> HTML not found' });
return;
}
if (headerHTML) {
const manifest = await askOpenAI(headerHTML, GPTSesion);
if (manifest) {
res.status(200).send({ manifest });
}
else
res.status(400).send({ error: 'GPT unavailable or failed to generate manifest' });
}
else {
res.status(400).send({ error: '<head> HTML not found' });
console.log(`HEAD: ${headerHTML}`);
const document = new JSDOM(headerHTML).window.document;
document.querySelectorAll('script').forEach((script) => script?.remove?.());
document.querySelectorAll('style').forEach((style) => style?.remove?.());
document.querySelectorAll('meta[http-equiv="origin-trial"],meta[http-equiv="content-type"],meta[name="google-site-verification"]').forEach((meta) => meta?.remove?.());
document.querySelectorAll('link[rel="stylesheet"],link[rel="modulepreload"],link[rel="preload"],link[rel="dns-prefetch"],link[rel="preload"]').forEach((link) => link?.remove?.());
const preparedHTML = document.head.innerHTML.replace(/&amp;|&{2,}|<!--|-->/g, '');
const manifest = await askOpenAI(manifestPrompt(preparedHTML), OpenAISesion);
if (manifest) {
res.status(200).send({ manifest });
}
else
res.status(400).send({ error: 'LLM unavailable or failed to generate manifest' });
});
app.post('/generateWinPackage', async (req, res) => {
return;
Expand Down
29 changes: 21 additions & 8 deletions dist/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ export async function initOpenAI() {
});
return new OpenAIApi(configuration);
}
export function manifestPrompt(html) {
const prompt = `INSTRUCTION
parse this html and generate web manifest with this string properties: lang,name[max lenght 45],short_name[max lenght 12],description[max lenght 160],background_color[hex],dir,display,orientation,scope,start_url,theme_color[hex]. And array properties: categories[max lenght 3],icons[max lenght 1]. Take all information and icons from html, use only biggest icon in array of icons.
HTML
${html}
OUTPUT FILE
web app manifests in JSON format no new line or return symbols, compact: <code>`;
return prompt;
}
export async function askOpenAI(prompt, openai) {
let gptAnswer = null;
let davinciAnswer = null;
console.log(`prompt: ${prompt}`);
try {
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: `parse this html and generate web manifest in JSON format, silent answer with no comments or explains. Take all information and icons from html tags, use only one biggest icon in array of icons. ${prompt}`,
temperature: 0.6,
model: "code-davinci-002",
prompt,
temperature: 0.1,
max_tokens: 256,
});
gptAnswer = completion.data;
console.warn(gptAnswer);
davinciAnswer = completion.data;
console.log(`answer: ${JSON.stringify(davinciAnswer)}`);
}
catch (error) {
if (error?.response) {
Expand All @@ -25,10 +36,12 @@ export async function askOpenAI(prompt, openai) {
}
}
let manifest = null;
if (gptAnswer && gptAnswer.choices[0].text)
if (davinciAnswer?.choices && davinciAnswer.choices[0]?.text) {
const restoredResponse = `<code>${davinciAnswer.choices[0].text}`;
try {
manifest = JSON.parse(gptAnswer.choices[0].text);
manifest = JSON.parse(restoredResponse.match(/<code>(.*)<\/code>/)[1]);
}
catch (error) { }
}
return manifest;
}
Loading

0 comments on commit 7d5dae3

Please sign in to comment.