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 similarity search and clustering of dense vectors. It supports different vector comparisons and index types, runs on CPUs and GPUs, and can handle billions of vectors. section 7.5.1 section 10.3.3

For question answering over enterprise documents, FAISS can index embeddings: numerical representations of document content. A vector index helps retrieve embeddings similar to an input vector, making relevant content easier to find. section 7.5.1 section 10.3.3

Vector indexes can use exact or approximate nearest-neighbor methods, different similarity metrics, and compression techniques. Index selection involves a trade-off between speed, accuracy, and memory consumption. section 7.5.1

Index PDFs with LangChain

Load PDFs from a local folder, split their text into chunks, and create embeddings with OpenAI. LangChain's FAISS.from_documents(chunks, embeddings) then creates the vector index. The example uses 2,048-character chunks with 200 characters of overlap. 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()

Inspect the chunking and index-creation steps in the LangChain example. section 10.3.3

For enterprise use, consider the confidentiality of source documents, dependency maintenance, and the risk of incorrect or undesirable language-model output. The example is a starting point, not a complete production design. section 10.3.3

How Generative AI in Action covers it

How deep it goes
Introduces vector-index trade-offs and illustrates index creation, without detailing FAISS internals.
Examples
Indexes confidential dog-related PDFs after splitting their text and generating embeddings.
What you can do after
Apply it via LangChain to create a vector index from document chunks and embeddings.
How it connects
Connects similarity search with vector embeddings, vector indexes, and nearest-neighbor methods.

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.