Lecture-3: RAG for Beginners – Day 3: Create Embeddings with Clean & Reusable Code
Creating Clean and Reusable Embeddings Code in JavaScript
Welcome to our third installment in the "RAG for Beginners" series! In this tutorial, we will focus on creating a clean and reusable way to generate embeddings using JavaScript. By the end of this post, you'll have a better understanding of how to structure your code for better maintainability and efficiency.
Table of Contents
- Setting Up Your Project
- Creating the
srcDirectory - Creating
embeddings.js - Updating the Main File
- Executing the Code
- Conclusion
Setting Up Your Project
Before we dive into creating our embeddings code, ensure that you have your environment variables set up correctly. If you missed the previous lectures, it's crucial to have your OpenAI API key stored in an .env file. This key will allow us to interact with the OpenAI API seamlessly.
Creating the src Directory
To keep our project organized, we will create a new directory named src. This directory will house all our source code files.
- Open your terminal or file explorer.
- Create a new folder named
src.
Your project structure should look something like this:
/your-project
├── .env
└── src
Creating embeddings.js
Next, we will create a JavaScript file named embeddings.js inside the src directory. This file will contain all the functions related to generating embeddings.
- Inside the
srcdirectory, right-click and create a new file calledembeddings.js.
Here’s a basic structure of what embeddings.js will look like:
// embeddings.js
const { Configuration, OpenAIApi } = require('openai');
require('dotenv').config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function createEmbeddings(texts) {
const response = await openai.createEmbeddings({
model: "text-embedding-ada-002",
input: texts,
});
return response.data.data;
}
async function createEmbedding(text) {
return await createEmbeddings(text);
}
async function createEmbeddingsWithDetails(texts) {
const response = await openai.createEmbeddings({
model: "text-embedding-ada-002",
input: texts,
});
return {
embeddings: response.data.data,
usage: response.data.usage,
};
}
module.exports = { createEmbeddings, createEmbedding, createEmbeddingsWithDetails };
Explanation of Functions
- createEmbeddings: This function accepts either a single text or an array of texts and returns the embeddings generated by the OpenAI API.
- createEmbedding: A helper function that calls
createEmbeddingsfor a single piece of text. - createEmbeddingsWithDetails: This function provides both the embeddings and usage details, allowing for a comprehensive look at the API's response.
Updating the Main File
Now that we have our embeddings.js file ready, we need to update the main file to utilize these functions. Create a new file named index.js in the root of your project:
- Create a new file called
index.jsin the root directory.
Here’s how you can structure index.js:
// index.js
const { createEmbedding, createEmbeddings, createEmbeddingsWithDetails } = require('./src/embeddings');
(async () => {
const singleText = "What are embeddings in artificial intelligence?";
const singleEmbedding = await createEmbedding(singleText);
console.log("Single Embedding:", singleEmbedding);
const multipleTexts = [
"The cat is sleeping on the sofa.",
"A dog is resting on the couch.",
"I love pizza with extra cheese."
];
const multipleEmbeddings = await createEmbeddings(multipleTexts);
console.log("Multiple Embeddings:", multipleEmbeddings);
})();
Explanation of Main File
- In
index.js, we import the functions fromembeddings.js. - We then call
createEmbeddingfor a single text and log its result. - For multiple texts, we call
createEmbeddingsand log the results as well.
Executing the Code
To run your code, open your terminal, navigate to your project directory, and execute the following command:
node index.js
Upon running the code, you should see the output of both the single embedding and the multiple embeddings printed to the console, confirming that everything works as expected.
Conclusion
Congratulations! You've successfully created a clean and reusable way to generate embeddings using JavaScript. By organizing your code into separate files and functions, you not only improve readability but also pave the way for easier future modifications.
If you found this tutorial helpful, please consider liking and subscribing for more content. Stay tuned for our next lecture, where we will explore further aspects of working with embeddings and enhancing our applications!
Connect with SkillBakery Studios
Explore more tutorials, tools, and resources:
Posted by SkillBakery Studios


No comments:
Post a Comment