Home / Class/ VectorDBQAWithSourcesChain Class — langchain Architecture

VectorDBQAWithSourcesChain Class — langchain Architecture

Architecture documentation for the VectorDBQAWithSourcesChain class in vector_db.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  bc06888f_4a28_013c_303e_b80ce2d45145["VectorDBQAWithSourcesChain"]
  916d1ca4_2df3_6007_0b8a_5943c3b4bd54["BaseQAWithSourcesChain"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|extends| 916d1ca4_2df3_6007_0b8a_5943c3b4bd54
  de0f008b_5a13_6232_14aa_0d6a4879e132["StuffDocumentsChain"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|extends| de0f008b_5a13_6232_14aa_0d6a4879e132
  dbe941e0_6e65_a06f_11f1_ee39bd5dd14e["vector_db.py"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|defined in| dbe941e0_6e65_a06f_11f1_ee39bd5dd14e
  246e2fac_dca4_4d84_a7ec_b60960c0698d["_reduce_tokens_below_limit()"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|method| 246e2fac_dca4_4d84_a7ec_b60960c0698d
  ae770a63_2c4b_596c_7f6c_a2ae300f2422["_get_docs()"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|method| ae770a63_2c4b_596c_7f6c_a2ae300f2422
  d6f8963e_c59b_50db_f624_85e800246a56["_aget_docs()"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|method| d6f8963e_c59b_50db_f624_85e800246a56
  b2ff1e48_02a1_1dc7_b539_fdeaf5d54522["_raise_deprecation()"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|method| b2ff1e48_02a1_1dc7_b539_fdeaf5d54522
  bf1df67a_3086_8a49_6408_8ebf1b50e52c["_chain_type()"]
  bc06888f_4a28_013c_303e_b80ce2d45145 -->|method| bf1df67a_3086_8a49_6408_8ebf1b50e52c

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/qa_with_sources/vector_db.py lines 19–89

class VectorDBQAWithSourcesChain(BaseQAWithSourcesChain):
    """Question-answering with sources over a vector database."""

    vectorstore: VectorStore = Field(exclude=True)
    """Vector Database to connect to."""
    k: int = 4
    """Number of results to return from store"""
    reduce_k_below_max_tokens: bool = False
    """Reduce the number of results to return from store based on tokens limit"""
    max_tokens_limit: int = 3375
    """Restrict the docs to return from store based on tokens,
    enforced only for StuffDocumentChain and if reduce_k_below_max_tokens is to true"""
    search_kwargs: dict[str, Any] = Field(default_factory=dict)
    """Extra search args."""

    def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
        num_docs = len(docs)

        if self.reduce_k_below_max_tokens and isinstance(
            self.combine_documents_chain,
            StuffDocumentsChain,
        ):
            tokens = [
                self.combine_documents_chain.llm_chain._get_num_tokens(doc.page_content)  # noqa: SLF001
                for doc in docs
            ]
            token_count = sum(tokens[:num_docs])
            while token_count > self.max_tokens_limit:
                num_docs -= 1
                token_count -= tokens[num_docs]

        return docs[:num_docs]

    @override
    def _get_docs(
        self,
        inputs: dict[str, Any],
        *,
        run_manager: CallbackManagerForChainRun,
    ) -> list[Document]:
        question = inputs[self.question_key]
        docs = self.vectorstore.similarity_search(
            question,
            k=self.k,
            **self.search_kwargs,
        )
        return self._reduce_tokens_below_limit(docs)

    async def _aget_docs(
        self,
        inputs: dict[str, Any],
        *,
        run_manager: AsyncCallbackManagerForChainRun,
    ) -> list[Document]:
        msg = "VectorDBQAWithSourcesChain does not support async"
        raise NotImplementedError(msg)

    @model_validator(mode="before")
    @classmethod
    def _raise_deprecation(cls, values: dict) -> Any:
        warnings.warn(
            "`VectorDBQAWithSourcesChain` is deprecated - "
            "please use `from langchain_classic.chains import "
            "RetrievalQAWithSourcesChain`",
            stacklevel=5,
        )
        return values

    @property
    def _chain_type(self) -> str:
        return "vector_db_qa_with_sources_chain"

Frequently Asked Questions

What is the VectorDBQAWithSourcesChain class?
VectorDBQAWithSourcesChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/qa_with_sources/vector_db.py.
Where is VectorDBQAWithSourcesChain defined?
VectorDBQAWithSourcesChain is defined in libs/langchain/langchain_classic/chains/qa_with_sources/vector_db.py at line 19.
What does VectorDBQAWithSourcesChain extend?
VectorDBQAWithSourcesChain extends BaseQAWithSourcesChain, StuffDocumentsChain.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free