-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_system.py
More file actions
183 lines (164 loc) · 7.11 KB
/
rag_system.py
File metadata and controls
183 lines (164 loc) · 7.11 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# rag_system.py
import os
import subprocess
from typing import List
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
from langchain.vectorstores import Qdrant
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_experimental.text_splitter import SemanticChunker
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader, PyPDFLoader, Docx2txtLoader, JSONLoader
from langchain.schema import Document
from langchain.memory import ConversationBufferMemory
from langchain import PromptTemplate
from preprocess import *
import streamlit as st
import zipfile
from langchain_openai import AzureChatOpenAI
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams
@st.cache_resource
def load_model():
with st.spinner("Downloading Instructor XL Embeddings Model locally....please be patient"):
embedding_model=HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-large")
return embedding_model
class RAGSystem:
def __init__(self, openai_api_key: str, qdrant_url: str, collection_name: str):
self.openai_api_key = openai_api_key
self.qdrant_url = qdrant_url
self.collection_name = collection_name
self.embedding_dimension = 768 # Dimension of the embeddings
self.embedding_model = load_model()
self.setup_conversation_memory()
self.priming_text = ''
self.setup_prompt()
self.pages = []
os.environ["AZURE_OPENAI_API_KEY"] = openai_api_key
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://openai2699.openai.azure.com/"
print(f"OpenAI API Key: {os.getenv('AZURE_OPENAI_API_KEY')}")
self.qdrant_client = QdrantClient(url=self.qdrant_url)
self.vector_store = self.initialize_vector_store()
self.qa_chain = self.setup_qa_chain()
def initialize_vector_store(self):
collections = self.qdrant_client.get_collections().collections
if not any(collection.name == self.collection_name for collection in collections):
self.qdrant_client.create_collection(
collection_name=self.collection_name,
vectors_config=VectorParams(size=self.embedding_dimension, distance=Distance.COSINE),
)
return Qdrant(
client=self.qdrant_client,
collection_name=self.collection_name,
embeddings=self.embedding_model)
def returnSystemText(self, log_data):
PACKET_WHISPERER = f"""
log_info : {log_data}
"""
return PACKET_WHISPERER
def pcap_to_json(pcap_path, json_path):
command = f'tshark -nlr {pcap_path} -T json > {json_path}'
subprocess.run(command, shell=True)
def setup_conversation_memory(self):
self.memory = ConversationBufferMemory(memory_key="history", input_key="question", return_messages=True)
def setup_prompt(self):
template = """
Use the following context (delimited by <ctx></ctx>) and the chat history (delimited by <hs></hs>) to answer the question:
------
<ctx>
{context}
</ctx>
--------
<hs>
{history}
</hs>
------
{question}
Answer:
"""
self.prompt = PromptTemplate.from_template(template)
def generate_priming(self):
log_summary = " ".join([page.page_content for page in self.pages])
return self.returnSystemText(log_summary)
def load_and_process_documents(self, file_path: str) -> List[Document]:
_, file_extension = os.path.splitext(file_path)
try:
if file_extension.lower() == '.txt':
# loader = TextLoader(file_path)
loader = TextLoader(file_path)
elif file_extension.lower() == '.pdf':
loader = PyPDFLoader(file_path)
elif file_extension.lower() in ['.docx', '.doc']:
loader = Docx2txtLoader(file_path)
elif file_extension.lower() == '.etl':
final_path = file_path + '.txt'
command = f'pktmon etl2txt "{file_path}"'
# Execute the PowerShell command
subprocess.run(["powershell", "-Command", command], capture_output=True, text=True)
txt_file = os.path.splitext(file_path)[0] + ".txt"
process_large_file_line_by_line(txt_file, final_path, "filters.json")
loader = TextLoader(final_path)
elif file_extension.lower() == '.pcap':
json_path = file_path + ".json"
self.pcap_to_json(file_path, json_path)
loader = JSONLoader(file_path)
elif file_extension.lower() == '.zip':
with zipfile.ZipFile(file_path, 'r') as zip_ref:
extract_path = os.path.splitext(file_path)[0]
zip_ref.extractall(extract_path)
documents = []
for root, _, files in os.walk(extract_path):
for file in files:
file_path = os.path.join(root, file)
documents.extend(self.load_and_process_documents(file_path))
return documents
else:
raise ValueError(f"Unsupported file type: {file_extension}")
except ValueError as e:
print(f"Error processing file {file_path}: {e}")
return []
self.pages.extend(loader.load_and_split())
self.text_splitter = SemanticChunker(self.embedding_model)
# documents = loader.load()
# text_splitter = RecursiveCharacterTextSplitter(
# chunk_size=10000,
# chunk_overlap=200,
# length_function=len,
# )
return self.text_splitter.split_documents(self.pages)
def setup_qa_chain(self):
retriever = self.vector_store.as_retriever(search_kwargs={"k": 5})
llm = AzureChatOpenAI(
azure_deployment="packetcopilot2", # or your deployment
api_version="2023-06-01-preview", # or your api version
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2
)
return RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=False,
verbose=True,
chain_type_kwargs={
"verbose": True,
"prompt": self.prompt,
"memory": self.memory,
}
)
def add_documents(self, file_path: str):
documents = self.load_and_process_documents(file_path)
if st.session_state.username == "admin":
self.vector_store.add_documents(documents)
else:
self.priming_text += self.generate_priming()
def query(self, question: str):
query = self.priming_text + "\n\n" + question
self.priming_text = ""
result = self.qa_chain.invoke({"query": query})
return {
"answer": result["result"]
}