Implementation: Phase 1

This page describes the implementation of key features in the project. The application integrates document processing, a vector database for semantic search, a chat-based user interface, and robust resource management. Each section below explains one of the core features along with code snippets and diagrams where appropriate.

1. Document Processing and Database Connection

Our system supports processing various file formats (PDF, Markdown, Word) by leveraging the following libraries:

The code (in populate_database.py and embedding.py) performs the following:

The following snippet shows how chunk IDs are calculated:

def calculate_chunk_ids(chunks):
            last_page_id = None
            current_chunk_index = 0
            for chunk in chunks:
                source = chunk.metadata.get("source")
                page = chunk.metadata.get("page")
                current_page_id = f"{source}:{page}"
                if current_page_id == last_page_id:
                    current_chunk_index += 1
                else:
                    current_chunk_index = 0
                chunk.metadata["id"] = f"{current_page_id}:{current_chunk_index}"
                last_page_id = current_page_id
            return chunks
            

2. Chat UI and Interaction

The user interface is built with Tkinter, Python's standard GUI toolkit. The main file (main.py) is responsible for:

The UI uses a grid layout to ensure responsiveness. For example, the chat display area is initialized as follows:

# Initialize main application window
        root = tk.Tk()
        root.title("AI RAG Assistant")
        root.geometry("800x600")
        
        # Chat display area
        output_box = scrolledtext.ScrolledText(root, wrap=tk.WORD)
        output_box.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
            

4. Resource Management and Cleanup

Effective resource management is essential for stability and performance. The implementation ensures:

The following function demonstrates the cleanup process executed when the application is closed:

def on_closing():
            try:
                # Close the vector database connection
                db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function())
                db._client._system.stop()
                
                # Release LLM resources
                global model, tokenizer, generator
                model = None
                tokenizer = None
                generator = None
        
                # Clear GPU memory if available
                if torch.cuda.is_available():
                    torch.cuda.empty_cache()
            except Exception as e:
                print(f"Cleanup error: {e}")
            root.destroy()
            

5. Additional Features

Other important features include:

These features enhance the usability of the application by offering a flexible and interactive interface.

Conclusion

Our project integrates various modern tools and libraries to create a robust AI-assisted retrieval system. With document processing, semantic search capabilities, and an interactive chat UI, the system offers a comprehensive solution for managing and querying large sets of documents. The detailed implementation provided here should serve as a useful reference for understanding and extending the system.