Home / File/ base.py — langchain Source File

base.py — langchain Source File

Architecture documentation for base.py, a python file in the langchain codebase. 13 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  4b3d5656_35af_69ae_140b_cdc033a089a4["base.py"]
  2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  bc46b61d_cfdf_3f6b_a9dd_ac2a328d84b3["langchain_core.embeddings"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> bc46b61d_cfdf_3f6b_a9dd_ac2a328d84b3
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> e6b4f61e_7b98_6666_3641_26b069517d4a
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  01158a5b_b299_f45d_92e9_2a7433a1a91a["langchain_classic.chains.base"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 01158a5b_b299_f45d_92e9_2a7433a1a91a
  05d7dd18_8585_097d_321b_edfb0b735e7d["langchain_classic.chains.hyde.prompts"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 05d7dd18_8585_097d_321b_edfb0b735e7d
  31974615_0d58_bd26_13f1_776e0a9d1413["langchain_classic.chains.llm"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> 31974615_0d58_bd26_13f1_776e0a9d1413
  cd17727f_b882_7f06_aadc_71fbf75bebb0["numpy"]
  4b3d5656_35af_69ae_140b_cdc033a089a4 --> cd17727f_b882_7f06_aadc_71fbf75bebb0
  style 4b3d5656_35af_69ae_140b_cdc033a089a4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Hypothetical Document Embeddings.

https://arxiv.org/abs/2212.10496
"""

from __future__ import annotations

import logging
from typing import Any

from langchain_core.callbacks import CallbackManagerForChainRun
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import BasePromptTemplate
from langchain_core.runnables import Runnable
from pydantic import ConfigDict

from langchain_classic.chains.base import Chain
from langchain_classic.chains.hyde.prompts import PROMPT_MAP
from langchain_classic.chains.llm import LLMChain

logger = logging.getLogger(__name__)


class HypotheticalDocumentEmbedder(Chain, Embeddings):
    """Generate hypothetical document for query, and then embed that.

    Based on https://arxiv.org/abs/2212.10496
    """

    base_embeddings: Embeddings
    llm_chain: Runnable

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        extra="forbid",
    )

    @property
    def input_keys(self) -> list[str]:
        """Input keys for Hyde's LLM chain."""
        return self.llm_chain.input_schema.model_json_schema()["required"]

    @property
    def output_keys(self) -> list[str]:
        """Output keys for Hyde's LLM chain."""
        if isinstance(self.llm_chain, LLMChain):
            return self.llm_chain.output_keys
        return ["text"]

    def embed_documents(self, texts: list[str]) -> list[list[float]]:
        """Call the base embeddings."""
        return self.base_embeddings.embed_documents(texts)

    def combine_embeddings(self, embeddings: list[list[float]]) -> list[float]:
        """Combine embeddings into final embeddings."""
        try:
            import numpy as np

// ... (68 more lines)

Subdomains

Dependencies

  • langchain_classic.chains.base
  • langchain_classic.chains.hyde.prompts
  • langchain_classic.chains.llm
  • langchain_core.callbacks
  • langchain_core.embeddings
  • langchain_core.language_models
  • langchain_core.output_parsers
  • langchain_core.prompts
  • langchain_core.runnables
  • logging
  • numpy
  • pydantic
  • typing

Frequently Asked Questions

What does base.py do?
base.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What does base.py depend on?
base.py imports 13 module(s): langchain_classic.chains.base, langchain_classic.chains.hyde.prompts, langchain_classic.chains.llm, langchain_core.callbacks, langchain_core.embeddings, langchain_core.language_models, langchain_core.output_parsers, langchain_core.prompts, and 5 more.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/chains/hyde/base.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains/hyde).

Analyze Your Own Codebase

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

Try Supermodel Free