What is Prompt Engineering?
Prompt engineering is the process of crafting and refining prompts through repeated trial and error. While this approach can work, it is highly labor-intensive and relies heavily on intuition and assumptions. It consumes significant time, effort, and cost. When experiments fail, which happens frequently. It often leads to frustration and forces you to start the entire process from scratch.
This method is useful for initial exploration and small-scale experiments. However, it becomes difficult to scale effectively when dealing with real-world data, diverse scenarios, and production-level requirements.
Key pain areas in prompt engineering:
Creating relevant prompt that AI understands
Generating golden dataset for verification
Handling change management
What is Automated Prompt Engineering?
Automated prompt engineering, also known as prompt optimization, is a systematic technique that removes much of the guesswork from the process. It automates the creation, testing, and refinement of prompts while providing clear, data-driven insights into why a particular prompt performs best for your use case.
By integrating prompt optimization into your workflows, you can significantly reduce manual effort, accelerate development cycles, and dramatically improve the reliability and performance of your Generative AI applications.
We will explore a prompt optimization approach using a prompt adapter that uses GEPA (Genetic-Pareto) optimization algorithm to optimize prompts. GEPA uses iterative mutation, reflection, and Pareto-aware candidate selection to improve text components like prompts. It leverages large language models to reflect on system behavior and propose improvements. This experiment will be done using MLflow running with help of Mistral.
Now, let's dive into hands-on for a use case. Let's take a use case to build a natural language based mathematician. We want an AI that can take any simple math question written in natural language and return the correct answer. We will follow below steps
Start with a basic prompt
Let GEPA automatically optimize it
End up with a high-accuracy mathematician prompt
Now, we will follow below steps to implement prompt optimization.
Step 1: Install Required Packages
pip install mlflow openaiMake sure you have:
An MLflow server running with the GenAI gateway enabled
Ollama running locally with the ministral-3:3b model (used for reflection and scoring).
Step 2: Import Libraries
import mlflow
from mlflow.genai.optimize import GepaPromptOptimizer
from mlflow.genai.scorers import Correctness
from openai import OpenAIStep 3: Register the Base Prompt in MLflow Prompt Registry
base_prompt = mlflow.genai.register_prompt(
name="mathematician-prompt",
template=(
"You are a mathematician. For any maths question in natural language you have to return response.\n\n"
"Question: {{ question }}\n\n"
"Answer:"
),
)This creates version 1 of our starting prompt in the registry.
Step 4: Prepare Training Data (Golden Dataset)
train_data = [
{
"inputs": {"question": "What is one plus one?"},
"expectations": {"expected_response": "Two"},
},
{
"inputs": {"question": "What is one minus one?"},
"expectations": {"expected_response": "Zero"},
},
{
"inputs": {"question": "What is three multiply by two?"},
"expectations": {"expected_response": "Six"},
},
{
"inputs": {"question": "What is fifty divided by five?"},
"expectations": {"expected_response": "Ten"},
}
]These examples will be used both for optimization and scoring.
Step 5: Define the Prediction Function
This function loads the current prompt version, formats it, and calls your LLM (in this case, an Ollama endpoint via MLflow gateway).
def my_predict_fn(question: str):
prompt = mlflow.genai.load_prompt("prompts:/mathematician-prompt/1")
client = OpenAI(
base_url="http://localhost:5000/gateway/mlflow/v1",
api_key="", # API key not needed (configured server-side)
)
messages = [{"role": "user", "content": prompt.format(question=question)}]
response = client.chat.completions.create(
model="ollama-dev", # Your endpoint name
messages=messages,
)
return response.choices[0].message


Join the conversation! Your thoughts help the community grow.