
    dhX                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJrJr  SS	KJrJrJrJr  SS
KJr  SSKJr   " S S\5      r\R3                  5         g)z-Toolkit for interacting with an SQL database.    )List)	BaseCache)	Callbacks)BaseLanguageModel)BaseTool)BaseToolkit)
ConfigDictField)InfoSQLDatabaseToolListSQLDatabaseToolQuerySQLCheckerToolQuerySQLDatabaseTool)QuerySQLDataBaseTool)SQLDatabasec                       \ rS rSr% Sr\" SS9r\\S'   \" SS9r	\
\S'   \S\4S j5       r\" SS	9rS\\   4S
 jrS\4S jrSrg)SQLDatabaseToolkit   a+  SQLDatabaseToolkit for interacting with SQL databases.

Setup:
    Install ``langchain-community``.

    .. code-block:: bash

        pip install -U langchain-community

Key init args:
    db: SQLDatabase
        The SQL database.
    llm: BaseLanguageModel
        The language model (for use with QuerySQLCheckerTool)

Instantiate:
    .. code-block:: python

        from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
        from langchain_community.utilities.sql_database import SQLDatabase
        from langchain_openai import ChatOpenAI

        db = SQLDatabase.from_uri("sqlite:///Chinook.db")
        llm = ChatOpenAI(temperature=0)

        toolkit = SQLDatabaseToolkit(db=db, llm=llm)

Tools:
    .. code-block:: python

        toolkit.get_tools()

Use within an agent:
    .. code-block:: python

        from langchain import hub
        from langgraph.prebuilt import create_react_agent

        # Pull prompt (or define your own)
        prompt_template = hub.pull("langchain-ai/sql-agent-system-prompt")
        system_message = prompt_template.format(dialect="SQLite", top_k=5)

        # Create agent
        agent_executor = create_react_agent(
            llm, toolkit.get_tools(), state_modifier=system_message
        )

        # Query agent
        example_query = "Which country's customers spent the most?"

        events = agent_executor.stream(
            {"messages": [("user", example_query)]},
            stream_mode="values",
        )
        for event in events:
            event["messages"][-1].pretty_print()
T)excludedbllmreturnc                 .    U R                   R                  $ )z3Return string representation of SQL dialect to use.)r   dialectselfs    f/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/agent_toolkits/sql/toolkit.pyr   SQLDatabaseToolkit.dialectV   s     ww    )arbitrary_types_allowedc                 "   [        U R                  S9nSUR                   S3n[        U R                  US9nSUR                   S3n[	        U R                  US9nSUR                   S3n[        U R                  U R                  US	9nUUUU/$ )
zGet the tools in the toolkit.)r   zInput to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables. Be sure that the tables actually exist by calling z- first! Example Input: table1, table2, table3)r   descriptiona4  Input to this tool is a detailed and correct SQL query, output is a result from the database. If the query is not correct, an error message will be returned. If an error is returned, rewrite the query, check the query, and try again. If you encounter an issue with Unknown column 'xxxx' in 'field list', use z# to query the correct table fields.zUse this tool to double check if your query is correct before executing it. Always use this tool before executing a query with !)r   r   r!   )r   r   namer   r   r   r   )r   list_sql_database_tool"info_sql_database_tool_descriptioninfo_sql_database_tool#query_sql_database_tool_descriptionquery_sql_database_tool"query_sql_checker_tool_descriptionquery_sql_checker_tools           r   	get_toolsSQLDatabaseToolkit.get_tools_   s    !4!@A &**+ ,44 	+ "5ww$F"
+ ,B+F+F*G H1	1 	, #7ww$G#
F&++,A/ 	+
 "5wwDHH2T"
 $"""	
 	
r   c                 6    U R                   R                  5       $ )z4Return db context that you may want in agent prompt.)r   get_contextr   s    r   r.   SQLDatabaseToolkit.get_context   s    ww""$$r    N)__name__
__module____qualname____firstlineno____doc__r
   r   r   __annotations__r   r   propertystrr   r	   model_configr   r   r+   dictr.   __static_attributes__r0   r   r   r   r      sn    8t D)B)"40C	0    $L%
4> %
N%T %r   r   N)r5   typingr   langchain_core.cachesr   langchain_core.callbacksr   langchain_core.language_modelsr   langchain_core.toolsr   langchain_core.tools.baser   pydanticr	   r
   +langchain_community.tools.sql_database.toolr   r   r   r   r   *langchain_community.utilities.sql_databaser   r   model_rebuildr0   r   r   <module>rF      sO    3  8 ; < ) 1 &  Cp% p%f      "r   