-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
485 lines (401 loc) · 17.1 KB
/
main.py
File metadata and controls
485 lines (401 loc) · 17.1 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
"""
main.py
An interactive script that prompts the user to choose actions dynamically.
"""
import os
from dotenv import load_dotenv
import sys
# Import managers and classes
from core.database_manager import DatabaseManager
from core.file_storage_manager import FileStorageManager
from core.faiss_index_manager import FaissIndexManager
from collectors.github_collector import GitHubCollector
from metadata.metadata_manager import MetadataManager
from rag.rag_engine import RAGEngine
from embeddings.embeddings import SentenceTransformerEmbeddingModel
from utils.cli_helpers import ask_repo, select_collection_interactively, ask_question, ask_top_k
import multiprocessing
from server.local_storage_server import LocalStorageServer
from LLMs.llm_manager import LLMManager
from LLMs.llm_interface import ILLM
from rag.query_recorder import RAGQueryRecorder
from typing import (Optional)
# Global variable to track server process
server_process = None
def interactive_menu():
"""
Displays a menu and returns user's choice as an integer.
"""
print("\n=== Archethic RAG Interactive Menu ===")
print("1) Configure/Change LLM Model (not implemented yet)")
print("2) List GitHub repositories (Test OK)")
print("3) Update all data for an organization (Don't use and test while validation on the first repo is not completed)")
print("4) Update all data for selected repositories (Test OK)")
print("5) Update specific data for multiple repositories (Test OK)")
print("6) List collections in the database (Test OK)")
print("7) Start local storage server (Test OK)")
print("8) Stop local storage server (Test OK)")
print("9) Show last 10 lines of local server log (Test OK)")
print("10) Generate/Update metadata for selected repositories and selected collections (TODO test)")
print("11) Run unit tests (not implemented yet)")
print("12) Start chat with the LLM (not implemented yet)")
print("13) Query RAG with Faiss")
print("0) Exit (Test OK)")
choice = input("Enter your choice: ")
return choice.strip()
def load_configuration():
"""
Loads environment variables and ensures required ones are set.
"""
load_dotenv()
required_vars = ["MONGO_URI", "DB_NAME", "LOCAL_STORAGE_PATH", "BASE_URL", "PORT"]
for var in required_vars:
if not os.getenv(var):
print(f"Warning: Environment variable {var} is not set!")
print("Environment variables loaded successfully.")
def run_main_loop():
"""
Main interactive loop handling user choices dynamically.
"""
# Load environment variables
load_configuration()
mongo_uri = os.getenv("MONGO_URI", "mongodb://localhost:27017")
db_name = os.getenv("DB_NAME", "archethic_rag")
# Initialize FileStorageManager once
local_storage_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.getenv("LOCAL_STORAGE_PATH", "local_storage"))
base_url = os.getenv("BASE_URL", f"http://localhost:{os.getenv('PORT', 8000)}")
storage_manager = FileStorageManager(base_storage_path=local_storage_path, base_url=base_url)
embedding_model = SentenceTransformerEmbeddingModel()
recorder = RAGQueryRecorder("experiments/rag_benchmark.jsonl")
try:
while True:
choice = interactive_menu()
if choice == "1":
# cmd_configure_llm()
pass
elif choice == "2":
cmd_list_github_repos(mongo_uri, db_name, storage_manager)
elif choice == "3":
cmd_update_org_data(mongo_uri, db_name, storage_manager)
elif choice == "4":
cmd_update_repos_data(mongo_uri, db_name, storage_manager)
elif choice == "5":
cmd_update_multiple_repos_specific_data(mongo_uri, db_name, storage_manager)
elif choice == "6":
cmd_list_collections(mongo_uri, db_name)
elif choice == "7":
cmd_start_local_server()
elif choice == "8":
cmd_stop_local_server()
elif choice == "9":
cmd_view_local_server_logs()
elif choice == "10":
cmd_update_metadata_multiple_repos_specific_data(mongo_uri, db_name, storage_manager)
elif choice == "11":
cmd_run_tests()
elif choice == "12":
cmd_start_chat()
elif choice == "13":
cmd_rag_query(mongo_uri, db_name, embedding_model, recorder)
elif choice == "0":
print("Goodbye!")
break
else:
print("[!] Invalid choice.")
except KeyboardInterrupt:
print("\n KeyboardInterrupt detected. Stopping all processes...")
finally:
cmd_stop_local_server() # Ensure the server stops before exit
# def cmd_configure_llm(current_llm: ILLM) -> ILLM:
# """
# Configures the LLM model, using environment variables if available.
# """
# print("\nChoose a model backend:")
# print("1) Local LLM (dummy or local model)")
# print("2) OpenAI GPT-3.5 or GPT-4 (API)")
# choice = os.getenv("LLM_MODEL_CHOICE", input("Model choice: ").strip())
# if choice == "1":
# model_path = os.getenv("LLM_LOCAL_PATH", input("Enter local model path (dummy for now): ").strip())
# new_llm = LLMManager.load_llm("local", {"model_path": model_path})
# print("[LLM] Switched to local model.")
# return new_llm
# elif choice == "2":
# api_key = os.getenv("OPENAI_API_KEY", input("Enter OpenAI API Key: ").strip())
# model_name = os.getenv("OPENAI_MODEL_NAME", input("Enter model name (e.g. gpt-3.5-turbo): ").strip())
# new_llm = LLMManager.load_llm("openai", {"api_key": api_key, "model_name": model_name})
# print("[LLM] Switched to OpenAI model.")
# return new_llm
# else:
# print("Invalid choice, keeping current LLM.")
# return current_llm
def cmd_list_github_repos(mongo_uri: str, db_name: str, storage_manager: FileStorageManager):
"""
Lists GitHub repositories, using the token from `.env` if available.
"""
token = os.getenv("GITHUB_TOKEN")
if not token:
token = input("Enter your GitHub personal access token: ").strip()
org = os.getenv("GITHUB_ORG")
if not org:
org = input("Enter GitHub owner/org: ").strip()
db_manager = DatabaseManager(mongo_uri, db_name)
collector = GitHubCollector(db_manager, token, org, storage_manager)
repos = collector.fetch_repositories()
if repos:
print(f"\n Retrieved {len(repos)} repositories for '{org}':")
for r in repos:
print(" -", r)
else:
print(f"\n No repositories found for '{org}' or an error occurred.")
def cmd_update_org_data(mongo_uri: str, db_name: str, storage_manager: FileStorageManager):
"""
Updates all repositories of a given GitHub organization.
"""
token = os.getenv("GITHUB_TOKEN")
if not token:
token = input("Enter your GitHub personal access token: ").strip()
org = os.getenv("GITHUB_ORG")
if not org:
org = input("Enter GitHub owner/org: ").strip()
db_manager = DatabaseManager(mongo_uri, db_name)
collector = GitHubCollector(db_manager, token, org, storage_manager)
print(f"Updating all repositories in {org}...")
collector.update_all_repos()
print("Organization data update complete.")
def cmd_update_repos_data(mongo_uri: str, db_name: str, storage_manager: FileStorageManager):
"""
Updates all selected repositories from a given GitHub organization.
"""
token = os.getenv("GITHUB_TOKEN")
if not token:
token = input("Enter your GitHub personal access token: ").strip()
org = os.getenv("GITHUB_ORG")
if not org:
org = input("Enter GitHub owner/org: ").strip()
repos_input = os.getenv("GITHUB_REPOS")
if not repos_input:
repos_input = input("Enter repositories (space-separated): ").strip()
repos = repos_input.split()
db_manager = DatabaseManager(mongo_uri, db_name)
collector = GitHubCollector(db_manager, token, org, storage_manager)
print(f"Updating selected repositories: {repos}")
collector.update_selected_repos(repos)
print("Repository data update complete.")
def cmd_update_multiple_repos_specific_data(mongo_uri: str, db_name: str, storage_manager: FileStorageManager):
"""
Updates selected data types for multiple repositories.
"""
token = os.getenv("GITHUB_TOKEN")
if not token:
token = input("Enter your GitHub personal access token: ").strip()
org = os.getenv("GITHUB_ORG")
if not org:
org = input("Enter GitHub owner/org: ").strip()
repos_input = os.getenv("GITHUB_REPOS")
if not repos_input:
repos_input = input("Enter repositories (space-separated): ").strip()
repos = repos_input.split()
options = {
"1": "repository info",
"2": "commits",
"3": "pull requests",
"4": "issues"
}
print("\nSelect the data to update:")
for key, value in options.items():
print(f"{key}) {value}")
choices = input("Enter numbers separated by spaces (e.g., '1 3'): ").split()
selected_data = [options[choice] for choice in choices if choice in options]
db_manager = DatabaseManager(mongo_uri, db_name)
collector = GitHubCollector(db_manager, token, org, storage_manager)
print(f"Updating {selected_data} for repositories: {repos}")
collector.update_multiple_repos_specific_data(repos, selected_data)
print("Data update complete.")
def cmd_update_metadata_multiple_repos_specific_data(mongo_uri: str, db_name: str, storage_manager: FileStorageManager):
"""
Updates metadata for a specified repository and collections.
Uses `.env` variables when available.
"""
token = os.getenv("GITHUB_TOKEN")
if not token:
token = input("Enter your GitHub personal access token: ").strip()
org = os.getenv("GITHUB_ORG")
if not org:
org = input("Enter GitHub owner/org: ").strip()
repos_input = os.getenv("GITHUB_REPOS")
if not repos_input:
repos_input = input("Enter repositories (space-separated): ").strip()
repos = repos_input.split()
selected_data = select_collection_interactively()
db_manager = DatabaseManager(mongo_uri, db_name)
metadata_manager = MetadataManager(db_manager, storage_manager)
metadata_manager.update_metadata_multiple_repos_specific_data(repos, selected_data)
def cmd_list_collections(mongo_uri: str, db_name: str):
"""
Lists all collections in the database.
"""
db_manager = DatabaseManager(mongo_uri, db_name, create_indexes=False)
cols = db_manager.list_collections()
print("\nCollections in the DB:")
for c in cols:
print(" -", c)
def cmd_start_local_server():
"""
Starts the local storage server in a separate process so it doesn't block the main script.
Uses `.env` values when available.
"""
global server_process
if server_process and server_process.is_alive():
print("Local storage server is already running.")
return
# Retrieve port from .env, ensuring it is a valid integer
port_env = os.getenv("PORT")
port = None
if port_env and port_env.isdigit():
port = int(port_env)
# Ask user only if PORT was not found in .env or is invalid
if port is None:
port_input = input("Port to serve on (default: 8000): ").strip()
if not port_input or not port_input.isdigit():
port = 8000
print("Invalid port. So default port 8000 applied.")
storage_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.getenv("LOCAL_STORAGE_PATH", "local_storage"))
print(f"\nStarting local storage server on port {port}, serving '{storage_path}' ...")
print("Server logs are being saved to 'local_server.log'. You can check logs anytime.")
# Start server process without passing the log file
server_process = multiprocessing.Process(target=start_server_process, args=(port, storage_path))
server_process.start()
print("[OK] Local storage server started in the background.")
def start_server_process(port, storage_directory):
"""
Function to start the local storage server in a separate process and redirect logs.
"""
log_file_path = "local_server.log"
with open(log_file_path, "a", buffering=1) as log_file:
sys.stdout = log_file # Redirect standard output
sys.stderr = log_file # Redirect error output
print(f"[LocalStorageServer] Starting server on port {port}, serving '{storage_directory}'")
server = LocalStorageServer(port=port, storage_directory=storage_directory)
server.start_server()
def cmd_stop_local_server():
"""
Stops the local storage server if it is running.
"""
global server_process
if server_process and server_process.is_alive():
print("Stopping local storage server...")
server_process.terminate()
server_process.join()
server_process = None
print("[OK] Local storage server stopped.")
else:
print("No active local storage server to stop.")
def cmd_view_local_server_logs():
"""
Displays the last few lines of the local storage server log file.
"""
log_file_path = "local_server.log"
if not os.path.exists(log_file_path):
print("No log file found. The server may not have been started yet.")
return
print("\nLast 10 lines of 'local_server.log':\n")
with open(log_file_path, "r") as log_file:
lines = log_file.readlines()
for line in lines[-10:]: # Show last 10 lines
print(line.strip())
print("\n(Use 'tail -f local_server.log' in a terminal to follow logs in real-time.)")
def cmd_run_tests():
"""
Runs unit tests.
"""
print("\nRunning unit tests...")
os.system("pytest archethic_rag/tests")
# def cmd_start_chat(llm: ILLM):
def cmd_start_chat(llm):
"""
Starts a simple chat with the chosen LLM.
(We store interactions in logs if desired).
"""
print("\n[Chat] Type 'exit' to quit.")
while True:
user_input = input("You: ")
if user_input.lower() in ("exit", "quit"):
print("Exiting chat.")
break
# Optionally we could pass context from RAG retrieval, etc.
answer = llm.chat(user_input, context=None)
print("Bot:", answer)
def cmd_rag_query(mongo_uri: str, db_name: str, embedding_model: SentenceTransformerEmbeddingModel, recorder: Optional[RAGQueryRecorder] = None):
"""
Ask the user a natural-language question, retrieve the top-k chunks
with Faiss, and generate an LLM answer.
Falls back to .env defaults for repo/collection if present.
"""
repo = ask_repo(os.getenv("GITHUB_REPOS", "").strip())
collections = select_collection_interactively()
if not collections:
print("No collection selected, cancelling.")
return
db_manager = DatabaseManager(mongo_uri, db_name, create_indexes=False)
llm_model_type = os.getenv("LLM_MODEL_TYPE", "local-llama")
llm_model_path = os.getenv("LLM_MODEL_PATH", "models/mistral-7b-instruct.Q4_K.gguf").strip()
llm = LLMManager.load_llm(
llm_model_type,
{
"model_path": llm_model_path,
"gpu_layers": os.getenv("LLM_GPU_LAYERS", "0"),
},
)
# Build / load Faiss index
index_mgr = FaissIndexManager(db_manager)
for collection in collections:
print(f"\n[Collection: {collection}]")
try:
index_mgr.load_index(repo, collection)
except FileNotFoundError:
print("[RAG] Index not found, building…")
try:
index_mgr.build_index(repo, [collection], force=True)
except ValueError as ve:
print(f"[RAG] No embeddings found for collection '{collection}'. Skipping.")
continue
print(f"[RAG] Loaded or built index for collection '{collection}'.")
question = ask_question()
top_k = ask_top_k()
# Instantiate RAG engine on-the-fly
rag = RAGEngine(
index_mgr=index_mgr,
embedding_model=embedding_model,
generative_llm=llm,
repo=repo,
collection_src=collection,
query_recorder=recorder
)
# Get answer
print("\n[Retrieving + generating…]\n")
answer = rag.answer_query(question, top_k=top_k)
print("\n--- ANSWER ---\n")
print(answer)
# def cmd_analyze_logs(llm: ILLM):
def cmd_analyze_logs(llm):
"""
Example usage of the LLM to analyze logs and propose improvements.
"""
# For the demo, let's just ask the user for some logs:
logs = []
print("\nEnter lines of logs (type 'done' to finish):")
while True:
line = input("> ")
if line.strip().lower() == "done":
break
logs.append(line)
if not logs:
print("No logs provided.")
return
analysis = llm.analyze_logs(logs)
print("\n[Analysis Result]\n", analysis)
def main():
print("Welcome to the Archethic RAG Interactive System.")
run_main_loop()
if __name__ == "__main__":
main()