langchain-ai / langchain

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

Type issue for langchain.chains.create_sql_query_chain using qrog and Llama3 #25270

Open SebastianGhafafian opened 3 months ago

SebastianGhafafian commented 3 months ago

URL

https://python.langchain.com/v0.2/docs/tutorials/sql_qa/

Checklist

Issue with current documentation:

Source: https://python.langchain.com/v0.2/docs/tutorials/sql_qa/ This tutorial gives you the option to choose the model. The documentation is supposed to adjust accordingly. Yet, for the Groq version, every invoke returns an unexpected result or an error. An example: The documentation states that:

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

returns 'SELECT COUNT("EmployeeId") AS "TotalEmployees" FROM "Employee"\nLIMIT 1;'

Yet, it returns a string: 'Question: How many employees are there\nSQLQuery: SELECT COUNT(*) FROM "Employee"' which is not runable in db.run(response) in the next step.

These types of errors run through the complete document, making it completely unusable.

Idea or request for content:

It seems like langchain_community.tools.sql_database.tool.QuerySQLDataBaseTool and create_sql_query_chain are not adjusted for groq using LLama3.

CalvHobbes commented 2 months ago

Hi @SebastianGhafafian ,

I encountered the same issue. I had to therefore use a custom function to parse the actual SQL Query from the response before executing it on the db:

def get_query(response):
  start_index = response.find("SQLQuery:") + len("SQLQuery:")
  return response[start_index:].strip()

then this works:

response = chain.invoke({"question": "How many employees are there"})
response = get_query(response)
db.run(response)

for the chain:

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 | get_query| execute_query
chain.invoke({"question": "How many employees are there"})