Share:

November 13, 2023

Justin Kazmierczak

Coding A.I. into your Node.JS Applications.

Coding A.I. into your Node.JS Applications.

Share:

Published on November 13, 2023

Justin Kazmierczak

Let's integrate OpenAI's API.

I'm building Pages by The Universe, which proposes unique challenges. After integrating AI into that software, these are my thoughts, specifically using AI to tackle the most significant challenge: Search Engine Optimization. SEO for dummies: lots of specialized meta tags.

What is Search Engine Optimization?

Ok. True Search Engine Optimization is all about providing the right content your viewers will love, NOT tricking search engines into favoring your website. Google's famous search engine has already stopped using the meta tag keywords. They did this because SEO experts and website designers have abused these tags to help their search index score.

That said, a search engine is still a robot scrolling through your content with its HTML. Many web crawling scripts use meta tags to help it understand your site. However, there is a naked truth about meta tags: these tags are meaningless to your user browsing the page. Instead, these meta tags enhance another part of your user's experience.

Years ago, web browsers would update their application's window to your page title, a tag in the header section of your website's HTML file. Some still do, but most modern applications don't show this title anywhere meaningfully. Only search engines seem to feature it on their index as a title to your page before highlighting the algorithm's choice of text in that displayed search entry description.

Responsible Use of AI: The Unincumbered User

In building pages, I'm challenging the reason why web designers and web developers do what they do. In most cases, these technologically trusted components are unimportant, set to a default value, or worse, in the hands of a CRM that makes you enter these variables manually. This example is where the integration of AI comes in. When developing pages, I encourage the user to focus on creating the content and develop systems around them to enhance, support, and uplift that page to rival enterprise CRM and marketing teams' capabilities.

Responsable use of Coding extents to the responsable use of Artificial Intelegence (nureal networks). AI can enhance and support human work, not replace it. AI should make the most complicated tasks easier and safer for humans. At some point, I'll have to share my rant on the complications of work and the evolving requirement for vast amounts of nearly unattainable data to compete, grow, and excel. AI is a helping hand in humanity's growth, enabling civilization to use a new transformative tool that promises to aid the human journey.

OpenAI's SDK

OpenAI has developed some exciting applications and developer SDKs that share ChatGPT with the world of developers. Its power enables me to specifically generate beautiful descriptions that enhance your SEO and how your pages (built in the universe) appear to social network link sharers and preview generators. That's a talk for another time!

Using OpenAI's API is easy, as shown in the script snippet below. What is tricky is the same thing you experience when you use the flagship ChatGPT interface. It's about supplying ChatGPT with the right prompt.

ua.c.code

The namespace ua.c.code is not (yet) registered or is not compatible with server side rendering. The element ua.c.code had a problem rendering. Universe App Tools renders this message to help the website owner diagnose the problem. Check the main console log for more information.

Error: The namespace ua.c.code is not (yet) registered or is not compatible with server side rendering.

Stach Trace
Error: The namespace ua.c.code is not (yet) registered or is not compatible with server side rendering.
    at convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:428:85)
    at convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:307:69)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:500:32)
    at async convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:307:63)
    at async convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:500:32)
    at async convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:307:63)
    at async convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:500:32)
    at async convertJSONToDOM_Recursion (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:307:63)
    at async Object.convertJSONToDOM (/Users/thanos/Documents/devl/universe-home/uat/src/interface/jsonRender.js:226:12)

Arguments
[
  {
    "previous": [
      {
        "namespace": "h2",
        "inner": "Let's integrate OpenAI's API."
      },
      {
        "namespace": "p",
        "inner": [
          "I'm building Pages by The Universe, which proposes unique challenges. After integrating AI into that software, these are my thoughts, specifically using AI to tackle the most significant challenge: Search Engine Optimization. SEO for dummies: lots of specialized meta tags."
        ]
      },
      {
        "namespace": "h2",
        "inner": "What is Search Engine Optimization?"
      },
      {
        "namespace": "p",
        "inner": [
          "Ok. True Search Engine Optimization is all about providing the right content your viewers will love, NOT tricking search engines into favoring your website. Google's famous search engine has already stopped using the meta tag keywords. They did this because SEO experts and website designers have abused these tags to help their search index score."
        ]
      },
      {
        "namespace": "p",
        "inner": [
          "That said, a search engine is still a robot scrolling through your content with its HTML. Many web crawling scripts use meta tags to help it understand your site. However, there is a naked truth about meta tags: these tags are meaningless to your user browsing the page. Instead, these meta tags enhance another part of your user's experience."
        ]
      },
      {
        "namespace": "p",
        "inner": "Years ago, web browsers would update their application's window to your page title, a tag in the header section of your website's HTML file. Some still do, but most modern applications don't show this title anywhere meaningfully. Only search engines seem to feature it on their index as a title to your page before highlighting the algorithm's choice of text in that displayed search entry description."
      },
      {
        "namespace": "h2",
        "inner": "Responsible Use of AI: The Unincumbered User"
      },
      {
        "namespace": "p",
        "inner": "In building pages, I'm challenging the reason why web designers and web developers do what they do. In most cases, these technologically trusted components are unimportant, set to a default value, or worse, in the hands of a CRM that makes you enter these variables manually. This example is where the integration of AI comes in. When developing pages, I encourage the user to focus on creating the content and develop systems around them to enhance, support, and uplift that page to rival enterprise CRM and marketing teams' capabilities."
      },
      {
        "namespace": "p",
        "inner": "Responsable use of Coding extents to the responsable use of Artificial Intelegence (nureal networks). AI can enhance and support human work, not replace it. AI should make the most complicated tasks easier and safer for humans. At some point, I'll have to share my rant on the complications of work and the evolving requirement for vast amounts of nearly unattainable data to compete, grow, and excel. AI is a helping hand in humanity's growth, enabling civilization to use a new transformative tool that promises to aid the human journey."
      },
      {
        "namespace": "h2",
        "inner": "OpenAI's SDK"
      },
      {
        "namespace": "p",
        "inner": "OpenAI has developed some exciting applications and developer SDKs that share ChatGPT with the world of developers. Its power enables me to specifically generate beautiful descriptions that enhance your SEO and how your pages (built in the universe) appear to social network link sharers and preview generators. That's a talk for another time!"
      },
      {
        "namespace": "p",
        "inner": "Using OpenAI's API is easy, as shown in the script snippet below. What is tricky is the same thing you experience when you use the flagship ChatGPT interface. It's about supplying ChatGPT with the right prompt."
      },
      {
        "name": "code1",
        "style": "height: 350px;",
        "type": "javascript",
        "namespace": "ua.c.code",
        "inner": "/**\r\n * The Open AI API library.\r\n */\r\nvar { OpenAI } = require('openai');\r\n/**\r\n * WARNING: Connects to OpenAI Chat GPT\r\n * Opens a connection to open AI.\r\n */\r\nvar openai = new OpenAI({ apiKey: \r\n'[YOUR-KEY-HERE]'\r\n});\r\n\r\n/**\r\n * WARNING: Connects to OpenAI Chat GPT\r\n * Uses ChatGPT 3.5 Turbo to generate a response to a message.\r\n * Generates a description for an article.\r\n * @param {*} title The Page Title\r\n * @param {*} body The Page Body\r\n * @returns {object} An object with the following properties:\r\n * @property {string} description The generated description\r\n * @property {boolean} isTruncated True if the response was truncated, false otherwise.\r\n */\r\nasync function GenerateDescription(title, body) {\r\n    try {\r\n\r\n      var response = await openai.chat.completions.create({\r\n        model: \"gpt-3.5-turbo\",\r\n        messages: [{\r\n          \"role\": \"system\",\r\n          \"content\": `Generate a Meta Tag Description for blog entries. The description should be engaging and informative. It should entice readers to read the full article as well as help search engines understand the content of the article. Do not use links.`\r\n        }, {\r\n          \"role\": \"user\", \r\n          \"content\": `${title}\\n${body}`\r\n        }],\r\n      });\r\n  \r\n      var articleDescription = response.choices[0].message.content.trim();\r\n      \r\n      // Check if the response is truncated\r\n     // This means that ChatGPT can generate more in the response.\r\n      var isTruncated = (response.choices[0].finish_reason == \"stop\") ? false : true;\r\n\r\n      return { \r\n        description: articleDescription, \r\n        isTruncated\r\n      };\r\n  \r\n    } catch (error) {\r\n  \r\n   \t// handle the error\r\n  \r\n    }\r\n    \r\n  }"
      },
      {
        "namespace": "p",
        "inner": "As shown, you create a system message that tailors OpenAI's GPT 3.5 turbo model to generate the specified response. You use the \"user\" tag to supply user-provided information. Sounds like a chat conversation? That's because it is."
      },
      {
        "namespace": "h2",
        "inner": "Using ChatGPT in your Application"
      },
      {
        "namespace": "p",
        "inner": "ChatGPT is a highly specialized and fine-tuned model for chat messages. The model works best when prompted by conversation and has a general understanding of the English language. The model does have some flaws - akin to handing a newborn baby the keys to a Bentley. You're going nowhere."
      },
      {
        "namespace": "p",
        "inner": "ChatGPT requires specific instructions and test rounds for you to build great responses. It feels like banging your head on the desk until you get a response you like. Just keep trying multiple types of user input (in the above example) to ensure that the answer doesn't come out robotic but genuinely enhances your goal."
      },
      {
        "namespace": "h2",
        "inner": "Important considerations,"
      },
      {
        "namespace": "p",
        "inner": "There are some pros and cons to using ChatGPT. "
      },
      {
        "namespace": "h3",
        "inner": "The Pros"
      },
      {
        "namespace": "p",
        "inner": "You can accomplish quite a lot in the pro column, but having everything viewed by a human is essential. Encourage your creators to adapt the work in their own words or make adjustments for correctness. Another big win for ChatGPT is the ability to trigger scripts: yes, your ChatGPT can accomplish things, run processes, access the web, etc. Understanding how to utilize them properly might take a mental leap, but it seems worth it."
      },
      {
        "namespace": "p",
        "inner": "Instant access to a wealth of knowledge gives excellent potential to content creation, but that content may have copyright concerns. I return to my earlier recommendation: ensure the user has seen the generated content and can approve it. Consider current AI models as software tools like Microsoft Word or Adobe Photoshop."
      },
      {
        "namespace": "h3",
        "inner": "The Cons."
      },
      {
        "namespace": "p",
        "inner": "The biggest one is it is not budget-friendly at scale. You pay per token - almost like per word but rather OpenAI's internal numbered system for portions of words, characters, and phrases. The secret sauce tricks a calculus equation to produce human-readable content. Like the use case above, small-generated content is very straightforward and cheap! However, the cost goes up for generating anything meaningful and allowing users to request regenerations. Costly AI cloud computation leads us to the next point."
      },
      {
        "namespace": "p",
        "inner": "OpenAI was, at one point, a provider of open-source AI models. Its models would be released open source and shared with the world. Instead, it's closed now. ChatGPT's latest model is said to have so many parameters that its file size is too large to be shared. As an editor with raw files, I can honestly say nothing is too big to be shared. Innovation in developing truly closed private systems under Open AI's platform may not be possible."
      },
      {
        "namespace": "p",
        "inner": "Corporations everywhere have barred employees from using ChatGPT, fearing that someone will leak proprietary intellectual property or sensitive information (like health information or credit card numbers) to either OpenAI's development team or future trained models off of chat's shared to its flagship product. Privacy is a big concern for AI."
      },
      {
        "namespace": "h2",
        "inner": "The vulnerabilities of OpenAI's ChatGPT"
      },
      {
        "namespace": "p",
        "inner": "A not-so-subtle con for ChatGPT's models is their programming. Through testing, I (and many) have discovered that ChatGPT's actual power has been throttled either under the programming to help users use \"using AI responsibly\" or through direct \"downwatering\" responses. In 2020, ChatGPT's code suggestions were powerful and successful. In iterations updated afterward, I discovered that its code suggestions, while now more commented, are water downed and don't work."
      },
      {
        "namespace": "p",
        "inner": "During complex coding conversations with ChatGPT, it would offer code examples that don't work about 1/3 times. We're not discussing traversing a binary tree but using the Black Magic DeckLink API to render frame-accurate uncompressed broadcast-ready SDI. During recent tests, like the complex query I mentioned above, code solutions fail 3/3 times. Advanced computational questions like understanding CMake, compiling open source projects, and more now seem out of the grasp of the turbo version shared for free. ChatGPT 4.0 didn't fare that well either. However, the newest models produce less \"fake news\" and contain many disclaimers to help users navigate the uncertainty of using AI."
      },
      {
        "namespace": "p",
        "inner": "I suspect we are just learning what neural network and natural language models can do and how to program them to operate morally and ethically sound while maintaining their vast natural potential. It will take time, study, and more research into these tools - just like any other tool our civilization has created."
      },
      {
        "namespace": "p",
        "inner": "I wouldn't be surprised to see us transition from natural language models to sophisticated engines of pure learning power. Either way, using any AI (open source or through a company) can enhance your workflow. I recommend it, at least considering it, for your current use case."
      },
      {
        "src": "https://egtuniverse.com/blog/images/0006.jpg",
        "class": "image-fill image-shadow",
        "namespace": "img"
      }
    ],
    "jsonObj": {
      "name": "code1",
      "style": "height: 350px;",
      "type": "javascript",
      "namespace": "ua.c.code",
      "inner": "/**\r\n * The Open AI API library.\r\n */\r\nvar { OpenAI } = require('openai');\r\n/**\r\n * WARNING: Connects to OpenAI Chat GPT\r\n * Opens a connection to open AI.\r\n */\r\nvar openai = new OpenAI({ apiKey: \r\n'[YOUR-KEY-HERE]'\r\n});\r\n\r\n/**\r\n * WARNING: Connects to OpenAI Chat GPT\r\n * Uses ChatGPT 3.5 Turbo to generate a response to a message.\r\n * Generates a description for an article.\r\n * @param {*} title The Page Title\r\n * @param {*} body The Page Body\r\n * @returns {object} An object with the following properties:\r\n * @property {string} description The generated description\r\n * @property {boolean} isTruncated True if the response was truncated, false otherwise.\r\n */\r\nasync function GenerateDescription(title, body) {\r\n    try {\r\n\r\n      var response = await openai.chat.completions.create({\r\n        model: \"gpt-3.5-turbo\",\r\n        messages: [{\r\n          \"role\": \"system\",\r\n          \"content\": `Generate a Meta Tag Description for blog entries. The description should be engaging and informative. It should entice readers to read the full article as well as help search engines understand the content of the article. Do not use links.`\r\n        }, {\r\n          \"role\": \"user\", \r\n          \"content\": `${title}\\n${body}`\r\n        }],\r\n      });\r\n  \r\n      var articleDescription = response.choices[0].message.content.trim();\r\n      \r\n      // Check if the response is truncated\r\n     // This means that ChatGPT can generate more in the response.\r\n      var isTruncated = (response.choices[0].finish_reason == \"stop\") ? false : true;\r\n\r\n      return { \r\n        description: articleDescription, \r\n        isTruncated\r\n      };\r\n  \r\n    } catch (error) {\r\n  \r\n   \t// handle the error\r\n  \r\n    }\r\n    \r\n  }"
    },
    "lastNamespace": null
  }
]

Namespace of Element: ua.c.code
Namespace of Error: ua.element.error
/** * The Open AI API library. */ var { OpenAI } = require('openai'); /** * WARNING: Connects to OpenAI Chat GPT * Opens a connection to open AI. */ var openai = new OpenAI({ apiKey: '[YOUR-KEY-HERE]' }); /** * WARNING: Connects to OpenAI Chat GPT * Uses ChatGPT 3.5 Turbo to generate a response to a message. * Generates a description for an article. * @param {*} title The Page Title * @param {*} body The Page Body * @returns {object} An object with the following properties: * @property {string} description The generated description * @property {boolean} isTruncated True if the response was truncated, false otherwise. */ async function GenerateDescription(title, body) { try { var response = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ "role": "system", "content": `Generate a Meta Tag Description for blog entries. The description should be engaging and informative. It should entice readers to read the full article as well as help search engines understand the content of the article. Do not use links.` }, { "role": "user", "content": `${title}\n${body}` }], }); var articleDescription = response.choices[0].message.content.trim(); // Check if the response is truncated // This means that ChatGPT can generate more in the response. var isTruncated = (response.choices[0].finish_reason == "stop") ? false : true; return { description: articleDescription, isTruncated }; } catch (error) { // handle the error } }

As shown, you create a system message that tailors OpenAI's GPT 3.5 turbo model to generate the specified response. You use the "user" tag to supply user-provided information. Sounds like a chat conversation? That's because it is.

Using ChatGPT in your Application

ChatGPT is a highly specialized and fine-tuned model for chat messages. The model works best when prompted by conversation and has a general understanding of the English language. The model does have some flaws - akin to handing a newborn baby the keys to a Bentley. You're going nowhere.

ChatGPT requires specific instructions and test rounds for you to build great responses. It feels like banging your head on the desk until you get a response you like. Just keep trying multiple types of user input (in the above example) to ensure that the answer doesn't come out robotic but genuinely enhances your goal.

Important considerations,

There are some pros and cons to using ChatGPT.

The Pros

You can accomplish quite a lot in the pro column, but having everything viewed by a human is essential. Encourage your creators to adapt the work in their own words or make adjustments for correctness. Another big win for ChatGPT is the ability to trigger scripts: yes, your ChatGPT can accomplish things, run processes, access the web, etc. Understanding how to utilize them properly might take a mental leap, but it seems worth it.

Instant access to a wealth of knowledge gives excellent potential to content creation, but that content may have copyright concerns. I return to my earlier recommendation: ensure the user has seen the generated content and can approve it. Consider current AI models as software tools like Microsoft Word or Adobe Photoshop.

The Cons.

The biggest one is it is not budget-friendly at scale. You pay per token - almost like per word but rather OpenAI's internal numbered system for portions of words, characters, and phrases. The secret sauce tricks a calculus equation to produce human-readable content. Like the use case above, small-generated content is very straightforward and cheap! However, the cost goes up for generating anything meaningful and allowing users to request regenerations. Costly AI cloud computation leads us to the next point.

OpenAI was, at one point, a provider of open-source AI models. Its models would be released open source and shared with the world. Instead, it's closed now. ChatGPT's latest model is said to have so many parameters that its file size is too large to be shared. As an editor with raw files, I can honestly say nothing is too big to be shared. Innovation in developing truly closed private systems under Open AI's platform may not be possible.

Corporations everywhere have barred employees from using ChatGPT, fearing that someone will leak proprietary intellectual property or sensitive information (like health information or credit card numbers) to either OpenAI's development team or future trained models off of chat's shared to its flagship product. Privacy is a big concern for AI.

The vulnerabilities of OpenAI's ChatGPT

A not-so-subtle con for ChatGPT's models is their programming. Through testing, I (and many) have discovered that ChatGPT's actual power has been throttled either under the programming to help users use "using AI responsibly" or through direct "downwatering" responses. In 2020, ChatGPT's code suggestions were powerful and successful. In iterations updated afterward, I discovered that its code suggestions, while now more commented, are water downed and don't work.

During complex coding conversations with ChatGPT, it would offer code examples that don't work about 1/3 times. We're not discussing traversing a binary tree but using the Black Magic DeckLink API to render frame-accurate uncompressed broadcast-ready SDI. During recent tests, like the complex query I mentioned above, code solutions fail 3/3 times. Advanced computational questions like understanding CMake, compiling open source projects, and more now seem out of the grasp of the turbo version shared for free. ChatGPT 4.0 didn't fare that well either. However, the newest models produce less "fake news" and contain many disclaimers to help users navigate the uncertainty of using AI.

I suspect we are just learning what neural network and natural language models can do and how to program them to operate morally and ethically sound while maintaining their vast natural potential. It will take time, study, and more research into these tools - just like any other tool our civilization has created.

I wouldn't be surprised to see us transition from natural language models to sophisticated engines of pure learning power. Either way, using any AI (open source or through a company) can enhance your workflow. I recommend it, at least considering it, for your current use case.