Skip to content

Commit 42f7c82

Browse files
first commit
1 parent b6ee6cb commit 42f7c82

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed

.env.sample

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GOOGLE_API_KEY="your_api_key"

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 5
6+
}

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,46 @@ The chatbot will be designed to understand and respond to layman language questi
1313

1414
A user interface will be created using Streamlit where users can input their PostgreSQL credentials and ask questions.
1515

16+
### Setup Instructions
17+
18+
Follow these steps to set up the project on your local machine:
19+
20+
21+
**1. Clone the Repository**
22+
Begin by cloning the repository to your local machine:
23+
```
24+
https://github.com/langchain-tech/postgres-chatbot.git
25+
cd postgres-chatbot
26+
```
27+
28+
**2. Create a Virtual Environment**
29+
It is recommended to create a virtual environment to manage dependencies:
30+
```
31+
python -m venv venv
32+
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
33+
```
34+
35+
**3. Install Dependencies**
36+
Install the necessary packages listed in the requirements.txt file:
37+
```
38+
pip install -r requirements.txt
39+
```
40+
41+
42+
**4. Set Up Environment Variables**
43+
Create a .env file in the root directory of your project and add the required environment variables. For example:
44+
```
45+
LANGCHAIN_API_KEY=your_langchain_api_key
46+
```
47+
48+
49+
**5. Start the Application**
50+
51+
Run the application using Streamlit:
52+
```
53+
streamlit run app.py
54+
```
55+
1656
### Notes
1757

1858
- Questions will not be limited to one table, so it should be done to ask from database

app.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
import asyncio
3+
import streamlit as st
4+
from langchain_community.utilities import SQLDatabase
5+
from langchain_google_genai import ChatGoogleGenerativeAI
6+
from langchain_community.agent_toolkits import create_sql_agent
7+
8+
from dotenv import load_dotenv
9+
load_dotenv()
10+
11+
os.environ["GOOGLE_API_KEY"]=os.getenv("GOOGLE_API_KEY")
12+
groq_api_key=os.getenv('GROQ_API_KEY')
13+
14+
async def get_answer_by_sql_query(db,input):
15+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro-latest",temperature=0)
16+
agent_executor = create_sql_agent(llm, db=db, verbose=True)
17+
18+
response= agent_executor.invoke({"input": input})
19+
return response
20+
21+
22+
def main():
23+
st.header('Chat with your Postgres Sql Database...')
24+
25+
if "conversation" not in st.session_state:
26+
st.session_state.conversation = None
27+
28+
if "activate_chat" not in st.session_state:
29+
st.session_state.activate_chat = False
30+
31+
if "messages" not in st.session_state:
32+
st.session_state.messages = []
33+
st.session_state.chat_history=[]
34+
35+
for message in st.session_state.messages:
36+
with st.chat_message(message["role"], avatar = message['avatar']):
37+
st.markdown(message["content"])
38+
39+
with st.sidebar:
40+
st.subheader('Please Enter your credentials')
41+
username = st.text_input("enter your username")
42+
password= st.text_input("enter your password")
43+
db_name= st.text_input("enter your database name")
44+
if st.button('Process'):
45+
if username is not None and password is not None:
46+
postgres_uri = f"postgresql+psycopg2://{username}:{password}@localhost:5432/{db_name}"
47+
st.write(f'Processed database: {postgres_uri}')
48+
st.write(f'Connection to database successfully..')
49+
50+
db = SQLDatabase.from_uri(postgres_uri)
51+
if "db" not in st.session_state:
52+
st.session_state.db = db
53+
st.session_state.activate_chat = True
54+
55+
if st.session_state.activate_chat == True:
56+
if prompt := st.chat_input("Ask your question from the Database"):
57+
with st.chat_message("user", avatar = '👨🏻'):
58+
st.markdown(prompt)
59+
st.session_state.messages.append({"role": "user", "avatar" :'👨🏻', "content": prompt})
60+
db = st.session_state.db
61+
response = asyncio.run(get_answer_by_sql_query(db,prompt))
62+
cleaned_response=response["output"]
63+
with st.chat_message("assistant", avatar='🤖'):
64+
st.markdown(cleaned_response)
65+
st.session_state.messages.append({"role": "assistant", "avatar" :'🤖', "content": cleaned_response})
66+
else:
67+
st.markdown('Please Enter your credentials to chat')
68+
69+
70+
if __name__ == '__main__':
71+
main()

requirements.txt

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
aiohttp==3.9.5
2+
aiosignal==1.3.1
3+
altair==5.3.0
4+
annotated-types==0.7.0
5+
anyio==4.3.0
6+
asttokens @ file:///home/conda/feedstock_root/build_artifacts/asttokens_1698341106958/work
7+
async-timeout==4.0.3
8+
attrs==23.2.0
9+
blinker==1.8.2
10+
cachetools==5.3.3
11+
certifi==2024.2.2
12+
charset-normalizer==3.3.2
13+
click==8.1.7
14+
comm @ file:///home/conda/feedstock_root/build_artifacts/comm_1710320294760/work
15+
dataclasses-json==0.6.6
16+
debugpy @ file:///croot/debugpy_1690905042057/work
17+
decorator @ file:///home/conda/feedstock_root/build_artifacts/decorator_1641555617451/work
18+
distro==1.9.0
19+
exceptiongroup @ file:///home/conda/feedstock_root/build_artifacts/exceptiongroup_1704921103267/work
20+
executing @ file:///home/conda/feedstock_root/build_artifacts/executing_1698579936712/work
21+
frozenlist==1.4.1
22+
gitdb==4.0.11
23+
GitPython==3.1.43
24+
google-ai-generativelanguage==0.6.4
25+
google-api-core==2.19.0
26+
google-api-python-client==2.129.0
27+
google-auth==2.29.0
28+
google-auth-httplib2==0.2.0
29+
google-generativeai==0.5.4
30+
googleapis-common-protos==1.63.0
31+
greenlet==3.0.3
32+
groq==0.7.0
33+
grpcio==1.64.0
34+
grpcio-status==1.62.2
35+
h11==0.14.0
36+
httpcore==1.0.5
37+
httplib2==0.22.0
38+
httpx==0.27.0
39+
idna==3.7
40+
importlib_metadata @ file:///home/conda/feedstock_root/build_artifacts/importlib-metadata_1710971335535/work
41+
ipykernel @ file:///home/conda/feedstock_root/build_artifacts/ipykernel_1708996548741/work
42+
ipython @ file:///home/conda/feedstock_root/build_artifacts/ipython_1701831663892/work
43+
jedi @ file:///home/conda/feedstock_root/build_artifacts/jedi_1696326070614/work
44+
Jinja2==3.1.4
45+
jsonpatch==1.33
46+
jsonpointer==2.4
47+
jsonschema==4.22.0
48+
jsonschema-specifications==2023.12.1
49+
jupyter_client @ file:///home/conda/feedstock_root/build_artifacts/jupyter_client_1710255804825/work
50+
jupyter_core @ file:///home/conda/feedstock_root/build_artifacts/jupyter_core_1710257447442/work
51+
langchain==0.2.0
52+
langchain-community==0.2.0
53+
langchain-core==0.2.0
54+
langchain-google-genai==1.0.4
55+
langchain-text-splitters==0.2.0
56+
langsmith==0.1.60
57+
markdown-it-py==3.0.0
58+
MarkupSafe==2.1.5
59+
marshmallow==3.21.2
60+
matplotlib-inline @ file:///home/conda/feedstock_root/build_artifacts/matplotlib-inline_1713250518406/work
61+
mdurl==0.1.2
62+
multidict==6.0.5
63+
mypy-extensions==1.0.0
64+
nest_asyncio @ file:///home/conda/feedstock_root/build_artifacts/nest-asyncio_1705850609492/work
65+
numpy==1.26.4
66+
orjson==3.10.3
67+
packaging==23.2
68+
pandas==2.2.2
69+
parso @ file:///home/conda/feedstock_root/build_artifacts/parso_1712320355065/work
70+
pexpect @ file:///home/conda/feedstock_root/build_artifacts/pexpect_1706113125309/work
71+
pickleshare @ file:///home/conda/feedstock_root/build_artifacts/pickleshare_1602536217715/work
72+
pillow==10.3.0
73+
platformdirs @ file:///home/conda/feedstock_root/build_artifacts/platformdirs_1715777629804/work
74+
prompt-toolkit @ file:///home/conda/feedstock_root/build_artifacts/prompt-toolkit_1702399386289/work
75+
proto-plus==1.23.0
76+
protobuf==4.25.3
77+
psutil @ file:///home/conda/feedstock_root/build_artifacts/psutil_1705722404069/work
78+
psycopg2-binary==2.9.9
79+
ptyprocess @ file:///home/conda/feedstock_root/build_artifacts/ptyprocess_1609419310487/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl
80+
pure-eval @ file:///home/conda/feedstock_root/build_artifacts/pure_eval_1642875951954/work
81+
pyarrow==16.1.0
82+
pyasn1==0.6.0
83+
pyasn1_modules==0.4.0
84+
pydantic==2.7.1
85+
pydantic_core==2.18.2
86+
pydeck==0.9.1
87+
Pygments @ file:///home/conda/feedstock_root/build_artifacts/pygments_1714846767233/work
88+
pyparsing==3.1.2
89+
python-dateutil @ file:///home/conda/feedstock_root/build_artifacts/python-dateutil_1709299778482/work
90+
python-dotenv==1.0.1
91+
pytz==2024.1
92+
PyYAML==6.0.1
93+
pyzmq @ file:///croot/pyzmq_1705605076900/work
94+
referencing==0.35.1
95+
requests==2.32.1
96+
rich==13.7.1
97+
rpds-py==0.18.1
98+
rsa==4.9
99+
six @ file:///home/conda/feedstock_root/build_artifacts/six_1620240208055/work
100+
smmap==5.0.1
101+
sniffio==1.3.1
102+
SQLAlchemy==2.0.30
103+
stack-data @ file:///home/conda/feedstock_root/build_artifacts/stack_data_1669632077133/work
104+
streamlit==1.34.0
105+
tenacity==8.3.0
106+
toml==0.10.2
107+
toolz==0.12.1
108+
tornado @ file:///home/conda/feedstock_root/build_artifacts/tornado_1708363103305/work
109+
tqdm==4.66.4
110+
traitlets @ file:///home/conda/feedstock_root/build_artifacts/traitlets_1713535121073/work
111+
typing-inspect==0.9.0
112+
typing_extensions @ file:///home/conda/feedstock_root/build_artifacts/typing_extensions_1712329955671/work
113+
tzdata==2024.1
114+
uritemplate==4.1.1
115+
urllib3==2.2.1
116+
watchdog==4.0.0
117+
wcwidth @ file:///home/conda/feedstock_root/build_artifacts/wcwidth_1704731205417/work
118+
yarl==1.9.4
119+
zipp @ file:///home/conda/feedstock_root/build_artifacts/zipp_1695255097490/work

0 commit comments

Comments
 (0)