-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (61 loc) · 2.15 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
import os
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import Cohere
from dotenv import load_dotenv
load_dotenv()
cohere_api = '30Q3NniolKt7XuDD8JIv08HU6l7QeSEo9DmNJRu3'
template = """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_messages(
[
("system", "Given an input question, convert it to a SQL query. No preamble."),
("human", template),
]
)
cohere_llm = Cohere(model="command", temperature=0.1, cohere_api_key=cohere_api)
st.set_page_config(
page_title="SQL Gen",
page_icon="🤖",
layout="wide",
initial_sidebar_state="auto",
menu_items={
'About': 'Hello'
}
)
def main():
st.title("SQL Gen")
st.write("A simple tool to generate SQL queries")
schema = st.text_area("Enter schema", key="schema")
if schema:
generate(schema)
def generate(schema):
question = st.text_input("Enter your question", key="query")
if st.button("Generate SQL"):
if not question:
st.error("Please enter question !!")
else:
with st.spinner("Please wait for a few seconds :)"):
try:
input_data = {"schema": schema, "question": question}
sql_response = (
prompt
| cohere_llm.bind(stop=["\nSQLResult:"])
| StrOutputParser()
)
result = sql_response.invoke(input_data)
st.success("SQL query generated successfully.")
st.code(result, language="sql")
except Exception as e:
st.error(f"An error occurred: {e}")
st.error(f"Details: {str(e)}")
if st.button("RESET"):
try:
st.rerun()
except Exception as e:
st.error(f"An error occurred while resetting the page: {e}")
if __name__ == "__main__":
main()