Skip to main content

Functions related to Haystack ML

Project description

Haystack ML Stack

Currently this project contains a FastAPI-based service designed for low-latency scoring of streams data coming from http requests

🚀 Features

  • FastAPI Service: Lightweight and fast web service for ML inference.
    • Asynchronous I/O: Utilizes aiobotocore for non-blocking S3 and DynamoDB operations.
    • Model Loading: Downloads and loads the ML model (using cloudpickle) from a configurable S3 path on startup.
    • Feature Caching: Implements a thread-safe Time-To-Live (TTL) / Least-Recently-Used (LRU) cache (cachetools.TLRUCache) for DynamoDB features, reducing latency and database load.
    • DynamoDB Integration: Fetches stream-specific features from DynamoDB to enrich the data before scoring.
    • Health Check: Provides a /health endpoint to monitor service status and model loading.

📦 Installation

This project requires Python 3.11 or later.

  1. Install package: The dependencies associated are listed in pyproject.toml.

    pip install haystack-ml-stack
    

⚙️ Configuration

The service is configured using environment variables, managed by pydantic-settings. You can use a .env file for local development.

Variable Name Alias Default Description
S3_MODEL_PATH S3_MODEL_PATH None Required. The s3://bucket/key URL for the cloudpickled ML model file.
FEATURES_TABLE FEATURES_TABLE "features" Name of the DynamoDB table storing stream features.
LOGS_FRACTION LOGS_FRACTION 0.01 Fraction of requests to log detailed stream data for sampling/debugging (0.0 to 1.0).
CACHE_MAXSIZE (none) 50000 Maximum size of the in-memory feature cache.

Example env vars

S3_MODEL_PATH="s3://my-ml-models/stream-scorer/latest.pkl"
FEATURES_TABLE="features"
LOGS_FRACTION=0.05

🌐 Endpoints

Method Path Description
GET / Root endpoint, returns a simple running message.
GET /health Checks if the service is running and if the ML model has been loaded.
POST /score Main scoring endpoint. Accepts stream data and returns model predictions.

💻 Technical Details

Model Structure

The ML model file downloaded from S3 is expected to be a cloudpickle-serialized Python dictionary with the following structure:

model = {
    "preprocess": <function>,  # Function to transform request data into model input.
    "predict": <function>,     # Function to perform the actual model inference.
    "params": <dict/any>,      # Optional parameters passed to preprocess/predict.
    "stream_features": <list[str]>, # Optional list of feature names to fetch from DynamoDB.
}

Feature Caching (cache.py)

The ThreadSafeTLRUCache ensures that feature lookups and updates are thread-safe. The _ttu (time-to-use) policy allows features to specify their own TTL via a cache_ttl_in_seconds key in the stored value.

DynamoDB Feature Fetching (dynamo.py)

The set_stream_features function handles:

  • Checking the in-memory cache for required stream_features.

  • Batch-fetching any missing features from DynamoDB.

  • Parsing the low-level DynamoDB items into Python types.

  • Populating the cache with the fetched data, respecting the feature's TTL.

  • Injecting the fetched feature values back into the streams list in the request payload.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

haystack_ml_stack-0.4.1.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

haystack_ml_stack-0.4.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file haystack_ml_stack-0.4.1.tar.gz.

File metadata

  • Download URL: haystack_ml_stack-0.4.1.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for haystack_ml_stack-0.4.1.tar.gz
Algorithm Hash digest
SHA256 240814360308617c059e01dba053f63059938d9e48f5f875a62b7e0987057fa8
MD5 74272c0693d85641a3b47eede50a283d
BLAKE2b-256 cf9000d42b0bf1ff7203728fd60d95390aa9d00fc842d6b9d6bf4476c7c6c6d0

See more details on using hashes here.

File details

Details for the file haystack_ml_stack-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for haystack_ml_stack-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f00782bfd15958419e9490ea054f2a1b88171de6a5c9eb1dae3c21c9dd4899f2
MD5 dfdb4708023f564375472d296c739c12
BLAKE2b-256 a8617d9ab3a1f54f2987a0afaee44b2b74446ed3fd166a1bb91596692735e5b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page