Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Open compass VLMEvalKit Load Env

From Leeroopedia
Field Value
Source VLMEvalKit|https://github.com/open-compass/VLMEvalKit
Domain Configuration, Evaluation
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete tool for loading environment variables and API credentials from a .env file provided by VLMEvalKit.

Description

The load_env() function in vlmeval/smp/misc.py performs two key operations:

  1. Configures Python logging — Sets up the root logger with a timestamped format (%(asctime)s %(levelname)s %(message)s) and a datefmt of %Y-%m-%d %H:%M:%S, at the INFO level.
  2. Loads environment variables — Locates the .env file relative to the vlmeval package path (two directories up from the package, i.e., the repository root). It reads the file line by line, parses each line as a key=value pair, and injects all non-empty values into os.environ using python-dotenv.

This function is the concrete realization of the Environment_Setup principle, providing the single entry point for credential and logging initialization in VLMEvalKit.

Usage

  • Call at the start of any evaluation script to ensure API keys are loaded and logging is configured.
  • Automatically called by build_judge() when constructing judge LLMs for evaluation scoring.
  • Required before any API-based inference (GPT-4o, Claude, Gemini) or evaluation that uses a judge model.

Code Reference

Source Location

Field Value
Repository VLMEvalKit
File vlmeval/smp/misc.py
Lines L200-223

Signature

def load_env() -> None:
    """
    Configures logging and loads environment variables from .env file.
    No parameters. Reads .env from repository root (relative to vlmeval package).
    Sets all non-empty key-value pairs into os.environ.
    """

Import

from vlmeval.smp import load_env

I/O Contract

Direction Description
Inputs .env file at repository root containing key=value pairs (OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_API_KEY, etc.)
Outputs Environment variables set in os.environ; Python logging configured with timestamped format at INFO level
Side Effects Modifies global os.environ dictionary; configures the root Python logger

Usage Examples

Basic Usage

import os
from vlmeval.smp import load_env

# Load environment variables from .env file
load_env()

# API keys are now available in os.environ
api_key = os.environ.get('OPENAI_API_KEY')
print(f"OpenAI key loaded: {api_key is not None}")

anthropic_key = os.environ.get('ANTHROPIC_API_KEY')
print(f"Anthropic key loaded: {anthropic_key is not None}")

Usage Before Model Inference

from vlmeval.smp import load_env
from vlmeval.config import supported_VLM

# Step 1: Load environment (required for API models)
load_env()

# Step 2: Instantiate an API-based model (uses keys from os.environ)
model = supported_VLM["GPT4o"]()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment