-
Checked other resources
Commit to Help
Example Codeimport os, getpass
from dotenv import load_dotenv
from langchain_community.utilities import SQLDatabase
load_dotenv()
db_url = f"mysql+mysqlconnector://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}"
db = SQLDatabase.from_uri(db_url)
print(db.dialect)
print(db.get_usable_table_names())
db.run("SELECT `CODE` FROM `CompanyOverview` WHERE `CMP_NM_KOR` = '삼성전자' LIMIT 1;")
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool
execute_query = QuerySQLDataBaseTool(db=db)
write_query = create_sql_query_chain(llm, db)
chain = write_query | execute_query
chain.invoke({"question": "What is CODE for '삼성전자' stock?"}) DescriptionI trying to execute SQL query generated by LLM with LangChain document Build a Question/Answering system over SQL data. However I encountered following error: How should I do to correctly execute the query? System Info$ pip freeze | grep langchain python version: 3.10.14 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Hello, @kj4483! I'm here to assist you with any bugs, questions, or contributions. Let's tackle this issue together! The error you're encountering is due to the SQL query being wrapped in triple backticks, which is not valid SQL syntax. To resolve this issue, you need to ensure that the query generated by the LLM does not include these backticks. Here’s how you can modify your code to handle this:
from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool
class CustomQuerySQLDataBaseTool(QuerySQLDataBaseTool):
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> Union[str, Sequence[Dict[str, Any]], Result]:
# Clean up the query by removing triple backticks
cleaned_query = query.strip("```sql\n").strip("\n```")
return self.db.run_no_throw(cleaned_query)
import os
from dotenv import load_dotenv
from langchain_community.utilities import SQLDatabase
from langchain_openai import ChatOpenAI
from langchain.chains import create_sql_query_chain
load_dotenv()
db_url = f"mysql+mysqlconnector://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}"
db = SQLDatabase.from_uri(db_url)
llm = ChatOpenAI(model="gpt-4o")
# Use the custom tool
execute_query = CustomQuerySQLDataBaseTool(db=db)
write_query = create_sql_query_chain(llm, db)
chain = write_query | execute_query
response = chain.invoke({"question": "What is CODE for '삼성전자' stock?"})
print(response) This should ensure that the SQL query generated by the LLM is correctly formatted and executable by the MySQL database. Additionally, make sure you are using the correct configurations and settings for MySQL databases to avoid syntax errors. Refer to the |
Beta Was this translation helpful? Give feedback.
-
any solution for this? tried using custom but still the same error |
Beta Was this translation helpful? Give feedback.
-
Hi I'm also trying to make chatbot based sql. I wish it help you.
|
Beta Was this translation helpful? Give feedback.
Hello, @kj4483! I'm here to assist you with any bugs, questions, or contributions. Let's tackle this issue together!
The error you're encountering is due to the SQL query being wrapped in triple backticks, which is not valid SQL syntax. To resolve this issue, you need to ensure that the query generated by the LLM does not include these backticks. Here’s how you can modify your code to handle this:
QuerySQLDataBaseTool
class to clean up the query:You can create a custom tool that extends
QuerySQLDataBaseTool
and overrides the_run
method to clean up the query before execution.