Skip to main content

Google Colab

colab.research.google.com
You’ll start by creating an experiment in Phoenix that can house the results of each of your resulting prompts. Next you’ll use a series of prompt optimization techniques to improve the performance of a jailbreak classification task. Each technique will be applied to the same base prompt, and the results will be compared using Phoenix. The techniques you’ll use are:
  • Few Shot Examples: Adding a few examples to the prompt to help the model understand the task.
  • Meta Prompting: Prompting a model to generate a better prompt based on previous inputs, outputs, and expected outputs.
  • Prompt Gradients: Using the gradient of the prompt to optimize individual components of the prompt using embeddings.
  • DSPy Prompt Tuning: Using DSPy, an automated prompt tuning library, to optimize the prompt.
This tutorial requires and OpenAI API key.
Let’s get started!

Setup Dependencies & Keys

Next you need to connect to Phoenix. The code below will connect you to a Phoenix Cloud instance. You can also connect to a self-hosted Phoenix instance if you’d prefer.

Load Dataset into Phoenix

Since we’ll be running a series of experiments, we’ll need a dataset of test cases that we can run each time. This dataset will be used to test the performance of each prompt optimization technique.
Next, you can define a base template for the prompt. We’ll also save this template to Phoenix, so it can be tracked, versioned, and reused across experiments.
You should now see that prompt in Phoenix:
Next you’ll need a task and evaluator for the experiment. A task is a function that will be run across each example in the dataset. The task is also the piece of your code that you’ll change between each run of the experiment. To start off, the task is simply a call to GPT 3.5 Turbo with a basic prompt. You’ll also need an evaluator that will be used to test the performance of the task. The evaluator will be run across each example in the dataset after the task has been run. Here, because you have ground truth labels, you can use a simple function to check if the output of the task matches the expected output.
You can also instrument your code to send all models calls to Phoenix. This isn’t necessary for the experiment to run, but it does mean all your experiment task runs will be tracked in Phoenix. The overall experiment score and evaluator runs will be tracked regardless of whether you instrument your code or not.
Now you can run the initial experiment. This will be the base prompt that you’ll be optimizing.
You should now see the initial experiment results in Phoenix:

1st experiment results

Prompt Optimization Technique #1: Few Shot Examples

One common prompt optimization technique is to use few shot examples to guide the model’s behavior. Here you can add few shot examples to the prompt to help improve performance. Conveniently, the dataset you uploaded in the last step contains a test set that you can use for this purpose.
Define a new prompt that includes the few shot examples. Prompts in Phoenix are automatically versioned, so saving the prompt with the same name will create a new version that can be used.
You’ll notice you now have a new version of the prompt in Phoenix:
Define a new task with your new prompt:
Now you can run another experiment with the new prompt. The dataset of test cases and the evaluator will be the same as the previous experiment.

Prompt Optimization Technique #2: Meta Prompting

Meta prompting involves prompting a model to generate a better prompt, based on previous inputs, outputs, and expected outputs. The experiment from round 1 serves as a great starting point for this technique, since it has each of those components.
Now construct a new prompt that will be used to generate a new prompt.
Now save that as a prompt in Phoenix:

Run this new prompt through the same experiment

Redefine the task, using the new prompt.

Prompt Optimization Technique #3: Prompt Gradient Optimization

Prompt gradient optimization is a technique that uses the gradient of the prompt to optimize individual components of the prompt using embeddings. It involves:
  1. Converting the prompt into an embedding.
  2. Comparing the outputs of successful and failed prompts to find the gradient direction.
  3. Moving in the gradient direction to optimize the prompt.
Here you’ll define a function to get embeddings for prompts, and then use that function to calculate the gradient direction between successful and failed prompts.

Run experiment with gradient-optimized prompt

Redefine the task, using the new prompt.

Prompt Optimization Technique #4: Prompt Tuning with DSPy

Finally, you can use an optimization library to optimize the prompt, like DSPy. DSPy supports each of the techniques you’ve used so far, and more.
DSPy makes a series of calls to optimize the prompt. It can be useful to see these calls in action. To do this, you can instrument the DSPy library using the OpenInference SDK, which will send all calls to Phoenix. This is optional, but it can be useful to have.
Now you’ll setup the DSPy language model and define a prompt classification task.
Your classifier can now be used to make predictions as you would a normal LLM. It will expect a prompt input and will output a label prediction.
However, DSPy really shines when it comes to optimizing prompts. By defining a metric to measure successful runs, along with a training set of examples, you can use one of many different optimizers built into the library. In this case, you’ll use the MIPROv2 optimizer to find the best prompt for your task.
DSPy takes care of our prompts in this case, however you could still save the resulting prompt value in Phoenix:

Run experiment with DSPy-optimized classifier

Redefine the task, using the new prompt.

Prompt Optimization Technique #5: DSPy with GPT-4o

In the last example, you used GPT-3.5 Turbo to both run your pipeline, and optimize the prompt. However, you can also use a different model to optimize the prompt, and a different model to run your pipeline. It can be useful to use a more powerful model for your optimization step, and a cheaper or faster model for your pipeline. Here you’ll use GPT-4o to optimize the prompt, and keep GPT-3.5 Turbo as your pipeline model.

Run experiment with DSPy-optimized classifier using GPT-4o

Redefine the task, using the new prompt.

Results

And just like that, you’ve run a series of prompt optimization techniques to improve the performance of a jailbreak classification task, and compared the results using Phoenix. You should have a set of experiments that looks like this:
From here, you can check out more examples on Phoenix, and if you haven’t already, please give us a star on GitHub! ⭐️