Using PDF attachments in playgrounds
Logging with attachments allows you to capture user-provided files like images, PDFs, and other documents, but you might also want to leverage these attachments directly within datasets for testing prompts in playgrounds. This cookbook guides you step-by-step through two primary methods—using the paperclip UI button or public URLs—to attach PDFs and quickly iterate on prompts in playgrounds. By the end of this guide, you'll know how to emit spans containing embedded attachments, log prompts into organized spans within a single trace, save these spans into datasets, and seamlessly move them into playgrounds. We'll demonstrate these techniques using earnings report transcripts as illustrative PDF files.
Getting started
To get started, you'll need Braintrust and OpenAI accounts, along with their corresponding API keys. Plug your OpenAI API key into your Braintrust account's AI providers configuration. You can also add an API key for any other AI provider you'd like, but be sure to change the code to use that model. Lastly, add your BRAINTRUST_API_KEY
to your .env.local
file:
To install the necessary dependencies, start by downloading pnpm or a package manager of your choice. Then, use the following package.json
file:
And install dependencies by running:
To follow along with this cookbook, create a file called log_pdfs.ts
and add each of the code snippets below to the file as we go through each step. Alternatively, you can download the complete file on GitHub.
Initializing the logger and OpenAI client
The first thing we'll do is import the modules we need and initialize our OpenAI client. We're wrapping the client so that we have access to Braintrust features.
Defining the PDFs
In this cookbook, we'll use PDFs of various earning calls from public companies. We'll create a list of objects that contain the filenames and their corresponding URLs:
Defining the system prompt
Next, we'll define the system prompt that instructs the LLM on how to analyze the PDFs. We want to use specific analysis criteria to make sure we get the desired output:
Processing the PDF files
Now, we need to implement a function to handle the PDF processing. This function fetches the PDF file from the URL, converts it to a base64 string, and passes it to the LLM for processing:
Logging spans
We'll create separate functions to log the system and user spans.
The system prompt span contains the instructions given to the LLM. This function creates a child span related to the root span and captures the system prompt along with the resulting summary:
We want to create and log the user prompt span as well, since it includes the actual PDF attachment. We'll reconstruct the PDF data from the base64 string and attach it to the span, which will make it available for use in the playground:
Executing the main process
Finally, we create and execute the main function that processes all the PDFs in our list. This function loops through each PDF file, processes it individually, and handles any errors that may occur:
Running your file
To execute the script, you can run one of the following commands, depending on how you've set up your environment. If you used the provided package.json
file and named your file log_pdfs.ts
, you can run:
You can also use this command with your file name:
Using the Braintrust UI
Once your traces have been logged, you can use the Braintrust UI to manage your spans and experiment with different prompts.
Creating a dataset
You can store the user spans from your PDF traces into a dataset. Select the span, and then select Add span to dataset, or use the hotkey D
to speed this up.
Trying system prompts in a playground
Select a system prompt span, and then select Try prompt to:
- Save the prompt (for example, "system1") to your library by selecting Save as custom prompt
- Launch a playground using the saved prompt by selecting Create playground with prompt
File attachment methods
There are two ways to attach PDF files in playgrounds: using the paperclip button in the UI, or specifying a public URL. Let's walk through each method:
- To upload files directly from your local machine, start by selecting + Message to add a user prompt. Then, select + Message Part > File. This will display a paperclip icon on the right side. Select it to upload a file from your local machine.
This method is particularly useful when you're working with local files that aren't accessible via public URL.
- To use the public URL method, paste the URL directly into the file message input field. You can also use mustache syntax to extract the URL from metadata.
This method streamlines the process when you're working with publicly available PDFs, like the earnings call transcripts we're using in this cookbook.
Both methods result in the PDF being attached to your prompt, allowing the LLM to analyze its contents. Choose the approach that best fits your workflow based on where your files are stored.
Next steps
Now that you understand the process of converting spans with PDF attachments into a dataset and executing the PDFs in the playground, you can:
- Learn more about multimodal prompts in playgrounds
- Read more about logging with attachments