Integrating OpenAI Assistants

Integrating OpenAI Assistants for Enhanced Keyword Analysis

OpenAI has emerged as a leader in the AI landscape, offering tools that redefine automation and machine learning. Its impact is particularly evident in the realm of content strategy, where AI-driven tools are increasingly vital.

Among OpenAI’s innovative offerings, the OpenAI Assistants stand out as a particularly intriguing beta feature. As with much of the OpenAI API and the entire AI ecosystem, these assistants are constantly evolving, with features and functionalities likely to undergo changes in the near future.

Today, we’re diving into a fascinating application of these assistants by writing a program to automatically assign rankings to a list of keywords in relation to given content. We recently used this technique while working with Gospel in Life to help them analyze and categorize their vast content library and we’ll show you how it works.

Setting Up the OpenAI Assistant for Keyword Analysis

Understanding the Keyword Ranking Challenge

Our project involved evaluating the website’s content using specific keywords, each with unique definitions specifically relevant to the organization. The goal was to assign a relevance ranking to these keywords for each piece of content on the website. Initially, we faced challenges as OpenAI’s tools were not very effective in accurately determining the relevance of these specialized keywords within our content.

In response, we adopted a complex, multi-stage process involving refinement and validation of content. This approach was somewhat effective, but it was also time-consuming. However, the release of OpenAI’s “Assistants” marked a turning point, significantly simplifying the workflow. This innovation allowed us to streamline our process, enhancing efficiency and making it easier to meet our project’s objectives.

Creating a Customized OpenAI Assistant

To utilize OpenAI’s capabilities for your specific needs, begin by visiting platform.openai.com. This platform allows you to create a customized ‘Assistant’. For tasks involving keyword analysis, an HTML file with keyword definitions is essential. This file will guide the Assistant in understanding how these keywords relate to your content.

Here’s how to set it up:

  1. Go to OpenAI Assistants.
  2. Create a new Assistant. For example, you might name it “Summary Ranking Definitions.”
  3. Set specific instructions for your Assistant, such as aiding in content creation for a particular context (e.g., “You are an assistant who helps with content creation at a Presbyterian church”).
  4. Select the latest GPT model for optimal performance.
  5. Enable the “Retrieval” feature, which is crucial for the Assistant to process and analyze the information in your HTML file effectively.

This process ensures your Assistant is tailored to analyze the relationship between keywords and content, based on the parameters you’ve set.

Uploading HTML Files for Keyword Definitions

After setting up the Assistant on OpenAI’s platform, upload an HTML file containing the keyword definitions. This file is a key component as it forms the backbone of the project, providing the essential data needed for the Assistant to analyze the relationships between keywords and content. The HTML file essentially acts as a dictionary or reference point, enabling the Assistant to understand the context and relevance of each keyword within the content it evaluates. By defining the keywords in this file, we’re able to guide the Assistant’s analysis, ensuring it aligns with our specific needs and objectives.

The process of uploading this file to the OpenAI Assistant is designed to be user-friendly, making it accessible even for those who are new to the platform. Once you’re logged into your OpenAI account and have your Assistant set up, there’s a simple interface for uploading your HTML file. You can either drag and drop the file or browse your computer to select it. Once the file is uploaded, it’s important to save your changes to the Assistant. Upon saving, OpenAI generates a unique ID for your Assistant. This ID is critical as it serves as a unique identifier for your project, enabling you to reference and interact with your Assistant in subsequent operations. It’s advisable to note down or copy this ID, as you’ll need it for future steps in your workflow, like when you’re sending requests to the Assistant or retrieving analysis results.

Integrating OpenAI API with Javascript

After setting up your Assistant and uploading the HTML file, the next step is to connect your javascript environment to the OpenAI API. This stage involves a bit of technical work. If you need help with this process, let us know.

To start integrating javascript with the OpenAI API, you need to prepare your javascript environment for sending HTTP requests. One way to do this is by installing a necessary package through Node Package Manager (npm). You can install the OpenAI package by using the command npm install --save openai in your terminal or command prompt. This command installs the OpenAI package, equipping your environment with the tools required to communicate with the OpenAI API.

Next, obtain your API key from OpenAI. This API key is like a password that allows your scripts to access and interact with OpenAI’s features. Without this key, your requests to the API won’t be authenticated, meaning they won’t be recognized or accepted. After getting your API key, you can start writing your javascript script to send requests to the OpenAI API and handle responses. In your script, you’ll need to include a section where you establish a connection with the API, using the API key for authentication. It should look something like this:

const openai = new OpenAIAPI({
	apiKey: OPENAI_API_KEY,
	organization: OPENAI_ORG
})

Executing the Ranking Process with OpenAI and Javascript

Sending Content and Requests to OpenAI

Once you’ve successfully established a connection with the OpenAI API from your javascript environment, the next step is to prepare and send your specific content or request. It’s important to format the request in a manner that the OpenAI Assistant can easily understand and analyze. The clarity and structure of your request are key to ensuring the Assistant processes your query effectively and provides the desired analysis.

After structuring your request, you will use a function in the OpenAI npm library to send it to the correct destination within the OpenAI system. Once your request reaches the API endpoint, OpenAI processes it and assigns a unique identifier to it, known as a thread ID. This thread ID is significant because it acts like a tracking number for your request. You can use this ID later to track the progress of your query, check its status, and retrieve the results from the OpenAI Assistant.

Your code should look something like this:

// Construct the request content
const sendContent =
"Here is a of keywords as a JSON-encoded array:\n" +
"KEYWORDS: " + JSON.stringify( topics ) + "\n\n" +

"For each of these keywords, assign a relevancy ranking between 0 and 100 as it relates to the content that follows. " +
"The keyword list contains random phrases, and not every keyword will be relevant to the content. It is not possible for " +
"more than 25% of the keywords to be relevant, although there should always be at least 3 words that rank greater than 0. " +
"The content is HTML from a transcript of a sermon that was delivered to a Presbyterian church by Tim Keller.\n" +
"CONTENT:\n" +
content + "\n";

// Create a thread
const thread = await openai.beta.threads.create({
	messages: [
		{
			"role": "user",
			"content": sendContent,
			"file_ids": [ definitionsFileId ]
		}
	]
});

// Run the thread
const run = await openai.beta.threads.runs.create(
	thread.id,
	{
		assistant_id: assistantId,
		instructions: "Please format the output as a JSON-encoded array of key-value pairs. Each keyword should be the key " +
					"and its relevancy ranking the value. This will be used as programmatic input, so do not provide any " +
					"introduction, headers or explanation in the final response."
	}
);

Managing the Process and Awaiting Results

Once you’ve sent your request to the OpenAI Assistant, the next phase involves some patience. The Assistant starts processing your request, which involves analyzing both the content and keywords as per the instructions and data provided in your HTML file. The duration of this analysis can vary. It largely depends on how complex your request is and the current demand on OpenAI’s servers, which means sometimes you might get a response quickly, and other times it might take a bit longer.

To manage this waiting period in javascript, you can set up a mechanism to periodically check on the progress of your request. This is typically done by using a recursive function – a function that calls itself repeatedly. Each time it calls itself, it asks the OpenAI API for an update on your request’s status, identified by the thread ID you received. It’s important to space out these check-in calls with a reasonable delay, so you don’t overload the server with too many requests in a short span. Additionally, setting a limit on the number of times the function will try to check the status (also known as recursion limit) is a good practice. This acts as a fail-safe, preventing the function from running indefinitely if, for some reason, a response isn’t received.

Here is what this looks like:

await this.waitForThread( openai, thread.id, run.id, 0 );

/**
* Wait for an OpenAI Assistant thread to complete
* @param {*} openai
* @param {*} threadId
* @param {*} runId
* @param {*} recursions
* @returns int				The Thread ID
*/
async waitForThread( openai, threadId, runId, recursions ) {

	return new Promise(
		async (resolve, reject) => {

			const retrieve = await openai.beta.threads.runs.retrieve(
				threadId,
				runId
			);

			if( retrieve.status !== 'completed'  ) {
				await this.sleep( 2000 );
				return resolve( this.waitForThread( openai, threadId, runId, (recursions + 1) ) );
			} else {
				console.log( "[THREAD COMPLETED] (" + recursions + ")" );
				return resolve( retrieve.id );
			}

		}
	);

}

Analyzing Results

Retrieving and Interpreting OpenAI’s Response

After the thread finishes processing your request, the next step is to collect and examine the results. The OpenAI Assistant, having completed its analysis, provides you with detailed feedback. This feedback includes an assessment of how the keywords you specified relate to the content you submitted.

With this analysis you can assess the effectiveness of your keywords in the context of your content. The Assistant’s response offers insights into the relevance and ranking of each keyword, allowing you to gauge how well they align with the content. This information is particularly useful for content optimization and improving your SEO strategies. We used this response to automatically tag content with the most relevant tags and categories.

The Future of AI in Content Strategy

Integrating OpenAI Assistants into javascript for automatic keyword ranking is a significant advancement in the field of content analysis and optimization. This development, currently in the beta phase of the OpenAI API, opens up exciting possibilities for future enhancements and innovations.

It’s important to remember that the world of artificial intelligence is constantly evolving. To fully harness the power of AI, it’s crucial to stay up to date and flexible in adapting to new changes. As you start using OpenAI Assistants in your javascript projects, continue to explore and experiment. Push the limits of what can be achieved with AI. The potential for AI to transform content strategy is immense, and by engaging with these tools, you are actively participating in shaping this promising future.

If you would like to use AI to optimize and categorize your content, let us know! We’d love to help you turn your content archive into a powerful tool to help further your mission.

17279

Was this article helpful?

Sign up to get more content like this sent right to your inbox!

Leave a Reply