Skip to main content
Google Colab

colab.research.google.com

This notebook serves as an end-to-end example of how to trace and evaluate an agent. The example uses a “talk-to-your-data” agent as its example.

The notebook shows examples of:

  • Manually instrumenting an agent using Phoenix decorators
  • Evaluating function calling accuracy using LLM as a Judge
  • Evaluating function calling accuracy by comparing to ground truth
  • Evaluating SQL query generation
  • Evaluating Python code generation
  • Evaluating the path of an agent

Notebook Walkthrough#

We will go through key code snippets on this page. To follow the full tutorial, check out the full notebook.

Prepare dataset#

Your agent will interact with a local database. Start by loading in that data:

Define the tools#

Now you can define your agent tools.

Tool 1: Database Lookup#

Tool 2: Data Visualization#

Tool 3: Data Analysis#

Tool Schema:#

You’ll need to pass your tool descriptions into your agent router. The following code allows you to easily do so:

Agent logic#

With the tools defined, you’re ready to define the main routing and tool call handling steps of your agent.

Run the agent#

Your agent is now good to go! Let’s try it out with some example questions:

Agent Traces

Evaluating the agent#

So your agent looks like it’s working, but how can you measure its performance?

Function Calling Evals using LLM as a Judge#

This first evaluation will evaluate your agent router choices using another LLM.

It follows a standard pattern:

  1. Export traces from Phoenix
  2. Prepare those exported traces in a dataframe with the correct columns
  3. Use async_evaluate_dataframe with a ToolSelectionEvaluator and ToolInvocationEvaluator to classify each row and produce eval labels
  4. Upload the results back into Phoenix

You should now see eval labels in Phoenix.

Function Calling Evals using Ground Truth#

The above example works, however if you have ground truth labled data, you can use that data to get an even more accurate measure of your router’s performance by running an experiments.

Experiments also follow a standard step-by-step process in Phoenix:

  1. Create a dataset of test cases, and optionally, expected outputs
  2. Create a task to run on each test case - usually this is invoking your agent or a specific step of it
  3. Create evaluator(s) to run on each output of your task
  4. Visualize results in Phoenix

For your task, you can simply run just the router call of your agent:

Your evaluator can also be simple, since you have expected outputs. If you didn’t have those expected outputs, you could instead use an LLM as a Judge here, or even basic code:

Tool Evals#

The next piece of your agent to evaluate is its tools. Each tool is usually evaluated differently - we’ve included some examples below. If you need other ideas, Phoenix’s built-in evaluators give you an idea of other metrics to use.

Evaluating our SQL generation tool#

Evaluating our Python code generation tool#

In this case, you don’t have ground truth data to compare to. Instead you can just use a simple code evaluator: trying to run the generated code and catching any errors.

Evaluating the agent path and convergence#

Finally, the last piece of your agent to evaluate is its path. This is important to evaluate to understand how efficient your agent is in its execution. Does it need to call the same tool multiple times? Does it skip steps it shouldn’t, and have to backtrack later? Convergence or path evals can tell you this.

Convergence evals operate slightly differently. The one you’ll use below relies on knowing the minimum number of steps taken by the agent for a given type of query. Instead of just running an experiment, you’ll run an experiment then after it completes, attach a second evaluator to calculate convergence.

The workflow is as follows:

  1. Create a dataset of the same type of question, phrased different ways each time - the agent should take the same path for each, but you’ll often find it doesn’t.
  2. Create a task that runs the agent on each question, while tracking the number of steps it takes.
  3. Run the experiment without an evaluator.
  4. Calculate the minimum number of steps taken to complete the task.
  5. Create an evaluator that compares the steps taken of each run against that min step number.
  6. Run this evaluator on your experiment from step 3.
  7. View your results in Phoenix

Advanced - Combining all the evals into our experiment#

As an optional final step, you can combine all the evaluators and experiments above into a single experiment. This requires some more advanced data wrangling, but gives you a single report on your agent’s performance.

Build a version of our agent that tracks all the necessary information for evals#

Final Results 🎉#

You’ve now evaluated every aspect of your agent. If you’ve made it this far, you’re now an expert in evaluating agent routers, tools, and paths!