I am new to Langchain and I try to currently wrap my head around a tutorial to build a LLM Agent to interact with a SQL data base (https://python.langchain.com/v0.2/docs/tutorials/sql_qa/).
I am running into issues early on. When I run the code, the create_sql_query_chain always produces a different output than expected. In the provided link, the following code snippet is supposed to result a variable ‘response’ containing a pure SQL query which is runnable using for example db.run(response)
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass()
# Comment out the below to opt-out of using LangSmith in this notebook. Not required.
if not os.environ.get("LANGCHAIN_API_KEY"):
os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
os.environ["LANGCHAIN_TRACING_V2"] = "true"
from langchain_community.utilities import SQLDatabase
db = SQLDatabase.from_uri("sqlite:///Chinook.db")
print(db.dialect)
print(db.get_usable_table_names())
db.run("SELECT * FROM Artist LIMIT 10;")
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
from langchain.chains import create_sql_query_chain
chain = create_sql_query_chain(llm, db)
response = chain.invoke({"question": "How many employees are there"})
response
Yet in my case, it results in a response containing more junk:
‘sqlnSQLQuery: SELECT COUNT("EmployeeId") AS "EmployeeCount" FROM "Employee";n
nn“`sql’
This behavior renders the function langchain.chains.create_sql_query_chain unusable without further processing.
I have a hard time figuring out, how to create a prompt which leads to the desired output only containing a SQL query. I looked probably 10 other tutorials covering the same topic. In each of them I run into the same issue.
I tried different models and coming up with my own prompt, which did not solve the problem at hand. I tried it locally with Ollama and LLama3.1 such LLama3.1 using the Groq to get faster results.
Sebastian Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.