import openai import json from dotenv import load_dotenv import os def handle_creating_json(output_1): print("Before GPT: " , output_1) #output_1= [{'invoice_number': '349136', 'table_data': [{'label': 'Product_Code', 'text': ''}, {'label': 'Description', 'text': '1ST FLOOR WALLS'}, {'label': 'Price', 'text': ''}, {'label': 'Line_Amount', 'text': ''}, {'label': 'Product_Code', 'text': 'CPL1216'}, {'label': 'Description', 'text': "11.875 X 16 ' Pro Lam 2.0 LVL 1.75 ( 7 @ 16 ' , 4 @\n8 ' )"}, {'label': 'Price', 'text': '139.09'}, {'label': 'Line_Amount', 'text': '1,251.81'}, {'label': 'Product_Code', 'text': 'CPL1210'}, {'label': 'Description', 'text': "COLUMN\n11.875 X 10 ' Pro Lam 2.0 LVL 1.75"}, {'label': 'Price', 'text': '87.56'}, {'label': 'Line_Amount', 'text': '525.36'}, {'label': 'Product_Code', 'text': 'CPCB35558'}, {'label': 'Description', 'text': "Power Column 3 1/2 X 5 1/2 - 08 '"}, {'label': 'Price', 'text': '82.51'}, {'label': 'Line_Amount', 'text': '330.04'}]}] load_dotenv() # Initialize OpenAI with your API key openai.api_key = os.getenv("OPENAI_API_KEY") prompt = f""" You are an excellent programmer and specialize in the construction industry, knowing everything about building a house. Given a JSON which resembles a table, you have two tasks: 1. extract product description and determine or predict whether product descriptions is Exterior Door/Finish/ Framing/Siding/Windows or Roofing. Think well and do some self reflection. Do not share your thought process with me though. 2. Once you have thought through, produce a json, easily convertible to a dataframe in python, which would contain invoice number, product description, predicted material, confidence ( b/w 0-1, your confidence score which shows how sure are you about your prediction) Remember: You just have to share the output json, no thought process or extra words or anything else. If you are not able to identify the invoice number just write NA. No apologies or regret. Always produce an output. Here is the json: {json.dumps(output_1)} """ messages=[{"role": "user", "content":prompt}] # Use OpenAI to generate a completion using GPT-4 (replace 'gpt-4.0-turbo' with the correct engine ID once available) response = openai.ChatCompletion.create( model="gpt-4", max_tokens=5000, temperature=0, messages = messages ) # Extracting the result result = response.choices[0]["message"]["content"] print("After gpt") print(json.loads(result)) return json.loads(result)