cboettig commited on
Commit
9d1df78
β€’
1 Parent(s): c8c9b14

here we go

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +66 -119
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: indigo
5
  colorTo: gray
6
  sdk: streamlit
7
  sdk_version: 1.32.2
8
- app_file: minimal_agent.py
9
  pinned: false
10
  license: bsd-2-clause
11
  ---
 
5
  colorTo: gray
6
  sdk: streamlit
7
  sdk_version: 1.32.2
8
+ app_file: app.py
9
  pinned: false
10
  license: bsd-2-clause
11
  ---
app.py CHANGED
@@ -1,121 +1,68 @@
1
  import streamlit as st
2
- from openai import OpenAI
3
- import duckdb
4
-
5
- EXAMPLE_PROMPTS = [
6
- "What are the unique values of Mang Type?",
7
- "What is the mean species richness of each GAP_Sts?",
8
- "STAT, LOC, FED, and DIST Mang types are all 'Public' land types. PVT and NGO are 'Private' land types. What is the average richness in public, private, and other land types?",
9
- ]
10
-
11
- TITLE = "Protected Lands SQL Assistant"
12
- # DESCRIPTION = """[DBRX Instruct](https://huggingface.co/databricks/dbrx-instruct) is a mixture-of-experts (MoE) large language model trained by the Mosaic Research team at Databricks. Users can interact with this model in the [DBRX Playground](https://huggingface.co/spaces/databricks/dbrx-instruct), subject to the terms and conditions below.
13
- # This demo is powered by [Databricks Foundation Model APIs](https://docs.databricks.com/en/machine-learning/foundation-models/index.html).
14
- DESCRIPTION="""
15
- This is a test
16
- """
17
-
18
-
19
-
20
- duckdb.install_extension("spatial")
21
- duckdb.load_extension("spatial")
22
- duckdb.install_extension("httpfs")
23
- duckdb.load_extension("httpfs")
24
-
25
- duckdb.sql("create or replace view pad as select * from read_parquet('https://data.source.coop/cboettig/pad-us-3/pad-mobi.parquet')")
26
-
27
- st.title(TITLE)
28
-
29
- # Set OpenAI API key from Streamlit secrets
30
- client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
31
-
32
- # Set a default model
33
- if "openai_model" not in st.session_state:
34
- st.session_state["openai_model"] = "gpt-3.5-turbo"
35
- # "gpt-4"
36
-
37
- # Initialize chat history
38
- if "messages" not in st.session_state:
39
- st.session_state.messages = []
40
-
41
-
42
- setup = '''
43
- You are a database administrator, and expert in SQL. You will be helping me write complex SQL queries. I will explain you my needs, you will generate SQL queries against my database.
44
-
45
- My application does: Conservation prioritization of protected areas to help meet US 30x30 conservation goals.
46
-
47
- Please reply only with the SQL code that I will need to execute. Do not include an explanation of the code.
48
- Please reply with raw SQL, do not use markdown formatting.
49
-
50
- The database is a POSTGIS Postgres database, please take it into consideration when generating PLSQL/SQL. Please avoid ST_Within queries if possible, because they are so slow.
51
-
52
- I will provide you with a description of the structure of my tables. You must remember them and use them for generating SQL queries. Once you read them all, just remember them for future and say nothing else.
53
-
54
- Here are the tables :
55
-
56
- Table "pad"
57
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
58
- β”‚ column_name β”‚ column_type β”‚ null β”‚ key β”‚ default β”‚ extra β”‚
59
- β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚
60
- β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
61
- β”‚ FID β”‚ INTEGER β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
62
- β”‚ time β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
63
- β”‚ rsr β”‚ DOUBLE β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
64
- β”‚ richness β”‚ DOUBLE β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
65
- β”‚ bucket β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
66
- β”‚ FeatClass β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
67
- β”‚ Mang_Name β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
68
- β”‚ Mang_Type β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
69
- β”‚ Des_Tp β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
70
- β”‚ Pub_Access β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
71
- β”‚ GAP_Sts β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
72
- β”‚ IUCN_Cat β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
73
- β”‚ Unit_Nm β”‚ VARCHAR β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
74
- β”‚ area β”‚ DOUBLE β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
75
- β”‚ geometry β”‚ BLOB β”‚ YES β”‚ NULL β”‚ NULL β”‚ NULL β”‚
76
- β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
77
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
78
- '''
79
-
80
- # Display chat messages from history on app rerun
81
- #for message in st.session_state.messages:
82
- # with st.chat_message(message["role"]):
83
- # st.markdown(message["content"])
84
-
85
- # Accept user input
86
- if prompt := st.chat_input("What is the total area in each GAP_Sts?"):
87
- # Add user message to chat history
88
- st.session_state.messages.append({"role": "system", "content": setup})
89
-
90
- st.session_state.messages.append({"role": "user", "content": prompt})
91
- # Display user message in chat message container
92
- # with st.chat_message("user"):
93
- #st.markdown(prompt)
94
-
95
- # Display assistant response in chat message container
96
  with st.chat_message("assistant"):
97
- stream = client.chat.completions.create(
98
- model=st.session_state["openai_model"],
99
- messages=[
100
- {"role": m["role"], "content": m["content"]}
101
- for m in st.session_state.messages
102
- ],
103
- stream=True,
104
- )
105
- response = st.write_stream(stream)
106
- st.divider()
107
- # st.write(response)
108
- df = duckdb.sql(response).df()
109
- st.table(df)
110
- st.session_state.messages.append({"role": "assistant", "content": response})
111
-
112
-
113
-
114
- with st.sidebar:
115
- with st.container():
116
- st.title("Examples")
117
- for prompt in EXAMPLE_PROMPTS:
118
- st.markdown(prompt)
119
-
120
-
121
- # st.button(prompt, args=(prompt,)), on_click=handle_user_input)
 
1
  import streamlit as st
2
+ from pathlib import Path
3
+ from langchain.llms.openai import OpenAI
4
+ from langchain.agents import create_sql_agent
5
+ from langchain.sql_database import SQLDatabase
6
+ from langchain.agents.agent_types import AgentType
7
+ from langchain.callbacks import StreamlitCallbackHandler
8
+ from langchain.agents.agent_toolkits import SQLDatabaseToolkit
9
+ from sqlalchemy import create_engine
10
+ import sqlite3
11
+ import os
12
+ from langchain_openai import ChatOpenAI
13
+
14
+
15
+ os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
16
+ openai_api_key = st.secrets["OPENAI_API_KEY"]
17
+
18
+ st.set_page_config(page_title="Chat with Protected Areas Database", page_icon="🦜")
19
+ st.title("🦜 LangChain: Chat with Protected Areas Database")
20
+
21
+ INJECTION_WARNING = """
22
+ Experimental!
23
+ """
24
+ LOCALDB = "duckdb:///pad.duckdb"
25
+
26
+ # User inputs
27
+ radio_opt = ["US Protected Areas v3"]
28
+ selected_opt = st.sidebar.radio(label="Choose suitable option", options=radio_opt)
29
+ if radio_opt.index(selected_opt) == 1:
30
+ st.sidebar.warning(INJECTION_WARNING, icon="⚠️")
31
+ db_uri = st.sidebar.text_input(
32
+ label="Database URI", placeholder="duckdb:///pad.duckdb"
33
+ )
34
+ else:
35
+ db_uri = LOCALDB
36
+
37
+ #st.sidebar.text_input(label="OpenAI API Key", type="password")
38
+
39
+
40
+ # Setup agent
41
+ llm = OpenAI(openai_api_key=openai_api_key, model="gpt-3.5-turbo", temperature=0, streaming=True)
42
+
43
+ @st.cache_resource(ttl="2h")
44
+ def configure_db(db_uri):
45
+ return SQLDatabase.from_uri(database_uri=db_uri, view_support=True)
46
+
47
+ db = configure_db(db_uri)
48
+
49
+ llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
50
+ agent = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True)
51
+
52
+ if "messages" not in st.session_state or st.sidebar.button("Clear message history"):
53
+ st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
54
+
55
+ for msg in st.session_state.messages:
56
+ st.chat_message(msg["role"]).write(msg["content"])
57
+
58
+ user_query = st.chat_input(placeholder="Ask me anything!")
59
+
60
+ if user_query:
61
+ st.session_state.messages.append({"role": "user", "content": user_query})
62
+ st.chat_message("user").write(user_query)
63
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with st.chat_message("assistant"):
65
+ st_cb = StreamlitCallbackHandler(st.container())
66
+ response = agent.run(user_query, callbacks=[st_cb])
67
+ st.session_state.messages.append({"role": "assistant", "content": response})
68
+ st.write(response)