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
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
It takes an LLMChain and a ReduceDocumentsChain. It first applies the LLMChain to each document, and then passes all the results to the ReduceDocumentsChain
Specify a schema and tag a document with those attributes
As opposed to Extraction, this extracts only one instance of that schema so its more useful for classification of attributes pertaining to the text as a whole
Wouldn't it be nice if there was a way to compose prompts together, reusing pieces across prompts?
In the newest Python and JS release there now is with `Pipeline Prompt`!
Links 👇
The way this works is you define a `PipelinePrompt` with two components:
- FinalPrompt: the final prompt template to be formatted
- PipelinePrompts: a sequence of tuples of (name, PromptTemplate)
The `name` argument is how the formatted prompt will be passed to future prompts
When `.format` is called, the PipelinePrompts are first formatted in order, and are then used in future formatting steps with their respective `name` arguments
Finally, the FinalPrompt.format is called using any previously formatted values as neccesary
Most "chat-your-data" applications involve three steps:
1⃣Condense the chat history into a standalone question
2⃣Retrieve relevant docs based on the standalone question
3⃣Generate a final answer based on the retrieved documents
This involves two total calls to the LLM!
But these calls are not created equal
Condensing the chat history is a relatively easy (and less important) step, while generating the final answer can be trickier and more important to get right
With @LangChainAI you can easily use a different LLM for each step