Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookPrompt + LLMRAGMultiple chainsQuerying a SQL DBAgentsCode writingAdding memoryAdding moderationUsing toolsLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageCookbookQuerying a SQL DBQuerying a SQL DBWe can replicate our SQLDatabaseChain with Runnables.from langchain.prompts import ChatPromptTemplatetemplate = """Based on the table schema below, write a SQL query that would answer the user's question:{schema}Question: {question}SQL Query:"""prompt = ChatPromptTemplate.from_template(template)from langchain.utilities import SQLDatabaseWe'll need the Chinook sample DB for this example. There's many places to download it from, e.g. https://database.guide/2-sample-databases-sqlite/db = SQLDatabase.from_uri("sqlite:///./Chinook.db")def get_schema(_):    return db.get_table_info()def run_query(query):    return db.run(query)from langchain.chat_models import ChatOpenAIfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughmodel = ChatOpenAI()sql_response = (        RunnablePassthrough.assign(schema=get_schema)        | prompt        | model.bind(stop=["\nSQLResult:"])        | StrOutputParser()    )sql_response.invoke({"question": "How many employees are there?"})    'SELECT COUNT(*) FROM Employee'template = """Based on the table schema below, question, sql query, and sql response, write a natural language response:{schema}Question: {question}SQL Query: {query}SQL Response: {response}"""prompt_response = ChatPromptTemplate.from_template(template)full_chain = (    RunnablePassthrough.assign(query=sql_response)     | RunnablePassthrough.assign(        schema=get_schema,        response=lambda x:
We can replicate our SQLDatabaseChain with Runnables.
We can replicate our SQLDatabaseChain with Runnables. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookPrompt + LLMRAGMultiple chainsQuerying a SQL DBAgentsCode writingAdding memoryAdding moderationUsing toolsLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageCookbookQuerying a SQL DBQuerying a SQL DBWe can replicate our SQLDatabaseChain with Runnables.from langchain.prompts import ChatPromptTemplatetemplate = """Based on the table schema below, write a SQL query that would answer the user's question:{schema}Question: {question}SQL Query:"""prompt = ChatPromptTemplate.from_template(template)from langchain.utilities import SQLDatabaseWe'll need the Chinook sample DB for this example. There's many places to download it from, e.g. https://database.guide/2-sample-databases-sqlite/db = SQLDatabase.from_uri("sqlite:///./Chinook.db")def get_schema(_):    return db.get_table_info()def run_query(query):    return db.run(query)from langchain.chat_models import ChatOpenAIfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughmodel = ChatOpenAI()sql_response = (        RunnablePassthrough.assign(schema=get_schema)        | prompt        | model.bind(stop=["\nSQLResult:"])        | StrOutputParser()    )sql_response.invoke({"question": "How many employees are there?"})    'SELECT COUNT(*) FROM Employee'template = """Based on the table schema below, question, sql query, and sql response, write a natural language response:{schema}Question: {question}SQL Query: {query}SQL Response: {response}"""prompt_response = ChatPromptTemplate.from_template(template)full_chain = (    RunnablePassthrough.assign(query=sql_response)     | RunnablePassthrough.assign(        schema=get_schema,        response=lambda x: