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.

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.

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.

Formatting Messages with Templates

To interact effectively with the chat model, we need to format our messages using prompt templates. In TypeScript, we use the ChatPromptTemplate class to define a structured prompt with placeholders for the context and question. This approach ensures type safety and clarity in how we construct our prompts.

In this code, we define a prompt template as a string with {context} and {question} placeholders. The ChatPromptTemplate class manages the structure and ensures that the correct values are inserted.

To fill in the template, we use the format method, passing an object with the required properties:

This approach leverages TypeScript’s type checking to ensure that the correct data is provided to the template, resulting in a well-structured prompt for the chat model.

To verify the structure of the formatted prompt, you can print it:

The output will look similar to:

This confirms that the context and question are correctly inserted into the template.

Asking a Question with Retrieved Context to a Chat Model

With our prompt ready, we can now interact with the chat model. We’ll instantiate the chat model, invoke it with our formatted prompt, and display the response.

In this section, we use TypeScript’s type annotations to ensure that the response from the chat model is of type AIMessage. The model processes the prompt and generates an answer based on the provided context and question. By printing both the question and the answer, you can see how the integration of context retrieval and prompt templates leads to accurate and relevant responses.

A typical output might be:

This demonstrates how the chat model leverages the retrieved context to provide a precise answer.

Summary and Next Steps

You've successfully completed this lesson, where you learned how to integrate context retrieval with a chat model using LangChain in TypeScript. We explored the use of templates to format messages with extra context, allowing you to ask questions and receive answers based on the retrieved document content. This lesson consolidated all the skills you've acquired so far, equipping you with a solid understanding of document processing and retrieval with LangChain in TypeScript. As you continue your learning journey, consider experimenting with different queries and document types to deepen your understanding. Stay tuned for the next course, where we'll build on these concepts and explore even more advanced techniques.

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