import streamlit as st
import langchain
from langchain.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain
from langchain.chat_models import ChatOpenAI
from langsmith import Client
from langchain.smith import RunEvalConfig, run_on_dataset
from pydantic import BaseModel, Field

db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = ChatOpenAI(temperature=0)
db_chain = SQLDatabaseChain.from_llm(llm, db, return_intermediate_steps=True)

from langsmith import Client
client = Client()
def send_feedback(run_id, score):
    client.create_feedback(run_id, "user_score", score=score)

st.set_page_config(page_title='🦜🔗 Ask the SQL DB App')
st.title('🦜🔗 Ask the SQL DB App')
st.info("Most 'question answering' applications run over unstructured text data. But a lot of the data in the world is tabular data! This is an attempt to create an application using [LangChain](https://github.com/langchain-ai/langchain) to let you ask questions of data in tabular format. For this demo application, we will use the Chinook dataset in a SQL database. Please explore the schema [here](https://www.sqlitetutorial.net/wp-content/uploads/2015/11/sqlite-sample-database-color.jpg) to get a sense for what questions you can ask. Please leave feedback on well the question is answered, and we will use that improve the application!")

query_text = st.text_input('Enter your question:', placeholder = 'Ask something like "How many artists are there?" or "Which artist has the most albums"')
# Form input and query
result = None
with st.form('myform', clear_on_submit=True):
	submitted = st.form_submit_button('Submit')
	if submitted:
		with st.spinner('Calculating...'):
			inputs = {"query": query_text}
			response = db_chain(inputs, include_run_info=True)
			result = response["result"]
			sql_command = response["intermediate_steps"][1]
			sql_result = response["intermediate_steps"][3]
			run_id = response["__run"].run_id
if result is not None:
	st.info(result)
	st.code(sql_command)
	st.code(sql_result)
	col_blank, col_text, col1, col2 = st.columns([10, 2,1,1])
	with col_text:
		st.text("Feedback:")
	with col1:
		st.button("👍", on_click=send_feedback, args=(run_id, 1))
	with col2:
		st.button("👎", on_click=send_feedback, args=(run_id, 0))


langchain_experimental.sql.SQLDatabaseChain.from_llm langchain.utilities.SQLDatabase.from_uri langchain.chat_models.ChatOpenAI
[((316, 360), 'langchain.utilities.SQLDatabase.from_uri', 'SQLDatabase.from_uri', (['"""sqlite:///Chinook.db"""'], {}), "('sqlite:///Chinook.db')\n", (336, 360), False, 'from langchain.utilities import SQLDatabase\n'), ((367, 392), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'temperature': '(0)'}), '(temperature=0)\n', (377, 392), False, 'from langchain.chat_models import ChatOpenAI\n'), ((404, 470), 'langchain_experimental.sql.SQLDatabaseChain.from_llm', 'SQLDatabaseChain.from_llm', (['llm', 'db'], {'return_intermediate_steps': '(True)'}), '(llm, db, return_intermediate_steps=True)\n', (429, 470), False, 'from langchain_experimental.sql import SQLDatabaseChain\n'), ((510, 518), 'langsmith.Client', 'Client', ([], {}), '()\n', (516, 518), False, 'from langsmith import Client\n'), ((616, 670), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""🦜🔗 Ask the SQL DB App"""'}), "(page_title='🦜🔗 Ask the SQL DB App')\n", (634, 670), True, 'import streamlit as st\n'), ((671, 704), 'streamlit.title', 'st.title', (['"""🦜🔗 Ask the SQL DB App"""'], {}), "('🦜🔗 Ask the SQL DB App')\n", (679, 704), True, 'import streamlit as st\n'), ((705, 1358), 'streamlit.info', 'st.info', (['"""Most \'question answering\' applications run over unstructured text data. But a lot of the data in the world is tabular data! This is an attempt to create an application using [LangChain](https://github.com/langchain-ai/langchain) to let you ask questions of data in tabular format. For this demo application, we will use the Chinook dataset in a SQL database. Please explore the schema [here](https://www.sqlitetutorial.net/wp-content/uploads/2015/11/sqlite-sample-database-color.jpg) to get a sense for what questions you can ask. Please leave feedback on well the question is answered, and we will use that improve the application!"""'], {}), '(\n    "Most \'question answering\' applications run over unstructured text data. But a lot of the data in the world is tabular data! This is an attempt to create an application using [LangChain](https://github.com/langchain-ai/langchain) to let you ask questions of data in tabular format. For this demo application, we will use the Chinook dataset in a SQL database. Please explore the schema [here](https://www.sqlitetutorial.net/wp-content/uploads/2015/11/sqlite-sample-database-color.jpg) to get a sense for what questions you can ask. Please leave feedback on well the question is answered, and we will use that improve the application!"\n    )\n', (712, 1358), True, 'import streamlit as st\n'), ((1363, 1512), 'streamlit.text_input', 'st.text_input', (['"""Enter your question:"""'], {'placeholder': '"""Ask something like "How many artists are there?" or "Which artist has the most albums\\""""'}), '(\'Enter your question:\', placeholder=\n    \'Ask something like "How many artists are there?" or "Which artist has the most albums"\'\n    )\n', (1376, 1512), True, 'import streamlit as st\n'), ((1547, 1586), 'streamlit.form', 'st.form', (['"""myform"""'], {'clear_on_submit': '(True)'}), "('myform', clear_on_submit=True)\n", (1554, 1586), True, 'import streamlit as st\n'), ((1601, 1632), 'streamlit.form_submit_button', 'st.form_submit_button', (['"""Submit"""'], {}), "('Submit')\n", (1622, 1632), True, 'import streamlit as st\n'), ((1966, 1981), 'streamlit.info', 'st.info', (['result'], {}), '(result)\n', (1973, 1981), True, 'import streamlit as st\n'), ((1983, 2003), 'streamlit.code', 'st.code', (['sql_command'], {}), '(sql_command)\n', (1990, 2003), True, 'import streamlit as st\n'), ((2005, 2024), 'streamlit.code', 'st.code', (['sql_result'], {}), '(sql_result)\n', (2012, 2024), True, 'import streamlit as st\n'), ((2060, 2085), 'streamlit.columns', 'st.columns', (['[10, 2, 1, 1]'], {}), '([10, 2, 1, 1])\n', (2070, 2085), True, 'import streamlit as st\n'), ((2102, 2122), 'streamlit.text', 'st.text', (['"""Feedback:"""'], {}), "('Feedback:')\n", (2109, 2122), True, 'import streamlit as st\n'), ((2137, 2193), 'streamlit.button', 'st.button', (['"""👍"""'], {'on_click': 'send_feedback', 'args': '(run_id, 1)'}), "('👍', on_click=send_feedback, args=(run_id, 1))\n", (2146, 2193), True, 'import streamlit as st\n'), ((2208, 2264), 'streamlit.button', 'st.button', (['"""👎"""'], {'on_click': 'send_feedback', 'args': '(run_id, 0)'}), "('👎', on_click=send_feedback, args=(run_id, 0))\n", (2217, 2264), True, 'import streamlit as st\n'), ((1655, 1683), 'streamlit.spinner', 'st.spinner', (['"""Calculating..."""'], {}), "('Calculating...')\n", (1665, 1683), True, 'import streamlit as st\n')]