This document provides an in-depth exploration of the implementation and utilization of Python, LLM, and Machine Learning, within an anomaly detection pipeline. The project integrates data preparation, feature extraction, anomaly detection, and evaluation into a cohesive system.
- Project Overview
- Technology Stack
- Pipeline Architecture
- Python Implementation
- AI and Language Models
- Machine Learning Techniques
- Data Flow and Processing
- Visualization and Interpretability
- API Integration
- Scalability and Performance
- Error Handling and Logging
- Future Enhancements and Comments
- Installation and Usage
The anomaly detection pipeline is a multi-stage system designed to process text data, identify outliers (anomalies), and evaluate performance. It comprises four core stages:
- Data Preparation: Augments text data using GPT-4o.
- Feature Extraction & Vectorization: Extracts semantic features and converts text into numerical embeddings.
- Anomaly Detection: Applies an Isolation Forest model to detect anomalies in the vectorized data.
- Evaluation: Assesses the model's performance against known anomalies.
The pipeline is orchestrated via a FastAPI-based RESTful API, enabling asynchronous execution and status monitoring. This project exemplifies the fusion of AI-driven NLP and unsupervised ML for anomaly detection.
📂 project-root
├── 📂 data
├── 📂 images
├── 📂 src
│ ├── 🐍 __init__.py
│ ├── 🐍 anomaly_detection.py
│ ├── 🐍 config.py
│ ├── 🐍 data_preparation.py
│ ├── 🐍 evaluation.py
│ ├── 🐍 feature_extraction.py
│ ├── 🐍 models.py
│ ├── 🐍 openai_client.py
│ ├── 🐍 utils.py
├── 🧪 .env.copy
├── 🙈 .gitignore
├── 📜 .python-version
├── 🐍 app.py
├── 🐍 instance.py
├── 📜 LICENSE
├── 📦 pyproject.toml
├── 📄 README.md
├── 🔒 uv.lock
The project leverages a robust stack of Python libraries and tools:
- Python 3.12: Core programming language for scripting and orchestration.
- Pandas & NumPy: Data manipulation and numerical computation.
- Scikit-learn: Machine learning algorithms (Isolation Forest, PCA) and evaluation metrics.
- OpenAI API (GPT-4o): Language model for text augmentation, feature extraction, and anomaly summarization.
- Matplotlib & Seaborn: Data visualization for anomaly analysis.
- Graphviz: Visualization of decision trees in Isolation Forest.
- FastAPI & Uvicorn: Asynchronous API framework and server.
- Concurrent.futures: Parallel processing for scalability.
- Pydantic: Data validation and type enforcement.
- Logging: Error tracking and debugging.
The pipeline follows a modular, sequential architecture:
-
Data Preparation (
data_preparation.py):- Input: Raw text dataset (CSV).
- Process: Augments data using GPT-4o paraphrasing.
- Output: Augmented dataset (CSV).
-
Feature Extraction & Vectorization (
feature_extraction.py):- Input: Augmented dataset.
- Process: Extracts features (entities, sentiment, theme) and vectorizes text (TF-IDF or OpenAI embeddings).
- Output: Vectorized dataset with features (CSV).
-
Anomaly Detection (
anomaly_detection.py):- Input: Vectorized dataset.
- Process: Trains an Isolation Forest model, detects anomalies, and summarizes them with GPT-4o.
- Output: Dataset with anomaly scores and summaries (CSV), visualizations (PNG).
-
Evaluation (
evaluation.py):- Input: Anomaly-detected dataset, true anomaly labels.
- Process: Computes performance metrics (precision, recall, F1, ROC-AUC).
- Output: Metadata with evaluation results (JSON).
-
API Layer (
app.py&instance.py):- Orchestrates pipeline execution and provides status endpoints.
Python serves as the backbone of the project, enabling modular design, efficient data handling, and integration with AI/ML libraries.
- Type Hints: Functions use type annotations (e.g.,
Tuple[IsolationForest, np.ndarray, np.ndarray]) for clarity and static type checking. - Exception Handling: Comprehensive
try-exceptblocks ensure robustness (e.g., intrain_isolation_forest). - Parallel Processing:
concurrent.futures.ThreadPoolExecutoraccelerates tasks like feature extraction and anomaly summarization. - Object-Oriented Design:
PipelineConfigandPipelineRunRequest(via Pydantic) encapsulate configuration and data models. - File I/O: Custom utilities (
load_dataset,save_dataset) handle CSV and JSON operations.
def summarize_anomalies_parallel(client, df: pd.DataFrame, num_workers: int = 5) -> List[str]:
anomaly_texts = df[df["is_anomaly"]]["text"].tolist()
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
summaries = list(executor.map(lambda t: summarize_anomalies(client, t), anomaly_texts))
return summariesThis leverages Python's threading capabilities to parallelize GPT-4o summarization, improving throughput for large datasets.
The OpenAI GPT-4o model is a cornerstone of the pipeline, enhancing data augmentation, feature extraction, and anomaly interpretation.
- Client Initialization:
init_openai_client(insrc.openai_client) configures two clients: one for text generation (client_text) and one for embeddings (client_embedding). - API Calls: Structured using
client.chat.completions.createfor text tasks andclient.embeddings.createfor vectorization.
-
Text Augmentation (
data_preparation.py)- GPT-4o paraphrases input texts with high creativity (
temperature=1.5) to enrich the dataset. - Example:
response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": f"Paraphrase this: '{text}'"}], max_tokens=100, temperature=1.5, )
- Output: Synthetic texts stored with unique IDs and timestamps.
- GPT-4o paraphrases input texts with high creativity (
-
Feature Extraction (
feature_extraction.py)- GPT-4o extracts entities, sentiment, and themes via structured parsing (
response_format=Features). - Warning: The current implementation uses
evalfor parsing, which is insecure in production; JSON parsing is recommended. - Example:
class Features(BaseModel): entities: List[str] sentiment: str theme: str response = client.beta.chat.completions.parse( model="gpt-4o", messages=[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": f"Extract entities, sentiment, and theme from by going through step by step: '{text}'"}], response_format=Features, )
- GPT-4o extracts entities, sentiment, and themes via structured parsing (
-
Anomaly Summarization (
anomaly_detection.py)- GPT-4o summarizes why flagged texts are anomalous, providing human-readable insights.
- Latency: API calls introduce network latency; parallelization mitigates this.
- Cost: Frequent GPT-4o usage can be expensive; batching requests could optimize costs.
- Error Handling: Fallbacks return original text or default values if API calls fail.
The core ML component is the Isolation Forest algorithm, an unsupervised method for anomaly detection, implemented via scikit-learn.
- Purpose: Identifies anomalies by isolating data points in a feature space.
- Implementation:
model = IsolationForest( contamination=0.1, n_estimators=200, max_samples=50, # 33 samples created max_features=100, # Because TFID vector has 100 feautres. random_state=42, n_jobs=-1 ) model.fit(embeddings)
- Purpose: Reduces high-dimensional embeddings to 2D for visualization.
- Implementation:
pca = PCA(n_components=2, random_state=42) embeddings_2d = pca.fit_transform(embeddings)
- Usage: Enables scatter plots of anomaly scores in 2D space.
- Metrics Computed:
- Precision, Recall, F1: Assess binary classification performance.
- ROC-AUC: Measures ranking quality of anomaly scores.
- PR-AUC & Average Precision: Focus on positive (anomaly) class performance.
- Confusion Matrix: Detailed breakdown of TP, FP, TN, FN.
- Implementation:
precision = precision_score(y_true, y_pred, zero_division=0) roc_auc = roc_auc_score(y_true, y_scores)
- Parallel Contamination Testing:
- Tests multiple
contaminationvalues (e.g.,[0.05, 0.15]) to analyze sensitivity. - Uses
concurrent.futuresfor efficiency.
- Tests multiple
- Input: CSV with a
textcolumn. - Intermediate:
- Augmented data adds
idandtimestamp. - Feature extraction adds
entities,sentiment,theme, andembedding. - Anomaly detection adds
anomaly_score,is_anomaly, andanomaly_summary.
- Augmented data adds
- Output: CSV with all columns, plus JSON metadata.
- Loading:
load_datasetreads CSVs into Pandas DataFrames. - Embedding Parsing:
parse_embeddingconverts string embeddings to NumPy arrays. - Saving:
save_datasetpersists DataFrames to CSV.
Visualizations enhance understanding of anomaly detection results:
- Scatter Plot: 2D PCA projection with anomaly scores and flagged anomalies.
- Histogram: Distribution of anomaly scores with decision boundary.
- Decision Tree: Graphviz rendering of an Isolation Forest tree.
- Heatmap: Top 10 feature contributions for anomalous samples.
- PCA: Reduces dimensionality for scatter plots.
- Graphviz: Custom
export_tree_to_dotfunction recursively builds DOT format for tree visualization. - Seaborn: Heatmaps and histograms leverage statistical plotting capabilities.
- Endpoints:
GET /pipeline/status: Returns current pipeline status.POST /pipeline/run: Triggers pipeline execution with configurable parameters.
- Asynchronous Execution:
BackgroundTasksruns the pipeline non-blocking. - CORS: Enabled for cross-origin requests.
- Polling:
poll_until_pipeline_completionchecks status periodically. - Custom Config: Allows overriding defaults via JSON payload.
- Parallelization: ThreadPoolExecutor speeds up GPT-4o calls and experiments.
- Multiprocessing: Isolation Forest uses
n_jobs=-1for CPU parallelism. - Bottlenecks: Network latency from OpenAI API calls; mitigated by batching and caching potential.
- Memory: Large embeddings may strain RAM; PCA or sparse formats could help.
- Try-Except: Wraps all critical operations (e.g., model training, API calls).
- Logging:
logging.infoandlogging.errortrack progress and issues. - Fallbacks: Default values (e.g., "unknown" for sentiment) ensure pipeline continuity.
Some limitations have been encountered since the synthetic data generated was not diverse and numerically large enough.
- Model Tuning: Hyperparameter optimization for Isolation Forest. We can use search algorithms as GridSearchCV for this purpose if we had larger and diverse dataset.
- Pre-process: If the dataset consisted of data that was not between 0 and 1, they would be scaled before fitting the model. Very high dimensionality in data could introduce noise or computational overhead. Techniques like PCA could reduce the number of dimensions while preserving most of the variance. In our case (100) dimensions (features) are big enough to handle for IsolationForest.
- Alternative Algorithms: Experiment with other outlier detection algorithms.
- Real-Time Processing: Stream data via WebSockets and better API implementation with cool UI.
- Distributed Computing: Use a real dataset to work on.
First, clone the Git repository to your local machine:
git clone https://github.com/DoganK01/LLM_Assignment.gitNavigate to the project directory:
cd LLM_AssignmentCopy the stub .env.copy file to .env and replace the placeholder values with the required credentials:
cp .env.copy .envMake sure to update the values in .env with your actual credentials, such as API keys, endpoints or any other necessary information.
Install uv by following the instructions from the official documentation. For example, you can install uv via the following command:
pip install uvFor more details on installing uv, visit the official documentation.
After installing uv, you can sync all dependencies and packages for the project by running:
uv sync --all-groupsThis will install all the required dependencies specified for the project.
Now that the dependencies are installed, you can run the application using uv. To start the app, run:
uv run instance.pyThis will start the application, and it should be accessible locally.
This project is licensed under the MIT License - see the LICENSE file for details.