The Chat LSE Project is based on the Rag on Postgres project with modifictaions to adapt to LSE's development and production environment.
Major modifications are:
- Removed all Azure components
- Removed all VSCode-related configurations
- Removed dependency on close-sourced OpenAI chat and embedding models
The code has been tested on Windows 11, Ubuntu 20.04 LTS and MacOS(M2).
The overall architecture of the app is shown in the figure:
There are 4 main components, the Frontend APP (FluentUI, React JS), the backend APP (FastAPI), PostgreSQL database and Ollama service.
To run the full app, all four components have to be setup properly either on a local development machine or on a remote server.
For development purpose, the frontend APP, backend APP should always be setup on a local machine.
PostgreSQL and Ollama could be setup locally or use a remote setup.
Make sure the following software are properly installed on your OS:
- Setup PostgreSQL locally
- (Optionally) Setup Ollama locally
- Setup environment
3.1. Install Python dependencies
3.2. Config environment variables - Initialise the database
4.1. Run crawler to populate database
4.2. Run the embedding script - Start the FastAPI APP
- Setup and run Frontend APP
6.1. Install npm dependencies
6.2. Start the frontend APP
6.3. Use the APP
Run PostrgeSQL using docker container:
$ docker run -itd --name chatlse-postgres --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=chatlse -e POSTGRES_USER=chatlse -e POSTGRES_DB=chatlse -d pgvector/pgvector:0.7.1-pg16To verify the container is up and running:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45d7301f5ef8 pgvector/pgvector:0.7.1-pg16 "docker-entrypoint.s…" 2 seconds ago Up 2 seconds 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp chatlse-postgresThe STATUS of the container shoule be something like "Up 2 seconds".
Download Ollama (recommended) or pull its docker image using:
CPU only:
$ docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollamaNvidia GPU
docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollamaOpen the terminal in VSCode by clicking on 'Terminal' -> 'New Terminal' and create a virtual environment.
# Use conda as an example. Can also use other virtual environments like venv
conda create -n chat-lse python=3.11 ipython
conda activate chat-lse # or the equivalent for your OS(Important) Ensure that pip refers to the pip inside the conda environment we just created:
which pipThis should output something like /home/your-username/miniconda3/envs/chat-lse/bin/pip
Install dependencies
pip install -r requirements.txtCopy .env.sample into .env.
# For local setup
POSTGRES_HOST=localhost
# Remote setup, for testing and deployment only
POSTGRES_HOST=<Host IP address>
# For local setup. Only use this if you have local Ollama running.
OLLAMA_ENDPOINT=http://localhost:11434/v1
# For Remote setup
OLLAMA_ENDPOINT=http://<Host IP address>:11434/v1
The following script will take a while for the first time you run it as it crawls through all the files and webpages with lse.ac.uk domain name. Subsequent runs of the crawler should be quicker as it only updates the files and webpages that has changed.
Run the following code to start the crawler :
sh scripts/start_crawlers.sh Set up embedding type in the .env file:
# Select embedding type from ["simple_embeddings", "title_embeddings", "context_embeddings"]
EMBEDDING_TYPE=title_embeddings
The default setting is title_embeddings as our experiments show that it provides the best results.
The following script will take a while for the first time you run it as it generates embeddings for all the documents in the database. Subsequent runs of the embedding script should be quicker as it only updates the embeddings for the documents that has changed.
Run the following code to start the embedding script:
sh scripts/embed_db.shWe need our API to be running in the background, to handle requests from the website to LLAMA and Postgres:
sh ./scripts/start_fastapi_app.shYou should see something like:
INFO: Will watch for changes in these directories: ['<your-path-to>/chat-lse']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [7609] using WatchFiles
WARNING: ASGI app factory detected. Using it, but please consider setting the --factory flag explicitly.
INFO: Started server process [7611]
INFO: Waiting for application startup.
INFO:ragapp:Authenticating to PostgreSQL using password...
INFO:ragapp:Authenticating to OpenAI using Ollama...
INFO:ragapp:Authenticating to OpenAI using Ollama...
INFO: Application startup complete.# Go to chat-lse/frontend
cd frontend
npm installYou might see the following warning. We can ignore it for now.
1 moderate severity vulnerability
To address all issues, run:
npm audit fix
Run `npm audit` for details.Open a new terminal and run:
# Go to chat-lse/frontend
$ cd frontend
$ npm run devYou should see something like:
> frontend@0.0.0 dev
> vite
VITE v4.5.2 ready in 309 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h to show helpOpen http://localhost:5173/ in the web browser to try the app.
