DSPy: The framework for programming—not prompting—language models
Python
Add MLFLow <> GEPA support (#8763)
TomuHiratac3ae691e3c0c
diff --git a/dspy/teleprompt/gepa/gepa.py b/dspy/teleprompt/gepa/gepa.pyindex 2481f90da..c2ab1b68f 100644--- a/dspy/teleprompt/gepa/gepa.py+++ b/dspy/teleprompt/gepa/gepa.py@@ -310,6 +310,7 @@ class GEPA(Teleprompter):wandb_init_kwargs: dict[str, Any] | None = None,track_best_outputs: bool = False,warn_on_score_mismatch: bool = True,+ use_mlflow: bool = False,# Reproducibilityseed: int | None = 0,):@@ -369,6 +370,7 @@ class GEPA(Teleprompter):self.wandb_api_key = wandb_api_keyself.wandb_init_kwargs = wandb_init_kwargsself.warn_on_score_mismatch = warn_on_score_mismatch+ self.use_mlflow = use_mlflowif track_best_outputs:assert track_stats, "track_stats must be True if track_best_outputs is True."@@ -528,6 +530,7 @@ class GEPA(Teleprompter):use_wandb=self.use_wandb,wandb_api_key=self.wandb_api_key,wandb_init_kwargs=self.wandb_init_kwargs,+ use_mlflow=self.use_mlflow,track_best_outputs=self.track_best_outputs,display_progress_bar=True,raise_on_exception=True,diff --git a/tests/teleprompt/test_gepa.py b/tests/teleprompt/test_gepa.pyindex b56c2893c..b78e80894 100644--- a/tests/teleprompt/test_gepa.py+++ b/tests/teleprompt/test_gepa.py@@ -1,6 +1,7 @@import jsonimport threadingfrom typing import Any+from unittest import mockimport pytest@@ -42,10 +43,17 @@ def bad_metric(example, prediction):return 0.0-def test_basic_workflow():+def mock_mlflow():+ mock_mlflow = mock.MagicMock()+ with mock.patch.dict("sys.modules", {"mlflow": mock_mlflow}):+ yield mock_mlflow++[email protected]("use_mlflow", [True, False])+def test_basic_workflow(use_mlflow, mock_mlflow):"""Test to ensure the basic compile flow runs without errors."""student = SimpleModule("input -> output")- teacher = SimpleModule("input -> output")with open("tests/teleprompt/gepa_dummy_lm.json") as f:data = json.load(f)@@ -55,17 +63,26 @@ def test_basic_workflow():lm_main = DictDummyLM(lm_history)dspy.settings.configure(lm=lm_main)reflection_lm = DictDummyLM(reflection_lm_history)- optimizer = dspy.GEPA(metric=simple_metric, reflection_lm=reflection_lm, max_metric_calls=5)++ optimizer = dspy.GEPA(+ metric=simple_metric,+ reflection_lm=reflection_lm,+ max_metric_calls=5,+ use_mlflow=use_mlflow+ )++trainset = [Example(input="What is the color of the sky?", output="blue").with_inputs("input"),Example(input="What does the fox say?", output="Ring-ding-ding-ding-dingeringeding!").with_inputs("input"),]- o = optimizer.compile(student, trainset=trainset, valset=trainset)- assert (- o.predictor.signature.instructions- == 'Given the field `input` containing a question or phrase, produce the field `output` containing the exact, direct, and contextually appropriate answer or response that the user expects, without additional explanations, commentary, or general knowledge unless explicitly requested.\n\nKey details and guidelines:\n\n1. The `input` field contains a question or phrase that may be literal, factual, or culturally specific (e.g., references to popular culture or memes).\n\n2. The `output` must be the precise answer or response that directly addresses the `input` as intended by the user, not a general or encyclopedic explanation.\n\n3. If the `input` is a well-known phrase or question from popular culture (e.g., "What does the fox say?"), the `output` should reflect the expected or canonical answer associated with that phrase, rather than a factual or scientific explanation.\n\n4. Avoid providing additional background information, scientific explanations, or alternative interpretations unless explicitly requested.\n\n5. The goal is to produce the answer that the user expects or the "correct" answer in the context of the question, including culturally recognized or meme-based answers.\n\n6. If the `input` is a straightforward factual question (e.g., "What is the color of the sky?"), provide the commonly accepted direct answer (e.g., "Blue") rather than a detailed scientific explanation.\n\n7. The output should be concise, clear, and focused solely on answering the question or phrase in the `input`.\n\nExample:\n\n- Input: "What is the color of the sky?"\n- Output: "Blue."\n\n- Input: "What does the fox say?"\n- Output: "Ring-ding-ding-ding-dingeringeding!"\n\nThis approach ensures that the assistant provides the expected, contextually appropriate answers rather than general or overly detailed responses that may be considered incorrect by the user.'- )+ optimized_program = optimizer.compile(student, trainset=trainset, valset=trainset)+ assert optimized_program.predictor.signature.instructions == 'Given the field `input` containing a question or phrase, produce the field `output` containing the exact, direct, and contextually appropriate answer or response that the user expects, without additional explanations, commentary, or general knowledge unless explicitly requested.\n\nKey details and guidelines:\n\n1. The `input` field contains a question or phrase that may be literal, factual, or culturally specific (e.g., references to popular culture or memes).\n\n2. The `output` must be the precise answer or response that directly addresses the `input` as intended by the user, not a general or encyclopedic explanation.\n\n3. If the `input` is a well-known phrase or question from popular culture (e.g., "What does the fox say?"), the `output` should reflect the expected or canonical answer associated with that phrase, rather than a factual or scientific explanation.\n\n4. Avoid providing additional background information, scientific explanations, or alternative interpretations unless explicitly requested.\n\n5. The goal is to produce the answer that the user expects or the "correct" answer in the context of the question, including culturally recognized or meme-based answers.\n\n6. If the `input` is a straightforward factual question (e.g., "What is the color of the sky?"), provide the commonly accepted direct answer (e.g., "Blue") rather than a detailed scientific explanation.\n\n7. The output should be concise, clear, and focused solely on answering the question or phrase in the `input`.\n\nExample:\n\n- Input: "What is the color of the sky?"\n- Output: "Blue."\n\n- Input: "What does the fox say?"\n- Output: "Ring-ding-ding-ding-dingeringeding!"\n\nThis approach ensures that the assistant provides the expected, contextually appropriate answers rather than general or overly detailed responses that may be considered incorrect by the user.'+ if use_mlflow:+ assert mock_mlflow.start_run.call_count == 1+ else:+ assert mock_mlflow.start_run.call_count == 0def test_workflow_with_custom_instruction_proposer_and_component_selector():"""Test to ensure the basic compile flow runs without errors when using a custom instruction proposer and component selector."""