Flama Production AI Framework

repository·master·Indexed 43 days ago

https://github.com/vortico/flama

A production framework for serving Predictive (classic ML) and Generative (LLM) AI models. Flama allows developers to package models into a portable .flm format and deploy them as high-performance APIs with built-in support for the Model Context Protocol (MCP) and multiple protocols including OpenAI, Anthropic, and Ollama. It includes a CLI for model management, one-shot inference, and streaming, with native support for vLLM and MLX.

Tokens
18.5K
Snippets
66
Records
82
Agent score
49%

What's inside Flama

  1. Understand Flama performance benchmark metrics

    master

    Flama performance benchmarks are measured using Valgrind/Callgrind to provide deterministic, hardware-independent CPU cost estimates. Instead of wall-clock time, which varies by machine, Flama uses estimated cycles and raw instruction counts.

    Most benchmarks simulate a full Flama application exercising real HTTP request/response cycles via ASGI transport, while others target specific CPU-bound components like serialization or tool-call parsing.

  2. Quickstart: Serve a Predictive Model

    master

    Flama can package and serve classic machine learning models (e.g., scikit-learn, TensorFlow, PyTorch) using the .flm format.

    Packaging a local model

    Use flama.dump(model, "filename.flm") to convert a trained model object into a portable Flama model file.

    Serving a model from the Hub

    You can fetch a model directly from HuggingFace and serve it immediately via the CLI.

    import flama
    from sklearn.neural_network import MLPClassifier
    
    model = MLPClassifier(activation="tanh", hidden_layer_sizes=(10,))
    # ... training ...
    flama.dump(model, "model.flm")
    # Fetch and serve from HuggingFace
    flama get --family ml --source huggingface scikit-learn/Fish-Weight
    flama serve --model file=scikit-learn_Fish-Weight.flm,url=/model,name=fish
  3. Quickstart: Serve an LLM

    master

    You can deploy a Large Language Model (LLM) as a production API with a built-in chat UI using only the CLI.

    1. Install dependencies: Ensure you have the llm and pydantic extras.
    2. Download and package: Use flama get to fetch a model from HuggingFace and convert it into a .flm file.
    3. Test locally: Use flama model ... stream to pipe prompts directly into your terminal.
    4. Serve: Use flama serve to start an HTTP server. This provides a streaming chat interface at /chat/ and multiple protocol endpoints (OpenAI, Anthropic, Ollama).
    # 1. Install requirements
    pip install "flama[llm,pydantic]"
    
    # 2. Download and package a model from HuggingFace
    flama get --family llm --source huggingface mlx-community/gemma-4-E2B-it-qat-4bit
    
    # 3. Try it straight from your terminal
    echo "What is Flama?" | flama model mlx-community_gemma-4-E2B-it-qat-4bit.flm stream --system "Be concise."
    
    # 4. Serve it over HTTP
    flama serve --model file=mlx-community_gemma-4-E2B-it-qat-4bit.flm,url=/,name=gemma
  4. Install Flama

    master

    Flama is available on PyPI and provides native wheels for Python 3.10 to 3.14 on Linux, macOS, and Windows. You can install the core package using pip.

    Additional functionality is provided via optional extras:

    • flama[pydantic]: Adds schema validation (also supports typesystem and marshmallow).
    • flama[database]: Adds SQLAlchemy-backed resources.
    • flama[llm]: Adds generative AI serving (supports vLLM on Linux and MLX on Apple Silicon).
    • flama[full]: Installs all optional extras.
    # Core installation
    pip install flama
    
    # Installation with extras
    pip install "flama[pydantic]"
    pip install "flama[database]"
    pip install "flama[llm]"
    pip install "flama[full]"
  5. Access modules and components in Flama

    master

    Flama applications are composed of several core modules that can be accessed via attribute access (using __getattr__) or through the modules property.

    Core modules included by default:

    • mcp: MCPModule for Model Context Protocol.
    • resources: ResourcesModule for managing resources.
    • schema: SchemaModule for OpenAPI/schema management.
    • models: ModelsModule for model management.

    To access components registered in the application (including those from the router and parent apps), use the components property, which returns an injection.Components instance.

  6. How MCPServer handles Elicitation

    master

    In Flama's MCP implementation, Elicitation is a special type used to handle interactive user input during a tool execution.

    When a handler function includes a parameter annotated with Elicitation, the MCPServer logic:

    1. Excludes that parameter from the generated input_schema (so the AI agent doesn't try to provide it as a standard argument).
    2. Identifies it as the elicitation_param for that tool.
    3. Injects the response from the user/system into that parameter during execution.

    This allows tools to pause and ask for clarification or additional information from the user mid-process.

  7. Manage application lifecycle with the Events class

    master

    The Events class serves as a registry for application lifecycle events. It specifically supports two types of asynchronous event handlers:

    1. startup: A list of coroutines to be executed when the application starts.
    2. shutdown: A list of coroutines to be executed when the application shuts down.

    You can instantiate Events using the build class method or register handlers manually using the register method.

    from flama.events import Events
    
    async def my_startup_task():
        print("Starting up...")
    
    async def my_shutdown_task():
        print("Shutting down...")
    
    # Build the registry with handlers
    events = Events.build(
        startup=[my_startup_task],
        shutdown=[my_shutdown_task]
    )
  8. Use the `flama model` CLI to interact with models

    master

    The flama model command allows you to interact with packaged .flm model artifacts directly from your terminal without a server. It supports both traditional Machine Learning (ML) models and Large Language Models (LLMs).

    LLM artifacts automatically select the best available runtime (e.g., vLLM on Linux/CUDA or MLX on macOS) based on your environment. To serve models over HTTP instead of local CLI interaction, use flama serve.

    flama model <FLAMA_MODEL_PATH> [COMMAND]
    
    Arguments:
      FLAMA_MODEL_PATH  Path to the .flm model file (can also be set via FLAMA_MODEL_PATH env var)
    
    LLM-only Options:
      --channel-scanner <path>  Scanner format (auto, passthrough, or pkg.module:Object)
      --tool-scanner <path>     Scanner format (auto, passthrough, or pkg.module:Object)
      --tool-parser <path>      Parser format (auto, passthrough, or pkg.module:Object)
  9. Report bugs in Flama

    master

    To report a bug, open a new issue on GitHub using the provided bug report template. When reporting, ensure you include:

    • A minimal reproducible example
    • The current Flama version
    • Your environment details
  10. Expose tools to AI agents with MCP

    master

    Flama provides native support for the Model Context Protocol (MCP). You can expose Python functions as tools to AI agents (like Claude, Cursor, or VS Code Copilot) using the app.mcp.add_server method and the @app.mcp.tool decorator.

    Flama automatically derives the required JSON Schema for the tool by inspecting your Python type hints.

    from flama import Flama
    
    app = Flama()
    # Mount the MCP server at a specific path
    app.mcp.add_server("/mcp/tools/", "tools")
    
    # Define a tool. The 'mcp' argument links it to the server name defined above.
    @app.mcp.tool("add", description="Add two integers", mcp="tools")
    def add(a: int, b: int) -> int:
        return a + b