Learn how to leverage Vector Databases and embedding models to build a RAG pipeline in TypeScript with Weaviate and OpenAI models.
Duration
10 min
Access
Free
Transcript
Available
Learn how to leverage Vector Databases and embedding models to build a RAG pipeline in TypeScript with Weaviate and OpenAI models.
Duration
10 min
Access
Free
Transcript
Available
Video Transcript
To start our RAG pipeline in our project, we'll initialize a new project with npm init and then install project dependencies with npm install weviate client and .env to handle environment variables. With our packages added, we'll go to our browser and create a WeaveAid account if we don't have one. So we can set up a new cluster. We'll create Create Cluster and name our cluster Rag pipeline. We'll click create.
While our cluster is being provisioned, we'll go to OpenAI to get an API key to manage our embeddings. We'll log into the API platform, go to settings, API keys, and generate a new secret key called rag pipeline Select default project and create the secret key with our secret created We'll copy it and paste it in our dot n file. We'll now go to vs code and so in our terminal we'll open VS Code and create a new file called .env. We'll paste all the important environment variables that we need for our project. Our OpenAI API key, our Weave 8 URL, and our Weave 8 admin key.
We'll go to our browser and get those keys. Done. In our Weaviate account, we get our REST endpoint, Paste that under Weaviate URL, and then we'll get our API admin key, and paste that there, and save. We'll close that file and create a new file called main.ts. At the top of main.ts, we'll import Weaviate as well as WeaviateClient from our WeaviateClient package.
Next, we'll import dotenv config and create a main function, where we'll put our pipeline. At the bottom of this, we'll run our function with void main. In our function, we'll create a variable called WeaviateURL, and this will store our WeaviateURL environment variable. We'll handle this as string. We'll copy this three times and change this Weaviate URL to Weaviate Key and change this to admin key.
We'll change our Weaviate URL to our OpenAI key, and change this to OpenAI API key. Great. With our environment variables initialized, we initialize a new variable called client of type Weaviate client. And we use this to initialize a connection to our Weave8 account or to our Weave8 server and cluster running on Weave8 Cloud. So connect to Weave8 Cloud.
And inside our connection function, we'll pass in our Weaviate URL, and then auth credentials of new Weaviate.API key, and then pass in our Weaviate key. After this, we'll pass in our headers. In that, we have a string of value x OpenAI API key. And this is where we put our OpenAI key. Now that the initialization is done, we'll create a new collection in Weave 8 with await client.collections.create.
And we'll pass in our collection creation object. Our first item will be our collection name, which we'll call Wikipedia. The second item will be a vectorizer. And in that we use ueviate.configure.vectorizer.text to vec OpenAI. And in this we'll pass the source properties, title, and text.
After initializing our vectorizer, To take a look at the data that we're importing, let's go into our browser and paste this URL. So we have a bunch of Wikipedia data, about 10 objects with properties, text, title, URL, and ID. After defining an embedding model, we'll define a generative model and we'll do this by using the configure class again and using generative and defining our generative model as OpenAI. And we are missing a comma. And with our collection created, we'll now import some data.
In a try block, we'll define a collection as wiki collection or a variable as wiki collection, and use this to refer to a collection that we just created with clients.collections.use and reference the wiki Pedia collection we created above and store URL in a variable called URL. And I'll paste that there and define a variable called response. This will await a fetch to our URL with our data. And then we'll define a variable called wiki pages that will have the JSON data from our fetch. So response.json.
We'll then use the wiki collection variable that we defined to interact with our collection to insert data. And we'll insert wiki pages, the JSON that we get back from our request. And if this is successful, we want to log data insertion complete. If we have an error, we catch that error, and we console.error what we get back. Once data is inserted, we'll create a reference here called my collection.
And this time we'll define it as a constant to reference the Wikipedia collection, and then we'll run a RAG query. And so we'll define a variable called rag result. And this will store the result of a rag query on our collection. And so my collection dot generate to use a generative model and run a near text semantic search with the first parameter of this being a search term. We'll search for women in the Olympics.
Our second parameter would be a grouped task and our prompt would be, summarize all the results you receive and explain them to me in one line of French. So this is our instruction for the language model. We'll console.log our output, and we'll say, rag response, and have this be our rag result dot generative. We only want to get the generative portion of this text. Get rid of this.
And when this is all successful, we want to close our connection with our cluster at the top here. Get rid of that. Get rid of the squiggly line. We misspelled our collection name. And we'll make sure that our collection name appears everywhere the same.
Then we'll go back to our terminal and run the command. So we see our insertion is completed and we get a summary of our responses in French. And that is how you build a RAG pipeline with Weave8 and OpenAI in TypeScript.