Spaces:
Sleeping
Sleeping
updated libraries
Browse files
app.py
CHANGED
|
@@ -8,8 +8,8 @@ from langchain_community.chat_message_histories import (
|
|
| 8 |
)
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
|
| 14 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 15 |
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
|
|
@@ -40,68 +40,6 @@ embedding_args = {
|
|
| 40 |
}
|
| 41 |
embedding_model = HuggingFaceEmbeddings(**embedding_args)
|
| 42 |
|
| 43 |
-
# Mongo Connection
|
| 44 |
-
connection = pymongo.MongoClient(os.environ["MONGO_URI"])
|
| 45 |
-
alert_collection = connection[database][collection]
|
| 46 |
-
|
| 47 |
-
# Redis connection
|
| 48 |
-
r = redis.Redis(host=os.environ['REDIS_HOST'], password=os.environ['REDIS_PWD'], port=16652)
|
| 49 |
-
|
| 50 |
-
# Preprocessing
|
| 51 |
-
async def create_textual_description(entry_data):
|
| 52 |
-
entry_dict = {k.decode(): v.decode() for k, v in entry_data.items()}
|
| 53 |
-
category = entry_dict["Category"]
|
| 54 |
-
created_at = entry_dict["CreatedAt"]
|
| 55 |
-
acknowledged = "Acknowledged" if entry_dict["Acknowledged"] == "1" else "Not Acknowledged"
|
| 56 |
-
remedy = entry_dict["Remedy"]
|
| 57 |
-
severity = entry_dict["Severity"]
|
| 58 |
-
source = entry_dict["Source"]
|
| 59 |
-
node = entry_dict["node"]
|
| 60 |
-
description = f"A {severity} alert of category {category} was raised from the {source} source for node {node} at {created_at}. The alert is {acknowledged}. The recommended remedy is: {remedy}."
|
| 61 |
-
return description, entry_dict
|
| 62 |
-
|
| 63 |
-
# Saving alert doc
|
| 64 |
-
async def save(entry):
|
| 65 |
-
vector_search = MongoDBAtlasVectorSearch.from_documents(
|
| 66 |
-
documents=[Document(
|
| 67 |
-
page_content=entry["content"],
|
| 68 |
-
metadata=entry["metadata"]
|
| 69 |
-
)],
|
| 70 |
-
embedding=embedding_model,
|
| 71 |
-
collection=alert_collection,
|
| 72 |
-
index_name="alert_index",
|
| 73 |
-
)
|
| 74 |
-
logging.info("Alerts stored successfully!")
|
| 75 |
-
|
| 76 |
-
# Listening to alert stream
|
| 77 |
-
async def listen_to_alerts(r):
|
| 78 |
-
logging.info("Listening to alerts...")
|
| 79 |
-
try:
|
| 80 |
-
last_id = '$'
|
| 81 |
-
while True:
|
| 82 |
-
entries = r.xread({stream_name: last_id}, block=0, count=None)
|
| 83 |
-
if entries:
|
| 84 |
-
stream, new_entries = entries[0]
|
| 85 |
-
for entry_id, entry_data in new_entries:
|
| 86 |
-
description, entry_dict = await create_textual_description(entry_data)
|
| 87 |
-
await save({"content": description, "metadata": entry_dict})
|
| 88 |
-
# Update the last ID read
|
| 89 |
-
last_id = entry_id
|
| 90 |
-
st.toast(description, icon='π')
|
| 91 |
-
except KeyboardInterrupt:
|
| 92 |
-
print("Exiting...")
|
| 93 |
-
|
| 94 |
-
# Start Redis listener in a separate thread
|
| 95 |
-
def start_redis_listener():
|
| 96 |
-
try:
|
| 97 |
-
loop = asyncio.new_event_loop()
|
| 98 |
-
asyncio.set_event_loop(loop)
|
| 99 |
-
loop.run_until_complete(listen_to_alerts(r))
|
| 100 |
-
except Exception as e:
|
| 101 |
-
print(f"Error in Redis listener: {e}")
|
| 102 |
-
finally:
|
| 103 |
-
loop.close()
|
| 104 |
-
|
| 105 |
# Streamlit Application
|
| 106 |
st.set_page_config(
|
| 107 |
page_title="ASMR Query Bot π",
|
|
@@ -115,10 +53,6 @@ st.set_page_config(
|
|
| 115 |
|
| 116 |
st.title('ASMR Query Bot π')
|
| 117 |
|
| 118 |
-
# Start Redis listener in a separate thread
|
| 119 |
-
redis_listener_thread = threading.Thread(target=start_redis_listener)
|
| 120 |
-
redis_listener_thread.start()
|
| 121 |
-
|
| 122 |
# vector search
|
| 123 |
vector_search = MongoDBAtlasVectorSearch.from_connection_string(
|
| 124 |
os.environ["MONGO_URI"],
|
|
|
|
| 8 |
)
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
+
from langchain_mongodb import MongoDBAtlasVectorSearch
|
| 12 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 13 |
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
|
| 14 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 15 |
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
|
|
|
|
| 40 |
}
|
| 41 |
embedding_model = HuggingFaceEmbeddings(**embedding_args)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Streamlit Application
|
| 44 |
st.set_page_config(
|
| 45 |
page_title="ASMR Query Bot π",
|
|
|
|
| 53 |
|
| 54 |
st.title('ASMR Query Bot π')
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# vector search
|
| 57 |
vector_search = MongoDBAtlasVectorSearch.from_connection_string(
|
| 58 |
os.environ["MONGO_URI"],
|