-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathagent_cli.py
More file actions
executable file
·289 lines (227 loc) · 9.66 KB
/
agent_cli.py
File metadata and controls
executable file
·289 lines (227 loc) · 9.66 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
#!/usr/bin/env python3
import os
import sys
import subprocess
import time
from src.load_model import ensure_model_loaded, get_db_connection
from typing import Optional, List
from threading import Thread
import textwrap
try:
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt, Confirm
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich import print as rprint
import questionary
except ImportError:
print(
"Error: 'rich' and 'questionary' libraries are required. Please install them with: pip install rich questionary"
)
sys.exit(1)
console = Console()
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def run_command(cmd, desc="Running..."):
with console.status(f"[bold green]{desc}...[/bold green]"):
try:
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
console.print(f"[green]Done![/green]")
if result.stdout:
console.print(result.stdout)
else:
console.print(f"[red]Error (code {result.returncode}):[/red]")
console.print(result.stderr)
except Exception as e:
console.print(f"[red]Failed to execute command: {e}[/red]")
def print_header():
clear_screen()
console.print(
Panel.fit(
"[bold cyan]AGENTIC RAG SYSTEM CLI[/bold cyan]\n[dim]Oracle AI Vector Search + Ollama (Qwen3.5:9b)[/dim]",
border_style="cyan",
)
)
console.print(f"[dim]Working Directory: {os.getcwd()}[/dim]\n")
def menu_process_pdfs():
print_header()
choices = [
questionary.Choice("Process a single PDF file", value="1"),
questionary.Choice("Process all PDFs in a directory", value="2"),
questionary.Choice("Process a PDF from URL", value="3"),
questionary.Separator(),
questionary.Choice("Back to Main Menu", value="0"),
]
choice = questionary.select("PDF Processor", choices=choices, use_arrow_keys=True).ask()
if not choice or choice == "0":
return
output_file = Prompt.ask("Output JSON file path", default="chunks.json")
if choice == "1":
input_path = Prompt.ask("Enter path to PDF file")
if not os.path.exists(input_path):
console.print(f"[red]File not found: {input_path}[/red]")
input("Press Enter to continue...")
return
run_command(
["python", "-m", "src.pdf_processor", "--input", input_path, "--output", output_file],
"Processing PDF...",
)
elif choice == "2":
input_path = Prompt.ask("Enter directory path")
if not os.path.isdir(input_path):
console.print(f"[red]Directory not found: {input_path}[/red]")
input("Press Enter to continue...")
return
run_command(
["python", "-m", "src.pdf_processor", "--input", input_path, "--output", output_file],
"Processing Directory...",
)
elif choice == "3":
input_url = Prompt.ask("Enter PDF URL")
run_command(
["python", "-m", "src.pdf_processor", "--input", input_url, "--output", output_file],
"Downloading and Processing PDF...",
)
input("\nPress Enter to continue...")
def menu_process_websites():
print_header()
choices = [
questionary.Choice("Process a single website URL", value="1"),
questionary.Choice("Process multiple URLs from a file", value="2"),
questionary.Separator(),
questionary.Choice("Back to Main Menu", value="0"),
]
choice = questionary.select("Website Processor", choices=choices, use_arrow_keys=True).ask()
if not choice or choice == "0":
return
output_file = Prompt.ask("Output JSON file path", default="docs/web_content.json")
if choice == "1":
url = Prompt.ask("Enter website URL")
run_command(
["python", "-m", "src.web_processor", "--input", url, "--output", output_file],
"Processing Website...",
)
elif choice == "2":
input_file = Prompt.ask("Enter URLs file path", default="urls.txt")
if not os.path.exists(input_file):
console.print(f"[red]File not found: {input_file}[/red]")
input("Press Enter to continue...")
return
run_command(
["python", "-m", "src.web_processor", "--input", input_file, "--output", output_file],
"Processing URLs from file...",
)
input("\nPress Enter to continue...")
def menu_manage_vector_store():
print_header()
choices = [
questionary.Choice("Add PDF chunks to vector store", value="1"),
questionary.Choice("Add Web chunks to vector store", value="2"),
questionary.Choice("Query vector store directly", value="3"),
questionary.Choice("Check Vector Store Statistics", value="4"),
questionary.Separator(),
questionary.Choice("Back to Main Menu", value="0"),
]
choice = questionary.select("Manage Vector Store", choices=choices, use_arrow_keys=True).ask()
if not choice or choice == "0":
return
if choice == "1":
input_file = Prompt.ask("Enter chunks JSON file", default="chunks.json")
if not os.path.exists(input_file):
console.print(f"[red]File not found: {input_file}[/red]")
input("Press Enter to continue...")
return
run_command(["python", "-m", "src.store", "--add", input_file], "Adding PDF chunks...")
elif choice == "2":
input_file = Prompt.ask("Enter web content JSON file", default="docs/web_content.json")
if not os.path.exists(input_file):
console.print(f"[red]File not found: {input_file}[/red]")
input("Press Enter to continue...")
return
run_command(["python", "-m", "src.store", "--add-web", input_file], "Adding Web chunks...")
elif choice == "3":
query = Prompt.ask("Enter search query")
run_command(["python", "-m", "src.store", "--query", query], "Querying Vector Store...")
elif choice == "4":
run_command(["python", "-m", "src.db_stats"], "Fetching Statistics...")
input("\nPress Enter to continue...")
def menu_test_oradb():
print_header()
choices = [
questionary.Choice("Run basic connection tests", value="1"),
questionary.Choice("Show collection statistics only", value="2"),
questionary.Choice("Run text similarity search", value="3"),
questionary.Choice("Verify ONNX Model", value="4"),
questionary.Separator(),
questionary.Choice("Back to Main Menu", value="0"),
]
choice = questionary.select(
"Test Oracle DB Connection", choices=choices, use_arrow_keys=True
).ask()
if not choice or choice == "0":
return
if choice == "1":
run_command(["python", "tests/test_oradb.py"], "Testing Oracle DB...")
elif choice == "2":
run_command(["python", "tests/test_oradb.py", "--stats-only"], "Fetching Stats...")
elif choice == "3":
query = Prompt.ask("Enter test query", default="artificial intelligence")
run_command(["python", "tests/test_oradb.py", "--query", query], "Running Vector Search...")
elif choice == "4":
run_command(["python", "tests/test_oradb.py", "--model-check"], "Verifying ONNX Model...")
input("\nPress Enter to continue...")
def menu_rag_agent():
while True:
print_header()
console.print("[bold yellow]RAG Agent Chat (Qwen3.5:9b)[/bold yellow]")
console.print("Enter your query to chat with the agent.")
console.print("Type 'exit' or '0' to return to main menu.")
query = Prompt.ask("\n[bold green]Query[/bold green]")
if query.lower() in ["exit", "quit", "0"]:
break
use_cot = Confirm.ask("Use Chain of Thought reasoning?", default=False)
cmd = ["python", "-m", "src.local_rag_agent", "--query", query]
if use_cot:
cmd.append("--use-cot")
run_command(cmd, "Generating Answer...")
input("\nPress Enter to continue...")
def main_menu():
while True:
print_header()
choices = [
questionary.Choice("Process PDFs", value="1"),
questionary.Choice("Process Websites", value="2"),
questionary.Choice("Manage Vector Store", value="3"),
questionary.Choice("Test Oracle DB", value="4"),
questionary.Choice("Chat with Agent (RAG)", value="5"),
questionary.Separator(),
questionary.Choice("Exit", value="0"),
]
choice = questionary.select("Select a Task:", choices=choices, use_arrow_keys=True).ask()
if not choice or choice == "0":
console.print("[bold]Goodbye![/bold]")
sys.exit(0)
elif choice == "1":
menu_process_pdfs()
elif choice == "2":
menu_process_websites()
elif choice == "3":
menu_manage_vector_store()
elif choice == "4":
menu_test_oradb()
elif choice == "5":
menu_rag_agent()
if __name__ == "__main__":
try:
console.print("[bold cyan]Checking AI Model availability...[/bold cyan]")
if not ensure_model_loaded(get_db_connection()):
console.print(
"[bold red]Failed to ensure AI model is loaded. Some features may not work.[/bold red]"
)
input("Press Enter to continue anyway...")
main_menu()
except KeyboardInterrupt:
console.print("\n[bold red]Interrupted by user. Exiting...[/bold red]")
sys.exit(0)