ML Intern

repository·main·Indexed 27 days ago

https://github.com/huggingface/ml-intern

An autonomous agent designed to research, write, and ship ML-related code using the Hugging Face ecosystem. It features deep integration with docs, papers, datasets, and cloud compute, supporting both local and hosted model execution via LiteLLM. The agent includes a CLI with interactive and headless modes, a ToolRouter for local or HF Space sandbox tools, and support for Model Context Protocol (MCP) servers.

Tokens
7.2K
Snippets
11
Records
47
Agent score
95%

What's inside ml-intern

  1. Overview of the ML Intern Agent architecture

    main

    The ML Intern Agent is an asynchronous, queue-based system powered by LiteLLM. The architecture follows a flow where user input submissions enter an Agent Loop, which then emits events that can be used for UI updates.

    Key architectural features include:

    • Session State: Maintains context and tools to support future Context Engineering.
    • Event-Driven Control: Handlers manage operations such as USER_INPUT, COMPACT, UNDO, and SHUTDOWN to enable UI control.
  2. ML Intern Agent component overview

    main

    The agent is composed of several core modules:

    • agent_loop.py: The core agentic loop. It processes user input, calls LLMs via LiteLLM, and iteratively executes tool calls until completion, emitting events throughout the process.
    • session.py: Manages session state, interaction with UIs (via context, config, and event queues), handles interrupts, and assigns unique session IDs for tracing.
    • tools.py: Contains the ToolRouter, which manages built-in tools (like bash, read_file, and write_file) and MCP tools, converting their specifications into OpenAI format.
    • context_manager/: Handles conversation history and rudimentary context engineering.
    • config.py: Responsible for loading JSON configuration files for the agent.
    • main.py: Provides an interactive CLI using an async queue architecture (submission $\rightarrow$ agent, agent $\rightarrow$ events).
  3. Understand the ML Intern Architecture

    main

    ML Intern operates via a submission_loop (defined in agent_loop.py) that processes operations from a submission_queue and emits status updates to an event_queue.

    Key components include:

    • Handlers.run_agent(): Manages the core agentic loop (up to 300 iterations).
    • ContextManager: Manages message history (using litellm.Message[]), handles auto-compaction (at 170k tokens), and uploads sessions to Hugging Face.
    • ToolRouter: Routes requests to various capabilities including HF docs/research, HF repos/datasets/jobs/papers, GitHub code search, Sandbox/local tools, Planning, and MCP server tools.
    • Doom Loop Detector: Monitors for repeated tool patterns and injects corrective prompts to prevent infinite loops.
  4. Use ML Intern CLI

    main

    You can run ml-intern in two modes:

    1. Interactive mode: Starts a chat session.
      ml-intern
    2. Headless mode: Executes a single prompt with auto-approval.
      ml-intern "your prompt"

    CLI Options

    OptionDescription
    --sandbox-tools "prompt"Use HF Space sandbox tools instead of the local filesystem.
    --max-iterations <int>Set the maximum number of agent iterations.
    --no-stream "prompt"Run the prompt without streaming output.
    --model <model_id>Specify a model ID (e.g., openai/gpt-5.5:fal-ai).
  5. Configure Slack notifications

    main

    ML Intern can send one-way status updates (approval requests, errors, turn completions) to Slack.

    Setup

    1. Create a Slack app with a bot token containing the chat:write scope.
    2. Invite the bot to your target channel.
    3. Set SLACK_BOT_TOKEN and SLACK_CHANNEL_ID in your environment.

    Configuration via JSON

    You can define persistent messaging destinations in ~/.config/ml-intern/cli_agent_config.json or by setting the ML_INTERN_CLI_CONFIG environment variable.

    {
      "messaging": {
        "enabled": true,
        "auto_event_types": ["approval_required", "error", "turn_complete"],
        "destinations": {
          "slack.ops": {
            "provider": "slack",
            "token": "${SLACK_BOT_TOKEN}",
            "channel": "${SLACK_CHANNEL_ID}",
            "allow_agent_tool": true,
            "allow_auto_events": true
          }
        }
      }
    }
  6. Format the review summary and verdict

    main

    Every review must follow a specific structure. Start with a single-line tally and an explicit verdict on two lines, followed by a "What I checked" list.

    Verdict Options:

    • Verdict: ready to merge: No P0 findings; contributor can merge once CI passes.
    • Verdict: changes requested: At least one P0 must be addressed.
    • Verdict: needs discussion: A design-level concern for maintainers.
    • For clean reviews, use LGTM followed by Verdict: ready to merge.

    Example Summary Format:

    2 P0, 3 P1
    Verdict: changes requested

    Verification Requirement: Every behavior claim must cite file:line. If you cannot cite a specific line, do not post the finding.

  7. Configure environment variables for ML Intern

    main

    Create a .env file in the project root or export the following variables in your shell to provide necessary credentials and configuration:

    VariableDescription
    HF_TOKENRequired for Hugging Face Inference Providers and Hub actions.
    GITHUB_TOKENRequired for GitHub interactions.
    LOCAL_LLM_BASE_URL(Optional) Shared base URL for local inference endpoints.
    LOCAL_LLM_API_KEY(Optional) Shared API key for local inference endpoints.
    SLACK_BOT_TOKENRequired for Slack notifications.
    SLACK_CHANNEL_IDRequired for Slack notifications.
    HF_TOKEN=<your-hugging-face-token>
    GITHUB_TOKEN=<github-personal-access-token>
  8. Manage and share session traces

    main

    Every session is automatically uploaded to a private Hugging Face dataset in Claude Code JSONL format (e.g., {your-hf-username}/ml-intern-sessions).

    Commands (Inside Interactive Mode)

    • /share-traces: Shows current visibility and the dataset URL.
    • /share-traces public: Makes the dataset public.
    • /share-traces private: Makes the dataset private.

    Configuration

    To opt out of sharing, set share_traces: false in your config file. To change the destination, use personal_trace_repo_template.

  9. Review dependency PRs

    main

    For PRs that only involve lockfile bumps, pyproject.toml changes, or new dependencies, focus on provenance and framing rather than code logic:

    • Ensure every claim in the title or body (CVE IDs, version numbers, behavior fixes) matches the actual diff.
    • Require justification for any new transitive dependencies.
    • A PR that misrepresents its purpose or changes in its framing is considered a P0 issue.
  10. Run ML Intern with local models

    main

    ML Intern supports local models via OpenAI-compatible HTTP endpoints through LiteLLM. You must start your inference server first. Use provider-specific prefixes to select the model.

    Supported Prefixes

    • ollama/
    • vllm/
    • lm_studio/
    • llamacpp/

    Configuration

    Set LOCAL_LLM_BASE_URL and LOCAL_LLM_API_KEY for a shared endpoint. You can override these with provider-specific variables like OLLAMA_BASE_URL or VLLM_API_KEY which take precedence.

  11. Apply severity levels to review findings

    main

    When reviewing this repository, categorize every finding into one of three priority levels. Use plain text labels (P0, P1, P2) in finding headers. Do not use emojis or colored markers.

    • P0: Blocks the merge. Must be addressed.
    • P1: Worth fixing, but does not block the merge.
    • P2: Informational only.

    Use judgment to determine the level; the repository does not provide an exhaustive list of P0 cases.

  12. Install ML Intern

    main

    Install the ml-intern CLI tool using uv. After installation, the ml-intern command will be available in any directory.

    Prerequisites

    • uv installed on your system.
    • A Hugging Face token (HF_TOKEN) for inference and Hub actions.
    • A GitHub personal access token (GITHUB_TOKEN) for repository interactions.
    git clone git@github.com:huggingface/ml-intern.git
    cd ml-intern
    uv sync
    uv tool install -e .