-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
356 lines (327 loc) · 14.3 KB
/
app.py
File metadata and controls
356 lines (327 loc) · 14.3 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import streamlit as st
from phi.agent import Agent, AgentKnowledge
from phi.vectordb.pgvector import PgVector
from phi.embedder.ollama import OllamaEmbedder
from phi.model.ollama import Ollama
from phi.knowledge.pdf import PDFKnowledgeBase
from phi.knowledge.text import TextKnowledgeBase
import boto3
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from atlassian import Confluence
import io
# Optional: Set page configuration, including title and favicon
st.set_page_config(
page_title="RAGForge",
# Uncomment and set the favicon URL if desired
page_icon="https://pinsker.ai/web/image/1479-c1f10525/icon.png",
layout="centered",
initial_sidebar_state="expanded",
)
# Display the logo in the sidebar using the provided URL
logo_url = "https://pinsker.ai/web/image/1477-9ebaaf6e/ragforge.webp"
st.sidebar.image(logo_url, width=200) # Adjust the width as needed
# Set title and subtitle in the main page
st.title("RAGForge")
st.subheader("RAG Application with Ollama Embedding and Agent")
# Sidebar for Knowledge Base Settings
st.sidebar.title("Knowledge Base Settings")
# Database Settings
st.sidebar.subheader("Database Settings")
db_url_input = st.sidebar.text_input("Database URL", value='postgresql://ai:ai@localhost:5532/ai')
embeddings_table_input = st.sidebar.text_input("Embeddings Table Name", value='embeddings')
reload_knowledge_base = st.sidebar.button("Reload Knowledge Base")
# Update the database connection
db_url = db_url_input
embeddings_table = embeddings_table_input
# Create knowledge base with updated settings
def create_knowledge_base():
vector_db = PgVector(
db_url=db_url,
table_name=embeddings_table,
embedder=OllamaEmbedder(),
)
knowledge_base = AgentKnowledge(
vector_db=vector_db,
num_documents=5,
include_sources=True,
)
return knowledge_base
# Initialize knowledge base
if 'knowledge_base' not in st.session_state or reload_knowledge_base:
st.session_state['knowledge_base'] = create_knowledge_base()
st.session_state['selected_files'] = []
knowledge_base = st.session_state['knowledge_base']
# Section to select local files using file browser
st.sidebar.subheader("Local Files")
if 'selected_files' not in st.session_state:
st.session_state['selected_files'] = []
# Function to browse and select files
def file_browser(base_path):
files = []
for root, dirs, filenames in os.walk(base_path):
for filename in filenames:
if filename.lower().endswith(('.pdf', '.txt', '.md')):
files.append(os.path.join(root, filename))
return files
base_path = '/app/application_materials/knowledge_files'
if os.path.exists(base_path):
all_files = file_browser(base_path)
selected_files = st.sidebar.multiselect('Select files to load', all_files)
if st.sidebar.button("Load Selected Files"):
for file in selected_files:
if file.lower().endswith('.pdf'):
kb = PDFKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
elif file.lower().endswith(('.txt', '.md')):
kb = TextKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
else:
st.warning(f"Unsupported file format: {file}")
continue
kb.load()
st.session_state['selected_files'].extend(selected_files)
st.success(f"Loaded {len(selected_files)} files into the knowledge base.")
else:
st.error(f"Base path does not exist: {base_path}")
# Add Drag and Drop File Uploader
st.sidebar.subheader("Upload Files")
uploaded_files = st.sidebar.file_uploader(
"Drag and drop files here, or click to select files",
type=['pdf', 'txt', 'md'],
accept_multiple_files=True
)
if uploaded_files:
uploaded_file_paths = []
for uploaded_file in uploaded_files:
# Save uploaded files to the knowledge_files directory
save_path = os.path.join(base_path, uploaded_file.name)
with open(save_path, "wb") as f:
f.write(uploaded_file.getbuffer())
uploaded_file_paths.append(save_path)
# Load uploaded files into the knowledge base
for file in uploaded_file_paths:
if file.lower().endswith('.pdf'):
kb = PDFKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
elif file.lower().endswith(('.txt', '.md')):
kb = TextKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
else:
st.warning(f"Unsupported file format: {file}")
continue
kb.load()
st.success(f"Uploaded and loaded {len(uploaded_file_paths)} files into the knowledge base.")
# Section for AWS S3 connection
st.sidebar.subheader("AWS S3")
aws_access_key = st.sidebar.text_input("AWS Access Key ID")
aws_secret_key = st.sidebar.text_input("AWS Secret Access Key", type="password")
aws_bucket_name = st.sidebar.text_input("S3 Bucket Name")
load_s3_files = st.sidebar.button("Load Files from S3")
# Section for Google Drive connection
st.sidebar.subheader("Google Drive")
gdrive_credentials_json = st.sidebar.text_area("Google Drive Credentials JSON")
load_gdrive_files = st.sidebar.button("Load Files from Google Drive")
# Section for Confluence connection
st.sidebar.subheader("Confluence")
confluence_url = st.sidebar.text_input("Confluence URL")
confluence_username = st.sidebar.text_input("Confluence Username")
confluence_api_token = st.sidebar.text_input("Confluence API Token", type="password")
load_confluence_files = st.sidebar.button("Load Files from Confluence")
# Function to load files from AWS S3
def load_s3_files_to_kb(access_key, secret_key, bucket_name):
if access_key and secret_key and bucket_name:
try:
s3 = boto3.client(
's3',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
)
# List supported files in the bucket
s3_objects = s3.list_objects_v2(Bucket=bucket_name)
if 'Contents' in s3_objects:
supported_keys = [obj['Key'] for obj in s3_objects['Contents'] if obj['Key'].lower().endswith(('.pdf', '.txt', '.md'))]
if supported_keys:
# Download files to a temporary directory
os.makedirs('/tmp/s3_files', exist_ok=True)
downloaded_files = []
for key in supported_keys:
local_path = f'/tmp/s3_files/{os.path.basename(key)}'
s3.download_file(bucket_name, key, local_path)
downloaded_files.append(local_path)
# Load files into knowledge base
for file in downloaded_files:
if file.lower().endswith('.pdf'):
kb = PDFKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
elif file.lower().endswith(('.txt', '.md')):
kb = TextKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
else:
st.warning(f"Unsupported file format: {file}")
continue
kb.load()
st.success(f"Loaded {len(downloaded_files)} files from S3 bucket {bucket_name}")
else:
st.warning("No supported files found in the specified S3 bucket.")
else:
st.warning("No files found in the specified S3 bucket.")
except Exception as e:
st.error(f"An error occurred while accessing S3: {e}")
else:
st.error("Please provide AWS credentials and bucket name.")
# Function to load files from Google Drive
def load_gdrive_files_to_kb(credentials_json):
if credentials_json:
try:
creds = service_account.Credentials.from_service_account_info(eval(credentials_json), scopes=['https://www.googleapis.com/auth/drive'])
drive_service = build('drive', 'v3', credentials=creds)
# List supported files in Drive
query = "mimeType='application/pdf' or mimeType='text/plain' or mimeType='text/markdown'"
results = drive_service.files().list(
q=query,
pageSize=100,
fields="files(id, name)"
).execute()
items = results.get('files', [])
if items:
# Download files to a temporary directory
os.makedirs('/tmp/gdrive_files', exist_ok=True)
downloaded_files = []
for item in items:
file_id = item['id']
file_name = item['name']
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
status, done = downloader.next_chunk()
fh.seek(0)
local_path = f'/tmp/gdrive_files/{file_name}'
with open(local_path, 'wb') as f:
f.write(fh.read())
downloaded_files.append(local_path)
# Load files into knowledge base
for file in downloaded_files:
if file.lower().endswith('.pdf'):
kb = PDFKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
elif file.lower().endswith(('.txt', '.md')):
kb = TextKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
else:
st.warning(f"Unsupported file format: {file}")
continue
kb.load()
st.success(f"Loaded {len(downloaded_files)} files from Google Drive")
else:
st.warning("No supported files found in Google Drive.")
except Exception as e:
st.error(f"An error occurred while accessing Google Drive: {e}")
else:
st.error("Please provide Google Drive credentials.")
# Function to load files from Confluence
def load_confluence_files_to_kb(url, username, api_token):
if url and username and api_token:
try:
confluence = Confluence(
url=url,
username=username,
password=api_token
)
# Get all pages
all_pages = confluence.get_all_pages()
downloaded_files = []
os.makedirs('/tmp/confluence_files', exist_ok=True)
for page in all_pages:
attachments = confluence.get_attachments_from_content(page['id'])
for attachment in attachments['results']:
if attachment['metadata']['mediaType'] in ['application/pdf', 'text/plain', 'text/markdown']:
file_name = attachment['title']
file_content = confluence.download_attachment(page['id'], file_name)
local_path = f'/tmp/confluence_files/{file_name}'
with open(local_path, 'wb') as f:
f.write(file_content)
downloaded_files.append(local_path)
if downloaded_files:
# Load files into knowledge base
for file in downloaded_files:
if file.lower().endswith('.pdf'):
kb = PDFKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
elif file.lower().endswith(('.txt', '.md')):
kb = TextKnowledgeBase(
path=file,
vector_db=knowledge_base.vector_db
)
else:
st.warning(f"Unsupported file format: {file}")
continue
kb.load()
st.success(f"Loaded {len(downloaded_files)} files from Confluence")
else:
st.warning("No supported files found in Confluence.")
except Exception as e:
st.error(f"An error occurred while accessing Confluence: {e}")
else:
st.error("Please provide Confluence credentials.")
# Load Files from AWS S3
if load_s3_files:
load_s3_files_to_kb(aws_access_key, aws_secret_key, aws_bucket_name)
# Load Files from Google Drive
if load_gdrive_files:
load_gdrive_files_to_kb(gdrive_credentials_json)
# Load Files from Confluence
if load_confluence_files:
load_confluence_files_to_kb(confluence_url, confluence_username, confluence_api_token)
# Add the knowledge base to the Agent, specify the Ollama model, and add instructions
agent = Agent(
model=Ollama(id="llama3.2"),
knowledge_base=knowledge_base,
instructions=["Always include sources"],
markdown=True,
add_history_to_messages=True,
num_history_responses=5,
)
# Initialize session state for chat history
if 'history' not in st.session_state:
st.session_state['history'] = []
# Display chat history
st.subheader("Chat History")
if st.session_state['history']:
for chat in st.session_state['history']:
st.markdown(f"**User:** {chat['user']}")
st.markdown(f"**Agent:** {chat['agent']}")
else:
st.write("No chat history yet.")
# User input
user_input = st.text_input("Ask a question:")
if user_input:
# Use the run method and get the content
run_response = agent.run(user_input)
response = run_response.content
# Store the interaction in the chat history
st.session_state['history'].append({'user': user_input, 'agent': response})
# Display the response
st.markdown(f"**Agent:** {response}")