Kush1 commited on
Commit
1f11f82
·
verified ·
1 Parent(s): d0ef7f5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)