Home / File/ query.py — langchain Source File

query.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 7 imports 3 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a["query.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> ba43b74d_3099_7e1c_aac3_cf594720469e
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> e6b4f61e_7b98_6666_3641_26b069517d4a
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  f977e89d_c4e5_ba97_2bfd_89feaefee8bf["langchain_classic.chains.sql_database.prompt"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> f977e89d_c4e5_ba97_2bfd_89feaefee8bf
  00e8e9cd_1dff_5fd8_a006_8b70033853e6["langchain_community.utilities.sql_database"]
  d5b91407_6084_6af2_57f9_a49dfe5fbe2a --> 00e8e9cd_1dff_5fd8_a006_8b70033853e6
  style d5b91407_6084_6af2_57f9_a49dfe5fbe2a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from typing import TYPE_CHECKING, Any, TypedDict

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, RunnablePassthrough

from langchain_classic.chains.sql_database.prompt import PROMPT, SQL_PROMPTS

if TYPE_CHECKING:
    from langchain_community.utilities.sql_database import SQLDatabase


def _strip(text: str) -> str:
    return text.strip()


class SQLInput(TypedDict):
    """Input for a SQL Chain."""

    question: str


class SQLInputWithTables(TypedDict):
    """Input for a SQL Chain."""

    question: str
    table_names_to_use: list[str]


def create_sql_query_chain(
    llm: BaseLanguageModel,
    db: SQLDatabase,
    prompt: BasePromptTemplate | None = None,
    k: int = 5,
    *,
    get_col_comments: bool | None = None,
) -> Runnable[SQLInput | SQLInputWithTables | dict[str, Any], str]:
    r"""Create a chain that generates SQL queries.

    *Security Note*: This chain generates SQL queries for the given database.

        The SQLDatabase class provides a get_table_info method that can be used
        to get column information as well as sample data from the table.

        To mitigate risk of leaking sensitive data, limit permissions
        to read and scope to the tables that are needed.

        Optionally, use the SQLInputWithTables input type to specify which tables
        are allowed to be accessed.

        Control access to who can submit requests to this chain.

        See https://docs.langchain.com/oss/python/security-policy for more information.

    Args:
        llm: The language model to use.
        db: The SQLDatabase to generate the query for.
// ... (107 more lines)

Subdomains

Dependencies

  • langchain_classic.chains.sql_database.prompt
  • langchain_community.utilities.sql_database
  • langchain_core.language_models
  • langchain_core.output_parsers
  • langchain_core.prompts
  • langchain_core.runnables
  • typing

Frequently Asked Questions

What does query.py do?
query.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 query.py?
query.py defines 3 function(s): _strip, create_sql_query_chain, langchain_community.
What does query.py depend on?
query.py imports 7 module(s): langchain_classic.chains.sql_database.prompt, langchain_community.utilities.sql_database, langchain_core.language_models, langchain_core.output_parsers, langchain_core.prompts, langchain_core.runnables, typing.
Where is query.py in the architecture?
query.py is located at libs/langchain/langchain_classic/chains/sql_database/query.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains/sql_database).

Analyze Your Own Codebase

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

Try Supermodel Free