
    dh$                    x   S r SSKJr  SSKJrJrJrJrJrJ	r	J
r
JrJr  SSKJrJr  SSKJrJr  SSKJrJrJr  SSKJrJrJr  SSKJr  SS	KJrJr  \(       a$  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SS.                                     SS jjjr,g)z
SQL agent.    )annotations)	TYPE_CHECKINGAnyDictListLiteralOptionalSequenceUnioncast)	AIMessageSystemMessage)BasePromptTemplatePromptTemplate)ChatPromptTemplateHumanMessagePromptTemplateMessagesPlaceholder)SQL_FUNCTIONS_SUFFIX
SQL_PREFIX
SQL_SUFFIX)SQLDatabaseToolkit)InfoSQLDatabaseToolListSQLDatabaseTool)AgentExecutor	AgentType)BaseCallbackManager)BaseLanguageModel)BaseTool)SQLDatabaseN)dbpromptc                  ^ SSK JnJnJnJn  SSKJnJnJn  SSK	J
n  Uc  Uc  [        S5      eU(       a  U(       a  [        S5      eU=(       d	    [        XS9nU=(       d    UR                  nUR                  5       [        U5      -   nUc  [         nTc  UR#                  UR$                  US9nGOS	TR&                  ;   a  TR)                  [+        U5      S
9mSTR&                  ;   a  TR)                  UR$                  S9m[-        U4S jS 5       5      (       a  UR/                  5       nSTR&                  ;   a:  TR)                  US   S9mU Vs/ sH  n[1        U[2        5      (       a  M  UPM     nnSTR&                  ;   a:  TR)                  US   S9mU Vs/ sH  n[1        U[4        5      (       a  M  UPM     nnUUR                  :X  an  TcS  SSKJn  U=(       d    UR:                  nSR=                  USUU=(       d    [>        /5      n[@        RB                  " U5      mU" S#U" U UT5      S/S/S.UD6nGO,UURD                  :X  az  Tc`  [G        [I        [*        U5      S9[J        RB                  " S5      [M        U=(       d    [N        S9[Q        SS9/n [R        RT                  " U 5      mU" S#U" U UT5      S/S/S.UD6nOUS;   a  Tc`  [G        [I        [*        U5      S9[J        RB                  " S5      [M        U=(       d    [N        S9[Q        SS9/n [R        RT                  " U 5      mUS:X  a  U" U UT5      n!O
U" U UT5      n!U" S#U!S/S/S.UD6nO[        SU S 35      eU" S#S!UUUUU	U
US".U=(       d    0 D6$ s  snf s  snf )$a	  Construct a SQL agent from an LLM and toolkit or database.

Args:
    llm: Language model to use for the agent. If agent_type is "tool-calling" then
        llm is expected to support tool calling.
    toolkit: SQLDatabaseToolkit for the agent to use. Must provide exactly one of
        'toolkit' or 'db'. Specify 'toolkit' if you want to use a different model
        for the agent and the toolkit.
    agent_type: One of "tool-calling", "openai-tools", "openai-functions", or
        "zero-shot-react-description". Defaults to "zero-shot-react-description".
        "tool-calling" is recommended over the legacy "openai-tools" and
        "openai-functions" types.
    callback_manager: DEPRECATED. Pass "callbacks" key into 'agent_executor_kwargs'
        instead to pass constructor callbacks to AgentExecutor.
    prefix: Prompt prefix string. Must contain variables "top_k" and "dialect".
    suffix: Prompt suffix string. Default depends on agent type.
    format_instructions: Formatting instructions to pass to
        ZeroShotAgent.create_prompt() when 'agent_type' is
        "zero-shot-react-description". Otherwise ignored.
    input_variables: DEPRECATED.
    top_k: Number of rows to query for by default.
    max_iterations: Passed to AgentExecutor init.
    max_execution_time: Passed to AgentExecutor init.
    early_stopping_method: Passed to AgentExecutor init.
    verbose: AgentExecutor verbosity.
    agent_executor_kwargs: Arbitrary additional AgentExecutor args.
    extra_tools: Additional tools to give to agent on top of the ones that come with
        SQLDatabaseToolkit.
    db: SQLDatabase from which to create a SQLDatabaseToolkit. Toolkit is created
        using 'db' and 'llm'. Must provide exactly one of 'db' or 'toolkit'.
    prompt: Complete agent prompt. prompt and {prefix, suffix, format_instructions,
        input_variables} are mutually exclusive.
    **kwargs: Arbitrary additional Agent args.

Returns:
    An AgentExecutor with the specified agent_type agent.

Example:

    .. code-block:: python

        from langchain_openai import ChatOpenAI
        from langchain_community.agent_toolkits import create_sql_agent
        from langchain_community.utilities import SQLDatabase

        db = SQLDatabase.from_uri("sqlite:///Chinook.db")
        llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
        agent_executor = create_sql_agent(llm, db=db, agent_type="tool-calling", verbose=True)

r   )create_openai_functions_agentcreate_openai_tools_agentcreate_react_agentcreate_tool_calling_agent)r   RunnableAgentRunnableMultiActionAgentr   z@Must provide exactly one of 'toolkit' or 'db'. Received neither.z=Must provide exactly one of 'toolkit' or 'db'. Received both.)llmr!   )dialecttop_kr,   )r,   r+   )r+   c              3  >   >#    U H  oTR                   ;   v   M     g 7f)N)input_variables).0keyr"   s     c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/agent_toolkits/sql/base.py	<genexpr>#create_sql_agent.<locals>.<genexpr>   s     V8Uf,,,8Us   )
table_infotable_namesr4   )r4   r5   )r5   )r"   z

z{tools}inputoutput)runnableinput_keys_argreturn_keys_arg)contentz{input}agent_scratchpad)variable_name)openai-toolsztool-callingr>   zAgent type z not supported at the moment. Must be one of 'tool-calling', 'openai-tools', 'openai-functions', or 'zero-shot-react-description'.zSQL Agent Executor)nameagenttoolscallback_managerverbosemax_iterationsmax_execution_timeearly_stopping_method )+langchain.agentsr$   r%   r&   r'   langchain.agents.agentr   r(   r)   langchain.agents.agent_typesr   
ValueErrorr   ZERO_SHOT_REACT_DESCRIPTION	get_toolslistr   formatr+   r.   partialstranyget_context
isinstancer   r   langchain.agents.mrklr"   FORMAT_INSTRUCTIONSjoinr   r   from_templateOPENAI_FUNCTIONSr   r   r   r   r   r   r   from_messages)"r*   toolkit
agent_typerB   prefixsuffixformat_instructionsr.   r,   rD   rE   rF   rC   agent_executor_kwargsextra_toolsr!   r"   kwargsr$   r%   r&   r'   r   r(   r)   r   rA   
db_contexttoolreact_prompttemplater@   messagesr8   s"                   `                 r1   create_sql_agentrh   .   s   R  
 72:N
 	
 2K
 	
 ;+;GDyDDJ${"33E~~weDf,,,^^#e*^5F...^^GOO^<FV8UVVV ,,.Jv555:l3KL%*%*T*TCV2WDU    6 66J}4MN%*%*T*TCV2WDU   Y:::>D $G|'G'G   {{'(j	H $11(;F 
'UF;#9%J
 	
 
y11	1>d3&78*88C&"@,@A#2DE	H (55h?F 
23vF#9%J
 	
 
7	7>d3&78*88C&"@,@A#2DE	H (55h?F'0eVDH0eVDH( 
#9%J
 	
 * &- -
 	
  
!)%-3
 !&B
 
U
s   N0NN:N)NNNNNNN
      NforceFNrG   )&r*   r   r[   zOptional[SQLDatabaseToolkit]r\   zCOptional[Union[AgentType, Literal['openai-tools', 'tool-calling']]]rB   zOptional[BaseCallbackManager]r]   Optional[str]r^   rl   r_   rl   r.   zOptional[List[str]]r,   intrD   zOptional[int]rE   zOptional[float]rF   rQ   rC   boolr`   zOptional[Dict[str, Any]]ra   zSequence[BaseTool]r!   zOptional[SQLDatabase]r"   zOptional[BasePromptTemplate]rb   r   returnr   )-__doc__
__future__r   typingr   r   r   r   r   r	   r
   r   r   langchain_core.messagesr   r   langchain_core.promptsr   r   langchain_core.prompts.chatr   r   r   -langchain_community.agent_toolkits.sql.promptr   r   r   .langchain_community.agent_toolkits.sql.toolkitr   +langchain_community.tools.sql_database.toolr   r   rI   r   rJ   r   langchain_core.callbacksr   langchain_core.language_modelsr   langchain_core.toolsr   *langchain_community.utilities.sql_databaser    rh   rG       r1   <module>r~      sg    "
 
 
 = E  
 N
 46<@-F
 -1 	6:  )-+/$&*.!(6:&(#C& !%+/)C	C)CC 4C C C 'C )C C "C (C C C  4!C" $#C& 	'C( ))C* +C, -Cr}   