langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.1k stars 14.97k forks source link

V2.0 create_react_agent doesn't execute generated query on sql database #23293

Closed susandong closed 1 day ago

susandong commented 3 months ago

Checked other resources

Example Code

from this tutorial: https://python.langchain.com/v0.2/docs/tutorials/sql_qa/ from langchain_community.agent_toolkits import SQLDatabaseToolkit from langchain_core.messages import SystemMessage from langchain_core.messages import HumanMessage from langgraph.prebuilt import create_react_agent

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

tools = toolkit.get_tools()

SQL_PREFIX = """You are an agent designed to interact with a SQL database. Given an input question, create a syntactically correct SQLite query to run, then look at the results of the query and return the answer. Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 5 results. You can order the results by a relevant column to return the most interesting examples in the database. Never query for all the columns from a specific table, only ask for the relevant columns given the question. You have access to tools for interacting with the database. Only use the below tools. Only use the information returned by the below tools to construct your final answer. You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.

DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.

To start you should ALWAYS look at the tables in the database to see what you can query. Do NOT skip this step. Then you should query the schema of the most relevant tables."""

system_message = SystemMessage(content=SQL_PREFIX)

agent_executor = create_react_agent(llm, tools, messages_modifier=system_message)

for s in agent_executor.stream( {"messages": [HumanMessage(content="Which country's customers spent the most?")]} ): print(s) print("----")

Error Message and Stack Trace (if applicable)

The llm returns hallucinated query result, but the generated sql query looks reasonable. no error messages were surfaced.

Description

I am using the above code to create sql agent, the code runs, it generates reasonable sql queries, but the query results were all hallucinated, not the actual result based on the database. wondering how is the agent connected to db, since the agent arguments don't include db and why sql_db_query tool doesn't execute on the sql db.

System Info

langchain==0.2.5 langchain-aws==0.1.6 langchain-chroma==0.1.0 langchain-community==0.2.5 langchain-core==0.2.7 langchain-experimental==0.0.51 langchain-text-splitters==0.2.1

@dosu

susandong commented 3 months ago

Any updates?

knarayanamoorthy-wowcorp commented 3 months ago

Hi, I was also facing somewhat similar issue with the SQL generation. But my issue was mostly centered around wrong table name format (double quotes being added to the table names that are passed to SQL_DB_SCHEMA), which resulted in incomplete execution, displaying intermediate result to the user. To your question around DB details being passed to agent, the variable "tools" has necessary details about the DB along with the individual functions / tools needed for the SQL generation. Following are my suggestions.

  1. Review the log ( print(s)) in the debug console. Please check if the tables names are in correct format when passed to SQL_DB_SCHEMA function
  2. You may add description to the individual tool that fails to perform the intended task (in my case it was InfoSQLDatabaseTool). So I modified the description of the tool as follows(there might be a better way of doing this)

desc = ( """ALWAYS use this tool second AFTER 'sql_db_list_tables'. Input to this tool is a comma-separated list of tables with no schema, no double or single quotes. Output is the schema and sample rows for those tables. Be sure that the tables actually exist by calling 'sql_db_list_tables' first! Example Input: table1, table2, table3""" )

    tools[1].description = desc        
  1. Use Langsmith to trace the create_react_agent funtion. Helps you understand the flow and potentially address the issue

Hope this helps.

Regards, Kiran