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


















