Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +111 -0
- app_config.py +7 -0
- requirements.txt +3 -0
- session_manager.py +8 -0
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from app_config import SYSTEM_PROMPT
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import os
|
| 7 |
+
import session_manager
|
| 8 |
+
|
| 9 |
+
from langchain_community.utilities import GoogleSerperAPIWrapper
|
| 10 |
+
env_path = Path('.') / '.env'
|
| 11 |
+
load_dotenv(dotenv_path=env_path)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
st.markdown(
|
| 15 |
+
"""
|
| 16 |
+
<style>
|
| 17 |
+
.st-emotion-cache-janbn0 {
|
| 18 |
+
flex-direction: row-reverse;
|
| 19 |
+
text-align: right;
|
| 20 |
+
}
|
| 21 |
+
.st-emotion-cache-1ec2a3d{
|
| 22 |
+
display: none;
|
| 23 |
+
}
|
| 24 |
+
</style>
|
| 25 |
+
""",
|
| 26 |
+
unsafe_allow_html=True,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Intialize chat history
|
| 30 |
+
print("SYSTEM MESSAGE")
|
| 31 |
+
if "messages" not in st.session_state:
|
| 32 |
+
st.session_state.messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 33 |
+
|
| 34 |
+
print("SYSTEM MODEL")
|
| 35 |
+
if "llm" not in st.session_state:
|
| 36 |
+
st.session_state.llm = ChatGroq(
|
| 37 |
+
model="llama-3.3-70b-versatile",
|
| 38 |
+
temperature=0,
|
| 39 |
+
max_tokens=None,
|
| 40 |
+
timeout=None,
|
| 41 |
+
max_retries=2,
|
| 42 |
+
api_key=str(os.getenv('GROQ_API'))
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if "search_tool" not in st.session_state:
|
| 46 |
+
st.session_state.search_tool = GoogleSerperAPIWrapper(
|
| 47 |
+
serper_api_key=str(os.getenv('SERPER_API')))
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def get_answer(query):
|
| 51 |
+
new_search_query = st.session_state.llm.invoke(
|
| 52 |
+
f"Convert below query to english for Ahmedabad Municipal Corporation (AMC) You just need to give translated query. Don't add any additional details.\n Query: {query}").content
|
| 53 |
+
search_result = st.session_state.search_tool.run(
|
| 54 |
+
f"{new_search_query} site:https://ahmedabadcity.gov.in/")
|
| 55 |
+
|
| 56 |
+
system_prompt = """You are a helpful assistance for The Ahmedabad Municipal Corporation (AMC). which asnwer user query from given context only. Output language should be as same as `original_query_from_user`.
|
| 57 |
+
context: {context}
|
| 58 |
+
original_query_from_user: {original_query}
|
| 59 |
+
query: {query}"""
|
| 60 |
+
|
| 61 |
+
return st.session_state.llm.invoke(system_prompt.format(context=search_result, query=new_search_query, original_query=query)).content
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
session_manager.set_session_state(st.session_state)
|
| 65 |
+
|
| 66 |
+
print("container")
|
| 67 |
+
# Display chat messages from history
|
| 68 |
+
st.markdown("<h1 style='text-align: center;'>AMC Bot</h1>", unsafe_allow_html=True)
|
| 69 |
+
container = st.container(height=700)
|
| 70 |
+
for message in st.session_state.messages:
|
| 71 |
+
if message["role"] != "system":
|
| 72 |
+
with container.chat_message(message["role"]):
|
| 73 |
+
if message['type'] == "table":
|
| 74 |
+
st.dataframe(message['content'].set_index(
|
| 75 |
+
message['content'].columns[0]))
|
| 76 |
+
elif message['type'] == "html":
|
| 77 |
+
st.markdown(message['content'], unsafe_allow_html=True)
|
| 78 |
+
else:
|
| 79 |
+
st.write(message["content"])
|
| 80 |
+
|
| 81 |
+
# When user gives input
|
| 82 |
+
if prompt := st.chat_input("Enter your query here... "):
|
| 83 |
+
with container.chat_message("user"):
|
| 84 |
+
st.write(prompt)
|
| 85 |
+
st.session_state.messages.append(
|
| 86 |
+
{"role": "user", "content": prompt, "type": "string"})
|
| 87 |
+
st.session_state.last_query = prompt
|
| 88 |
+
|
| 89 |
+
with container.chat_message("assistant"):
|
| 90 |
+
current_conversation = """"""
|
| 91 |
+
|
| 92 |
+
# if st.session_state.next_agent != "general_agent" and st.session_state.next_agent in st.session_state.agent_history:
|
| 93 |
+
for message in st.session_state.messages:
|
| 94 |
+
if message['role'] == 'user':
|
| 95 |
+
current_conversation += f"""user: {message['content']}\n"""
|
| 96 |
+
if message['role'] == 'assistant':
|
| 97 |
+
current_conversation += f"""ai: {message['content']}\n"""
|
| 98 |
+
|
| 99 |
+
current_conversation += f"""user: {prompt}\n"""
|
| 100 |
+
|
| 101 |
+
print("****************************************** Messages ******************************************")
|
| 102 |
+
print("messages", current_conversation)
|
| 103 |
+
print()
|
| 104 |
+
print()
|
| 105 |
+
response = get_answer(current_conversation)
|
| 106 |
+
print("******************************************************** Response ********************************************************")
|
| 107 |
+
print("MY RESPONSE IS:", response)
|
| 108 |
+
|
| 109 |
+
st.write(response)
|
| 110 |
+
st.session_state.messages.append(
|
| 111 |
+
{"role": "assistant", "content": response, "type": "string"})
|
app_config.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SYSTEM_PROMPT = """You are a helpful assistance for The Ahmedabad Municipal Corporation (AMC). which asnwer user query from given context only. Output language should be as same as `original_query_from_user`.
|
| 2 |
+
context: {context}
|
| 3 |
+
original_query_from_user: {original_query}
|
| 4 |
+
query: {query}"""
|
| 5 |
+
|
| 6 |
+
MODEL = "llama-3.3-70b-versatile"
|
| 7 |
+
MAX_TOKENS = 4000
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain_groq
|
| 3 |
+
langchain_community
|
session_manager.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
session_state = None
|
| 2 |
+
|
| 3 |
+
def set_session_state(state):
|
| 4 |
+
global session_state
|
| 5 |
+
session_state = state
|
| 6 |
+
|
| 7 |
+
def get_session_state():
|
| 8 |
+
return session_state
|