Towards�Human-Level�Text Coding�with LLMs
1
�The Case of Fatherhood Roles�in Public Policy Documents�
Lorenzo Lupo, Oscar Magnusson,�Dirk Hovy, Elin Naurin, Lena Wängnerud
LLMs for text annotation…
Methodology:
Discussion about tradeoffs all along.
2
Why?
3
Step1: Data Selection
Data selection:
4
RQ: How have fatherhood roles evolved over time (in Sweden)?
Material: All public documents from 1993 to 2021 from riksdagen.se.
16390 documents: a mix of motions, protokoll, betänkanden, propositioner…
Step 2: Codebook
5
Step 3: (Little) Manual Annotation
6
Step 4: Choose Model and Prompt
7
Step 4: Choose Model
8
Step 4: Choose Model - GPT
9
import openai
�# Set your OpenAI API key
openai.api_key = 'your_api_key_here'
�# Load a list of texts to annotate
texts = your_loading_function()
�# Loop through each text in the list
annotations = []
for text in texts:
# Call the API to generate annotations for the text
response = openai.Completion.create(
prompt=text,
engine="text-davinci-003",
max_tokens=50,
temperature=0.0,
)
�# Extract the generated annotation from the API response
annotation = response.choices[0].text
�# Append the annotation to the list of annotations
annotations.append(annotation)
Step 4: Choose Model
10
Step 4: Choose Model – OS LLMs
11
from transformers import AutoModelForCausalLM, AutoTokenizer
�# Load the model and tokenizer
model_name = "meta-llama/Llama-2-13b-hf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Load a list of texts to annotate
texts = your_loading_function()
�# Loop through each text in the list
annotations = []
for text in texts:
# Tokenize the text
inputs = tokenizer.encode(text, return_tensors="pt")
�# Generate annotations using the model
with torch.no_grad():
outputs = model.generate(
inputs, max_length=50, temperature=0.7)
�# Decode the generated annotations
annotation = tokenizer.decode(
outputs[0],skip_special_tokens=True)
�# Append the annotation to the list of annotations
annotations.append(annotation)
Step 4: Choose Model
12
Step 4: Choose Model and Prompt
How to evaluate options:
13
Step 4: Choose Prompt
14
Step 4: Choose Prompt
15
Step 4: Choose Prompt
16
Step 4: Choose Prompt
17
Step 4: Choose Prompt
18
Step 4: Choose Prompt
19
Step 4: Choose Prompt
20
Step 4: Choose Model
21
Step 5: Annotate the Full Corpus
22
23
Accessibility
24
Next step: Substantial article
25
26
Conclusion
Summary:
Challenges:
27
This presentation…
28
Ideas
29
Escpaing the loop
30
GPT
190 Gold-labels
1721 labels
BERT
Human
Instructions + 10 example sentences
Kappa = .65
Eval = .68
Full dataset
Train
Train
Annotate
Annotate
Annotate
Annotate
31
32