FlareChain Class — langchain Architecture
Architecture documentation for the FlareChain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 7ff2dfc0_6147_ea78_db75_785084cb4e20["FlareChain"] 097a4781_5519_0b5d_6244_98c64eadc0d6["Chain"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|extends| 097a4781_5519_0b5d_6244_98c64eadc0d6 fcfa55b0_4a86_fa31_a156_3c38c76a0a9b["AIMessage"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|extends| fcfa55b0_4a86_fa31_a156_3c38c76a0a9b 8d3a235d_a08f_2979_f52a_1772067dd1d3["LLMChain"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|extends| 8d3a235d_a08f_2979_f52a_1772067dd1d3 9f618321_d150_9b02_038c_3e73d4accce8["ChatOpenAI"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|extends| 9f618321_d150_9b02_038c_3e73d4accce8 d38d05a1_8249_925c_6c83_6bec2124d52b["base.py"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|defined in| d38d05a1_8249_925c_6c83_6bec2124d52b 6b19d8fe_adf5_1072_cc32_77c95a85f751["input_keys()"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|method| 6b19d8fe_adf5_1072_cc32_77c95a85f751 8201c381_63b2_2377_cf29_5537b818d111["output_keys()"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|method| 8201c381_63b2_2377_cf29_5537b818d111 51b3b1db_750d_a8b7_f82a_e045160f15e7["_do_generation()"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|method| 51b3b1db_750d_a8b7_f82a_e045160f15e7 48506b03_7917_2621_09d9_f3f3a7ff7f78["_do_retrieval()"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|method| 48506b03_7917_2621_09d9_f3f3a7ff7f78 e4a21949_617a_3a26_b920_fcd4548e55a0["_call()"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|method| e4a21949_617a_3a26_b920_fcd4548e55a0 020cc6ef_1984_eb8e_ad64_7b7b782c97d2["from_llm()"] 7ff2dfc0_6147_ea78_db75_785084cb4e20 -->|method| 020cc6ef_1984_eb8e_ad64_7b7b782c97d2
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/flare/base.py lines 97–311
class FlareChain(Chain):
"""Flare chain.
Chain that combines a retriever, a question generator,
and a response generator.
See [Active Retrieval Augmented Generation](https://arxiv.org/abs/2305.06983) paper.
"""
question_generator_chain: Runnable
"""Chain that generates questions from uncertain spans."""
response_chain: Runnable
"""Chain that generates responses from user input and context."""
output_parser: FinishedOutputParser = Field(default_factory=FinishedOutputParser)
"""Parser that determines whether the chain is finished."""
retriever: BaseRetriever
"""Retriever that retrieves relevant documents from a user input."""
min_prob: float = 0.2
"""Minimum probability for a token to be considered low confidence."""
min_token_gap: int = 5
"""Minimum number of tokens between two low confidence spans."""
num_pad_tokens: int = 2
"""Number of tokens to pad around a low confidence span."""
max_iter: int = 10
"""Maximum number of iterations."""
start_with_retrieval: bool = True
"""Whether to start with retrieval."""
@property
def input_keys(self) -> list[str]:
"""Input keys for the chain."""
return ["user_input"]
@property
def output_keys(self) -> list[str]:
"""Output keys for the chain."""
return ["response"]
def _do_generation(
self,
questions: list[str],
user_input: str,
response: str,
_run_manager: CallbackManagerForChainRun,
) -> tuple[str, bool]:
callbacks = _run_manager.get_child()
docs = []
for question in questions:
docs.extend(self.retriever.invoke(question))
context = "\n\n".join(d.page_content for d in docs)
result = self.response_chain.invoke(
{
"user_input": user_input,
"context": context,
"response": response,
},
{"callbacks": callbacks},
)
if isinstance(result, AIMessage):
result = result.content
marginal, finished = self.output_parser.parse(result)
return marginal, finished
def _do_retrieval(
self,
low_confidence_spans: list[str],
_run_manager: CallbackManagerForChainRun,
user_input: str,
response: str,
initial_response: str,
) -> tuple[str, bool]:
question_gen_inputs = [
{
"user_input": user_input,
"current_response": initial_response,
"uncertain_span": span,
}
for span in low_confidence_spans
]
callbacks = _run_manager.get_child()
if isinstance(self.question_generator_chain, LLMChain):
Extends
Source
Frequently Asked Questions
What is the FlareChain class?
FlareChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/flare/base.py.
Where is FlareChain defined?
FlareChain is defined in libs/langchain/langchain_classic/chains/flare/base.py at line 97.
What does FlareChain extend?
FlareChain extends Chain, AIMessage, LLMChain, ChatOpenAI.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free