liveBook · Manning

FAISS

As explained in Generative AI in Action

FAISS is a library for efficient similarity search and clustering of dense vectors on CPUs and GPUs.

FAISS (Facebook AI Similarity Search) is a library for fast vector search and clustering on CPUs or GPUs. In a question-answering application, it can index document embeddings so a query can retrieve similar content. It is presented as capable of handling billions of vectors. section 7.5.1 section 10.3.3

A vector index organizes embeddings to make similarity searches efficient. Index choices involve trade-offs among search speed, accuracy, and memory use; FAISS supports different vector comparisons and index types. The PDF workflow uses FAISS through LangChain rather than configuring those choices directly. section 7.5.1 section 10.3.3

Index PDFs for question answering

Load the PDFs, split their text into chunks, create OpenAI embeddings, and build the index with `FAISS.from_documents(chunks, embeddings)`. The illustrated splitter uses 2,048-character chunks with 200 characters of overlap. The PDFs contain confidential business data, so this illustrative indexing call should not be mistaken for a complete deployment plan. 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 LangChain listing shows the PDF-loading, chunking, embedding, and FAISS index-creation steps together. section 10.3.3

How Generative AI in Action covers it

How deep it goes
Defines FAISS capabilities and shows index creation through LangChain, but does not work through FAISS index configuration or internals.
Examples
Uses confidential dog-related PDFs, text chunks, OpenAI embeddings, and `FAISS.from_documents(chunks, embeddings)`.
What it assumes
Assumes familiarity with Python workflow code; introduces vector indexes before showing PDF chunking and embedding creation.
What you can do after
Apply it via LangChain to index embedded PDF chunks, while considering index speed, accuracy, and memory trade-offs.
How it connects
Connects FAISS to vector embeddings, similarity search, vector indexes, LangChain, and document question answering.

Also explained in

Compare how 4 books explain FAISS →

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.