-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreatise_engine.py
172 lines (160 loc) · 8.45 KB
/
treatise_engine.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
import openai
import re
#import nltk
#from nltk.tokenize import sent_tokenize
#not using function that uses nltk
def generate_document(doctype, subject, jurisdiction):
if doctype == "memo":
return write_memo(subject, jurisdiction)
elif doctype == "longer_memo":
#this seems to crash flask because it passes the whole memo in URL
return write_long_memo(subject, jurisdiction)
elif doctype == "bullets":
return write_bullets(subject, jurisdiction)
elif doctype == "contract":
return write_contract(subject, jurisdiction)
elif doctype == "longer_contract":
return write_long_contract(subject, jurisdiction)
elif doctype == "claim_letter":
return write_claim_letter(subject, jurisdiction)
def write_memo(subject, jurisdiction):
prompt="Write a full and long explanation of the law of {subject} in {jurisdiction} with section headings and citations within the explanation text to statute and case law (with pin cites)".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize())
outcompletion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
return outcompletion.choices[0].message.content
#outcompletion = openai.Completion.create(
# model="text-davinci-003",
# prompt="Write a full and long explanation of the law of {subject} in {jurisdiction} with section headings and citations within the explanation text to statute and case law (with pin cites)".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
# temperature=0.6,
# max_tokens=3896 #EVENTUALLY TO COUNT TOKENS IN COMBINED PROMPT AND PUT MAX WITHOUT -- COULD USE HUGGINGFACE BPE MODEL
#)
#return outcompletion.choices[0].text
#Writes a long memo by first creating an outline and then writing the text
#for each item. Most reliable method so far, but will be repetitive
def write_long_memo(subject, jurisdiction):
outline = openai.Completion.create(
model="text-davinci-003",
prompt="Write an alphanumeric outline of a full and long explanation of the law of {subject} in {jurisdiction}".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
temperature=0.5,
max_tokens=3896
)
outline_string = outline.choices[0].text
print(outline_string)
outline_items = extract_outline_items(outline_string)
enriched_memo_list = []
for item in outline_items:
#item_text = write_memo_outline_item(item, subject, jurisdiction)
item_text = write_memo_outline_item_with_chat(item, subject, jurisdiction)
enriched_memo_list.append(item_text)
return " ".join(enriched_memo_list)
# Extracts the items from a memo outline
def extract_outline_items(outline):
items = []
lines = outline.strip().split("\n")
for line in lines:
item = line.strip()
items.append(item)
return items
# Takes an item from the outline for a memo and writes the text for it
def write_memo_outline_item(item, subject, jurisdiction):
outcompletion = openai.Completion.create(
model="text-davinci-003",
prompt="Write the text for a section called \"{item}\" of a full and long explanation of the law of {subject} in {jurisdiction} with citations within the explanation text to statute and case law (with pin cites)".format(item=item, subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
temperature=0.5,
max_tokens=3896
)
print(item, outcompletion.choices[0].text)
return outcompletion.choices[0].text
#same thing using gpt-3.5-turbo model
def write_memo_outline_item_with_chat(item, subject, jurisdiction):
prompt="Write the text for a section called \"{item}\" of a full and long explanation of the law of {subject} in {jurisdiction} with citations within the explanation text to statute and case law (with pin cites)".format(item=item, subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize())
outcompletion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
return outcompletion.choices[0].message.content
def write_bullets(subject, jurisdiction):
outcompletion = openai.Completion.create(
model="text-davinci-003",
prompt="Write a bullet point outline explaining the law of {subject} in {jurisdiction}".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
temperature=0.5,
max_tokens=3896 #EVENTUALLY TO COUNT TOKENS IN COMBINED PROMPT AND PUT MAX WITHOUT -- COULD USE HUGGINGFACE BPE MODEL
)
return outcompletion.choices[0].text
def write_contract(subject, jurisdiction):
outcompletion = openai.Completion.create(
model="text-davinci-003",
prompt="Write a {subject} contract governed by the law of {jurisdiction}".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
temperature=0.5,
max_tokens=3896 #EVENTUALLY TO COUNT TOKENS IN COMBINED PROMPT AND PUT MAX WITHOUT -- COULD USE HUGGINGFACE BPE MODEL
)
return outcompletion.choices[0].text
#Writes a long contract by first creating an outline and then writing the text
#for each section
#WORK IN PROGRESS AND TO ADD TO INTERFACE
def write_long_contract(subject, jurisdiction):
outline = openai.Completion.create(
model="text-davinci-003",
prompt="Write an alphanumeric outline (in the order: arabic numerals, capital letters, romanettes) of section headings of a {subject} contract governed by the law of {jurisdiction}".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
temperature=0.5,
max_tokens=3896
)
outline_string = outline.choices[0].text
print(outline_string)
outline_items = extract_outline_items(outline_string)
enriched_contract_list = []
for i, item in enumerate(outline_items):
#item_text = write_memo_outline_item(item, subject, jurisdiction)
if re.match(r'[a-zA-Z]', item[0]):
item_text = write_contract_outline_item_with_chat(item, subject, jurisdiction)
enriched_contract_list.append(item_text)
elif i == (len(outline_items) - 1):
item_text = write_contract_outline_item_with_chat(item, subject, jurisdiction)
enriched_contract_list.append(item_text)
elif re.match('r[1-9]', item[0]) and not re.match(r'[a-zA-Z]', outline_items[i+1][0]):
item_text = write_contract_outline_item_with_chat(item, subject, jurisdiction)
enriched_contract_list.append(item_text)
return " ".join(enriched_contract_list)
def write_contract_outline_item_with_chat(item, subject, jurisdiction):
prompt="Write the text for a section called \"{item}\" of a {subject} contract governed by the law of {jurisdiction}".format(item=item, subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize())
outcompletion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
return outcompletion.choices[0].message.content
def write_claim_letter(subject, jurisdiction):
outcompletion = openai.Completion.create(
model="text-davinci-003",
prompt="Write a legal claim letter concerning a {subject} violation under {jurisdiction} law. Include in-line citations to statutes and case law with pincites".format(subject=subject.capitalize(), jurisdiction=jurisdiction.capitalize()),
temperature=0.7,
max_tokens=3896 #EVENTUALLY TO COUNT TOKENS IN COMBINED PROMPT AND PUT MAX WITHOUT -- COULD USE HUGGINGFACE BPE MODEL
)
return outcompletion.choices[0].text
#the below function is unreliable for memos at this time -- adds random legal subjects
#between sentences -- sometimes very lengthy -- and other cases is null or short
#and not particularly helpful
#def elaborate_sentences(s):
# sentences = sent_tokenize(s)
# outtext=""
# for i in range(len(sentences)):
# outtext += sentences[i]
# if i < len(sentences) - 1:
# newtext=openai.Completion.create(
# model="text-davinci-003",
# prompt=sentences[i],
# suffix=sentences[i+1],
# temperature=0.7,
# max_tokens=2048
# )
# #below just for test
# print(newtext)
# outtext += newtext.choices[0].text
# return outtext