Making GPT Your RA
A guide to using GPT to classify free text
You’ve probably played around with ChatGPT
The Problem
Why we worry about large language models (LLMs):
They do weird things.
They are bizarrely overconfident.
1
2
The Challenge
On the other hand…
People also do weird things.
We have tools to understand and improve performance.
1
2
The Challenge
We wanted to collapse 60,000 free-text documents into a single categorical variable.
Here is how we did it with generative AI.
Overview of Technical Steps
How to Get Results:
• How GPTs work�• How to use the OpenAI API?
• How to parallelize code?�• How to navigate rate limits?
How to Get Good Results
• Prompt engineering�• Evaluating performance on labeled data
How to Even Better Results:
• Actor-critic
• Output token probabilities
Example of a
Classification Problem
How many clinical drug trials �enroll pregnant participants?
This is deceptively hard to answer.
Data Source: ClinicalTrials.gov
exclusion criteria
Database of clinical drug trials
A Sample Classification Problem
Data Source: ClinicalTrials.gov
Examples
A Sample Classification Problem
Approaches to classification
A Sample Classification Problem
How to Get Results
Setup
Brown University DSI workshops are held on
_______.
Brown University DSI workshops are held on
_______.
X
^
Y
Generative Pre-trained Transformers
The Problem
ChatGPT is not designed for this.
It may give you (bad) Python text mining code…
Setup
…or a result (as you feed it entries 1-by-1).
So we used the API.
Setup
OpenAI’s API can be used to process large datasets
To use OpenAI’s API, we need:
Setup
To use OpenAI’s API, we need:
Setup
API key
To use OpenAI’s API, we need:
https://openai.com/api/pricing/
Setup
Budget
To use OpenAI’s API, we need:
Setup
Code (and input data)
Writing Python code
Coding Instructions in Python
We coded in Python
Open up your favorite Python editor
Writing Python code
Coding Instructions in Python
Install the OpenAI Python Library
Writing Python code
Coding Instructions in Python
Other code options: Python (.py), R code
0) Install useful libraries
Coding Instructions in Python
These are the �libraries we used
Next: Call the API with Python
Coding Instructions in Python
General idea
Coding Instructions in Python
2a) Calling the API
This is further down in the sample file – we’ll explain the intervening lines in a minute
Coding Instructions in Python
model: Which LLM you use (https://platform.openai.com/docs/models)
2a) Calling the API
Coding Instructions in Python
response_format: Determines how output is returned
2a) Calling the API
Coding Instructions in Python
temperature: Determines randomness in responses (ranges from 0-1, 0 is close to deterministic/reproducible)
2a) Calling the API
Coding Instructions in Python
prompt: What you want the GPT to do! We asked it to classify study types. We will get into this more in a moment!
2a) Calling the API
Coding Instructions in Python
input_data: Information on a specific item to analyze. We fed in data from a randomized drug trial on ClinicalTrials.gov.
2b) Extract the useful output
Coding Instructions in Python
response: There’s a lot of information in the response (“completion”) object. The above code gives you the main output, but here is a full example object from OpenAI’s API: https://platform.openai.com/docs/guides/text-generation/chat-completions-api)
2c) Put together + catch errors & track progress
Coding Instructions in Python
3) Loop over data
So if your computer has 16 cores, you’d be limited to 16 simultaneous calls. With threading, you can do hundreds.
Parallelization
Put this API call in a loop
Parallelize for speed
3) Loop over data
Parallelization
3) Loop over data (w. rate limits)
Rate limits
API subscriptions have rate limits: https://platform.openai.com/�docs/guides/rate-limits.
We used a function that paused jobs if total token usage per minute exceeded a user-specified threshold, based on current account rate limits. You can adapt as needed.
How to Get Good Results
Training & validation
Training your GPT RA
Training
We break this into 3 steps
Training
Iterate on your instructions
Note that we ask GPT to explain itself in two ways:
Training
Our first attempt at instructions
Judgement call:
Hallucination:
Training
These variables help identify judgement calls & hallucinations
Training
Our next set of instructions
2) Check on a training set
Training
Give your GPT RA some labeled data
2) Check on a training set
Training
Consider several measures of good classification
Validation set
Validation
Give your GPT RA a report card
How to Refine Results
Improving results
Actor-Critic
In our example, GPT sometimes provided the right answer in the justification but the wrong classification. We explored two methods for addressing this.
First, we added an additional field to our prompt.
Actor-Critic
GPT can sometimes recognize its own errors
Actor-Critic
Next, we ran an entirely new set of API calls with a new prompt to help address a specific error (marking Unspecified as Excluded).
Actor-Critic
GPT can sometimes recognize its own errors
Actor-Critic
Both methods reduced errors, the second by ~50% in training data!
Original:
Critic:
Actor-Critic
GPT can sometimes recognize its own errors
Output token probabilities
Output token probabilities
You can also look under the hood at output token probabilities.
Need low-dimensional output
In our case, GPT was overconfident… nevertheless, reviewing observations with the lowest 6% of class probabilities could catch 50% of errors.
Good Luck!
Use this tool (and guide) for good please :-)
Feedback? Questions?
Alyssa Bilinski: alyssa_bilinski@brown.edu
Natalia Emanuel: natalia@nataliaemanuel.com
Ozair Ali: ozair@ekai.ai