Home / File/ base.py — langchain Source File

base.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 17 imports 6 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  f5ba879a_2b81_897a_976e_44886c3aa79a["base.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> ba43b74d_3099_7e1c_aac3_cf594720469e
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  d45ff838_1238_0965_d3b7_5b33a0452ec0["langchain_core.output_parsers.json"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> d45ff838_1238_0965_d3b7_5b33a0452ec0
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> e6b4f61e_7b98_6666_3641_26b069517d4a
  4fa023ed_e78b_1291_b37f_a81c605fc08d["langchain_core.prompts.few_shot"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 4fa023ed_e78b_1291_b37f_a81c605fc08d
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  df467e5f_e8f3_1115_76cf_5cb823192a54["langchain_core.structured_query"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> df467e5f_e8f3_1115_76cf_5cb823192a54
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 91721f45_4909_e489_8c1f_084f8bd87145
  31974615_0d58_bd26_13f1_776e0a9d1413["langchain_classic.chains.llm"]
  f5ba879a_2b81_897a_976e_44886c3aa79a --> 31974615_0d58_bd26_13f1_776e0a9d1413
  style f5ba879a_2b81_897a_976e_44886c3aa79a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""LLM Chain for turning a user text query into a structured query."""

from __future__ import annotations

import json
from collections.abc import Callable, Sequence
from typing import Any, cast

from langchain_core._api import deprecated
from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.output_parsers.json import parse_and_check_json_markdown
from langchain_core.prompts import BasePromptTemplate
from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.runnables import Runnable
from langchain_core.structured_query import (
    Comparator,
    Comparison,
    FilterDirective,
    Operation,
    Operator,
    StructuredQuery,
)
from typing_extensions import override

from langchain_classic.chains.llm import LLMChain
from langchain_classic.chains.query_constructor.parser import get_parser
from langchain_classic.chains.query_constructor.prompt import (
    DEFAULT_EXAMPLES,
    DEFAULT_PREFIX,
    DEFAULT_SCHEMA_PROMPT,
    DEFAULT_SUFFIX,
    EXAMPLE_PROMPT,
    EXAMPLES_WITH_LIMIT,
    PREFIX_WITH_DATA_SOURCE,
    SCHEMA_WITH_LIMIT_PROMPT,
    SUFFIX_WITHOUT_DATA_SOURCE,
    USER_SPECIFIED_EXAMPLE_PROMPT,
)
from langchain_classic.chains.query_constructor.schema import AttributeInfo


class StructuredQueryOutputParser(BaseOutputParser[StructuredQuery]):
    """Output parser that parses a structured query."""

    ast_parse: Callable
    """Callable that parses dict into internal representation of query language."""

    @override
    def parse(self, text: str) -> StructuredQuery:
        try:
            expected_keys = ["query", "filter"]
            allowed_keys = ["query", "filter", "limit"]
            parsed = parse_and_check_json_markdown(text, expected_keys)
            if parsed["query"] is None or len(parsed["query"]) == 0:
                parsed["query"] = " "
            if parsed["filter"] == "NO_FILTER" or not parsed["filter"]:
                parsed["filter"] = None
            else:
// ... (322 more lines)

Subdomains

Dependencies

  • collections.abc
  • json
  • langchain_classic.chains.llm
  • langchain_classic.chains.query_constructor.parser
  • langchain_classic.chains.query_constructor.prompt
  • langchain_classic.chains.query_constructor.schema
  • langchain_core._api
  • langchain_core.exceptions
  • langchain_core.language_models
  • langchain_core.output_parsers
  • langchain_core.output_parsers.json
  • langchain_core.prompts
  • langchain_core.prompts.few_shot
  • langchain_core.runnables
  • langchain_core.structured_query
  • typing
  • typing_extensions

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 functions are defined in base.py?
base.py defines 6 function(s): _format_attribute_info, construct_examples, fix_filter_directive, get_query_constructor_prompt, load_query_constructor_chain, load_query_constructor_runnable.
What does base.py depend on?
base.py imports 17 module(s): collections.abc, json, langchain_classic.chains.llm, langchain_classic.chains.query_constructor.parser, langchain_classic.chains.query_constructor.prompt, langchain_classic.chains.query_constructor.schema, langchain_core._api, langchain_core.exceptions, and 9 more.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/chains/query_constructor/base.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains/query_constructor).

Analyze Your Own Codebase

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

Try Supermodel Free