-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecbaprocessgptasync.py
385 lines (319 loc) · 14 KB
/
secbaprocessgptasync.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
import asyncio
import json
import logging
import random
import signal
from typing import List
import aiofiles
import tiktoken
from dotenv import load_dotenv
from openai import AsyncOpenAI
from pydantic import BaseModel
from tqdm import tqdm
# Configuration
INPUT_FILE = "secba_process.jsonl"
OUTPUT_FILE = "secba_processgpt.jsonl"
PROMPT_FILE = "zpromptsgpt.jsonl"
STARTING_FILE = "startingfilegpt.jsonl"
MAX_TOKENS = 115000
MAX_RETRIES = 50
INITIAL_RETRY_DELAY = 1 # seconds
STARTING_POINT = 0
NO_WORKERS = 5
# Ignore SIGHUP (hangup signal)
signal.signal(signal.SIGHUP, signal.SIG_IGN)
# Load environment variables from the specified path
load_dotenv("/home/arthrod/.env/.env")
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("contract_processor_detailed.log"),
logging.StreamHandler(),
],
)
logger = logging.getLogger(__name__)
# Initialize AsyncOpenAI client
client = AsyncOpenAI()
class ContractExtraction(BaseModel):
document_name: str
document_type: str | None
agreement_name: str | None
parties: List[str]
agreement_date: str | None
effective_date: str | None
expiration_date: str | None
contract_amount: float | None
currency: str | None
payment_terms: str | None
governing_law: str | None
jurisdiction: str | None
signatories: List[str] | None
amendment_history: List[str] | None
contract_term_period: str | None
renewal_terms: str | None
summary: str
operative_clause: str
confidentiality: str | None
termination: str | None
indemnification: str | None
limitation_of_liability: str | None
intellectual_property: str | None
dispute_resolution: str | None
force_majeure: str | None
assignment: str | None
non_compete: str | None
non_solicitation: str | None
warranties: str | None
insurance: str | None
audit_rights: str | None
data_protection: str | None
compliance_with_laws: str | None
affiliate_license_licensee: str | None
anti_assignment: str | None
change_of_control: str | None
claims: str | None
competitive_restriction: str | None
covenant_not_to_sue: str | None
early_termination: str | None
engagement: str | None
entire_agreement: str | None
escrow: str | None
exclusivity: str | None
fees: str | None
ip_ownership: str | None
license_grant: str | None
liquidated_damages: str | None
minimum_commitment: str | None
payment_and_fees: str | None
price_restrictions: str | None
renewal_term: str | None
representations_and_warranties: str | None
scope_of_use: str | None
services: str | None
severability_clause: str | None
survival: str | None
taxes: str | None
term: str | None
termination_for_convenience: str | None
third_party_beneficiary: str | None
waiver: str | None
average_confidence: float
total_sections: int
class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException("API call timed out")
# Set the signal handler and a 10-minute alarm
signal.signal(signal.SIGALRM, timeout_handler)
def adjust_prompt_length(prompt, max_tokens=115000):
# Get the encoding
encoding = tiktoken.encoding_for_model("gpt-4o-mini")
# Count the number of tokens in the prompt
num_tokens = len(encoding.encode(prompt))
# If the number of tokens exceeds the maximum, reduce the prompt size
if num_tokens > max_tokens:
logger.warning(
f"Prompt exceeds maximum length of {max_tokens} tokens. Truncating..."
)
# Calculate the percentage to keep
keep_ratio = max_tokens / num_tokens
# Find the split point
split_point = int(len(prompt) * keep_ratio)
# Split the prompt
truncated_content = prompt[:split_point]
# Add the end of instructions
end_of_instructions = """
</document>
2. Analyze the document text thoroughly. Pay attention to key details such as names, dates, amounts, all clauses, all aliases, and specific clauses that match the fields in the schema.
3. Extract the required entities from the document text. Make sure to capture all relevant information for each field specified in the schema.
4. Check item by item. This is an educational project, if you fail, many children will not learn correctly.
5. Double-check your work:
- Verify that all extracted information accurately reflects the content of the document
- Ensure no required fields are left empty unless the information is genuinely not present in the document
Remember, accuracy and adherence to the schema are crucial. Do not add any fields or information not specified in the schema, and do not omit any required fields unless the information is absent from the document text."""
reduced_prompt = truncated_content + end_of_instructions
# Recalculate the number of tokens
new_num_tokens = len(encoding.encode(reduced_prompt))
logger.info(f"Reduced prompt from {num_tokens} to {new_num_tokens} tokens")
return reduced_prompt, new_num_tokens
else:
return prompt, num_tokens
def ner_extractor(content: str) -> str:
"""
Named Entity Recognition (NER) extractor for contracts.
Args:
content (str): The content of the contract's body.
Returns:
str: A prompt for the model to extract entities, including the contract body.
"""
logger.info("Generating NER extraction prompt")
contract_extraction_prompt = """You are a document entity extraction specialist. Your task is to carefully read a document, which most likely is a contract, extract specific information according to a provided schema, and present it in a structured JSON format. Follow these steps precisely:
1. Read the following document text carefully:
<document>
{CONTRACT_TEXT}
</document>
2. Analyze the document text thoroughly. Pay attention to key details such as names, dates, amounts, all clauses, all aliases, and specific clauses that match the fields in the schema.
3. Extract the required entities from the document text. Make sure to capture all relevant information for each field specified in the schema.
4. Check item by item. This is an educational project, if you fail, many children will not learn correctly.
5. Double-check your work:
- Verify that all extracted information accurately reflects the content of the document
- Ensure no required fields are left empty unless the information is genuinely not present in the document
Remember, accuracy and adherence to the schema are crucial. Do not add any fields or information not specified in the schema, and do not omit any required fields unless the information is absent from the document text.
"""
combined_prompt = contract_extraction_prompt.format(CONTRACT_TEXT=content)
adjusted_prompt, num_tokens = adjust_prompt_length(combined_prompt)
logger.debug(f"Generated prompt of length: {num_tokens} tokens")
return adjusted_prompt
def exponential_backoff(attempt):
delay = min(INITIAL_RETRY_DELAY * (2**attempt) + random.uniform(0, 1), 300)
logger.info(
f"Calculated backoff delay: {delay:.2f} seconds for attempt {attempt + 1}"
)
return delay
async def process_item(json_obj, i, outfile, prompt_file):
logger.info(f"Processing item {i+1}")
processed_obj = json_obj.copy()
if "body" in json_obj and isinstance(json_obj["body"], str):
logger.info(
f"Found 'body' field in JSON object for item {i+1}. Proceeding with extraction."
)
for attempt in range(MAX_RETRIES):
try:
prompt = ner_extractor(json_obj["body"])
logger.info(f"Generated NER extraction prompt for item {i+1}")
async with aiofiles.open(PROMPT_FILE, "a") as f:
await f.write(json.dumps({"prompt": prompt}) + "\n")
logger.info(f"Sending prompt to OpenAI API for item {i+1}")
try:
completion = await asyncio.wait_for(
client.beta.chat.completions.parse(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": "You are an expert at structured data extraction. You will be given unstructured text from a contract and should convert it into the given structure.",
},
{"role": "user", "content": prompt},
],
response_format=ContractExtraction,
),
timeout=600,
)
if completion and completion.choices[0].message.parsed:
logger.debug(
f"Received non-empty response from model for item {i+1}"
)
try:
extracted_data = completion.choices[0].message.parsed
processed_obj["processed_bodygpt"] = extracted_data.dict()
logger.info(
f"Successfully processed document {json_obj.get('accessionNo', 'Unknown')} (item {i+1})"
)
break # Success, exit retry loop
except Exception as e:
logger.error(
f"Failed to process parsed response for document {json_obj.get('accessionNo', 'Unknown')} (item {i+1}): {str(e)}"
)
else:
logger.warning(
f"Empty response from model for document {json_obj.get('accessionNo', 'Unknown')} (item {i+1})"
)
except asyncio.TimeoutError:
logger.error(f"API call timed out for item {i+1}")
processed_obj["processed_bodygpt"] = "ERROR! TIMEOUT!"
break # Exit retry loop on timeout
except Exception as e:
logger.error(
f"Attempt {attempt + 1} failed for document {json_obj.get('accessionNo', 'Unknown')} (item {i+1}): {str(e)}"
)
if attempt < MAX_RETRIES - 1:
delay = exponential_backoff(attempt)
logger.info(f"Retrying in {delay:.2f} seconds for item {i+1}...")
await asyncio.sleep(delay)
else:
logger.warning(
f"Max retries reached for document {json_obj.get('accessionNo', 'Unknown')} (item {i+1}). Passing."
)
processed_obj["processed_bodygpt"] = "ERROR! MAX RETRIES REACHED!"
else:
logger.warning(
f"No 'body' field found or 'body' is not a string for document {json_obj.get('accessionNo', 'Unknown')} (item {i+1})."
)
processed_obj["processed_bodygpt"] = "ERROR! NO BODY!"
logger.info(f"Writing processed object to output file for item {i+1}")
await outfile.write(json.dumps(processed_obj) + "\n")
await outfile.flush()
logger.debug(
f"Wrote processed object to output file for document {json_obj.get('accessionNo', 'Unknown')} (item {i+1})"
)
return i + 1
async def process_file(input_file: str, output_file: str, num_items: int = None):
logger.info(f"Starting to process file: {input_file}")
logger.info(f"Output will be written to: {output_file}")
async with aiofiles.open(
input_file, "r", encoding="utf-8"
) as infile, aiofiles.open(
output_file, "a", encoding="utf-8"
) as outfile, aiofiles.open(
STARTING_FILE, "a+", encoding="utf-8"
) as starting_file, aiofiles.open(
PROMPT_FILE, "a", encoding="utf-8"
) as prompt_file:
# Get the last processed item
await starting_file.seek(0)
last_processed = await starting_file.readlines()
last_item = (
int(last_processed[-1].strip()) if last_processed else STARTING_POINT
)
# Skip to the last processed item
for _ in range(last_item):
await infile.readline()
pbar = tqdm(total=num_items, initial=last_item, desc="Processing items")
async def worker(queue):
while True:
item = await queue.get()
if item is None:
break
i, line = item
try:
json_obj = json.loads(line)
last_processed = await process_item(
json_obj, i, outfile, prompt_file
)
await starting_file.write(f"{last_processed}\n")
await starting_file.flush()
pbar.update(1)
except json.JSONDecodeError:
logger.warning(
f"Error decoding JSON for item {i+1}. Skipping line."
)
finally:
queue.task_done()
queue = asyncio.Queue()
workers = [asyncio.create_task(worker(queue)) for _ in range(NO_WORKERS)]
i = last_item
async for line in infile:
if num_items is not None and i >= num_items:
logger.info(
f"Reached specified limit of {num_items} items. Stopping processing."
)
break
await queue.put((i, line))
i += 1
# Signal workers to exit
for _ in range(NO_WORKERS):
await queue.put(None)
# Wait for all workers to complete
await asyncio.gather(*workers)
logger.info("Finished processing file")
async def main():
try:
await process_file(INPUT_FILE, OUTPUT_FILE)
except Exception as e:
logger.error(f"An unexpected error occurred: {str(e)}")
finally:
logger.info("Script execution completed.")
if __name__ == "__main__":
asyncio.run(main())