-
Checked other resources
Commit to Help
Example Codefrom langchain_experimental.sql import SQLDatabaseChain
_DEFAULT_TEMPLATE = """Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Use the following format:
Question: "Question here"
SQLQuery: "SQL Query to run"
SQLResult: "Result of the SQLQuery"
Answer: "Final answer here"
Only use the following tables:
{table_info}
Question: {input}"""
PROMPT = PromptTemplate(
input_variables=["input", "table_info", "dialect"], template=_DEFAULT_TEMPLATE
)
sql_db_chain = SQLDatabaseChain.from_llm(llm = llm, db = db, verbose = True, use_query_checker=True, prompt=PROMPT)
sql_db_chain.run("What are the total amount of unique users?") Description
'''sql instead of pure SQL: SELECT COUNT(DISTINCT id) System Infolangchain==0.3.12 |
Beta Was this translation helpful? Give feedback.
Answered by
feijoes
Dec 18, 2024
Replies: 1 comment 8 replies
-
@GameModes Could you please provide the error output from the terminal? |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I’ve never used SQLDatabaseChain before and didn’t notice anything unusual in the template or the provided code. You might want to try using the default prompt to see if it generates any errors. However, here are my recommendations:
1 Create a custom SQLDatabaseChain where it generates the query and executes it. You can try extracting the SQL from the string using something like:
.replace('"', "").replace("sql", "")
2 Consider using create_sql_agent. I think you can achieve the same result, and it’s easier to set up.