End to End Medical Chatbot Project Implementation | Generative AI | English | Euron
Introduction to the Medical Chatbot Project
Overview of the Course
- The speaker introduces themselves as B Hammed and welcomes viewers to a course focused on implementing an end-to-end medical chatbot project.
- The chatbot will provide responses related to medical queries, including disease diagnosis and medicine suggestions.
Project Implementation Details
- The implementation will involve creating a complete system, including both backend and frontend components for user interaction.
- Modular coding in Python will be utilized instead of traditional scripting, emphasizing organized code structure.
- Version control using Git and GitHub will be integrated into the development process.
Project Architecture and Tools
Understanding the Project Framework
- The speaker plans to explain the overall architecture of the project before diving into implementation details.
- Custom medical data from a comprehensive medical encyclopedia will serve as the knowledge base for the chatbot.
Data Source Description
- The referenced book is titled "The G Encyclopedia of Medicine," containing 637 pages filled with extensive medical information.
- It includes various diseases, corresponding treatments, diagnostic techniques, and images related to those diseases.
Knowledge Base Creation
Utilizing Pinecone Vector Database
- Information from the medical book will be extracted to create a knowledge base using Pinecone Vector database for efficient storage of embedding vectors.
- Cloud-based storage is preferred due to the large size of data (637 pages), which would not be efficiently managed by local databases.
Chunking Process Explained
- Data extraction involves chunking information into manageable pieces suitable for input into large language models (LLMs).
- Each chunk must adhere to specific input size limitations set by LLM architectures like Llama or GPT.
Understanding Large Language Models and Their Implementation
Input Length and Chunking
- The first step in using a large language model is to assess the maximum token size of the model. This helps determine how to perform chunking on input data.
- After chunking, each segment (chunk one, chunk two, etc.) will be processed individually to generate vector embeddings using an embedding model.
Building a Knowledge Base
- The generated vector embeddings are stored in a "semantic index," which serves as the knowledge base for the application.
- A vector database like Pinecone is suggested for storing these embeddings, forming the backend component of the system.
Query Processing
- When a user submits a query, it is converted into a query embedding (vector format), which then interacts with the knowledge base.
- The knowledge base returns ranked results based on similarity to the query embedding.
Utilizing Large Language Models
- An OpenAI large language model (LLM) is recommended for processing queries and generating responses due to its accessibility via API requests.
- Using open-source models may require significant local resources (GPU/RAM), making them less practical for many users.
Cost Considerations and Deployment
- Open-source models can incur high costs when deployed on cloud platforms due to their large sizes and resource requirements.
- LLM Ops platforms like AWS Bedrock or GCP Vertex AI are mentioned as tools that facilitate efficient deployment of various models without excessive costs.
Final Response Generation
- The ranked results from the knowledge base and user queries are processed by the LLM to generate accurate responses.
Understanding the Architecture of a Medical Chatbot
Overview of the Project
- The project aims to develop a medical chatbot that can diagnose fever and suggest appropriate medication based on user queries.
- A user-friendly interface will be created, similar to ChatGPT, allowing users to input their questions and receive responses seamlessly.
Technology Stack
- The project will utilize OpenAI's large language model (LLM) for natural language processing tasks.
- LangChain will serve as the general framework for developing the entire application.
- Pinecone, a cloud-based vector database, will be used for storing vector embeddings generated from user inputs.
Development Environment Setup
- GitHub will be employed for version control throughout the development process.
- The project will eventually be deployed on AWS Cloud, with plans to cover both simple and CI/CD deployment strategies.
Creating a GitHub Repository
Steps to Initialize Repository
- A new repository named "end-to-end medical chatbot Genera VI" is created on GitHub as a public repository for sharing code.
- Users are instructed to clone this repository onto their local machines using Git commands.
Local Development Setup
- After cloning, users navigate into the cloned directory using terminal commands and open Visual Studio Code (VS Code).
- A virtual environment is established within the project folder to manage dependencies effectively.
Installing Project Dependencies
Requirements Specification
- A
requirements.txtfile is created listing all necessary libraries such as Sentence Transformer for generating vector embeddings.
Key Libraries Included:
- Sentence Transformer: Utilizes Hugging Face's platform for downloading models needed for embedding generation.
- LangChain: Framework essential for building applications with LLM capabilities.
- Flask: Used to create the web interface of the chatbot.
Installation and Project Structure Setup
Installing Required Packages
- The speaker discusses the installation of necessary packages for a project, including Pine con, Lenore Pine con, langin community, and langin experimental.
- After executing the installation command, the speaker confirms that all packages have been installed successfully without errors.
Creating Project Folder Structure
- Emphasizes the importance of establishing a clear project folder structure before starting any end-to-end projects.
- The speaker encourages flexibility in designing folder structures based on individual project requirements rather than strictly adhering to a predefined template.
Automating Folder Structure Creation
- Introduces a Python file named
template.pythat will automate the creation of the entire project folder structure.
- Highlights the inefficiency of manually creating numerous folders and files; automating this process saves time and reduces errors.
Utilizing Python Libraries for Automation
- The speaker plans to use libraries such as
osandpathlibto facilitate folder creation within their script.
- Discusses setting up basic logging configurations using Python's built-in logging module to track actions during execution.
Defining Folder and File Structure
- A list is created in Python to define which folders and files need to be generated automatically (e.g., creating an
SRCfolder with an__init__.pyfile).
How to Structure Python Projects Using a Modular Approach
Understanding the Modular Approach
- The speaker emphasizes the importance of organizing code into separate folders and components rather than writing all functions in a single file. This method is referred to as a modular approach.
- A Constructor file is essential for creating local packages, allowing for easy imports from specific directories within the project structure.
- The speaker plans to create a
helper.pyfile within theSRCfolder, which will contain various functionalities such as data ingestion and model downloading.
Project File Structure
- In addition to
helper.py, another file namedprompt.pywill be created to manage system prompts used throughout the project.
- The speaker mentions needing a
setup.pyfile for installing local packages, indicating that this is crucial for project setup alongside an existing requirements.txt file.
- A Jupyter notebook named
trials.ipynbwill be included in a new folder called "research" for experimentation and testing purposes.
Handling File Paths Across Operating Systems
- The discussion includes looping through lists of files and paths, highlighting differences between Windows (which uses backslashes) and Linux/Mac (which use forward slashes).
- To address path compatibility issues across operating systems, the speaker utilizes the Path library from pathlib, which automatically adjusts paths based on the OS being used.
Ensuring Robustness in Software Design
- Emphasizing software robustness, it's important to handle different operating system scenarios during design to prevent failures in production environments.
- The speaker discusses splitting folder names and filenames using path functions, ensuring clarity when managing directory structures.
Creating Directories Programmatically
- To create directories programmatically, the speaker checks if a directory exists before attempting creation. This prevents errors related to non-existent paths.
Creating a Folder Structure and Setting Up a Python Project
Overview of the Script Functionality
- The speaker describes a Python script that checks for the existence of a file path before creating it. If the file exists and is not empty, it skips creation to avoid overwriting.
- The script also verifies if the file size is zero; if so, it creates the file. Otherwise, it informs that the file already exists.
Demonstration of Script Execution
- Upon executing the script, an automatic folder structure is created in the terminal without any prior setup.
- The speaker emphasizes that this code only needs to be written once and can be reused for future projects by copying and executing it.
Adding Files Dynamically
- To add more files like
test.py, users simply need to include them in a list within the script. This allows for easy expansion of project requirements.
- After running the updated script, additional files are automatically created as specified.
Setting Up Local Packages with setup.py
- The speaker introduces
setup.py, which includes essential information such as project name, version, author details, and package discovery through__init__.py.
- A command (
pip install -e .) is provided to set up local packages based on requirements listed in a separate file.
Committing Changes to GitHub
- Instructions are given on how to commit changes using Git commands (
git add,git commit,git push), ensuring updates reflect on GitHub.
Transitioning from Jupyter Notebook to Modular Code
- The speaker plans to implement project features first in Jupyter Notebook before converting them into modular code for better organization.
Working Directory Management
Understanding Directory Navigation and PDF Processing in Python
Navigating Directories
- The command
os.chdir()is used to change the current working directory. Using..allows navigation one folder back.
- It's crucial to always work within the project folder to avoid path issues, which can lead to errors like "data not found."
Importing Libraries for PDF Handling
- Libraries such as
PDFLoaderandDirectoryLoaderare imported from LangChain for handling PDF documents.
- A function named
load_pdf()is created to extract data specifically from PDF files located in a given directory.
Extracting Data from PDFs
- The
load_pdf()function utilizesPDFLoaderto extract information, returning it as document objects.
- Extraction may take time depending on the size of the PDF; in this case, it involves processing around 700 pages.
Chunking Extracted Data
- After extraction, chunking operations are performed using a recursive text splitter to manage large amounts of data effectively.
- The chunk size is set at 5500 characters with an overlap of 20 characters, resulting in approximately 720 chunks from the extracted data.
Vector Embedding and Storage
- An embedding model will be used for vector embedding, which will then be stored in a Pinecone vector database.
- The Hugging Face library is utilized to download an embedding model called "all-miniLM-L6-v2," which provides a vector dimension of 384.
Testing the Embedding Model
- After downloading, a test sentence ("hello world") is converted into vector embeddings using the function
embed_query().
- It confirms that the output length matches the expected dimensionality (384), validating that the model works correctly.
Initializing Pinecone Database
How to Set Up Pinecone for Vector Database
Introduction to Pinecone
- The speaker introduces the Pinecone website, highlighting its user-friendly interface and capabilities as a vector database provider. They express personal enthusiasm for the platform.
Creating an Index
- To store data in Pinecone, users must create an index (or cluster). The speaker mentions that code examples are provided for guidance.
- Users need to collect an API key from the left-hand side of the interface. The speaker demonstrates how to create one by naming it (e.g., "test").
Configuring the Environment
- After obtaining the API key, it should be copied into an environment file (EnV file). The importance of keeping this key confidential is emphasized.
Manual Index Creation Process
- The speaker explains how to manually create an index by selecting options such as name and dimension (e.g., 384), along with choosing a similarity matrix like cosine score.
- A free plan allows users to create up to two clusters with AWS cloud provider; however, exceeding 2 GB storage requires a premium subscription.
Subscription Plans and Options
- Details about different subscription plans are discussed. Free services provide limited storage while premium plans offer more space and features.
Automatic Index Creation via Python Code
- The manual approach is contrasted with creating an index through Python code. The speaker prepares to delete any manually created indexes before proceeding with coding.
Implementing Python Code for Index Creation
- Instructions on using pre-written code for creating indexes in Pinecone are shared. Users can specify parameters like index name and dimensions directly in their code.
Using Environment Variables Securely
- To enhance security, the speaker suggests importing environment variables instead of hardcoding sensitive information like API keys into scripts.
Finalizing Index Creation
- Once executed correctly, users will see their specified index (e.g., "medical bot") created automatically in Pinecone's dashboard.
Next Steps After Index Creation
Understanding Pinecone API Key Management
Setting Up Environment Variables
- The Pinecone service requires an API key stored in environment variables for secure access. This is achieved using the
os.environmethod to set the API key from an.envfile.
- Once the API key is set, it eliminates the need to repeatedly pass it during code execution, streamlining the process.
Storing Vector Embeddings
- To store vector embeddings in Pinecone, you must import
PineconeVectorStoreand use itsfrom_documentsfunction with your text chunks and index name.
- Ensure that the specified index already exists; this is where your data will be stored. You also need to provide an embedding model downloaded from Hugging Face.
Execution and Data Visualization
- Executing the code converts all data into vector embeddings and stores them in Pinecone. In this case, 720 chunks of vectors are expected to be stored.
- After refreshing the Pinecone dashboard, users can visualize their vectors, including a count of added chunks (e.g., 864), which enhances understanding compared to local storage methods.
Exploring Vector Representation and Query Operations
Detailed Vector Insights
- Users can view detailed visualizations of vector representations, including semantic scores between vectors and associated metadata like page numbers and PDF names.
- The platform allows for SQL-like query operations through Python code rather than manual approaches.
Knowledge Base Integration
- With over 7,000 vectors stored in Pinecone's index, these can serve as a knowledge base for connecting with large language models (LLMs).
- This integration enables semantic search capabilities where queries can retrieve relevant information based on user input.
Loading Existing Indexes and Performing Similarity Searches
Accessing Existing Data
- To load an existing index in Pinecone, import
PineConeVectoragain and utilize thefrom_existing_indexfunction along with specifying the index name and embedding model.
Conducting Similarity Searches
- Users can perform similarity searches by initializing a retrieval object that fetches relevant documents based on keywords (e.g., "acne").
- The system returns multiple results related to "acne," showcasing how it retrieves contextually relevant information from medical data sources.
Enhancing Output Quality with LLM Integration
Initializing the Model and Setting Up API Key
Initializing the OpenAI Model
- The speaker discusses initializing their model using OpenAI, mentioning parameters like temperature and max tokens.
- An error occurs due to the absence of an OpenAI API key; the speaker demonstrates how to set it up in their environment.
Environment Configuration
- After setting the API key, the speaker successfully initializes their large language model (LLM).
- The process of creating a retrieval-augmented generation (RAG) application is introduced, including importing necessary libraries.
Creating Prompt Templates and Chains
System Prompt Creation
- A system prompt is established for user input, which will guide responses from the LLM.
Chain Initialization
- The speaker explains how to create a chain that integrates both the LLM and user prompts for generating outputs.
Testing Responses with User Queries
Valid Query Example
- A query about acne yields a detailed response regarding its causes and treatment options, demonstrating effective information retrieval.
Out-of-context Query Handling
- When asked an irrelevant question about statistics, the model responds with "I don't know," highlighting its reliance on context-specific data provided during initialization.
Demonstrating Custom Chatbot Functionality
Custom Data Utilization
- The custom chatbot effectively answers questions based solely on specific data fed into it, showcasing its tailored functionality.
Additional Disease Inquiry
- Another disease-related question results in accurate information being retrieved from the model's knowledge base.
Modular Coding for End-to-End Implementation
Structuring Code for Clarity
- The speaker emphasizes creating modular code by organizing utility functions related to loading data and embedding models into separate files.
Error Resolution in Environment Setup
- Errors encountered are resolved by selecting the correct environment setup before proceeding with coding tasks.
Finalizing Project Structure
Organizing Helper Functions
How to Store and Update Data in a Vector Database
Storing Embeddings in the Knowledge Base
- The speaker discusses naming a file
store_index.py, emphasizing its role in updating book information by storing embeddings back into the knowledge base (Pinecone).
- Code is extracted from documents, chunks are created, and embeddings are pushed to the vector database. The importance of organizing code within a helper function is highlighted.
Importing Necessary Libraries
- The speaker imports essential libraries including functions for loading PDFs, splitting text, and downloading face embeddings from a source helper module.
- Environment variables such as Pinecone API credentials are set up before loading data and initializing the embedding model.
Initializing Pinecone and Creating Indexes
- Initialization of Pinecone creates a medical chatbot index that stores embeddings. This step must be executed initially to ensure proper vector conversion.
- Previous embeddings may need deletion if modular coding is being implemented; this ensures that only relevant data is stored.
Executing Store Index Script
- Before launching an application, executing
store_index.pyis crucial for creating necessary indexes for query operations.
- The script should only be run once unless new data needs to be added; it sets up the environment for future updates.
Application Launching Process
- After confirming deletions, the speaker demonstrates executing
store_index.pyvia terminal to create indexes and store vectors effectively.
- Successful execution results in all vectors being stored correctly within the index.
Building User Interface with Flask
- Introduction of Flask as the framework for building user applications; HTML and CSS will be used to create an appealing user interface.
- Bootstrap templates can be utilized for designing chat interfaces; various free templates are available online.
Setting Up HTML and CSS Files
- Instructions on creating folders named 'templates' and 'static' within Flask's structure; these will hold HTML (
index.html) and CSS (style.css) files respectively.
Creating a Medical Chatbot with Flask and Pinecone
Setting Up the Environment
- The speaker begins by outlining the required packages for the project, specifically mentioning Flask and its functionalities.
- The Pinecone index is introduced as a necessary component to load existing data, emphasizing the importance of embedding objects in this process.
- The prompt file is imported from
prompt.py, indicating that it will be used to manage user interactions within the application.
- An operating system module is also imported, which may be utilized for various system-level operations during development.
Initializing Flask and Loading Models
- The initialization of Flask is discussed, with a reference to official documentation for proper setup.
- Two routes are created: one for rendering the chat interface (default route), and another for handling user queries directed at the chatbot.
- The input message from users is processed through an LLM (Language Model), showcasing how responses are generated based on user queries.
Running the Application
- Instructions are provided on executing the application locally using Python, highlighting potential issues like spelling errors in commands.
- Upon successful execution, a demonstration of accessing the application via Google Chrome on localhost port 8080 showcases its user-friendly interface.
User Interaction with Chatbot
- Users can ask various questions about medical conditions; examples include inquiries about acne and fever treatment options.
- Responses from the chatbot provide relevant medical advice, illustrating its capability to assist users effectively.
Deployment Considerations
- Future plans include deploying the application after implementing additional projects. A dedicated session will focus on cloud deployment strategies.
- Code updates are committed to GitHub, ensuring version control and accessibility for others interested in replicating or building upon this project.
Final Touches and Documentation
- Updates made to README files include detailed steps for setting up environments and running applications locally, enhancing usability for future developers.