-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface.py
277 lines (230 loc) · 10 KB
/
interface.py
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
import os
import sys
import textwrap
from typing import Optional, Any
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.prompt import Prompt
from rich.markdown import Markdown
from core import RAGSystem, logger
# Initialize Rich console
console = Console()
def format_source_document(doc) -> str:
"""Format source document for display"""
source = doc.metadata.get("source", "Unknown source")
content = textwrap.fill(doc.page_content, width=100)
return f"\nSource: {source}\nContent: {content}\n"
def chat_loop(qa_chain):
"""Interactive chat loop"""
logger.info("Starting chat loop")
# Clear any previous content
console.clear()
console.print(Panel.fit(
"[bold green]Welcome to the Document Q&A System![/bold green]\n"
"Type 'quit', 'exit', or press Ctrl+C to end the conversation.\n"
"Type 'sources' to see the full source documents for the last answer.",
title="Chat System"
))
last_source_docs = None
while True:
try:
query = Prompt.ask("\n[bold blue]Your question[/bold blue]")
if query.lower() in ['quit', 'exit', 'q']:
logger.info("Chat session ended by user")
console.print("[bold green]Goodbye![/bold green]")
break
if query.lower() == 'sources' and last_source_docs:
console.print("\n[bold]Detailed Source Documents[/bold]")
for i, doc in enumerate(last_source_docs, 1):
console.print(Panel(
format_source_document(doc),
title=f"Source Document {i}",
border_style="blue"
))
continue
if not query:
console.print("[yellow]Please enter a question.[/yellow]")
continue
# Show thinking status
with console.status("[bold green]Thinking...", spinner="dots"):
logger.info(f"Processing query: {query}")
response = qa_chain.invoke({"query": query})
logger.info("Response generated successfully")
last_source_docs = response.get('source_documents', [])
# Display response in a panel
console.print(Panel(
response['result'],
title="Answer",
border_style="green"
))
# Display sources in a table
if last_source_docs:
sources_table = Table(title="Sources")
sources_table.add_column("№", style="cyan")
sources_table.add_column("Source", style="green")
for i, doc in enumerate(last_source_docs, 1):
source = doc.metadata.get("source", "Unknown source")
sources_table.add_row(str(i), source)
console.print(sources_table)
console.print("\nType 'sources' to see the full source documents.")
except KeyboardInterrupt:
logger.info("Chat session interrupted by user")
console.print("\n[bold green]Goodbye![/bold green]")
break
except Exception as e:
logger.error(f"Error in chat loop: {str(e)}")
console.print(f"\n[bold red]An error occurred:[/bold red] {str(e)}")
console.print("[yellow]Please try asking your question in a different way.[/yellow]")
# 1. Index documents and create vector store
def process_documents_with_status(rag: RAGSystem) -> Optional[Any]:
"""Process documents with status updates"""
try:
console.print("[bold blue]Loading documents...[/bold blue]")
documents = rag.load_documents("./documents")
if documents:
console.print(f"[bold blue]Processing {len(documents)} documents...[/bold blue]")
texts = rag.process_documents(documents)
if texts:
console.print("[bold blue]Creating vector store...[/bold blue]")
vectordb = rag.create_vector_store(texts)
return vectordb
except Exception as e:
logger.error(f"Error processing documents: {str(e)}")
return None
def display_menu() -> str:
"""Display the main menu"""
menu = Table(title="RAG System Menu", show_header=False, show_lines=True)
menu.add_column("Option", style="cyan")
menu.add_row("1. Index documents")
menu.add_row("2. Check total number of documents")
menu.add_row("3. Delete document store")
menu.add_row("4. Start RAG chat")
menu.add_row("5. Exit")
console.print(menu)
return Prompt.ask("\nSelect an option", choices=["1", "2", "3", "4", "5"])
# 4. Start chat
def start_chat(rag: RAGSystem):
"""Initialize and start the chat system"""
vectordb = None
qa_chain = None
try:
# First try to load the vector store
console.print("[bold blue]Loading vector store...[/bold blue]")
vectordb = rag.load_vector_store()
if not vectordb:
console.print("[bold red]No document store found. Please index documents first.[/bold red]")
return
# Create QA chain
console.print("[bold blue]Creating QA chain...[/bold blue]")
qa_chain = rag.create_qa_chain(vectordb)
if not qa_chain:
console.print("[bold red]Failed to create QA chain.[/bold red]")
return
# Start chat loop
console.print("[bold green]Chat system ready![/bold green]")
chat_loop(qa_chain)
except Exception as e:
logger.error(f"Error starting chat: {str(e)}")
console.print(f"\n[bold red]Failed to start chat:[/bold red] {str(e)}")
if not vectordb:
console.print("[yellow]Hint: Make sure you have indexed documents first (Option 1)[/yellow]")
elif not qa_chain:
console.print("[yellow]Hint: There might be an issue with the OpenAI API key or connection[/yellow]")
def main():
"""Main function"""
try:
logger.info("Starting RAG System")
rag = RAGSystem()
while True:
choice = display_menu()
if choice == "1":
logger.info("User selected: Index documents")
vectordb = process_documents_with_status(rag)
if vectordb:
console.print("[bold green]Documents indexed successfully![/bold green]")
else:
console.print("[bold red]Failed to index documents.[/bold red]")
elif choice == "2":
logger.info("User selected: Check document count")
console.print("[bold blue]Getting document count...[/bold blue]")
count = rag.get_document_count()
console.print(f"\nTotal documents: [bold green]{count}[/bold green]")
elif choice == "3":
logger.info("User selected: Delete document store")
console.print("[bold red]Deleting document store...[/bold red]")
if rag.delete_vector_store():
console.print("[bold green]Document store deleted successfully![/bold green]")
else:
console.print("[bold red]Failed to delete document store.[/bold red]")
elif choice == "4":
logger.info("User selected: Start RAG chat")
start_chat(rag)
elif choice == "5":
logger.info("User selected: Exit")
console.print("[bold green]Goodbye![/bold green]")
break
# Add a small pause between operations
console.print("\nPress Enter to continue...")
input()
except KeyboardInterrupt:
console.print("\n[bold green]Goodbye![/bold green]")
sys.exit(0)
except Exception as e:
logger.error(f"Fatal error: {str(e)}")
console.print(f"\n[bold red]A fatal error occurred:[/bold red] {str(e)}")
sys.exit(1)
def check_environment():
"""Check and validate environment setup"""
# Check for required directories
if not os.path.exists("documents"):
os.makedirs("documents")
console.print("[yellow]Created 'documents' directory[/yellow]")
if not os.path.exists("logs"):
os.makedirs("logs")
console.print("[yellow]Created 'logs' directory[/yellow]")
# Check for .env file
if not os.path.exists(".env"):
console.print("[bold red]Error: .env file not found![/bold red]")
console.print(Panel(
"Please create a .env file with the following contents:\n\n" +
"OPENAI_API_KEY=your-api-key-here\n" +
"MODEL_NAME=gpt-3.5-turbo\n" +
"COLLECTION_NAME=my_documents\n" +
"PERSIST_DIRECTORY=db",
title="Required Configuration",
border_style="red"
))
return False
return True
if __name__ == "__main__":
try:
# Show welcome banner
console.print(Panel.fit(
"[bold blue]RAG System[/bold blue]\n" +
"Document Q&A System using LangChain and ChromaDB",
border_style="blue"
))
# Check environment
if not check_environment():
sys.exit(1)
# Run main application
main()
except KeyboardInterrupt:
console.print("\n[bold green]Goodbye![/bold green]")
sys.exit(0)
except Exception as e:
logger.error(f"Fatal error: {str(e)}")
console.print(Panel(
str(e),
title="Error Details",
border_style="red"
))
sys.exit(1)
finally:
# Ensure proper cleanup
try:
sys.stdout.flush()
sys.stderr.flush()
except Exception:
pass