Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +66 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,68 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain_community.utilities import SQLDatabase
|
| 3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
from langchain_community.agent_toolkits import SQLDatabaseToolkit
|
| 5 |
+
from langgraph.prebuilt import create_react_agent
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
|
| 9 |
+
db = SQLDatabase.from_uri("sqlite:///SQLite.db")
|
| 10 |
+
print(db.dialect)
|
| 11 |
+
print(db.get_usable_table_names())
|
| 12 |
+
db.run("SELECT * FROM Artist LIMIT 10;")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 16 |
+
# Ensure GOOGLE_API_KEY is set in your environment
|
| 17 |
+
|
| 18 |
+
# Initialize the Chat Model
|
| 19 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0, api_key = api_key)
|
| 20 |
+
messages = [
|
| 21 |
+
("system", "You are a helpful assistant that translates English to French."),
|
| 22 |
+
("human", "I love programming."),
|
| 23 |
+
]
|
| 24 |
+
llm.invoke(messages)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
| 28 |
+
|
| 29 |
+
tools = toolkit.get_tools()
|
| 30 |
+
|
| 31 |
+
system_message = """
|
| 32 |
+
You are an agent designed to interact with a SQL database.
|
| 33 |
+
Given an input question, create a syntactically correct {dialect} query to run,
|
| 34 |
+
then look at the results of the query and return the answer. Unless the user
|
| 35 |
+
specifies a specific number of examples they wish to obtain, always limit your
|
| 36 |
+
query to at most {top_k} results.
|
| 37 |
+
|
| 38 |
+
You can order the results by a relevant column to return the most interesting
|
| 39 |
+
examples in the database. Never query for all the columns from a specific table,
|
| 40 |
+
only ask for the relevant columns given the question.
|
| 41 |
+
|
| 42 |
+
You MUST double check your query before executing it. If you get an error while
|
| 43 |
+
executing a query, rewrite the query and try again.
|
| 44 |
+
|
| 45 |
+
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the
|
| 46 |
+
database.
|
| 47 |
+
|
| 48 |
+
To start you should ALWAYS look at the tables in the database to see what you
|
| 49 |
+
can query. Do NOT skip this step.
|
| 50 |
+
|
| 51 |
+
Then you should query the schema of the most relevant tables.
|
| 52 |
+
""".format(
|
| 53 |
+
dialect="SQLite",
|
| 54 |
+
top_k=5,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
agent_executor = create_react_agent(llm, tools, prompt=system_message)
|
| 58 |
+
st.title("SQL AI Agent")
|
| 59 |
+
question = st.text_input("Enter your question about the database:")
|
| 60 |
+
if question:
|
| 61 |
+
for step in agent_executor.stream(
|
| 62 |
+
{"messages": [{"role": "user", "content": question}]},
|
| 63 |
+
stream_mode='values'
|
| 64 |
+
):
|
| 65 |
+
# Display each step's message content in Streamlit
|
| 66 |
+
print(step["messages"][-1].pretty_print())
|
| 67 |
+
|
| 68 |
+
st.write(step["messages"][-1].content)
|