-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
105 lines (82 loc) · 5.2 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import requests
import streamlit as st
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_groq import ChatGroq
Groq_KEY = st.secrets["Groq_KEY"]
Groq_KEY_2 = st.secrets["Groq_KEY_2"]
VDB_URL = st.secrets["VDB_URL"]
KPDES_URL = st.secrets["KPDES_URL"]
llm = ChatGroq(temperature=0, model_name="llama3-70b-8192", api_key=Groq_KEY)
llm2 = ChatGroq(temperature=0, model_name="llama3-70b-8192", api_key=Groq_KEY_2)
st.set_page_config(page_title="NPDES")
# Add a Chat history object to Streamlit session state
if "chat" not in st.session_state:
st.session_state.chat = []
# Display Form Title
st.markdown("### Chat with NPDES and KPDES")
# Display chat messages from history above current input box
for message in st.session_state.chat:
with st.chat_message(message['role']):
st.markdown(message['content'])
st.write("")
user_input = PromptTemplate(
template="""<|begin_of_text|><|start_header_id|>system<|end_header_id|> You are the expert of National Pollution
Discharge Elimination System (NPDES) and Kentucky Pollutant Discharge Elimination System (KPDES).
The National Pollutant Discharge Elimination System (NPDES) is a regulatory program implemented by the United
States Environmental Protection Agency (EPA) to control water pollution. It was established under the Clean
Water Act (CWA) to address the discharge of pollutants into the waters of the United States.
The NPDES program requires permits for any point source that discharges pollutants into navigable waters,
which include rivers, lakes, streams, coastal areas, and other bodies of water. Point sources are discrete
conveyances such as pipes, ditches, or channels.
Under the NPDES program, permits are issued to regulate the quantity, quality, and timing of the pollutants
discharged into water bodies. These permits include limits on the types and amounts of pollutants that can
be discharged, monitoring and reporting requirements, and other conditions to ensure compliance with water
quality standards and protect the environment and public health.
The goal of the NPDES program is to eliminate or minimize the discharge of pollutants into water bodies,
thereby improving and maintaining water quality, protecting aquatic ecosystems, and safeguarding human health.
It plays a critical role in preventing water pollution and maintaining the integrity of the nation's water
resources.
Based on the provided context, use easy understanding language to answer the question clear and precise with
references and explanations. If the local regulations (for example, KPDES for Kentucky Pollutant Discharge
Elimination System) can be applied, please include the details of both NPDES rules and KPDES rules, and make
clear indications of the sources of the rules.
If no information is provided in the context, return the result as "Sorry I dont know the answer", don't provide
the wrong answer or a contradictory answer.
Context:{context}
Question:{question}?
Answer: <|eot_id|><|start_header_id|>assistant<|end_header_id|>""",
input_variables=["question", "context"],
)
rag_chain = user_input | llm | StrOutputParser()
rag_chain_2 = user_input | llm2 | StrOutputParser()
# Accept user's next message, add to context, resubmit context to Gemini
if user_input := st.chat_input("What can I help you with?"):
# Display and save the user's input
st.chat_message("user").markdown(user_input)
st.session_state.chat.append({"role": "user", "content": user_input})
with st.chat_message("assistant"):
with st.spinner("We are in the process of retrieving the relevant provisions to give you the best possible answer."):
if "kentucky" in user_input.lower() or "KPDES" in user_input:
response = requests.get(f"{VDB_URL}?search_terms={user_input}")
datasets = json.loads(response.text)
datasets = datasets[0:4]
context = "NPDES regulations: "
context += "\n".join([dataset["description"] for dataset in datasets])
response = requests.get(f"{KPDES_URL}?search_terms={user_input}")
datasets = json.loads(response.text)
datasets = datasets[0:4]
context += "\nKPDES (Kentucky Pollutant Discharge Elimination System) regulations: "
context += "\n".join([dataset["description"] for dataset in datasets])
else:
response = requests.get(f"{VDB_URL}?search_terms={user_input}")
datasets = json.loads(response.text)
datasets = datasets[0:5]
context = "\n".join([dataset["description"] for dataset in datasets])
try:
result = rag_chain.invoke({"question": user_input, "context": context})
except Exception as e:
result = rag_chain_2.invoke({"question": user_input, "context": context})
st.markdown(result)
st.session_state.chat.append({"role": "assistant", "content": result})