irongit

Tools for Ozone roofing

ozone/main.py
81 lines2.2 KBPython
1import json
2import os
3import re
4
5from dotenv import load_dotenv
6from openai import OpenAI
7
8load_dotenv()
9JSON_REGEX = re.compile(r"```json(.*)```", re.DOTALL)
10
11# Get the pdf filenames
12pdf_files = []
13file_streams = []
14for root, dirs, files in os.walk(".data/leads"):
15 for file in files:
16 if file.endswith(".pdf"):
17 pdf_files.append(os.path.join(root, file))
18 file_streams.append(open(os.path.join(root, file), "rb"))
19
20# Create the assistant
21client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
22assistant = client.beta.assistants.create(
23 name="Lead Parser",
24 description="Parse leads from PDF files",
25 model="gpt-3.5-turbo",
26 tools=[{"type": "file_search"}],
27)
28
29# Ask for the leads
30file = file_streams[0]
31message_file = client.files.create(file=file, purpose="assistants")
32content = """
33Parse the leads from the PDF files into a json list in the following format.
34[
35 {
36 "todays_date": "",
37 "agent_name": "ANDREA",
38 "roofing_company": "CORE FOUR - AUSTIN",
39 "names": "BORAN ZHAO & TANIA BETANCOURT",
40 "appointment_date": "4/18/24",
41 "time": "2PM",
42 "phone": "979-218-4997",
43 "email": "TANIA@TXSTATE.EDU",
44 "address": "524 CARISMATIC LN",
45 "city": "AUSTIN",
46 "state": "TX",
47 "zip_code": "78748",
48 "additional_address": "",
49 "insurance_provider": "METROPOLITAN/FARMERS",
50 "age_of_roof": "3 years",
51 "animals_in_yard": "Yes",
52 "last_roof_inspection": "",
53 "notes": "",
54 "contact_number": "303-908-3193"
55 }
56]
57"""
58thread = client.beta.threads.create(
59 messages=[
60 {
61 "role": "user",
62 "content": content,
63 "attachments": [
64 {"file_id": message_file.id, "tools": [{"type": "file_search"}]}
65 ],
66 }
67 ]
68)
69run = client.beta.threads.runs.create_and_poll(
70 thread_id=thread.id, assistant_id=assistant.id
71)
72messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
73message_content = messages[0].content[0].text.value
74json_match = JSON_REGEX.search(message_content)
75if json_match:
76 json_content = json_match.group(1)
77 parsed_json = json.loads(json_content)
78 print(json.dumps(parsed_json, indent=2))
79else:
80 print("No JSON content found.")
81 print(message_content)