Skip to main content
Experiments allow you to test your model, prompts, or application against a set of evaluators using pre-defined inputs. This guide explains how to run an experiment against a RAG application, implemented using tool calling. The experiment will measure various evaluators associated with tools and RAG. The core of this experiment is an application you can run to get a mock horoscope from a mocked RAG system, using Splunk Agent Observability logging to trace the LLM, tool, and RAG interactions. This example shows how you can take an existing application and run it through an experiment with a defined dataset of inputs. In this guide you will:
  1. Set up your project with Splunk Agent Observability
  2. Create a basic RAG application
  3. Add logging with Splunk Agent Observability
  4. Run your app using an experiment

Get the code

You can find the code in this how-to guide in the Splunk Agent Observability SDK examples repo.

Before you start

To complete this how-to, you will need:

Install dependencies

To use Splunk Agent Observability, you need to install some package dependencies, and configure environment variables.
1

Install Required Dependencies

Install the required dependencies for your app. If you are using Python, create a virtual environment using your preferred method, then install dependencies inside that environment:
2

Create a .env file, and add the following values

Create a basic RAG application

In this section you will create a basic RAG application. This will use tool calling to call a function that simulates a RAG system by returning some pre-defined documents.
1

Create a file called app.py (Python)

2

Add imports

Add the following imports to your application file:
3

Add a RAG function

Add a function that simulates RAG. This function takes a star sign, and returns some dummy documents with horoscopes.
4

Add a tool to get the horoscope

Add code for a tool that can get the horoscope. This consists of a function that uses the RAG code to get horoscope information, and a tool definition that the LLM can use.
5

Add code to interact with an LLM

Add the following code to interact with OpenAI:
6

Add a function to generate the horoscope using the LLM and tools

Add the following code to request the horoscope from OpenAI, calling tools as required:
7

Add a main function

Add a main function to run the code:
8

Run your code

Run your app to ensure it is all working.
You should see a fake horoscope in the output:

Add logging with Splunk Agent Observability

You now have a basic AI application with a tool that uses RAG. Let’s add logging with Splunk Agent Observability.
1

Add imports for Splunk Agent Observability

Add the following imports to the top of your application file:
2

Create a session and trace

Change the main function to create a session and trace before generating the horoscope, then concluding it afterwards:
3

Use the Splunk Agent Observability OpenAI wrapper to log an LLM span

Splunk Agent Observability has a wrapper for the OpenAI SDK that logs all LLM interactions as LLM spans.If you are using Python, update the OpenAI import to the following.
4

Log the RAG call as a retriever span

Update the mock RAG function to log the call and result as a retriever span:
5

Log the tool call as a tool span

Update the get horoscope tool function to log the call and result as a tool span:
6

Run your code

Run your app to ensure it is all working and logs to Splunk Agent Observability.
View your Agent Stream in Splunk Agent Observability. You should see a session with a trace that has 2 LLM spans, a tool span, and a retriever span.

Run your app using an experiment

Now that your app is running, let’s create an experiment file to run the app as an experiment. In this code you’ll create a new file to run as a test, but in the real world you would probably create this as a unit test using your preferred framework of choice.
1

Create a file called experiment.py (Python)

2

Add code to run the experiment

Add the following code to the experiment file to run the get horoscope function in an experiment using a dataset of star signs.
This code runs the experiment, and calculates tool error rate, tool selection quality, and context adherence.The run experiment function runs the get horoscope code in the experiment. This code has logging for the LLM call, tool call, and RAG retriever code, but does not create a trace or a session. These are created in the applications main function.This is the correct way to write code that you want to run in an experiment. For each row in the dataset used in the experiment, a new trace is created. If you try to create other traces inside the code that the experiment runs, then the experiment will raise an error.You should either create sessions and traces outside the code that is run in the experiment, or inside your code check to see if you are in an experiment, and if so, not create a session or trace. See our documentation on running experiments against custom functions for more details.
3

Run your code

Run your experiment code to create the experiment in Splunk Agent Observability.
The terminal output will contain a link to the experiment:
Follow the link to see the results of the experiment.

Next steps

Run experiments in code

Learn how to run experiments in unit tests that you can use during development, or in your CI/CD pipelines.

Run experiments in unit tests

Learn how to run experiments in unit tests that you can use during development, or in your CI/CD pipelines.