-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks_zero_resource.py
262 lines (244 loc) · 8.53 KB
/
tasks_zero_resource.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
import argparse
import random
from functools import partial
import pandas as pd
from pandas.core.frame import DataFrame
from api import (
GooglePaLMCompletion,
LlamaChat,
OpenAIChat,
OpenAIChatGpt4,
TogetherAiLlamaChat,
)
from correctness_checks import nli_premise_hypothesis_check
from models import CourtCase, Query, Task
from settings import FD_SAMPLE_PATH, FSUPP_SAMPLE_PATH, RANDOM_SEED, SCDB_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
scdb_sample: DataFrame = pd.read_csv(SCDB_SAMPLE_PATH, index_col=False)
coa_sample: DataFrame = pd.read_csv(FD_SAMPLE_PATH, index_col=False)
usdc_sample: DataFrame = pd.read_csv(FSUPP_SAMPLE_PATH, index_col=False)
# Generate Case objects
scotus_cases: list[CourtCase] = [
CourtCase(
case_name=format_case_name(case["caseName"]),
us_citation=case["usCite"],
sct_citation=case["sctCite"],
lexis_citation=case["lexisCite"],
year=case["term"],
majority_author=case["majOpinWriter"],
majority_opinion=case["majority_opinion"],
disposition=case["caseDisposition"],
winner=case["partyWinning"],
court="scotus",
source="scdb",
importance=case["pauth_score"],
)
for case in scdb_sample.to_dict("records")
]
random.seed(RANDOM_SEED)
scotus_cases = random.sample(scotus_cases, 100)
coa_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=case["circuit"],
source="cap",
importance=get_importance_from_cap_dict(case),
)
for case in coa_sample.to_dict("records")
]
random.seed(RANDOM_SEED)
coa_cases = random.sample(coa_cases, 100)
usdc_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")
]
random.seed(RANDOM_SEED)
usdc_cases = random.sample(usdc_cases, 100)
def get_case_citation(case: CourtCase) -> str:
if case.court == "scotus":
if case.us_citation and not pd.isna(case.us_citation):
return case.us_citation
elif case.sct_citation and not pd.isna(case.sct_citation):
return case.sct_citation
else:
return ""
else:
if case.other_citation and not pd.isna(case.other_citation):
return case.other_citation
else:
return ""
for court, cases in [
("scotus", scotus_cases),
("coa", coa_cases),
("usdc", usdc_cases),
]:
posture_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="No more than two sentences.",
query_template="What was the procedural posture in {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": get_case_citation(case),
"case_year": str(case.year),
},
true_answer=None,
correctness_callback=partial(
nli_premise_hypothesis_check,
n_shot=3,
gpt4=True,
), # type: ignore
)
for case in cases
],
sampling_temperature=0.999,
save_string=f"{court}/posture",
max_tokens=256,
)
core_legal_question_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="No more than two sentences.",
query_template="What was the core legal question in {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": get_case_citation(case),
"case_year": str(case.year),
},
true_answer=None,
correctness_callback=partial(
nli_premise_hypothesis_check,
n_shot=3,
gpt4=True, # type: ignore
),
)
for case in cases
],
sampling_temperature=0.999,
save_string=f"{court}/core_legal_question",
max_tokens=256,
)
holding_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="No more than two sentences.",
query_template="What was the primary legal holding in {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": get_case_citation(case),
"case_year": str(case.year),
},
true_answer=None,
correctness_callback=partial(
nli_premise_hypothesis_check,
n_shot=3,
gpt4=True, # type: ignore
),
)
for case in cases
],
sampling_temperature=0.999,
save_string=f"{court}/holding",
max_tokens=256,
)
factual_background_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="No more than two sentences.",
query_template="What was the factual background in {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": get_case_citation(case),
"case_year": str(case.year),
},
true_answer=None,
correctness_callback=partial(
nli_premise_hypothesis_check,
n_shot=3,
gpt4=True, # type: ignore
),
)
for case in cases
],
sampling_temperature=0.999,
save_string=f"{court}/factual_background",
max_tokens=256,
)
subsequent_history_task: Task = Task(
api_backend_type=CURRENT_API,
queries=[
Query(
test_case=case,
system_message="No more than two sentences.",
query_template="What was the subsequent appellate history in {case_name}, {case_citation} ({case_year})? {system_message}",
query_content={
"case_name": format_case_name(case.case_name),
"case_citation": get_case_citation(case),
"case_year": str(case.year),
},
true_answer=None,
correctness_callback=partial(
nli_premise_hypothesis_check,
n_shot=3,
gpt4=True, # type: ignore
),
)
for case in cases
],
sampling_temperature=0.999,
save_string=f"{court}/subsequent_history",
max_tokens=256,
)
for task in [
posture_task,
core_legal_question_task,
holding_task,
factual_background_task,
subsequent_history_task,
]:
task.do()
task.save()