import os
#from dotenv import load_dotenv
import openai
import langchain

os.environ["OPENAI_API_KEY"] =""
os.environ["SQL_SERVER_USERNAME"] = "" 
os.environ["SQL_SERVER_ENDPOINT"] = ""
os.environ["SQL_SERVER_PASSWORD"] = ""  
os.environ["SQL_SERVER_DATABASE"] = ""

from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from langchain.sql_database import SQLDatabase

db_config = {  
    'drivername': 'mssql+pyodbc',  
    'username': os.environ["SQL_SERVER_USERNAME"] + '@' + os.environ["SQL_SERVER_ENDPOINT"],  
    'password': os.environ["SQL_SERVER_PASSWORD"],  
    'host': os.environ["SQL_SERVER_ENDPOINT"],  
    'port': 1433,  
    'database': os.environ["SQL_SERVER_DATABASE"],  
    'query': {'driver': 'ODBC Driver 17 for SQL Server'}  
}  

db_url = URL.create(**db_config)
db = SQLDatabase.from_uri(db_url)

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain.agents import create_sql_agent
#from langchain.callbacks import StreamlitCallbackHandler
import streamlit as st

# Page title
st.set_page_config(page_title='🦜🔗 Ask the SQLSaturday App')
st.title('📎Ask the SQLSaturda Oslo DB with Clippy!')


def generate_response(input_query):
    llm = OpenAI(temperature=0)
    toolkit = SQLDatabaseToolkit(db=db, llm=llm)
    agent_executor = create_sql_agent(
        llm=llm,
        toolkit=toolkit,
        verbose=True,
        agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    )
    response = agent_executor.run(input_query)
    return st.success(response)

question_list = [
  'How many rows are there?',
  'What kind of tables are here?',
  'How many are called John?',
  'Other']
query_text = st.selectbox('Select an example query:', question_list)
openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not (query_text))

# App logic
if query_text == 'Other':
  query_text = st.text_input('Enter your query:', placeholder = 'Enter query here ...')
if not openai_api_key.startswith('sk-'):
  st.warning('Please enter your OpenAI API key!', icon='⚠')
if openai_api_key.startswith('sk-'):
  st.header('Output')
  generate_response(query_text)






langchain.agents.create_sql_agent langchain.agents.agent_toolkits.SQLDatabaseToolkit langchain.llms.OpenAI langchain.sql_database.SQLDatabase.from_uri
[((785, 808), 'sqlalchemy.engine.url.URL.create', 'URL.create', ([], {}), '(**db_config)\n', (795, 808), False, 'from sqlalchemy.engine.url import URL\n'), ((814, 842), 'langchain.sql_database.SQLDatabase.from_uri', 'SQLDatabase.from_uri', (['db_url'], {}), '(db_url)\n', (834, 842), False, 'from langchain.sql_database import SQLDatabase\n'), ((1348, 1407), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""🦜🔗 Ask the SQLSaturday App"""'}), "(page_title='🦜🔗 Ask the SQLSaturday App')\n", (1366, 1407), True, 'import streamlit as st\n'), ((1408, 1460), 'streamlit.title', 'st.title', (['"""📎Ask the SQLSaturda Oslo DB with Clippy!"""'], {}), "('📎Ask the SQLSaturda Oslo DB with Clippy!')\n", (1416, 1460), True, 'import streamlit as st\n'), ((1965, 2020), 'streamlit.selectbox', 'st.selectbox', (['"""Select an example query:"""', 'question_list'], {}), "('Select an example query:', question_list)\n", (1977, 2020), True, 'import streamlit as st\n'), ((2038, 2111), 'streamlit.text_input', 'st.text_input', (['"""OpenAI API Key"""'], {'type': '"""password"""', 'disabled': '(not query_text)'}), "('OpenAI API Key', type='password', disabled=not query_text)\n", (2051, 2111), True, 'import streamlit as st\n'), ((1509, 1530), 'langchain.llms.OpenAI', 'OpenAI', ([], {'temperature': '(0)'}), '(temperature=0)\n', (1515, 1530), False, 'from langchain.llms import OpenAI\n'), ((1545, 1579), 'langchain.agents.agent_toolkits.SQLDatabaseToolkit', 'SQLDatabaseToolkit', ([], {'db': 'db', 'llm': 'llm'}), '(db=db, llm=llm)\n', (1563, 1579), False, 'from langchain.agents.agent_toolkits import SQLDatabaseToolkit\n'), ((1601, 1712), 'langchain.agents.create_sql_agent', 'create_sql_agent', ([], {'llm': 'llm', 'toolkit': 'toolkit', 'verbose': '(True)', 'agent_type': 'AgentType.ZERO_SHOT_REACT_DESCRIPTION'}), '(llm=llm, toolkit=toolkit, verbose=True, agent_type=\n    AgentType.ZERO_SHOT_REACT_DESCRIPTION)\n', (1617, 1712), False, 'from langchain.agents import create_sql_agent\n'), ((1805, 1825), 'streamlit.success', 'st.success', (['response'], {}), '(response)\n', (1815, 1825), True, 'import streamlit as st\n'), ((2168, 2238), 'streamlit.text_input', 'st.text_input', (['"""Enter your query:"""'], {'placeholder': '"""Enter query here ..."""'}), "('Enter your query:', placeholder='Enter query here ...')\n", (2181, 2238), True, 'import streamlit as st\n'), ((2284, 2341), 'streamlit.warning', 'st.warning', (['"""Please enter your OpenAI API key!"""'], {'icon': '"""⚠"""'}), "('Please enter your OpenAI API key!', icon='⚠')\n", (2294, 2341), True, 'import streamlit as st\n'), ((2381, 2400), 'streamlit.header', 'st.header', (['"""Output"""'], {}), "('Output')\n", (2390, 2400), True, 'import streamlit as st\n')]