Asking Questions with Retrieved Context and Templates

Asking Questions with Retrieved Context and Templates

Welcome to the final lesson of this course! In this lesson, we will integrate context retrieval with a chat model using LangChain in JavaScript. This builds on the skills you've developed in previous lessons, where you learned about document embeddings and similarity search. Today, we'll focus on using templates to format messages with extra context, enabling you to ask questions and receive answers based on the retrieved document content. This lesson will bring together all the skills you've learned so far, culminating in a comprehensive understanding of document processing and retrieval with LangChain in JavaScript.

Quick Reminder: Preparing Documents and Creating a Vector Store

Let's quickly recap what we've learned in previous lessons about preparing documents and creating a vector store. We'll load and prepare our document, "The Adventure of the Blue Carbuncle," and generate embeddings to create a vector store. This process is essential for effective context retrieval.

JavaScript
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { OpenAIEmbeddings } from "@langchain/openai";
import { FaissStore } from "@langchain/community/vectorstores/faiss";

// Define the file path
const filePath = "data/the_adventure_of_the_blue_carbuncle.pdf";

// Create a loader for our document
const loader = new PDFLoader(filePath);

// Load the document
const docs = await loader.load();

// Split the document into chunks
const textSplitter = new RecursiveCharacterTextSplitter({
  chunkSize: 1000,
  chunkOverlap: 100
});
const splitDocs = await textSplitter.splitDocuments(docs);

// Create a vector store for all the document chunks
const embeddingModel = new OpenAIEmbeddings({ model: "text-embedding-3-small" });
const vectorstore = await FaissStore.fromDocuments(splitDocs, embeddingModel);

In this code, we load a document, split it into chunks, generate embeddings, and create a vector store, setting the stage for efficient context retrieval in our question-answering tasks.

Combining Retrieved Context

Now that we have our vector store, we can integrate context retrieval with a chat model. First, we'll define a query and perform a similarity search to retrieve relevant documents based on the query. This will allow us to combine the retrieved document content to form a context for our question.

JavaScript
// Define a query
const query = "From whom was the stone stolen?";

// Retrieve relevant documents
const retrievedDocs = await vectorstore.similaritySearch(query, 3);

// Combine the content of retrieved documents
const context = retrievedDocs.map(doc => doc.pageContent).join("\n\n");

In this example, we define a query and retrieve the top three most relevant document chunks. The content of these chunks is combined to form a context that will be used in the next step.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal