Skip to content

SamSamhuns/log_analyzer_with_llms_and_sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Log Analyzer with LLMs and MySQL

masterCodacy Badge

Python 3.10Python 3.11Python 3.12

Tested with Docker version v27.0.3 and Docker Compose version v2.29.1.

Backend with fastapi+uvicorn for log analysis with LLMs and MySQL queries.

API Architecture Setup

Setup

1. Create .env file

Create a .env file with the following keys with updated values for usernames and passwords:

# set to ERROR for deployment DEBUG_LEVEL=DEBUG # http api server API_SERVER_PORT=8080 # openai api key OPENAI_API_KEY=<OPENAI_API_KEY> # langchain langsmith keys USER_AGENT=log_analyzer LANGCHAIN_PROJECT=log_analyzer LANGCHAIN_TRACING_V2=true LANGCHAIN_ENDPOINT=https://api.smith.langchain.com LANGCHAIN_API_KEY=<LANGCHAIN_API_KEY> # mysql mariadb MYSQL_HOST=mysql MYSQL_PORT=3306 MYSQL_USER=user MYSQL_PASSWORD=pass MYSQL_DATABASE=default MYSQL_ROOT_PASSWORD=admin # phpmyadmin mariadb PMA_GUI_PORT=8001 PMA_HOST=${MYSQL_HOST} PMA_PORT=${MYSQL_PORT} PMA_USER=${MYSQL_USER} PMA_PASSWORD=${MYSQL_PASSWORD}

2. Create shared volumes directory

mkdir -p volumes/log_analyzer mkdir -p volumes/store

Running the log analysis service

There are two options for running the analysis service. Both require docker compose (Available from the official docker site). $docker-compose ... style commands have been depreciated.

Option A) Docker Compose

Note: some services are set to bind to all addresses which should be changed in a production environment.

# build all required containers docker compose build # start all services docker compose up -d

The server will be available at http://localhost:8080 if using the default port.

Note:

When changing settings in docker-compose.yaml for the mongodb service, the existing docker and shared volumes might have to be purged i.e. when changing replicaset name.

Warning

This will delete all existing user, document, and vector records.

docker-compose down docker volume rm $(docker volume ls -q) rm -rf volumes/store

Option B) Docker and local virtual env

# build all required containers docker compose build # start mysql server & phpmyadmin server docker compose up -d mysql mysql-admin

Option Bi) uvicorn server with fastapi with Docker

Build server container and start server at HTTP port EXPOSED_HTTP_PORT

bash scripts/build_docker.sh bash scripts/run_docker.sh -p EXPOSED_HTTP_PORT

The server will be available at http://localhost:8080 if using the default port.

Option Bii) uvicorn server with fastapi with venv

Install requirements inside venv or conda environment

python -m venv venv source venv/bin/activate pip install -r requirements.txt

Start server at HTTP port EXPOSED_HTTP_PORT. Note the host names must contain addresses when using docker microservices and the fastapi+uvicorn server outside the docker compose environment.

python app/server.py -p EXPOSED_HTTP_PORT

The server will be available at http://localhost:8080 if using the default port.

Optional: frontend with streamlit

pip install streamlit==1.38.0 streamlit run app/streamlit_frontend.py

Optional: expose app through ngrok docker for sharing localhost on the internet

WARNING: Never use for production

# start log analyzer with python # sign up for ngrok account at https://ngrok.com/ # https://ngrok.com/docs/using-ngrok-with/docker/ docker pull ngrok/ngrok # for linux systems docker run --net=host -it -e NGROK_AUTHTOKEN=<NGROK_AUTHTOKEN> ngrok/ngrok:latest http <EXPOSED_HTTP_PORT> # for MacOS and windows docker run -it -e NGROK_AUTHTOKEN=<NGROK_AUTHTOKEN> ngrok/ngrok:latest http host.docker.internal:<EXPOSED_HTTP_PORT>

Testing

Note: all the microservices must already be running with docker compose.

Install requirements:

pip install -r tests/requirements.txt

Run tests:

pytest tests/

Generating coverage reports

coverage run -m pytest tests/ coverage report -m -i

For Developers

To change/add/delete new log table schemas

The new SQL table should also be created through the PHPMyAdmin GUI/mysql command line inside the mariadb container.

The following files must be edited.

  • Edit app/static/sql/init.sql for changing/adding log table schema
  • Edit app/models/model.py to add/edit the LogFileType
  • Edit app/api/log_format/log_parser.py for parsing logs
  • Edit app/core/setup.py for adding table schema and data sample info for text2sql conversion

Editing Tests

  • Edit tests/conftests.py for setting the correct values for the test database
  • Edit tests/api/test_mysql_api.py for setting the correct values for the test database

Reference

About

A log summarizer api service using uvicorn, fastapi, mysql and llms

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •