Harrison Chase Profile picture
@LangChainAI, previously @robusthq @kensho MLOps ∪ Generative AI ∪ sports analytics

Jul 5, 2023, 8 tweets

📄Documents x LLMs📄

Combining documents with LLMs is a key part of retrieval and chaining

We've improved our @LangChainAI reference documentation across the 5 major CombineDocumentsChains and helper functions to help with clarity and understanding of how these work

🧵


📄 `format_document`

Want to control which metadata keys show up in the prompt?

This helper function is rarely exposed, but is key to combining documents with LLMs

It takes a Document and formats it into a string using a PromptTemplate

Docs: https://t.co/Xrl5HtvFlvapi.python.langchain.com/en/latest/sche…

🧸Stuff Documents Chain

The most basic CombineDocumentsChain, this takes N documents, formats them into a string using a PromptTemplate and `format_document`, and then combines them into a single prompt and passes them to an LLM

Docs: https://t.co/NCvUNEbAVYapi.python.langchain.com/en/latest/chai…

🟥 ReduceDocumentsChain

But what if you have too many documents to fit into a single a prompt? That's where ReduceDocumentsChain comes into play

It recursively combines documents together

Docs: https://t.co/0VdYZm7WhXapi.python.langchain.com/en/latest/chai…

🗺️ Map Reduce Chain

This builds on top of the ReduceDocumentsChain

It takes an LLMChain and a ReduceDocumentsChain. It first applies the LLMChain to each document, and then passes all the results to the ReduceDocumentsChain

Docs: https://t.co/5s3IZ3XPbOapi.python.langchain.com/en/latest/chai…

👨‍🚒Refine Documents Chain

This chain uses the first document to get an initial response

It then loops over the remaining docs, making a call to the language model to combining the response with the next document

Docs: https://t.co/iONLv4ooo2api.python.langchain.com/en/latest/chai…

🌭Map Rerank

Finally, the Map Rerank Chain calls an LLM on each document, asking not only for an answer but also a score

It then sorts the responses by the score and returns the highest one

Docs: https://t.co/mdaswUaYw8api.python.langchain.com/en/latest/chai…

These chains form the backbone of several of the most popular use cases (question answering, summarization, etc)

We hope the updated reference documentation helps with understanding how these chains work and how to use them

Please let us know what other documentation we can add

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling