TLDR: This Jupyter Notebook extracts specific HTML tags from an HTML document using a Language Model (LLM). It loads the HTML, splits it into chunks, processes it with Ollama’s nuextract embeddings, stores it in PostgreSQL via PGVector, and uses a retrieval chain to search and extract relevant HTML content based on a custom prompt.
These instructions will help you set up and run the project locally.
Ensure Python is installed. We recommend version 3.9.
-
Open your terminal and navigate to your project directory:
cd path/to/your-project-directory -
Set up a virtual environment:
python3 -m venv .venv
-
Create a file named
requirements.txtin the root of your project, listing the necessary libraries. -
Activate the virtual environment:
- Windows:
.\.venv\Scripts�ctivate
- MacOS/Linux:
source .venv/bin/activate
- Windows:
-
Upgrade pip within the environment:
.venv\Scripts\python.exe -m pip install --upgrade pip
Once your virtual environment is activated, install the required dependencies:
pip install -r requirements.txt- Download and install Ollama by clicking here.
- To set up the nuextract model, download it by visiting this link OR Run the following command in your terminal to install the model:
ollama run nuextract
Note: If you encounter any issues setting up PgVector, please refer to this helpful video tutorial.
Start by pulling the official PgVector Docker image:
docker pull pgvector/pgvector:pg17Run the Docker container and set your own PostgreSQL password (make sure to remember it):
docker run -d --name pgvector-demo-test -e POSTGRES_PASSWORD=<YOUR_PASSWORD> -p 5432:5432 pgvector/pgvector:pg17This will start a PostgreSQL container with PgVector installed, and you'll use <YOUR_PASSWORD> as the password for the postgres user.
-
Open PgAdmin and create a new server connection:
- Name: Choose any name for the connection.
- Hostname:
localhost - Port:
5432 - Username:
postgres - Password: Enter the password you provided when running the Docker container.
-
Create a new database (e.g.,
vector_db) within PgAdmin.
To connect to the PgVector instance from your project, use the following connection string format:
postgresql+psycopg2://postgres:<YOUR_PASSWORD>@localhost:5432/vector_db
- Replace
<YOUR_PASSWORD>with the password you set earlier. - Create a
.envfile and add this connection string in the file like this:PGVECTOR_CONNECTION_STRING="<YOUR_CONNECTION_STRING>"
The provided Jupyter Notebook expects a file named sample.html. You can use this file or replace it with your own HTML file containing the content you want to extract.
Open the Jupyter Notebook and execute the cells in sequence:
- Load Libraries: Import necessary libraries for text processing, LLM handling, and scraping.
- Setup LLM and Embeddings: The code uses Ollama's
nuextractmodel for language processing. - Load HTML Content: Load the HTML file (
sample.html), which is used as input for scraping. - Text Splitting: Split the HTML text into smaller chunks for more efficient processing.
- PGVector Setup: Connect to PostgreSQL and store the vectorized documents.
- Retrieve and Extract HTML Tags: Use the retrieval chain to scrape the HTML tags based on your input query.