diff --git a/IntelliNode/README.md b/IntelliNode/README.md index be2e101..1eef947 100644 --- a/IntelliNode/README.md +++ b/IntelliNode/README.md @@ -39,7 +39,6 @@ const { SemanticSearch } = require('intellinode'); ```js const search = new SemanticSearch(apiKey); // pivotItem: item to search. -// searchArray: array of strings to search through. const results = await search.getTopMatches(pivotItem, searchArray, numberOfMatches); const filteredArray = search.filterTopMatches(results, searchArray) ``` @@ -50,14 +49,17 @@ const { Gen } = require('intellinode'); ``` 2. call: ```js -// one line to generate marketing description -const marketingDesc = await Gen.get_marketing_desc('gaming chair', openaiApiKey); - -// or one line to generate blog post +// one line to generate blog post const blogPost = await Gen.get_blog_post(prompt, openaiApiKey); - -// or generate images from production description -const image = await Gen.generate_image_from_desc(prompt, openaiApiKey, stabilityApiKey, true); +``` +```js +// or generate images from description +const image = await Gen.generate_image_from_desc(prompt, openaiApiKey, stabilityApiKey); +``` +```js +// or generate html page code +text = 'a registration page with flat modern theme.' +await Gen.save_html_page(text, folder, file_name, openaiKey); ``` ## Models Access diff --git a/IntelliNode/function/Gen.js b/IntelliNode/function/Gen.js index 914e0b6..438e98a 100644 --- a/IntelliNode/function/Gen.js +++ b/IntelliNode/function/Gen.js @@ -6,7 +6,9 @@ const ImageModelInput = require("../model/input/ImageModelInput"); const Text2SpeechInput = require("../model/input/Text2SpeechInput"); const { Chatbot, SupportedChatModels } = require("../function/Chatbot"); const { ChatGPTInput, ChatGPTMessage } = require("../model/input/ChatModelInput"); - +const SystemHelper = require("../utils/SystemHelper"); +const fs = require('fs'); +const path = require('path'); class Gen { static async get_marketing_desc(prompt, openaiKey) { @@ -53,6 +55,29 @@ class Gen { const audioContent = await speechModel.generateSpeech(input); return audioContent; } + + static async generate_html_page(text, openaiKey) { + // load and fill the template + const promptTemplate = new SystemHelper().loadPrompt("html_page"); + const prompt = promptTemplate.replace("${text}", text); + + // prepare the bot + const chatbot = new Chatbot(openaiKey); + const input = new ChatGPTInput('generate only html, css and javascript based on the user request in the following format {"html": "", "message":""}', + { maxTokens: 2000, model: 'gpt-4' }); + // set the user message with the template + input.addUserMessage(prompt); + const responses = await chatbot.chat(input); + return JSON.parse(responses[0].trim()); + } + + static async save_html_page(text, folder, file_name, openaiKey) { + const htmlCode = await Gen.generate_html_page(text, openaiKey); + const folderPath = path.join(folder, file_name + '.html'); + fs.writeFileSync(folderPath, htmlCode['html']); + return true; + } + } module.exports = { Gen }; \ No newline at end of file diff --git a/IntelliNode/function/TextAnalyzer.js b/IntelliNode/function/TextAnalyzer.js index f575dc1..ed42fc2 100644 --- a/IntelliNode/function/TextAnalyzer.js +++ b/IntelliNode/function/TextAnalyzer.js @@ -24,7 +24,7 @@ class TextAnalyzer { } async summarize(text, options = {}) { - const summaryPromptTemplate = this.systemHelper.loadSystem("summary"); + const summaryPromptTemplate = this.systemHelper.loadPrompt("summary"); const prompt = summaryPromptTemplate.replace("${text}", text); const modelInput = new LanguageModelInput({ prompt, @@ -37,7 +37,7 @@ class TextAnalyzer { } async sentimentAnalysis(text, options = {}) { - const mode = this.systemHelper.loadSystem("sentiment"); + const mode = this.systemHelper.loadPrompt("sentiment"); const prompt = `${mode}\n\nAnalyze the sentiment of the following text: ${text}\n\nSentiment: `; const modelInput = new LanguageModelInput({ diff --git a/IntelliNode/package.json b/IntelliNode/package.json index 8e2e6e0..c166ea1 100644 --- a/IntelliNode/package.json +++ b/IntelliNode/package.json @@ -1,6 +1,6 @@ { "name": "intellinode", - "version": "0.0.18", + "version": "0.0.19", "description": "Access various AI models, such as ChatGPT, Diffusion, Cohere, Google, and others", "main": "index.js", "keywords": ["AI", "ChatGPT", "GPT4", "stable diffusion", "openai", "huggingface", "language models", "image generation", "speech synthesis", "semantic search"], diff --git a/IntelliNode/resource/templates/html_page_prompt.in b/IntelliNode/resource/templates/html_page_prompt.in new file mode 100644 index 0000000..178f438 --- /dev/null +++ b/IntelliNode/resource/templates/html_page_prompt.in @@ -0,0 +1,14 @@ +Generate website, javascript and css in one page based on the user request. + +Output example: +{"html": "....", "message"":"the page ready for render"} + +Output example for unrelated request: +{"html": "", "message":"the request does not fit in one page"} + +If an image generated, add the image description in alt to use for image generation using DALL·E 2: +<image description to use with DALL·E 2> + +user request: ${text} + +output: \ No newline at end of file diff --git a/IntelliNode/resource/systems/sentiment_system.in b/IntelliNode/resource/templates/sentiment_prompt.in similarity index 100% rename from IntelliNode/resource/systems/sentiment_system.in rename to IntelliNode/resource/templates/sentiment_prompt.in diff --git a/IntelliNode/resource/systems/summary_system.in b/IntelliNode/resource/templates/summary_prompt.in similarity index 100% rename from IntelliNode/resource/systems/summary_system.in rename to IntelliNode/resource/templates/summary_prompt.in diff --git a/IntelliNode/test/Gen.test.js b/IntelliNode/test/Gen.test.js index 15cca5c..39ef1d9 100644 --- a/IntelliNode/test/Gen.test.js +++ b/IntelliNode/test/Gen.test.js @@ -1,7 +1,7 @@ -const Gen = require("../function/Gen"); +const { Gen } = require("../function/Gen"); require("dotenv").config(); const assert = require("assert"); - +const fs = require('fs'); const openaiApiKey = process.env.OPENAI_API_KEY; const stabilityApiKey = process.env.STABILITY_API_KEY; @@ -35,9 +35,32 @@ async function testGenerateSpeechSynthesis() { assert(speech.length > 0, "Test passed"); } + +async function testGenerateHtmlPage() { + const tempDir = '../temp'; + if (!fs.existsSync(tempDir)) { + fs.mkdirSync(tempDir); + } + + const text = "a registration page with flat modern theme."; + const htmlCode = await Gen.generate_html_page(text, openaiApiKey); + + fs.writeFileSync(`${tempDir}/generated_page_test.html`, htmlCode["html"]); + + assert(htmlCode["html"].length > 0, "Test passed"); +} + +async function testSaveHTML() { + prompt = "a registration page with flat modern theme." + status = await Gen.save_html_page(prompt, folder='../temp', file_name='test_register', openaiKeyclea=openaiApiKey); + assert.strictEqual(status, true, "Test passed"); +} + (async () => { await testGetMarketingDesc(); await testGetBlogPost(); await testGenerateImageFromDesc(); await testGenerateSpeechSynthesis(); + await testGenerateHtmlPage(); + await testSaveHTML(); })(); \ No newline at end of file diff --git a/IntelliNode/utils/SystemHelper.js b/IntelliNode/utils/SystemHelper.js index a5b2a0f..8c83f12 100644 --- a/IntelliNode/utils/SystemHelper.js +++ b/IntelliNode/utils/SystemHelper.js @@ -3,18 +3,22 @@ const path = require("path"); class SystemHelper { constructor() { - this.systemsPath = path.join(__dirname, "..", "resource", "systems"); + this.systemsPath = path.join(__dirname, "..", "resource", "templates"); } - loadSystem(file_type) { + loadPrompt(file_type) { if (file_type === "sentiment") { - const sentimentSystemPath = path.join(this.systemsPath, "sentiment_system.in"); - const sentimentSystem = fs.readFileSync(sentimentSystemPath, "utf8"); - return sentimentSystem; + const sentimentSystemPath = path.join(this.systemsPath, "sentiment_prompt.in"); + const promptTemplate = fs.readFileSync(sentimentSystemPath, "utf8"); + return promptTemplate; } else if (file_type === "summary") { - const summarySystemPath = path.join(this.systemsPath, "summary_system.in"); - const summarySystem = fs.readFileSync(summarySystemPath, "utf8"); - return summarySystem; + const summarySystemPath = path.join(this.systemsPath, "summary_prompt.in"); + const promptTemplate = fs.readFileSync(summarySystemPath, "utf8"); + return promptTemplate; + } else if (file_type === "html_page") { + const promptPath = path.join(this.systemsPath, "html_page_prompt.in"); + const promptTemplate = fs.readFileSync(promptPath, "utf8"); + return promptTemplate; } else { throw new Error(`File type '${file_type}' not supported`); } diff --git a/README.md b/README.md index bf70a50..015958e 100644 --- a/README.md +++ b/README.md @@ -120,11 +120,13 @@ const { Gen } = require('intellinode'); ``` call: ```js -// one line to generate marketing description -const marketingDesc = await Gen.get_marketing_desc('gaming chair', openaiApiKey); - -// or generate images from products description -const image = await Gen.generate_image_from_desc(prompt, openaiApiKey, stabilityApiKey, true); +// one line to generate blog post +const blogPost = await Gen.get_blog_post(prompt, openaiApiKey); +``` +```js +// or generate html page code +text = 'a registration page with flat modern theme.' +await Gen.save_html_page(text, folder, file_name, openaiKey); ``` For more examples, check [the samples](https://github.com/Barqawiz/IntelliNode/tree/main/samples/command_sample). diff --git a/images/model_output/register-page.png b/images/model_output/register-page.png new file mode 100644 index 0000000..5b2d846 Binary files /dev/null and b/images/model_output/register-page.png differ