⤵️ Quick shortcut: Select the module you’re currently studying ⤵️
Courses Module 1 Module 2 Module 3 Module 4 Module 5 Module 6
Lesson 1:
Key metrics for evaluating LLM responses.
Techniques for testing and debugging LLM behavior.
Lesson 2:
Methods for measuring vector search performance.
Best practices for optimizing vector databases.
Lesson 3:
Importance of monitoring and logging.
Tools for tracking API performance and setting up alerts.
Objective
Develop a reliable, efficient, and production-ready Jarvid pipeline by:
Writing a comprehensive test suite.
Measuring and optimizing vector search performance.
Implementing monitoring and logging for continuous performance tracking.
1.1 API Functionality Testing
Set Up Testing Environment:
Install pytest for testing.
bash
Copy code
pip install pytest requests
Write API Test Cases: File: tests/test_api.py
python
Copy code
import requests
BASE_URL = "http://localhost:8000"
def test_root_endpoint():
response = requests.get(f"{BASE_URL}/")
assert response.status_code == 200
assert "message" in response.json()
def test_error_endpoint():
response = requests.get(f"{BASE_URL}/error")
assert response.status_code == 500
assert response.json()["detail"] == "Internal Server Error"
Run Tests:
bash
Copy code
pytest tests/test_api.py
1.2 LLM Response Testing
Set Up Mock LLM Responses:
Use a mock server to simulate LLM API responses.
Install responses library:
bash
Copy code
pip install responses
Write LLM Test Cases: File: tests/test_llm.py
python
Copy code
import requests
import responses
@responses.activate
def test_llm_response():
responses.add(
responses.POST,
"https://api.openai.com/v1/completions",
json={"choices": [{"text": "Hello, Jarvid!"}]},
status=200
)
response = requests.post(
"https://api.openai.com/v1/completions",
json={"prompt": "Greet Jarvid", "max_tokens": 10}
)
assert response.status_code == 200
assert response.json()["choices"][0]["text"] == "Hello, Jarvid!"
Run Tests:
bash
Copy code
pytest tests/test_llm.py
2.1 Benchmark Vector Search Performance
Write Benchmark Script: File: benchmark_faiss.py
python
Copy code
import faiss
import numpy as np
import time
# Generate random embeddings
dimension = 128
num_embeddings = 1000000
embeddings = np.random.random((num_embeddings, dimension)).astype('float32')
query = np.random.random((1, dimension)).astype('float32')
# Create FAISS index
index = faiss.IndexFlatL2(dimension)
index.add(embeddings)
# Measure latency
start = time.perf_counter()
_, indices = index.search(query, 10)
end = time.perf_counter()
latency = (end - start) * 1000 # ms
print(f"Search Latency: {latency:.2f} ms")
Run the Script:
bash
Copy code
python benchmark_faiss.py
Optimize Vector Search
Switch to an Inverted Index (IVF):
python
Copy code
num_clusters = 100
quantizer = faiss.IndexFlatL2(dimension)
ivf_index = faiss.IndexIVFFlat(quantizer, dimension, num_clusters)
ivf_index.train(embeddings)
ivf_index.add(embeddings)
Benchmark IVF Index:
python
Copy code
ivf_index.nprobe = 10
start = time.perf_counter()
_, indices = ivf_index.search(query, 10)
end = time.perf_counter()
print(f"IVF Search Latency (nprobe=10): {(end - start) * 1000:.2f} ms")
3.1 Add Monitoring Metrics
Integrate Prometheus in API: Modify API Code:
python
Copy code
from prometheus_client import start_http_server, Counter, Summary
# Start Prometheus server
start_http_server(9100)
# Define Prometheus metrics
REQUEST_COUNT = Counter("request_count", "Total number of requests")
REQUEST_LATENCY = Summary("request_latency_seconds", "Latency of API requests")
@app.middleware("http")
async def add_metrics(request, call_next):
REQUEST_COUNT.inc()
start = time.perf_counter()
response = await call_next(request)
latency = time.perf_counter() - start
REQUEST_LATENCY.observe(latency)
return response
Run Prometheus and Confirm Metrics:
Check metrics at http://localhost:9100/metrics.
3.2 Set Up Alerts in Prometheus
Add Alert Rules: File: prometheus.yml
yaml
Copy code
groups:
- name: API Alerts
rules:
- alert: HighLatency
expr: request_latency_seconds > 0.5
for: 1m
labels:
severity: warning
annotations:
summary: "High API latency detected."
Reload Prometheus:
bash
Copy code
curl -X POST http://localhost:9090/-/reload
3.3 Add Logging
Set Up JSON Logging: Modify API Code:
python
Copy code
import logging
from pythonjsonlogger import jsonlogger
log_handler = logging.StreamHandler()
log_formatter = jsonlogger.JsonFormatter()
log_handler.setFormatter(log_formatter)
logger = logging.getLogger()
logger.addHandler(log_handler)
logger.setLevel(logging.INFO)
@app.middleware("http")
async def log_requests(request, call_next):
logger.info({"event": "request_received", "path": request.url.path})
response = await call_next(request)
logger.info({"event": "request_processed", "status_code": response.status_code})
return response
Test Logging:
Verify logs appear in the console.
Comprehensive Test Suite:
Validates API endpoints and LLM responses.
Vector Search Benchmark:
Provides performance metrics and optimization insights.
Monitoring and Logging:
Ensures real-time tracking of API performance and errors.
Integrated System:
A fully tested, monitored, and optimized Jarvid pipeline ready for production.
Objective
By the end of Module 6, you will have ensured that the Jarvid pipeline is reliable, efficient, and optimized for production use. This includes:
Writing a comprehensive test suite for the pipeline.
Measuring and optimizing vector search performance.
Implementing monitoring and logging for continuous performance tracking.
Expected Outcomes
A comprehensive test suite covering API functionality and LLM responses.
A benchmarking script for vector database performance.
A fully integrated monitoring and logging system for the deployed pipeline.
Learning Outcomes
After completing this module, you will:
Understand techniques for testing LLM responses and vector searches.
Gain hands-on experience optimizing pipeline performance.
Learn to implement monitoring and logging for real-world AI applications.
By completing Module 6, you'll have the skills to ensure the reliability and efficiency of AI pipelines in production. This module equips you with the tools to maintain and optimize complex AI systems for long-term success.