create_sql_agent #24176
Replies: 3 comments 6 replies
-
Hey @GayatriSharma23! I'm here to help you with your issue. Let's work together to solve this problem! To resolve the issues with creating an SQL agent using LangChain, you can follow these steps:
Here is an updated version of your code with these corrections: from langchain.agents import create_sql_agent
from langchain_community.utilities import SQLDatabase
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
from langchain_community.llms.ollama import Ollama
from langchain_core.prompts import PromptTemplate
from langchain.agents.agent_types import AgentType
# Define the prompt template
template = '''Answer the following questions as best you can. You have access to the following tools:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}
Thought:{agent_scratchpad}'''
prompt = PromptTemplate.from_template(template)
# Initialize the database and language model
db = SQLDatabase(engine)
llm = Ollama(model='llama3')
# Create the toolkit
toolkit = SQLDatabaseToolkit(db, llm)
# Create the SQL agent
agent = create_sql_agent(
llm=llm,
toolkit=toolkit,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
prompt=prompt,
verbose=True,
handle_parsing_errors=True,
)
# Invoke the agent with a query
response = agent.invoke("How many rows are there in Table A")
print(response) Ensure that you have the correct versions of the required packages installed and that your database connection is properly configured. This should help resolve the issues you are encountering with the SQL agent [1][2]. |
Beta Was this translation helpful? Give feedback.
-
Hi all, I'm relatively new to langchain/langchain_community andI have exactly the same issue except that I'm using an openai-based model. I'm also using the "miracle" prompts from langchain that are exactly the same. Besides the "invalid or incomplete response" and "invalid format missing Action after Thought" that happens most of times, like 90% of the times, the data that is returned, when it actually returns data, it's fabricated data, completly different from what it has in the db. It initially retrieves the sql_db_list_tables with fabricated data (coming from the LLM I think) and then retrieves the correct list of tables from the db, but then it uses the fabricated table with a fabricated schema in the next steps, and even if the query is correct the result is also fabricated . I did saw that in past versions there was a Anybody have expereince this and hopefully solved it? Thanks. |
Beta Was this translation helpful? Give feedback.
-
+1 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I am trying to create_sql_agent to create an agent that takes NL query and provide answer to it using information the connected database. But when I am using the above code I am getting invalid response with message invalid or incomplete response, if i don't give agent_type then I am getting error as invalid format missing Action after Thought
System Info
langchain==0.2.6
Beta Was this translation helpful? Give feedback.
All reactions