Replies: 3 comments 2 replies
-
Yes, LangChain has concepts related to querying structured data, such as SQL databases, which can be analogous to the Llama Index Pandas query pipeline. Below are some code examples demonstrating how to build a Question/Answering system over SQL data using LangChain. SetupFirst, install the required packages and set environment variables: %%capture --no-stderr
%pip install --upgrade --quiet langchain langchain-community langchain-openai faiss-cpu Initialize DatabaseSet up a SQLite connection with the Chinook database: 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;") Create SQL Query ChainCreate a chain that converts a question into a SQL query, executes it, and uses the result to answer the original question: from langchain_openai import ChatOpenAI
from langchain.chains import create_sql_query_chain
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
chain = create_sql_query_chain(llm, db)
response = chain.invoke({"question": "How many employees are there"})
print(response) Execute SQL QueryUse the 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
response = chain.invoke({"question": "How many employees are there"})
print(response) Answer the QuestionCombine the original question and SQL query result to generate a final answer: from operator import itemgetter
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
answer_prompt = PromptTemplate.from_template(
"""Given the following user question, corresponding SQL query, and SQL result, answer the user question.
Question: {question}
SQL Query: {query}
SQL Result: {result}
Answer: """
)
chain = (
RunnablePassthrough.assign(query=write_query).assign(
result=itemgetter("query") | execute_query
)
| answer_prompt
| llm
| StrOutputParser()
)
response = chain.invoke({"question": "How many employees are there"})
print(response) Using AgentsLangChain also provides a more flexible way of interacting with SQL databases using agents: from langchain_community.agent_toolkits import SQLDatabaseToolkit
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
tools = toolkit.get_tools()
from langchain_core.messages import SystemMessage
SQL_PREFIX = """You are an agent designed to interact with a SQL database.
Given an input question, create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 5 results.
You can order the results by a relevant column to return the most interesting examples in the database.
Never query for all the columns from a specific table, only ask for the relevant columns given the question.
You have access to tools for interacting with the database.
Only use the below tools. Only use the information returned by the below tools to construct your final answer.
You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.
To start you should ALWAYS look at the tables in the database to see what you can query.
Do NOT skip this step.
Then you should query the schema of the most relevant tables."""
system_message = SystemMessage(content=SQL_PREFIX)
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent
agent_executor = create_react_agent(llm, tools, messages_modifier=system_message)
for s in agent_executor.stream(
{"messages": [HumanMessage(content="Which country's customers spent the most?")]}
):
print(s)
print("----") These examples demonstrate how LangChain can be used to create a Q&A system over SQL data, similar to the Llama Index Pandas query pipeline [1]. |
Beta Was this translation helpful? Give feedback.
-
@dosu Do you have any specific examples related to pandas pipe line? I am interested in Pandas more than SQL for my use case. Thank you. |
Beta Was this translation helpful? Give feedback.
-
@dosu... is that possible to combine Pandas query pipe lines with regular retrievers? I can do that in Lama Index. Like to know some examples if possible. Thank you. |
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
Is it Langchanin has any concepts which related to Llama Index Pandas query pipe line?
If yes, can you give me some code examples please?
The reference from Llama Index: https://docs.llamaindex.ai/en/stable/examples/query_engine/pandas_query_engine/
System Info
Using latest version of Llangchain and Langgraph.
Beta Was this translation helpful? Give feedback.
All reactions