Skip to main content

Agent optimization with Opik

Project description

Opik Agent Optimizer

PyPI version Python versions Downloads License

The Opik Agent Optimizer refines your prompts to achieve better performance from your Large Language Models (LLMs). It supports a variety of optimization algorithms, including:

  • EvolutionaryOptimizer
  • FewShotBayesianOptimizer
  • MetaPromptOptimizer
  • MiproOptimizer

Opik Optimizer is a component of the Opik platform, an open-source LLM evaluation platform by Comet. For more information about the broader Opik ecosystem, visit our Website or Documentation.

Quickstart

Explore Opik Optimizer's capabilities with our interactive notebook:

Open in Colab

Setup

To get started with Opik Optimizer, follow these steps:

  1. Install the package:

    # using pip
    pip install opik-optimizer
    
    # using uv (faster)
    uv pip install opik-optimizer
    
  2. Configure Opik (Optional, for advanced features): If you plan to log optimization experiments to Comet or use Opik Datasets, you'll need to configure the Opik client:

    # Install the main Opik CLI (if not already installed)
    pip install opik
    
    # Configure your Comet API key and workspace
    opik configure
    # When prompted, enter your Opik API key and workspace details.
    

    Using Opik with Comet allows you to track your optimization runs, compare results, and manage datasets seamlessly.

  3. Set up LLM Provider API Keys: Ensure your environment variables are set for the LLM(s) you intend to use. For example, for OpenAI models:

    export OPENAI_API_KEY="your_openai_api_key"
    

    The optimizer utilizes LiteLLM, so you can configure keys for various providers as per LiteLLM's documentation.

You'll typically need:

  • An LLM model name (e.g., "gpt-4o-mini", "claude-3-haiku-20240307").
  • An Opik Dataset (or a compatible local dataset/data generator).
  • An Opik Metric (or a custom evaluation function).
  • A starting prompt (template string).

Example

Here's a brief example of how to use the FewShotBayesianOptimizer. We'll use a sample dataset provided by Opik.

Available sample datasets for testing:

  • "tiny-test"
  • "halu-eval-300"
  • "hotpot-300"
from opik.evaluation.metrics import LevenshteinRatio
from opik_optimizer import FewShotBayesianOptimizer, ChatPrompt
from opik_optimizer.datasets import hotpot_300

# Load a sample dataset
hot_pot_dataset = hotpot_300()

project_name = "optimize-few-shot-bayesian-hotpot" # For Comet logging

# Define the instruction for your chat prompt.
# Input parameters from dataset examples will be interpolated into the full prompt.
prompt = ChatPrompt(
    project_name=project_name,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "{question}"}
    ]
)

optimizer = FewShotBayesianOptimizer(
    model="gpt-4o-mini", # LiteLLM name to use for generation and optimization
    min_examples=3,      # Min few-shot examples
    max_examples=8,      # Max few-shot examples
    n_threads=16,        # Parallel threads for evaluation
    seed=42,
)

def levenshtein_ratio(dataset_item, llm_output):
    return LevenshteinRatio().score(reference=dataset_item["answer"], output=llm_output)

# Run the optimization
result = optimizer.optimize_prompt(
    prompt=prompt,
    dataset=hot_pot_dataset,
    metric=levenshtein_ratio,
    n_trials=10,   # Number of optimization trials
    n_samples=150, # Number of dataset samples for evaluation per trial
)

# Display the best prompt and its score
result.display()

The result object contains the optimized prompt, evaluation scores, and other details from the optimization process. If project_name is provided and Opik is configured, results will also be logged to your Comet workspace.

Development

To contribute or use the Opik Optimizer from source:

  1. Clone the Opik repository:
    git clone git@github.com:comet-ml/opik.git
    
  2. Navigate to the optimizer's directory:
    cd opik/sdks/opik_optimizer  # Adjust 'opik' if you cloned into a different folder name
    
  3. Install in editable mode (with development dependencies):
    pip install -e .[dev]
    
    The [dev] extra installs dependencies useful for development, such as pytest.

Requirements

  • Python >=3.9,<3.13
  • Opik API key (recommended for full functionality, configure via opik configure)
  • API key for your chosen LLM provider (e.g., OpenAI, Anthropic, Gemini), configured as per LiteLLM guidelines.

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

opik_optimizer-1.0.5.tar.gz (110.9 kB view details)

Uploaded Source

Built Distribution

opik_optimizer-1.0.5-py3-none-any.whl (126.1 kB view details)

Uploaded Python 3

File details

Details for the file opik_optimizer-1.0.5.tar.gz.

File metadata

  • Download URL: opik_optimizer-1.0.5.tar.gz
  • Upload date:
  • Size: 110.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for opik_optimizer-1.0.5.tar.gz
Algorithm Hash digest
SHA256 3cdc3f6606b1a178a1d0183906161a2259ae56b7edcbcbabf204be7f3acd5614
MD5 a83553f2f0aba2958b46eac2293c2742
BLAKE2b-256 ec5922127b0ff545b0020f3ed18dd3a977533374fa85b34dc3642543fb260610

See more details on using hashes here.

File details

Details for the file opik_optimizer-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: opik_optimizer-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 126.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for opik_optimizer-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4486ef2a68bf3aa88b1601b5f738205e0bdf6f531fa149b6b0de2273103e95ac
MD5 751da646153074af6aefe48ea806e279
BLAKE2b-256 ffe83f26ab63a356ea5b00101cf1bc4f20d577dccb687c52e45f6ec77e3fc026

See more details on using hashes here.

Supported by

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