Gemini is a JavaScript project based on its namesake LLM that uses Google's Generative AI to generate content based on input context. It uses the @google/generative-ai
package to interact with the Google Generative AI API. The inspiration for this project came from the dearth of any online content on creating a Gemini RAG in javascript. This projects intends to fill that gap!
- Introduced a new function
generateContextEmbeddings(texts, fileName)
that generates embeddings for a given array of texts using theembedding-001
model fromgenAI
. The generated embeddings are stored in an array of objects, each containing the original text and its corresponding embedding. - The
parsePdf()
function is now used to get the texts that are to be embedded. - Two new variables
contextFile
andstoredFile
have been added.contextFile
is the name of the context file to be embedded andstoredFile
is the name of the stored context file to ask question from.
- A new folder named
contexts
has been initialized. This folder is used to store the context files that are generated by thegenerateContextEmbeddings
function and also to load thestoredEmbeddings
to provide context to our question.
- Clone the repository.
- Install the dependencies with
npm install
. - Set up your Google Generative AI API key in a
.env
file in the root of the project:
GEMINI_API_KEY=your_api_key_here
Please replace your_api_key_here with your actual API key.
- Upload a PDF file to generate context embeddings in the root directory of the project.
- Pass the name of the context file to be embedded in the
contextFile
inmain.js
. - Pass the name of the stored context file to ask question from in the
storedFile
inmain.js
. - Run the main script in terminal with
node main.js
.
The main.js
script performs the following steps:
- Configures the generation parameters.
- Uses the
embedding-001
model to generate embeddings for a set of texts parsed from a PDF file. - Stores the generated embeddings in
contexts
folder. - Loads the stored embeddings from the
contexts
folder and embeds the question. - Uses the
gemini-pro
model to find the best passage that answers the question from the stored embeddings.
@google/generative-ai
: To interact with the Google Generative AI API.dotenv
: To load environment variables from a .env file.fs
: To read and write files.mathjs
: To perform mathematical operations.pdf-parse
: To parse PDF files.
Contributions are welcome. Please open an issue or submit a pull request.
This project is licensed under the ISC license.