liveBook · Manning

LangChain

As explained in Generative AI in Action

LangChain is a software framework that provides modular components for building applications around large language models and related natural-language services.

LangChain is a framework for building applications that use generative AI models. For a question-answering application over PDFs, its components connect document loading, text splitting, embeddings, a vector index, and a language model. section 10.3.3

A practical workflow loads PDFs, splits their text into overlapping chunks, creates embeddings, and stores the chunks in a FAISS index. The application then uses an OpenAI model for question answering. This illustrates an application pattern, not a complete deployment plan: real workflows also need to be scalable, secure, and reliable. section 10.3.3

Listing 10.4 Q&A over my PDFs using LangChain
from langchain_community.vector stores import FAISS
from langchain_community.docstore.document import Document
from langchain.chains.question_answering import load_qa_chain
from langchain.text_splitter import CharacterTextSplitter
...

def create_index():
    # load the documents and create the index
    docs = load_pdfs()

    text_splitter = CharacterTextSplitter(
        separator="\n",
        chunk_size=2048,
        chunk_overlap=200,
        length_function=len
    )

    # Convert the chunks of text into embeddings
    print("Chunking and creating embeddings...")
    chunks = text_splitter.split_documents(docs)
    embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_KEY)
    vectordb = FAISS.from_documents(chunks, embeddings)

    return vectordb

def main():
    vectordb = create_index()
    llm = OpenAI(openai_api_key=OPENAI_KEY)
    chain = load_qa_chain(llm, chain_type='stuff')

    while True:
        prompt = check_prompt(input(
            'Ask a question against the PDF (type "quit" to exit):'))
        docs = vectordb.similarity_search(prompt, k=3, fetch_k=10)
        response = chain.invoke({'input_documents': docs,
                                 'question': prompt},
                                return_only_outputs=True)
        print(f"Answer:\n {response['output_text']}")

if __name__ == "__main__":
    main()

The Python listing shows the LangChain components used to create the PDF index, including CharacterTextSplitter, OpenAIEmbeddings, and FAISS. section 10.3.3

How Generative AI in Action covers it

How deep it goes
Shows a Python indexing workflow with chunk sizes and overlap, but does not detail production deployment.
Examples
Uses a PDF question-answering listing that builds a FAISS index with OpenAI embeddings.
What you can do after
Apply it via LangChain to construct a PDF index for question answering.
How it connects
Connects document loading, text splitting, embeddings, vector search, and language models.

Also explained in

Compare how 19 books explain LangChain →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.