Skip to content

Agentic Optimizer #2489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open

Agentic Optimizer #2489

wants to merge 48 commits into from

Conversation

dsblank
Copy link
Contributor

@dsblank dsblank commented Jun 16, 2025

Details

Previously, we could only optimize prompts for a generic LiteLLM model. This PR allows optimizing chat-prompts on any agent framework.

Here is an example of the changes in the simplest case:

BEFORE:

from typing import Any, Dict
from opik.evaluation.metrics import LevenshteinRatio
from opik.evaluation.metrics.score_result import ScoreResult
from opik_optimizer.datasets import hotpot_300
from opik_optimizer import (
    ChatPrompt, 
    FewShotBayesianOptimizer,
)
dataset = hotpot_300()

def levenshtein_ratio(dataset_item: Dict[str, Any], llm_output: str) -> ScoreResult:
    metric = LevenshteinRatio()
    return metric.score(reference=dataset_item["answer"], output=llm_output)

project_name = "optimize-few-shot-bayesian-hotpot"

prompt = ChatPrompt(system="Answer the question.", user="{question}")

optimizer = FewShotBayesianOptimizer(
    model="openai/gpt-4o-mini",
    project_name=project_name,
    min_examples=3,
    max_examples=8,
    n_threads=16,
    seed=42,
)

optimization_result = optimizer.optimize_prompt(
    prompt=prompt,
    dataset=dataset,
    metric=levenshtein_ratio,
    n_trials=10,
    n_samples=150,
)

optimization_result.display()

AFTER:

from typing import Dict, Any
from opik.evaluation.metrics import LevenshteinRatio
from opik.evaluation.metrics.score_result import ScoreResult
from opik_optimizer.datasets import hotpot_300
from opik_optimizer import (
    OptimizableAgent,
    ChatPrompt,
    FewShotBayesianOptimizer,
    AgentConfig,
)

dataset = hotpot_300()

def levenshtein_ratio(dataset_item: Dict[str, Any], llm_output: str) -> ScoreResult:
    metric = LevenshteinRatio()
    return metric.score(reference=dataset_item["answer"], output=llm_output)

class LiteLLMAgent(OptimizableAgent):
    model = "openai/gpt-4o-mini"
    project_name = "optimize-few-shot-bayesian-hotpot"

agent_config = AgentConfig(
    chat_prompt=ChatPrompt(system="Answer the question.", user="{question}")
)

optimizer = FewShotBayesianOptimizer(
    model="openai/gpt-4o-mini",
    min_examples=3,
    max_examples=8,
    n_threads=16,
    seed=42,
)

optimization_result = optimizer.optimize_agent(
    agent_class=LiteLLMAgent,
    agent_config=agent_config,
    dataset=dataset,
    metric=levenshtein_ratio,
    n_trials=10,
    n_samples=50,
)
optimization_result.display()

The main difference is replacing:

project_name = "optimize-few-shot-bayesian-hotpot"

prompt = ChatPrompt(system="Answer the question.", user="{question}")

with:

class LiteLLMAgent(OptimizableAgent):
    model = "openai/gpt-4o-mini"
    project_name = "optimize-few-shot-bayesian-hotpot"

agent_config = AgentConfig(
    chat_prompt=ChatPrompt(system="Answer the question.", user="{question}")
)

And passing agent_class and agent_config to the optimize_agent() method.

@dsblank dsblank marked this pull request as ready for review June 18, 2025 22:59
@dsblank dsblank requested review from vincentkoc and a team as code owners June 18, 2025 22:59
@dsblank dsblank force-pushed the dsb/agentic-optimizer branch from 3cba5db to 16a7bac Compare June 18, 2025 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant