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 TypeScript. Building on your previous experience with document embeddings and similarity search, you’ll now learn how to use prompt templates to format messages with additional context. This enables you to ask questions and receive answers based on the content retrieved from your documents. By the end of this lesson, you’ll have a comprehensive understanding of document processing and retrieval workflows using LangChain in TypeScript.

Quick Reminder: Preparing Documents and Creating a Vector Store

Let’s quickly review the process of preparing documents and creating a vector store in TypeScript. We’ll load and process our document, "The Adventure of the Blue Carbuncle," generate embeddings, and store them in a FAISS vector database. This setup is essential for efficient context retrieval.

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";
import { Document } from "@langchain/core/documents";

// Define the file path with a type annotation
const filePath: string = "data/the_adventure_of_the_blue_carbuncle.pdf";

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

// Load the document and ensure type safety
const docs: Document[] = await loader.load();

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

// Create a vector store for all the document chunks
const embeddingModel = new OpenAIEmbeddings();
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

With our vector store ready, we can now retrieve relevant context for a given question. We’ll define a question, perform a similarity search, and combine the content of the most relevant document chunks to form a context string.

// Define a question with a type annotation
const question: string = "From whom was the stone stolen?";

// Retrieve the top three most relevant document chunks
const retrievedDocs: Document[] = await vectorstore.similaritySearch(question, 3);

// Combine the content of the retrieved documents into a single context string
const context: string = retrievedDocs.map((doc: Document) => doc.pageContent).join("\n\n");

Here, we use TypeScript’s type annotations to clarify the types of our variables. The context variable now contains the combined content of the most relevant document chunks, ready to be used in our prompt.

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