leninqwerty03@gmail.com commited on
Commit
3bff3ea
·
1 Parent(s): 6d61216

Add LlamaIndex integration for enhanced environmental knowledge retrieval and update requirements

Browse files
Files changed (3) hide show
  1. app.py +17 -36
  2. llamaindex_rag.py +675 -0
  3. requirements.txt +4 -1
app.py CHANGED
@@ -9,11 +9,13 @@ import io
9
  import base64
10
  from dotenv import load_dotenv
11
  from mcpserver import MCPOrchestrator
 
12
 
13
  load_dotenv()
14
  # Initialize Anthropic client and MCP Orchestrator
15
  client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
16
  mcp = MCPOrchestrator(api_key=os.environ.get("ANTHROPIC_API_KEY"))
 
17
 
18
  # ===== AUTONOMOUS AGENT SYSTEM =====
19
  class EcoAgent:
@@ -90,44 +92,14 @@ Provide a concise reasoning summary (3-4 sentences)."""
90
 
91
  # ===== RAG COMPONENT: RETRIEVAL AUGMENTED GENERATION =====
92
  def retrieve_environmental_knowledge(product_name: str, reasoning: str) -> str:
93
- """RAG: Retrieve relevant environmental knowledge and context"""
94
 
95
- # Knowledge base (in production, this would be a vector database)
96
- knowledge_base = {
97
- "electronics": {
98
- "keywords": ["phone", "laptop", "headphones", "computer", "tablet", "device", "earbuds", "speaker"],
99
- "context": "E-waste is growing 5x faster than recycling. Contains rare earth metals (lithium, cobalt, neodymium). Manufacturing accounts for 80% of carbon footprint. Conflict minerals common. Planned obsolescence drives waste."
100
- },
101
- "plastics": {
102
- "keywords": ["bottle", "plastic", "packaging", "container", "bag"],
103
- "context": "Only 9% of plastic is recycled globally. Takes 450+ years to decompose. Microplastics enter food chain. 8M tons enter oceans yearly. Virgin plastic production = high carbon footprint."
104
- },
105
- "textiles": {
106
- "keywords": ["shirt", "clothing", "fabric", "apparel", "shoes", "leather"],
107
- "context": "Fashion industry = 10% global carbon emissions. Water intensive (2,700L per cotton shirt). Textile waste fills landfills. Synthetic fabrics shed microplastics. Dyeing causes water pollution."
108
- },
109
- "food": {
110
- "keywords": ["beef", "meat", "food", "avocado", "coffee"],
111
- "context": "Food system = 26% global emissions. Animal agriculture drives deforestation. Meat production = high water/land use. Food waste = 8% global emissions. Transportation impacts vary."
112
- },
113
- "vehicles": {
114
- "keywords": ["car", "vehicle", "electric", "transport"],
115
- "context": "Transportation = 27% US emissions. EV battery production energy-intensive. Lifetime emissions lower for EVs. Manufacturing accounts for 15-20% of vehicle emissions. Public transport most efficient."
116
- }
117
- }
118
-
119
- # Retrieve relevant context based on keywords
120
- product_lower = product_name.lower()
121
- retrieved_context = []
122
-
123
- for category, data in knowledge_base.items():
124
- if any(keyword in product_lower for keyword in data["keywords"]):
125
- retrieved_context.append(f"**{category.title()} Context:** {data['context']}")
126
 
127
- if not retrieved_context:
128
- retrieved_context.append("**General Context:** Consider full lifecycle: extraction, manufacturing, use, disposal.")
129
 
130
- return "\n\n".join(retrieved_context)
131
 
132
  # ===== IMPROVED MCP INTEGRATION: VISION ANALYSIS =====
133
  def analyze_image_with_mcp(image) -> tuple:
@@ -227,6 +199,7 @@ def mcp_lifecycle_assessment(product_name: str, product_data: Dict) -> Dict:
227
  except Exception as e:
228
  return {"status": "error", "error": str(e)}
229
 
 
230
  # ===== PHASE 3: EXECUTION WITH CONTEXT ENGINEERING =====
231
  def execute_assessment(agent: EcoAgent, product_name: str, vision_data: Dict = None) -> Dict:
232
  """PHASE 3: EXECUTION - Agent executes assessment with RAG and MCP"""
@@ -292,8 +265,16 @@ Using ALL the above context, provide a comprehensive assessment in this EXACT JS
292
  IMPORTANT:
293
  - name MUST be: {product_name}
294
  - eco_score: realistic number 1-10 for THIS SPECIFIC product
 
295
  - Be specific and accurate based on the context provided
296
- - Return ONLY valid JSON, no extra text."""
 
 
 
 
 
 
 
297
 
298
  try:
299
  message = client.messages.create(
 
9
  import base64
10
  from dotenv import load_dotenv
11
  from mcpserver import MCPOrchestrator
12
+ from llamaindex_rag import LlamaIndexEnvironmentalRAG
13
 
14
  load_dotenv()
15
  # Initialize Anthropic client and MCP Orchestrator
16
  client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
17
  mcp = MCPOrchestrator(api_key=os.environ.get("ANTHROPIC_API_KEY"))
18
+ llamaindex_rag = LlamaIndexEnvironmentalRAG()
19
 
20
  # ===== AUTONOMOUS AGENT SYSTEM =====
21
  class EcoAgent:
 
92
 
93
  # ===== RAG COMPONENT: RETRIEVAL AUGMENTED GENERATION =====
94
  def retrieve_environmental_knowledge(product_name: str, reasoning: str) -> str:
95
+ """RAG: Retrieve relevant environmental knowledge using LlamaIndex semantic search"""
96
 
97
+ print(f"🔍 LlamaIndex searching for: {product_name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ # Use LlamaIndex for advanced semantic search
100
+ knowledge = llamaindex_rag.retrieve_knowledge(product_name, top_k=2)
101
 
102
+ return f"**LlamaIndex Retrieved Knowledge:**\n\n{knowledge}"
103
 
104
  # ===== IMPROVED MCP INTEGRATION: VISION ANALYSIS =====
105
  def analyze_image_with_mcp(image) -> tuple:
 
199
  except Exception as e:
200
  return {"status": "error", "error": str(e)}
201
 
202
+ # ===== PHASE 3: EXECUTION WITH CONTEXT ENGINEERING =====
203
  # ===== PHASE 3: EXECUTION WITH CONTEXT ENGINEERING =====
204
  def execute_assessment(agent: EcoAgent, product_name: str, vision_data: Dict = None) -> Dict:
205
  """PHASE 3: EXECUTION - Agent executes assessment with RAG and MCP"""
 
265
  IMPORTANT:
266
  - name MUST be: {product_name}
267
  - eco_score: realistic number 1-10 for THIS SPECIFIC product
268
+ - alternative: Recommend ONLY NEW sustainable products (NOT second-hand, vintage, used, refurbished, or pre-owned items). Focus on eco-friendly materials, sustainable manufacturing, certifications, or innovative green technologies
269
  - Be specific and accurate based on the context provided
270
+ - Return ONLY valid JSON, no extra text.
271
+
272
+ Examples of good alternatives:
273
+ - For shoes: "Vegan leather shoes made from recycled materials with score (8.5/10) - Zero animal products and 70% lower carbon footprint"
274
+ - For electronics: "Energy Star certified laptop with modular design with score (7.8/10) - 50% longer lifespan and easy repair"
275
+ - For clothing: "Organic cotton t-shirt with Fair Trade certification with score (8.2/10) - Pesticide-free farming and ethical production"
276
+
277
+ DO NOT recommend: second-hand, vintage, used, refurbished, pre-owned, thrift store items."""
278
 
279
  try:
280
  message = client.messages.create(
llamaindex_rag.py ADDED
@@ -0,0 +1,675 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ LlamaIndex-powered RAG for Environmental Knowledge Retrieval
3
+ Enhanced with 15 comprehensive product categories
4
+ """
5
+
6
+ import os
7
+ from llama_index.core import (
8
+ VectorStoreIndex,
9
+ Document,
10
+ Settings,
11
+ StorageContext,
12
+ load_index_from_storage
13
+ )
14
+ from llama_index.llms.anthropic import Anthropic
15
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
16
+ from typing import List, Dict
17
+
18
+ class LlamaIndexEnvironmentalRAG:
19
+ """Advanced RAG using LlamaIndex for environmental knowledge"""
20
+
21
+ def __init__(self):
22
+ self.index = None
23
+
24
+ # Configure LlamaIndex settings
25
+ Settings.llm = Anthropic(
26
+ model="claude-sonnet-4-20250514",
27
+ api_key=os.environ.get("ANTHROPIC_API_KEY")
28
+ )
29
+ Settings.embed_model = HuggingFaceEmbedding(
30
+ model_name="BAAI/bge-small-en-v1.5"
31
+ )
32
+
33
+ self._initialize_index()
34
+
35
+ def _initialize_index(self):
36
+ """Initialize or load vector index"""
37
+ persist_dir = "./storage"
38
+
39
+ # Try to load existing index
40
+ if os.path.exists(persist_dir):
41
+ try:
42
+ storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
43
+ self.index = load_index_from_storage(storage_context)
44
+ print("✅ Loaded existing LlamaIndex from storage")
45
+ return
46
+ except:
47
+ print("⚠️ Could not load existing index, creating new one...")
48
+
49
+ # Create new index with 15-category environmental knowledge
50
+ documents = self._create_comprehensive_knowledge_base()
51
+
52
+ print("🔄 Building LlamaIndex vector store...")
53
+ self.index = VectorStoreIndex.from_documents(documents)
54
+
55
+ # Persist index
56
+ self.index.storage_context.persist(persist_dir=persist_dir)
57
+ print("✅ Created and persisted LlamaIndex with 15 categories")
58
+
59
+ def _create_comprehensive_knowledge_base(self) -> List[Document]:
60
+ """Create comprehensive 15-category environmental knowledge base"""
61
+
62
+ knowledge_docs = [
63
+ # Category 1: Electronics
64
+ Document(
65
+ text="""
66
+ Electronics and E-Waste Environmental Impact
67
+
68
+ E-waste is the fastest-growing waste stream globally, expanding 5x faster than recycling capacity.
69
+ Electronic devices contain rare earth metals: lithium, cobalt, neodymium, tantalum, and gold.
70
+ Manufacturing accounts for 80% of total carbon footprint for most electronic devices.
71
+ Conflict minerals sourcing causes human rights violations and environmental destruction in mining regions.
72
+ Planned obsolescence deliberately shortens device lifespan, driving unnecessary waste.
73
+
74
+ Product Examples: smartphones, laptops, headphones, tablets, computers, smart devices, earbuds, speakers, monitors, TVs, cameras
75
+
76
+ Carbon Footprint Data:
77
+ - Smartphone: 70-80 kg CO2e (manufacturing phase)
78
+ - Laptop: 300-400 kg CO2e (full lifecycle)
79
+ - Wireless headphones: 15-20 kg CO2e
80
+ - Desktop computer: 500 kg CO2e
81
+ - Smart TV: 300 kg CO2e
82
+
83
+ Environmental Issues:
84
+ - Toxic materials (lead, mercury, cadmium, brominated flame retardants)
85
+ - Energy-intensive mining operations causing habitat destruction
86
+ - Global recycling rate only 20%
87
+ - Average smartphone replaced every 2 years
88
+ - E-waste exports to developing countries
89
+
90
+ Sustainable Alternatives:
91
+ - Refurbished devices (90% lower carbon footprint)
92
+ - Modular phones like Fairphone (easily repairable)
93
+ - Long-lasting, Right-to-Repair certified electronics
94
+ - Electronics leasing and take-back programs
95
+ - Energy Star certified devices
96
+ """,
97
+ metadata={"category": "electronics", "impact_level": "critical", "coverage": "comprehensive"}
98
+ ),
99
+
100
+ # Category 2: Plastics
101
+ Document(
102
+ text="""
103
+ Plastics Environmental Impact and Pollution Crisis
104
+
105
+ Only 9% of all plastic ever produced has been recycled globally - a recycling failure.
106
+ Plastics take 450+ years to decompose, breaking into harmful microplastics.
107
+ Microplastics have entered the entire food chain, found in human blood and organs.
108
+ 8 million tons of plastic waste enter oceans every year, forming massive garbage patches.
109
+ Virgin plastic production from petroleum creates massive carbon emissions.
110
+
111
+ Product Examples: water bottles, plastic bags, food containers, packaging materials, straws, cups, cutlery, wrap
112
+
113
+ Carbon Footprint Data:
114
+ - Single plastic bottle: 82g CO2e
115
+ - Plastic grocery bag: 10g CO2e
116
+ - Plastic food container: 150g CO2e
117
+ - Styrofoam cup: 25g CO2e
118
+ - Plastic wrap (per meter): 5g CO2e
119
+
120
+ Environmental Issues:
121
+ - Ocean pollution causing marine life deaths (1 million seabirds, 100,000 marine mammals annually)
122
+ - Microplastic contamination in drinking water (93% of bottled water contains microplastics)
123
+ - Non-biodegradable waste accumulating in landfills for centuries
124
+ - Petroleum extraction and refining for plastic production
125
+ - Toxic chemical leaching into soil and water
126
+
127
+ Sustainable Alternatives:
128
+ - Reusable stainless steel bottles (saves 30kg CO2e per year)
129
+ - Glass containers (infinitely recyclable)
130
+ - Biodegradable materials (PLA from corn starch)
131
+ - Zero-waste refill programs
132
+ - Beeswax wraps instead of plastic wrap
133
+ """,
134
+ metadata={"category": "plastics", "impact_level": "critical", "urgency": "immediate"}
135
+ ),
136
+
137
+ # Category 3: Textiles
138
+ Document(
139
+ text="""
140
+ Textiles and Fashion Industry Environmental Impact
141
+
142
+ Fashion industry accounts for 10% of global carbon emissions, more than aviation and shipping combined.
143
+ Water intensive: 2,700 liters needed for one cotton t-shirt (drinking water for 1 person for 2.5 years).
144
+ Textile waste fills landfills with 92 million tons annually, most from fast fashion.
145
+ Synthetic fabrics like polyester shed microplastics into water with every wash.
146
+ Textile dyeing is the second-largest water polluter globally after agriculture.
147
+
148
+ Product Examples: clothing, shirts, jeans, shoes, leather goods, fast fashion items, dresses, jackets, socks, sportswear
149
+
150
+ Carbon Footprint Data:
151
+ - Cotton t-shirt: 7 kg CO2e
152
+ - Pair of jeans: 33 kg CO2e
153
+ - Running shoes/sneakers: 14 kg CO2e
154
+ - Leather shoes: 30 kg CO2e
155
+ - Winter coat: 50 kg CO2e
156
+ - Dress: 20 kg CO2e
157
+
158
+ Environmental Issues:
159
+ - Fast fashion waste (average garment worn only 7 times before disposal)
160
+ - Water pollution from toxic dyes contaminating rivers
161
+ - Microplastic shedding from polyester (500,000 tons per year into oceans)
162
+ - Unethical labor practices in garment factories
163
+ - Cotton farming using 25% of world's pesticides
164
+
165
+ Sustainable Alternatives:
166
+ - Organic cotton (40% lower carbon footprint, no pesticides)
167
+ - Recycled materials and upcycled fashion
168
+ - Second-hand and vintage clothing
169
+ - Slow fashion brands (Patagonia, Eileen Fisher)
170
+ - Clothing rental and sharing services
171
+ - Hemp and bamboo fabrics
172
+ """,
173
+ metadata={"category": "textiles", "impact_level": "high", "water_intensive": True}
174
+ ),
175
+
176
+ # Category 4: Food & Agriculture
177
+ Document(
178
+ text="""
179
+ Food System and Agriculture Environmental Impact
180
+
181
+ Food system accounts for 26% of global greenhouse gas emissions.
182
+ Animal agriculture drives 80% of Amazon rainforest deforestation.
183
+ Meat production requires massive water and land use compared to plant-based alternatives.
184
+ Food waste generates 8% of global emissions - if food waste were a country, it would be 3rd largest emitter.
185
+ Transportation impact varies dramatically by product type and distance (food miles).
186
+
187
+ Product Examples: beef, meat products, dairy, cheese, chicken, fish, coffee, avocados, processed foods, vegetables
188
+
189
+ Carbon Footprint Data:
190
+ - Beef (1kg): 60 kg CO2e
191
+ - Lamb (1kg): 24 kg CO2e
192
+ - Cheese (1kg): 21 kg CO2e
193
+ - Pork (1kg): 7 kg CO2e
194
+ - Chicken (1kg): 6 kg CO2e
195
+ - Plant-based burger: 2 kg CO2e
196
+ - Coffee (1kg beans): 15 kg CO2e
197
+ - Avocado (1kg): 0.8 kg CO2e
198
+
199
+ Environmental Issues:
200
+ - Methane emissions from cattle (28x more potent than CO2)
201
+ - Deforestation for agriculture and grazing land
202
+ - Water scarcity in agricultural regions
203
+ - Pesticide and fertilizer pollution
204
+ - Food miles and cold chain transportation
205
+ - Fishing industry overfishing and bycatch
206
+
207
+ Sustainable Alternatives:
208
+ - Plant-based proteins (90% lower carbon footprint than beef)
209
+ - Local and seasonal produce (reduces transport emissions)
210
+ - Regenerative agriculture practices
211
+ - Reduced food waste through meal planning
212
+ - Sustainable fishing certifications (MSC, ASC)
213
+ - Organic farming methods
214
+ """,
215
+ metadata={"category": "food", "impact_level": "critical", "deforestation_driver": True}
216
+ ),
217
+
218
+ # Category 5: Vehicles & Transportation
219
+ Document(
220
+ text="""
221
+ Vehicles and Transportation Environmental Impact
222
+
223
+ Transportation accounts for 27% of US greenhouse gas emissions, 14% globally.
224
+ EV battery production is energy-intensive but lifetime emissions are 50-70% lower than ICE vehicles.
225
+ Manufacturing accounts for 15-20% of total vehicle lifetime emissions.
226
+ Public transportation is the most efficient option per passenger mile.
227
+ Aviation contributes 2.5% of global CO2 emissions but growing rapidly.
228
+
229
+ Product Examples: cars, electric vehicles, bikes, e-bikes, scooters, motorcycles, buses, trains
230
+
231
+ Carbon Footprint Data:
232
+ - ICE Car (lifetime 150,000 miles): 35,000 kg CO2e
233
+ - Electric Car (lifetime, grid mix): 24,000 kg CO2e
234
+ - Electric Car (renewable energy): 15,000 kg CO2e
235
+ - E-bike (lifetime): 500 kg CO2e
236
+ - Bicycle (manufacturing): 50 kg CO2e
237
+ - Motorcycle (lifetime): 20,000 kg CO2e
238
+
239
+ Environmental Issues:
240
+ - Fossil fuel dependence and air pollution
241
+ - Lithium and cobalt mining for EV batteries
242
+ - Urban air quality deterioration
243
+ - Infrastructure emissions (roads, parking)
244
+ - Resource depletion for manufacturing
245
+
246
+ Sustainable Alternatives:
247
+ - Electric vehicles powered by renewable energy
248
+ - Bicycles and e-bikes for short trips
249
+ - Public transportation (bus, train, metro)
250
+ - Car-sharing and ride-sharing services
251
+ - Walking for ultra-short distances
252
+ - Hybrid vehicles as transition technology
253
+ """,
254
+ metadata={"category": "vehicles", "impact_level": "high", "ev_better": True}
255
+ ),
256
+
257
+ # Category 6: Home & Furniture
258
+ Document(
259
+ text="""
260
+ Home Furniture and Appliances Environmental Impact
261
+
262
+ Furniture production drives deforestation, especially for tropical hardwoods.
263
+ VOC emissions from paints, finishes, and adhesives harm indoor air quality.
264
+ Fast furniture culture creates massive landfill waste (similar to fast fashion).
265
+ Appliances contain refrigerants that are potent greenhouse gases (HFCs).
266
+ Average sofa produces 90kg CO2e during manufacturing.
267
+
268
+ Product Examples: sofas, tables, chairs, mattresses, beds, desks, shelves, cabinets, refrigerators, washing machines
269
+
270
+ Carbon Footprint Data:
271
+ - Sofa: 90 kg CO2e
272
+ - Wooden table: 40 kg CO2e
273
+ - Mattress: 100 kg CO2e
274
+ - Office chair: 30 kg CO2e
275
+ - Refrigerator: 200 kg CO2e
276
+ - Washing machine: 150 kg CO2e
277
+
278
+ Environmental Issues:
279
+ - Deforestation for lumber (illegal logging in rainforests)
280
+ - Formaldehyde and VOC off-gassing
281
+ - Landfill waste from disposable furniture
282
+ - HFC refrigerants (1,000-3,000x more potent than CO2)
283
+ - Energy consumption of appliances
284
+
285
+ Sustainable Alternatives:
286
+ - Certified sustainable wood (FSC, SFI)
287
+ - Second-hand and vintage furniture
288
+ - Energy Star rated appliances
289
+ - Natural latex mattresses
290
+ - Modular and repairable furniture
291
+ - Low-VOC paints and finishes
292
+ """,
293
+ metadata={"category": "home_furniture", "impact_level": "moderate", "voc_emissions": True}
294
+ ),
295
+
296
+ # Category 7: Personal Care & Cosmetics
297
+ Document(
298
+ text="""
299
+ Personal Care and Cosmetics Industry Environmental Impact
300
+
301
+ Beauty industry produces 120 billion packaging units per year, mostly plastic.
302
+ Microplastics in products (microbeads) enter oceans and food chain.
303
+ Palm oil in cosmetics drives rainforest deforestation in Southeast Asia.
304
+ Chemical pollution from production affects waterways globally.
305
+ Single-use packaging dominates the industry.
306
+
307
+ Product Examples: shampoo, soap, cosmetics, makeup, skincare, deodorant, toothpaste, lotion, perfume, sunscreen
308
+
309
+ Carbon Footprint Data:
310
+ - Shampoo bottle: 200g CO2e
311
+ - Makeup palette: 150g CO2e
312
+ - Deodorant: 100g CO2e
313
+ - Toothpaste tube: 80g CO2e
314
+ - Perfume: 300g CO2e
315
+ - Sunscreen: 180g CO2e
316
+
317
+ Environmental Issues:
318
+ - 120 billion plastic packaging units annually
319
+ - Microbeads in scrubs (banned in many countries)
320
+ - Palm oil plantations replacing rainforests
321
+ - Chemical runoff polluting waterways
322
+ - Animal testing in some markets
323
+ - Single-use sample sachets
324
+
325
+ Sustainable Alternatives:
326
+ - Solid bars (shampoo, soap) with no packaging
327
+ - Refillable containers and bulk buying
328
+ - Natural and organic ingredients
329
+ - Cruelty-free and vegan products
330
+ - Biodegradable packaging
331
+ - Zero-waste brands (Lush, Ethique)
332
+ """,
333
+ metadata={"category": "personal_care", "impact_level": "moderate", "plastic_intensive": True}
334
+ ),
335
+
336
+ # Category 8: Cleaning Products
337
+ Document(
338
+ text="""
339
+ Cleaning Products Environmental Impact
340
+
341
+ Household cleaners release volatile organic compounds (VOCs) affecting air quality.
342
+ Phosphates in detergents cause water eutrophication and algal blooms.
343
+ Plastic bottles account for 8 billion units per year in cleaning products alone.
344
+ Chemical production for cleaners is energy-intensive with toxic byproducts.
345
+ Concentrated formulas can reduce emissions by 50% through smaller packaging.
346
+
347
+ Product Examples: laundry detergent, dish soap, surface cleaners, bleach, disinfectant, glass cleaner, floor cleaner
348
+
349
+ Carbon Footprint Data:
350
+ - Laundry detergent (3L): 500g CO2e
351
+ - Dish soap: 200g CO2e
352
+ - All-purpose cleaner: 300g CO2e
353
+ - Bleach: 400g CO2e
354
+ - Glass cleaner: 150g CO2e
355
+
356
+ Environmental Issues:
357
+ - VOC emissions affecting indoor and outdoor air quality
358
+ - Phosphate pollution causing dead zones in waterways
359
+ - Plastic bottle waste (mostly non-recycled)
360
+ - Toxic chemical production processes
361
+ - Antimicrobial resistance from overuse of disinfectants
362
+
363
+ Sustainable Alternatives:
364
+ - Concentrated formulas (use less packaging)
365
+ - Refill stations and bulk buying
366
+ - DIY cleaners (vinegar, baking soda, castile soap)
367
+ - Plant-based ingredients
368
+ - Recyclable or compostable packaging
369
+ - Seventh Generation, Method brands
370
+ """,
371
+ metadata={"category": "cleaning_products", "impact_level": "moderate", "water_pollution": True}
372
+ ),
373
+
374
+ # Category 9: Toys & Games
375
+ Document(
376
+ text="""
377
+ Toys and Gaming Environmental Impact
378
+
379
+ 80% of toys are made from plastic, contributing to waste crisis.
380
+ Average child in US receives 70 new toys per year.
381
+ Short toy lifespan due to trends and breakage creates high waste.
382
+ Gaming consoles and electronics become e-waste quickly.
383
+ Sports equipment often made from single materials difficult to recycle.
384
+
385
+ Product Examples: action figures, dolls, LEGO, puzzles, board games, video game consoles, sports equipment
386
+
387
+ Carbon Footprint Data:
388
+ - Plastic toy (average): 200g CO2e
389
+ - LEGO set (1kg): 2 kg CO2e
390
+ - Board game: 500g CO2e
391
+ - Gaming console: 50 kg CO2e
392
+ - Sports ball: 300g CO2e
393
+ - Bicycle toy: 1 kg CO2e
394
+
395
+ Environmental Issues:
396
+ - Plastic waste accumulation (90% of toys)
397
+ - Trend-driven consumption (movie tie-ins)
398
+ - E-waste from electronic toys and consoles
399
+ - Toxic materials (phthalates, BPA)
400
+ - Difficult disassembly for recycling
401
+
402
+ Sustainable Alternatives:
403
+ - Wooden toys from sustainable sources
404
+ - Second-hand and toy libraries
405
+ - Durable, timeless designs
406
+ - LEGO take-back programs
407
+ - Games-as-a-service (digital instead of physical)
408
+ - Sports equipment made from recycled materials
409
+ """,
410
+ metadata={"category": "toys_games", "impact_level": "moderate", "plastic_dominant": True}
411
+ ),
412
+
413
+ # Category 10: Paper Products & Books
414
+ Document(
415
+ text="""
416
+ Paper Products and Publishing Environmental Impact
417
+
418
+ Paper production accounts for 26% of landfill waste globally.
419
+ Deforestation for virgin pulp affects biodiversity and carbon sinks.
420
+ Recycled paper uses 70% less energy than virgin paper production.
421
+ Printing and transportation add significant emissions.
422
+ Digital alternatives can reduce carbon footprint by 90%.
423
+
424
+ Product Examples: books, notebooks, paper, cardboard packaging, magazines, newspapers, tissue, office supplies
425
+
426
+ Carbon Footprint Data:
427
+ - Paperback book: 1 kg CO2e
428
+ - Notebook (100 pages): 500g CO2e
429
+ - Cardboard box: 300g CO2e
430
+ - Magazine: 200g CO2e
431
+ - Tissue box: 150g CO2e
432
+ - Office paper (ream): 5 kg CO2e
433
+
434
+ Environmental Issues:
435
+ - Deforestation (42% of wood harvest for paper)
436
+ - Energy-intensive pulping and bleaching
437
+ - Chemical pollution from paper mills
438
+ - Landfill waste (recyclable but often discarded)
439
+ - Ink production environmental impact
440
+
441
+ Sustainable Alternatives:
442
+ - Recycled paper (70% less energy)
443
+ - Digital books and documents (e-readers)
444
+ - FSC-certified paper products
445
+ - Reduced printing (cloud storage)
446
+ - Reusable notebooks (Rocketbook)
447
+ - Hemp and bamboo paper
448
+ """,
449
+ metadata={"category": "paper_products", "impact_level": "moderate", "deforestation_link": True}
450
+ ),
451
+
452
+ # Category 11: Beverages
453
+ Document(
454
+ text="""
455
+ Beverage Industry Environmental Impact
456
+
457
+ Beverage packaging is major waste source globally.
458
+ Aluminum cans: 170g CO2e each to produce.
459
+ Glass bottles are heavy, requiring more transport fuel.
460
+ Coffee pods generate 11 billion units of waste annually.
461
+ Single-use plastic bottles dominate the market.
462
+
463
+ Product Examples: coffee, tea, soda, beer, wine, juice, bottled water, energy drinks, sports drinks
464
+
465
+ Carbon Footprint Data:
466
+ - Aluminum can: 170g CO2e
467
+ - Glass bottle: 200g CO2e
468
+ - Plastic bottle: 82g CO2e
469
+ - Coffee pod (single): 50g CO2e
470
+ - Tetra Pak carton: 100g CO2e
471
+ - Beer bottle: 300g CO2e
472
+
473
+ Environmental Issues:
474
+ - Packaging waste (major contributor)
475
+ - Water extraction affecting local communities
476
+ - Transportation emissions (heavy liquids)
477
+ - Single-use culture dominance
478
+ - Coffee pod waste (non-recyclable)
479
+
480
+ Sustainable Alternatives:
481
+ - Tap water in reusable bottles
482
+ - Bulk beverages (kegs, large containers)
483
+ - Recyclable aluminum (infinitely recyclable)
484
+ - Loose-leaf tea instead of pods
485
+ - Local products (reduced transport)
486
+ - SodaStream for homemade carbonation
487
+ """,
488
+ metadata={"category": "beverages", "impact_level": "moderate", "packaging_intensive": True}
489
+ ),
490
+
491
+ # Category 12: Healthcare & Medical
492
+ Document(
493
+ text="""
494
+ Healthcare and Medical Products Environmental Impact
495
+
496
+ Healthcare sector contributes 4.4% of global greenhouse gas emissions.
497
+ Pharmaceutical production is highly chemical and energy-intensive.
498
+ Single-use medical plastics create massive waste streams.
499
+ Proper disposal critical as pharmaceuticals pollute water systems.
500
+ Blister packs and medical packaging often non-recyclable.
501
+
502
+ Product Examples: medications, pills, supplements, bandages, medical devices, prescriptions, vitamins, first aid supplies
503
+
504
+ Carbon Footprint Data:
505
+ - Pharmaceutical production (per kg): 50 kg CO2e
506
+ - Blister pack medication: 100g CO2e
507
+ - Supplement bottle: 200g CO2e
508
+ - Medical device (average): 5 kg CO2e
509
+ - Bandage box: 50g CO2e
510
+
511
+ Environmental Issues:
512
+ - Pharmaceutical pollution in waterways
513
+ - Single-use medical plastic waste
514
+ - Chemical-intensive production
515
+ - Antimicrobial resistance from improper disposal
516
+ - Non-recyclable packaging (blister packs)
517
+
518
+ Sustainable Alternatives:
519
+ - Proper medication disposal programs
520
+ - Reusable medical equipment where safe
521
+ - Bulk medication packaging
522
+ - Generic medications (less packaging)
523
+ - Biodegradable bandages
524
+ - Pharmaceutical take-back programs
525
+ """,
526
+ metadata={"category": "healthcare", "impact_level": "moderate", "chemical_intensive": True}
527
+ ),
528
+
529
+ # Category 13: Energy Products
530
+ Document(
531
+ text="""
532
+ Energy Products and Batteries Environmental Impact
533
+
534
+ Battery production involves rare earth mining with massive environmental footprint.
535
+ Lithium extraction is extremely water-intensive (500,000L per ton).
536
+ E-waste from chargers and power banks accumulates rapidly.
537
+ LED bulbs save 75% energy compared to incandescent but contain electronics.
538
+ Solar panels offset their carbon footprint in 2-4 years of operation.
539
+
540
+ Product Examples: batteries, chargers, light bulbs, solar panels, power banks, extension cords, LED lights
541
+
542
+ Carbon Footprint Data:
543
+ - AA battery (alkaline): 20g CO2e
544
+ - Lithium-ion battery (phone): 50g CO2e
545
+ - Phone charger: 100g CO2e
546
+ - LED bulb: 50g CO2e
547
+ - Solar panel (per kW): 500 kg CO2e (offset in 2-4 years)
548
+ - Power bank: 200g CO2e
549
+
550
+ Environmental Issues:
551
+ - Rare earth mining (cobalt, lithium)
552
+ - Water-intensive lithium extraction
553
+ - E-waste from disposable chargers
554
+ - Toxic materials in batteries
555
+ - Energy use in production
556
+
557
+ Sustainable Alternatives:
558
+ - Rechargeable batteries (500+ cycles)
559
+ - Solar chargers for small devices
560
+ - LED bulbs (10x longer lasting)
561
+ - Energy efficient appliances
562
+ - Solar panels for home energy
563
+ - Battery recycling programs
564
+ """,
565
+ metadata={"category": "energy_products", "impact_level": "high", "mining_intensive": True}
566
+ ),
567
+
568
+ # Category 14: Pet Products
569
+ Document(
570
+ text="""
571
+ Pet Products Industry Environmental Impact
572
+
573
+ Pet food industry generates 64 million tons CO2e per year globally.
574
+ Meat-based pet food has similar footprint to meat for humans.
575
+ Plastic pet toys are non-recyclable and short-lived.
576
+ Cat litter production involves destructive clay mining.
577
+ Pet waste contributes to methane emissions in landfills.
578
+
579
+ Product Examples: dog food, cat food, pet toys, cat litter, pet beds, collars, leashes, treats
580
+
581
+ Carbon Footprint Data:
582
+ - Dry dog food (10kg): 20 kg CO2e
583
+ - Wet cat food (12 cans): 10 kg CO2e
584
+ - Plastic pet toy: 300g CO2e
585
+ - Clay cat litter (20kg): 15 kg CO2e
586
+ - Pet bed: 5 kg CO2e
587
+ - Dog leash: 500g CO2e
588
+
589
+ Environmental Issues:
590
+ - Meat-based food = high carbon footprint
591
+ - Plastic toy waste
592
+ - Clay mining for litter
593
+ - Pet waste methane emissions
594
+ - Overfishing for fish-based food
595
+
596
+ Sustainable Alternatives:
597
+ - Insect-based or plant-based pet food
598
+ - Biodegradable poop bags
599
+ - Natural wood or paper cat litter
600
+ - Durable, natural material toys
601
+ - Composting pet waste properly
602
+ - Recycled material pet beds
603
+ """,
604
+ metadata={"category": "pet_products", "impact_level": "moderate", "meat_based": True}
605
+ ),
606
+
607
+ # Category 15: Construction Materials
608
+ Document(
609
+ text="""
610
+ Construction Materials Environmental Impact
611
+
612
+ Cement production alone accounts for 8% of global CO2 emissions.
613
+ Steel production contributes 7% of global greenhouse gas emissions.
614
+ Deforestation for lumber affects biodiversity and carbon sequestration.
615
+ Paint releases volatile organic compounds (VOCs) harming air quality.
616
+ Construction and demolition waste makes up 40% of landfill content.
617
+
618
+ Product Examples: cement, concrete, steel beams, lumber, paint, insulation, drywall, tiles, bricks
619
+
620
+ Carbon Footprint Data:
621
+ - Cement (per ton): 900 kg CO2e
622
+ - Concrete (per ton): 410 kg CO2e
623
+ - Steel (per ton): 1,850 kg CO2e
624
+ - Lumber (per m³): 100 kg CO2e
625
+ - Paint (per gallon): 5 kg CO2e
626
+ - Insulation (per m²): 10 kg CO2e
627
+
628
+ Environmental Issues:
629
+ - Cement calcination releases massive CO2
630
+ - Steel blast furnaces use coal
631
+ - Illegal logging and deforestation
632
+ - Paint VOC off-gassing
633
+ - Construction waste (40% of landfills)
634
+ - Mining for aggregates
635
+
636
+ Sustainable Alternatives:
637
+ - Green cement (fly ash, slag cement)
638
+ - Recycled steel (60% less emissions)
639
+ - Certified sustainable lumber (FSC)
640
+ - Low-VOC and zero-VOC paints
641
+ - Recycled insulation materials
642
+ - Modular construction (less waste)
643
+ - Hempcrete and bamboo materials
644
+ """,
645
+ metadata={"category": "construction", "impact_level": "critical", "industrial_scale": True}
646
+ )
647
+ ]
648
+
649
+ return knowledge_docs
650
+
651
+ def retrieve_knowledge(self, product_name: str, top_k: int = 2) -> str:
652
+ """Retrieve relevant environmental knowledge using semantic search"""
653
+
654
+ if not self.index:
655
+ return "Knowledge base not available"
656
+
657
+ # Create query engine
658
+ query_engine = self.index.as_query_engine(
659
+ similarity_top_k=top_k,
660
+ response_mode="compact"
661
+ )
662
+
663
+ # Query for product-specific knowledge
664
+ query = f"Environmental impact, carbon footprint, issues, and sustainable alternatives for: {product_name}"
665
+
666
+ response = query_engine.query(query)
667
+
668
+ return str(response)
669
+
670
+ def add_document(self, text: str, metadata: Dict = None):
671
+ """Add new environmental document to index"""
672
+ doc = Document(text=text, metadata=metadata or {})
673
+ self.index.insert(doc)
674
+ self.index.storage_context.persist()
675
+ print(f"✅ Added new document to LlamaIndex")
requirements.txt CHANGED
@@ -2,4 +2,7 @@ gradio
2
  anthropic
3
  requests
4
  Pillow
5
- python-dotenv
 
 
 
 
2
  anthropic
3
  requests
4
  Pillow
5
+ python-dotenv
6
+ llama-index
7
+ llama-index-llms-anthropic
8
+ llama-index-embeddings-huggingface