November 13, 2023

Justin Kazmierczak

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

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


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.

{"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{\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 }"}

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.