33from openai import AzureOpenAI
44from datetime import datetime
55
6- # Configuration
6+ # Configuration - Replace with your actual credentials
77endpoint = "https://ago.openai.azure.com"
88model_name = "gpt-4o"
99deployment = "gpt-4o"
@@ -30,33 +30,27 @@ def process_energy_file(file_path):
3030 try :
3131 with open (file_path , 'r' ) as f :
3232 content = f .read ()
33-
34- # Parse JSON with schema validation
3533 bill_data = json .loads (content )
3634
3735 if not isinstance (bill_data , dict ):
3836 raise ValueError ("Invalid JSON structure - expected dictionary" )
3937
40- # Extract required fields
4138 fields = bill_data .get ("bill_structure" , {}).get ("fields" , {})
4239 if "total_kWh" not in fields :
4340 raise ValueError ("Missing required total_kWh field" )
4441
45- # Convert string values to actual numbers
4642 try :
4743 kwh = float (fields ["total_kWh" ])
4844 except (ValueError , TypeError ):
4945 raise ValueError ("total_kWh must be a numeric value" )
5046
51- # Extract additional context
5247 metadata = {
5348 'tariff_type' : fields .get ("tariff_type" , "Unknown" ),
5449 'location_type' : fields .get ("location_type" , "Unknown" ),
5550 'billing_period' : fields .get ("billing_period" , "Unknown" ),
5651 'source' : bill_data .get ("source" , "Unknown provider" )
5752 }
5853
59- # Get interpretation rules
6054 rules = bill_data .get ("interpretation_rules" , {})
6155
6256 return {
@@ -74,10 +68,9 @@ def process_energy_file(file_path):
7468 except Exception as e :
7569 print (f"File processing error: { str (e )} " )
7670 return None
77- # ... [previous code remains the same] ...
7871
7972def generate_response (user_prompt ):
80- """Generate AI response with error handling and content filtering """
73+ """Generate AI response with error handling"""
8174 try :
8275 response = client .chat .completions .create (
8376 model = deployment ,
@@ -89,11 +82,32 @@ def generate_response(user_prompt):
8982 max_tokens = 1000
9083 )
9184 return response .choices [0 ].message .content
92-
9385 except Exception as e :
9486 print (f"API Error: { str (e )} " )
9587 return CONTENT_FILTER_MESSAGE
9688
89+ def handle_sustainability_goal ():
90+ """Configure sustainability scope with validation"""
91+ global current_scope
92+ print ("Available Scopes:" )
93+ print ("1. Scope 1 (Direct emissions)" )
94+ print ("2. Scope 2 (Electricity indirect emissions)" )
95+ print ("3. Scope 3 (Value chain emissions)" )
96+
97+ choice = input ("Select scope (1-3): " ).strip ()
98+ scope_map = {
99+ "1" : "Scope 1 - Direct Emissions" ,
100+ "2" : "Scope 2 - Electricity Indirect" ,
101+ "3" : "Scope 3 - Value Chain"
102+ }
103+
104+ if choice in scope_map :
105+ current_scope = scope_map [choice ]
106+ return f"Scope set to: { current_scope } "
107+ else :
108+ current_scope = "Not Specified"
109+ return "Invalid scope selection - using default analysis"
110+
97111def handle_energy_bill ():
98112 """Enhanced energy analysis for Egyptian bills"""
99113 global bill_analysis
@@ -107,7 +121,6 @@ def handle_energy_bill():
107121 if not bill_data :
108122 return "Failed to process file. Ensure it matches Egyptian EEHC format."
109123
110- # Build context-aware prompt
111124 context = f"""
112125 Egyptian Energy Bill Context:
113126 - Provider: { bill_data ['metadata' ]['source' ]}
@@ -136,28 +149,9 @@ def handle_energy_bill():
136149 4. Flag any usage anomalies per EEHC standards
137150
138151 Format with markdown tables and Egyptian-specific examples."""
139- bill_analysis = generate_response (analysis_prompt ) # Now properly defined return bill_analysis if bill_analysis != CONTENT_FILTER_MESSAGE else "Analysis blocked"def handle_sustainability_goal():
140- """Configure sustainability scope with validation"""
141- global current_scope
142- print ("Available Scopes:" )
143- print ("1. Scope 1 (Direct emissions)" )
144- print ("2. Scope 2 (Electricity indirect emissions)" )
145- print ("3. Scope 3 (Value chain emissions)" )
146-
147- choice = input ("Select scope (1-3): " ).strip ()
148- scope_map = {
149- "1" : "Scope 1 - Direct Emissions" ,
150- "2" : "Scope 2 - Electricity Indirect" ,
151- "3" : "Scope 3 - Value Chain"
152- }
153-
154- if choice in scope_map :
155- current_scope = scope_map [choice ]
156- return f"Scope set to: { current_scope } "
157- else :
158- current_scope = "Not Specified"
159- return "Invalid scope selection - using default analysis"
160152
153+ bill_analysis = generate_response (analysis_prompt )
154+ return bill_analysis if bill_analysis != CONTENT_FILTER_MESSAGE else "Analysis blocked"
161155
162156def handle_recommendations ():
163157 """Recommendation generation"""
@@ -174,9 +168,6 @@ def handle_recommendations():
174168 response = generate_response (recommendation_prompt )
175169 return response if response != CONTENT_FILTER_MESSAGE else "Recommendation blocked"
176170
177-
178-
179-
180171def display_menu ():
181172 """Display main menu and get user choice"""
182173 print ("\n Main Menu:" )
@@ -190,7 +181,7 @@ def main():
190181 print ("Welcome to Carbon Footprint Analysis" )
191182 print (f"AI Model: { model_name } | API: { api_version } " )
192183 while True :
193- choice = display_menu () # Now properly defined
184+ choice = display_menu ()
194185 if choice == "1" :
195186 print ("\n === Scope Configuration ===" )
196187 print (handle_sustainability_goal ())
0 commit comments