-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks_usdc.py
494 lines (452 loc) · 16.2 KB
/
tasks_usdc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import argparse
import inspect
from typing import cast
import pandas as pd
from pandas import DataFrame
from api import (
GooglePaLMCompletion,
LlamaChat,
OpenAIChat,
OpenAIChatGpt4,
TogetherAiLlamaChat,
)
from correctness_checks import (
bool_correctness,
citation_correctness,
cited_precedent_correctness,
clean_district,
clean_judge_name,
clean_quotation,
name_correctness,
quotation_correctness,
usdc_court_id_correctness,
)
from models import CourtCase, Query, Task
from settings import FSUPP_SAMPLE_PATH
from utils import (
APIBackendType,
format_case_name,
get_citation_from_cap_dict,
get_importance_from_cap_dict,
get_majority_opinion_from_cap_dict,
)
parser = argparse.ArgumentParser()
parser.add_argument(
"--api", type=str, help="api to use", choices=["llama", "gpt3.5", "palm", "gpt4"]
)
args = parser.parse_args()
CURRENT_API: APIBackendType = OpenAIChatGpt4
match args.api:
case "llama":
CURRENT_API = LlamaChat # TogetherAiLlamaChat also okay
case "gpt3.5":
CURRENT_API = OpenAIChat
case "palm":
CURRENT_API = GooglePaLMCompletion
case "gpt4":
CURRENT_API = OpenAIChatGpt4
# Load data
usdc_sample: DataFrame = pd.read_csv(FSUPP_SAMPLE_PATH, index_col=False)
# Generate Case objects
cases: list[CourtCase] = [
CourtCase(
case_name=case["name_abbreviation"],
other_citation=get_citation_from_cap_dict(eval(case["citations"])),
year=case["decision_date"][0:4],
majority_author=case["majority_author"],
majority_opinion=get_majority_opinion_from_cap_dict(case),
court=eval(case["court"])["slug"],
state=case["state"],
source="cap",
importance=get_importance_from_cap_dict(case),
)
for case in usdc_sample.to_dict("records")
]
###################################
# Case existence task
###################################
case_existence_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message='Say "yes" or "no" only.',
query_template="Is the case {case_name}, {case_citation} ({case_year}), a real case? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": "1"}, # parsed as True/"yes" downstream
correctness_callback=bool_correctness,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/case_existence",
)
case_existence_task.do()
case_existence_task.save()
###################################
# Case existence task (few shot)
###################################
case_existence_task_few_shot: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message='Say "yes" or "no" only.',
query_template=inspect.cleandoc(
"""
Is the given case a real case? {system_message}
Examples:
```
Case: Scheck v. Burger King Corp., 756 F. Supp. 543 (1991)
Answer: Yes
Case: United States v. Apple Inc., 952 F. Supp. 2d 638 (2013)
Answer: Yes
Case: Columbia University v. Rodham, 564 F. Supp. 911 (1982)
Answer: No
```
Case: {case_name}, {case_citation} ({case_year})
Answer:
"""
),
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": "1"}, # parsed as True/"yes" downstream
correctness_callback=bool_correctness,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/case_existence_few_shot",
)
case_existence_task_few_shot.do()
case_existence_task_few_shot.save()
###################################
# Court ID task
###################################
court_id_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="Provide the name of the district court ONLY, nothing else.",
query_template="Which federal district court decided the case {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": cast(str, case.state)},
correctness_callback=usdc_court_id_correctness,
llm_answer_postprocess=clean_district,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/court_id",
)
court_id_task.do()
court_id_task.save()
###################################
# Court ID task (few shot)
###################################
court_id_task_few_shot: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="Provide the name of the district court ONLY, nothing else.",
query_template=inspect.cleandoc(
"""
Which federal district court decided the given case? {system_message}
Examples:
```
Case: Scheck v. Burger King Corp., 756 F. Supp. 543 (1991)
Answer: Southern District of Florida
Case: United States v. Apple Inc., 952 F. Supp. 2d 638 (2013)
Answer: Southern District of New York
Case: United States v. Progressive, Inc., 467 F. Supp. 990 (1979)
Answer: Western District of Wisconsin
```
Case name: {case_name}, {case_citation} ({case_year})
Answer:
"""
),
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": cast(str, case.state)},
correctness_callback=usdc_court_id_correctness,
llm_answer_postprocess=clean_district,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/court_id_few_shot",
)
court_id_task_few_shot.do()
court_id_task_few_shot.save()
###################################
# Citation retrieval task
###################################
citation_retrieval_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message='Provide ONLY the citation in "<volume>, <reporter>, <page>" format, nothing else.',
query_template="What is the citation for the district court case {case_name}? {system_message}",
query_content={"case_name": format_case_name(case.case_name)},
true_answer={"answer": cast(str, case.other_citation)},
correctness_callback=citation_correctness,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/citation_retrieval",
)
citation_retrieval_task.do()
citation_retrieval_task.save()
###################################
# Citation retrieval task (few shot)
###################################
citation_retrieval_task_few_shot: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message='Provide ONLY the citation in "<volume>, <reporter>, <page>" format, nothing else.',
query_template=inspect.cleandoc(
"""
What is the citation for the given district court case? {system_message}
Examples:
```
Case: Scheck v. Burger King Corp.
Answer: 756 F. Supp. 543 (S.D. Fla. 1991)
Case: United States v. Apple Inc.
Answer: 952 F. Supp. 2d 638 (S.D.N.Y. 2013)
Case: United States v. Progressive, Inc.
Answer: 467 F. Supp. 990 (W.D. Wis. 1979)
```
Case: {case_name}
Answer:
"""
),
query_content={"case_name": format_case_name(case.case_name)},
true_answer={"answer": cast(str, case.other_citation)},
correctness_callback=citation_correctness,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/citation_retrieval_few_shot",
)
citation_retrieval_task_few_shot.do()
citation_retrieval_task_few_shot.save()
###################################
# Majority opinion author task
###################################
majority_author_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="Provide the first and the last name of the judge ONLY.",
query_template="Who wrote the majority opinion in {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": clean_judge_name(cast(str, case.majority_author))},
correctness_callback=name_correctness,
llm_answer_postprocess=clean_judge_name,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/majority_author",
)
majority_author_task.do()
majority_author_task.save()
###################################
# Majority opinion author task (few shot)
###################################
majority_author_task_few_shot: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="Provide the first and the last name of the judge ONLY.",
query_template=inspect.cleandoc(
"""
Who wrote the opinion in the given case? {system_message}
Examples:
```
Case: Scheck v. Burger King Corp., 756 F. Supp. 543 (1991)
Answer: William Hoeveler
Case: United States v. Apple Inc., 952 F. Supp. 2d 638 (2013)
Answer: Denise Cote
Case: United States v. Progressive, Inc., 467 F. Supp. 990 (1979)
Answer: Robert Warren
```
Case: {case_name}, {case_citation} ({case_year})
Answer:
"""
),
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": clean_judge_name(cast(str, case.majority_author))},
correctness_callback=name_correctness,
llm_answer_postprocess=clean_judge_name,
)
for case in cases
],
sampling_temperature=1,
save_string="usdc/majority_author_few_shot",
)
majority_author_task_few_shot.do()
majority_author_task_few_shot.save()
###################################
# Quotation task
###################################
quotation_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="The quotation MUST be word-for-word from the majority opinion. Wrap the quotation in <quote></quote> tags.",
query_template="Provide a verbatim quotation from the majority opinion in the case {case_name}, {case_citation} ({case_year}). {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": cast(str, case.majority_opinion)},
correctness_callback=quotation_correctness,
llm_answer_postprocess=clean_quotation,
)
for case in cases
],
sampling_temperature=-99,
save_string="usdc/quotation",
)
quotation_task.do()
quotation_task.save()
###################################
# Quotation task (few shot)
###################################
quotation_task_few_shot: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="The quotation MUST be word-for-word from the majority opinion. Wrap the quotation in <quote></quote> tags.",
query_template=inspect.cleandoc(
"""
Provide a verbatim quotation from the majority opinion in the given case. {system_message}
Examples:
```
Case: Scheck v. Burger King Corp., 756 F. Supp. 543 (1991)
Answer: <quote>In cases in which jurisdiction depends upon diversity of citizenship, Federal courts must follow conflict of laws rules prevailing in the states in which they sit.</quote>
Case: United States v. Apple Inc., 952 F. Supp. 2d 638 (2013)
Answer: <quote>Another company's alleged violation of antitrust laws is not an excuse for engaging in your own violations of law.</quote>
Case: United States v. Progressive, Inc., 467 F. Supp. 990 (1979)
Answer: <quote>First Amendment rights are not absolute. They are not boundless.</quote>
```
Case: {case_name}, {case_citation} ({case_year})
Answer:
"""
),
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": cast(str, case.majority_opinion)},
correctness_callback=quotation_correctness,
llm_answer_postprocess=clean_quotation,
)
for case in cases
],
sampling_temperature=-99,
save_string="usdc/quotation_few_shot",
)
quotation_task_few_shot.do()
quotation_task_few_shot.save()
###################################
# Cited precedent task
###################################
cited_precedent_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message='Provide ONLY the citation of the precedent in "<volume>, <reporter>, <page>" format, nothing else.',
query_template="What is a precedent that is cited in the majority opinion of the case {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": cast(str, case.majority_opinion)},
correctness_callback=cited_precedent_correctness,
)
for case in cases
],
sampling_temperature=-99,
save_string="usdc/cited_precedent",
)
cited_precedent_task.do()
cited_precedent_task.save()
###################################
# Cited precedent task (few shot)
###################################
cited_precedent_task_few_shot: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message='Provide ONLY the citation of the precedent in "<volume>, <reporter>, <page>" format, nothing else.',
query_template=inspect.cleandoc(
"""
What is a precedent that is cited in the opinion of the given case? {system_message}
Examples:
```
Case: Scheck v. Burger King Corp., 756 F. Supp. 543 (1991)
Answer: Klaxon Co. v. Stentor Electric Manufacturing Co., 313 U.S. 487
Case: United States v. Apple Inc., 952 F. Supp. 2d 638 (2013)
Answer: Anderson News, L.L.C. v. American Media, Inc., 680 F.3d 162
Case: United States v. Progressive, Inc., 467 F. Supp. 990 (1979)
Answer: New York Times v. United States, 403 U.S. 713
```
Case: {case_name}, {case_citation} ({case_year})
Answer:
"""
),
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": cast(str, case.other_citation),
"case_year": str(case.year),
},
true_answer={"answer": cast(str, case.majority_opinion)},
correctness_callback=cited_precedent_correctness,
)
for case in cases
],
sampling_temperature=-99,
save_string="usdc/cited_precedent_few_shot",
)
cited_precedent_task_few_shot.do()
cited_precedent_task_few_shot.save()