""" LlamaIndex-powered RAG for Environmental Knowledge Retrieval Enhanced with 15 comprehensive product categories """ import os from llama_index.core import ( VectorStoreIndex, Document, Settings, StorageContext, load_index_from_storage ) from llama_index.llms.anthropic import Anthropic from llama_index.embeddings.huggingface import HuggingFaceEmbedding from typing import List, Dict class LlamaIndexEnvironmentalRAG: """Advanced RAG using LlamaIndex for environmental knowledge""" def __init__(self): self.index = None # Configure LlamaIndex settings Settings.llm = Anthropic( model="claude-sonnet-4-20250514", api_key=os.environ.get("ANTHROPIC_API_KEY") ) Settings.embed_model = HuggingFaceEmbedding( model_name="BAAI/bge-small-en-v1.5" ) self._initialize_index() def _initialize_index(self): """Initialize or load vector index""" persist_dir = "./storage" # Try to load existing index if os.path.exists(persist_dir): try: storage_context = StorageContext.from_defaults(persist_dir=persist_dir) self.index = load_index_from_storage(storage_context) print("✅ Loaded existing LlamaIndex from storage") return except: print("⚠️ Could not load existing index, creating new one...") # Create new index with 15-category environmental knowledge documents = self._create_comprehensive_knowledge_base() print("🔄 Building LlamaIndex vector store...") self.index = VectorStoreIndex.from_documents(documents) # Persist index self.index.storage_context.persist(persist_dir=persist_dir) print("✅ Created and persisted LlamaIndex with 15 categories") def _create_comprehensive_knowledge_base(self) -> List[Document]: """Create comprehensive 15-category environmental knowledge base""" knowledge_docs = [ # Category 1: Electronics Document( text=""" Electronics and E-Waste Environmental Impact E-waste is the fastest-growing waste stream globally, expanding 5x faster than recycling capacity. Electronic devices contain rare earth metals: lithium, cobalt, neodymium, tantalum, and gold. Manufacturing accounts for 80% of total carbon footprint for most electronic devices. Conflict minerals sourcing causes human rights violations and environmental destruction in mining regions. Planned obsolescence deliberately shortens device lifespan, driving unnecessary waste. Product Examples: smartphones, laptops, headphones, tablets, computers, smart devices, earbuds, speakers, monitors, TVs, cameras Carbon Footprint Data: - Smartphone: 70-80 kg CO2e (manufacturing phase) - Laptop: 300-400 kg CO2e (full lifecycle) - Wireless headphones: 15-20 kg CO2e - Desktop computer: 500 kg CO2e - Smart TV: 300 kg CO2e Environmental Issues: - Toxic materials (lead, mercury, cadmium, brominated flame retardants) - Energy-intensive mining operations causing habitat destruction - Global recycling rate only 20% - Average smartphone replaced every 2 years - E-waste exports to developing countries Sustainable Alternatives: - Refurbished devices (90% lower carbon footprint) - Modular phones like Fairphone (easily repairable) - Long-lasting, Right-to-Repair certified electronics - Electronics leasing and take-back programs - Energy Star certified devices """, metadata={"category": "electronics", "impact_level": "critical", "coverage": "comprehensive"} ), # Category 2: Plastics Document( text=""" Plastics Environmental Impact and Pollution Crisis Only 9% of all plastic ever produced has been recycled globally - a recycling failure. Plastics take 450+ years to decompose, breaking into harmful microplastics. Microplastics have entered the entire food chain, found in human blood and organs. 8 million tons of plastic waste enter oceans every year, forming massive garbage patches. Virgin plastic production from petroleum creates massive carbon emissions. Product Examples: water bottles, plastic bags, food containers, packaging materials, straws, cups, cutlery, wrap Carbon Footprint Data: - Single plastic bottle: 82g CO2e - Plastic grocery bag: 10g CO2e - Plastic food container: 150g CO2e - Styrofoam cup: 25g CO2e - Plastic wrap (per meter): 5g CO2e Environmental Issues: - Ocean pollution causing marine life deaths (1 million seabirds, 100,000 marine mammals annually) - Microplastic contamination in drinking water (93% of bottled water contains microplastics) - Non-biodegradable waste accumulating in landfills for centuries - Petroleum extraction and refining for plastic production - Toxic chemical leaching into soil and water Sustainable Alternatives: - Reusable stainless steel bottles (saves 30kg CO2e per year) - Glass containers (infinitely recyclable) - Biodegradable materials (PLA from corn starch) - Zero-waste refill programs - Beeswax wraps instead of plastic wrap """, metadata={"category": "plastics", "impact_level": "critical", "urgency": "immediate"} ), # Category 3: Textiles Document( text=""" Textiles and Fashion Industry Environmental Impact Fashion industry accounts for 10% of global carbon emissions, more than aviation and shipping combined. Water intensive: 2,700 liters needed for one cotton t-shirt (drinking water for 1 person for 2.5 years). Textile waste fills landfills with 92 million tons annually, most from fast fashion. Synthetic fabrics like polyester shed microplastics into water with every wash. Textile dyeing is the second-largest water polluter globally after agriculture. Product Examples: clothing, shirts, jeans, shoes, leather goods, fast fashion items, dresses, jackets, socks, sportswear Carbon Footprint Data: - Cotton t-shirt: 7 kg CO2e - Pair of jeans: 33 kg CO2e - Running shoes/sneakers: 14 kg CO2e - Leather shoes: 30 kg CO2e - Winter coat: 50 kg CO2e - Dress: 20 kg CO2e Environmental Issues: - Fast fashion waste (average garment worn only 7 times before disposal) - Water pollution from toxic dyes contaminating rivers - Microplastic shedding from polyester (500,000 tons per year into oceans) - Unethical labor practices in garment factories - Cotton farming using 25% of world's pesticides Sustainable Alternatives: - Organic cotton (40% lower carbon footprint, no pesticides) - Recycled materials and upcycled fashion - Second-hand and vintage clothing - Slow fashion brands (Patagonia, Eileen Fisher) - Clothing rental and sharing services - Hemp and bamboo fabrics """, metadata={"category": "textiles", "impact_level": "high", "water_intensive": True} ), # Category 4: Food & Agriculture Document( text=""" Food System and Agriculture Environmental Impact Food system accounts for 26% of global greenhouse gas emissions. Animal agriculture drives 80% of Amazon rainforest deforestation. Meat production requires massive water and land use compared to plant-based alternatives. Food waste generates 8% of global emissions - if food waste were a country, it would be 3rd largest emitter. Transportation impact varies dramatically by product type and distance (food miles). Product Examples: beef, meat products, dairy, cheese, chicken, fish, coffee, avocados, processed foods, vegetables Carbon Footprint Data: - Beef (1kg): 60 kg CO2e - Lamb (1kg): 24 kg CO2e - Cheese (1kg): 21 kg CO2e - Pork (1kg): 7 kg CO2e - Chicken (1kg): 6 kg CO2e - Plant-based burger: 2 kg CO2e - Coffee (1kg beans): 15 kg CO2e - Avocado (1kg): 0.8 kg CO2e Environmental Issues: - Methane emissions from cattle (28x more potent than CO2) - Deforestation for agriculture and grazing land - Water scarcity in agricultural regions - Pesticide and fertilizer pollution - Food miles and cold chain transportation - Fishing industry overfishing and bycatch Sustainable Alternatives: - Plant-based proteins (90% lower carbon footprint than beef) - Local and seasonal produce (reduces transport emissions) - Regenerative agriculture practices - Reduced food waste through meal planning - Sustainable fishing certifications (MSC, ASC) - Organic farming methods """, metadata={"category": "food", "impact_level": "critical", "deforestation_driver": True} ), # Category 5: Vehicles & Transportation Document( text=""" Vehicles and Transportation Environmental Impact Transportation accounts for 27% of US greenhouse gas emissions, 14% globally. EV battery production is energy-intensive but lifetime emissions are 50-70% lower than ICE vehicles. Manufacturing accounts for 15-20% of total vehicle lifetime emissions. Public transportation is the most efficient option per passenger mile. Aviation contributes 2.5% of global CO2 emissions but growing rapidly. Product Examples: cars, electric vehicles, bikes, e-bikes, scooters, motorcycles, buses, trains Carbon Footprint Data: - ICE Car (lifetime 150,000 miles): 35,000 kg CO2e - Electric Car (lifetime, grid mix): 24,000 kg CO2e - Electric Car (renewable energy): 15,000 kg CO2e - E-bike (lifetime): 500 kg CO2e - Bicycle (manufacturing): 50 kg CO2e - Motorcycle (lifetime): 20,000 kg CO2e Environmental Issues: - Fossil fuel dependence and air pollution - Lithium and cobalt mining for EV batteries - Urban air quality deterioration - Infrastructure emissions (roads, parking) - Resource depletion for manufacturing Sustainable Alternatives: - Electric vehicles powered by renewable energy - Bicycles and e-bikes for short trips - Public transportation (bus, train, metro) - Car-sharing and ride-sharing services - Walking for ultra-short distances - Hybrid vehicles as transition technology """, metadata={"category": "vehicles", "impact_level": "high", "ev_better": True} ), # Category 6: Home & Furniture Document( text=""" Home Furniture and Appliances Environmental Impact Furniture production drives deforestation, especially for tropical hardwoods. VOC emissions from paints, finishes, and adhesives harm indoor air quality. Fast furniture culture creates massive landfill waste (similar to fast fashion). Appliances contain refrigerants that are potent greenhouse gases (HFCs). Average sofa produces 90kg CO2e during manufacturing. Product Examples: sofas, tables, chairs, mattresses, beds, desks, shelves, cabinets, refrigerators, washing machines Carbon Footprint Data: - Sofa: 90 kg CO2e - Wooden table: 40 kg CO2e - Mattress: 100 kg CO2e - Office chair: 30 kg CO2e - Refrigerator: 200 kg CO2e - Washing machine: 150 kg CO2e Environmental Issues: - Deforestation for lumber (illegal logging in rainforests) - Formaldehyde and VOC off-gassing - Landfill waste from disposable furniture - HFC refrigerants (1,000-3,000x more potent than CO2) - Energy consumption of appliances Sustainable Alternatives: - Certified sustainable wood (FSC, SFI) - Second-hand and vintage furniture - Energy Star rated appliances - Natural latex mattresses - Modular and repairable furniture - Low-VOC paints and finishes """, metadata={"category": "home_furniture", "impact_level": "moderate", "voc_emissions": True} ), # Category 7: Personal Care & Cosmetics Document( text=""" Personal Care and Cosmetics Industry Environmental Impact Beauty industry produces 120 billion packaging units per year, mostly plastic. Microplastics in products (microbeads) enter oceans and food chain. Palm oil in cosmetics drives rainforest deforestation in Southeast Asia. Chemical pollution from production affects waterways globally. Single-use packaging dominates the industry. Product Examples: shampoo, soap, cosmetics, makeup, skincare, deodorant, toothpaste, lotion, perfume, sunscreen Carbon Footprint Data: - Shampoo bottle: 200g CO2e - Makeup palette: 150g CO2e - Deodorant: 100g CO2e - Toothpaste tube: 80g CO2e - Perfume: 300g CO2e - Sunscreen: 180g CO2e Environmental Issues: - 120 billion plastic packaging units annually - Microbeads in scrubs (banned in many countries) - Palm oil plantations replacing rainforests - Chemical runoff polluting waterways - Animal testing in some markets - Single-use sample sachets Sustainable Alternatives: - Solid bars (shampoo, soap) with no packaging - Refillable containers and bulk buying - Natural and organic ingredients - Cruelty-free and vegan products - Biodegradable packaging - Zero-waste brands (Lush, Ethique) """, metadata={"category": "personal_care", "impact_level": "moderate", "plastic_intensive": True} ), # Category 8: Cleaning Products Document( text=""" Cleaning Products Environmental Impact Household cleaners release volatile organic compounds (VOCs) affecting air quality. Phosphates in detergents cause water eutrophication and algal blooms. Plastic bottles account for 8 billion units per year in cleaning products alone. Chemical production for cleaners is energy-intensive with toxic byproducts. Concentrated formulas can reduce emissions by 50% through smaller packaging. Product Examples: laundry detergent, dish soap, surface cleaners, bleach, disinfectant, glass cleaner, floor cleaner Carbon Footprint Data: - Laundry detergent (3L): 500g CO2e - Dish soap: 200g CO2e - All-purpose cleaner: 300g CO2e - Bleach: 400g CO2e - Glass cleaner: 150g CO2e Environmental Issues: - VOC emissions affecting indoor and outdoor air quality - Phosphate pollution causing dead zones in waterways - Plastic bottle waste (mostly non-recycled) - Toxic chemical production processes - Antimicrobial resistance from overuse of disinfectants Sustainable Alternatives: - Concentrated formulas (use less packaging) - Refill stations and bulk buying - DIY cleaners (vinegar, baking soda, castile soap) - Plant-based ingredients - Recyclable or compostable packaging - Seventh Generation, Method brands """, metadata={"category": "cleaning_products", "impact_level": "moderate", "water_pollution": True} ), # Category 9: Toys & Games Document( text=""" Toys and Gaming Environmental Impact 80% of toys are made from plastic, contributing to waste crisis. Average child in US receives 70 new toys per year. Short toy lifespan due to trends and breakage creates high waste. Gaming consoles and electronics become e-waste quickly. Sports equipment often made from single materials difficult to recycle. Product Examples: action figures, dolls, LEGO, puzzles, board games, video game consoles, sports equipment Carbon Footprint Data: - Plastic toy (average): 200g CO2e - LEGO set (1kg): 2 kg CO2e - Board game: 500g CO2e - Gaming console: 50 kg CO2e - Sports ball: 300g CO2e - Bicycle toy: 1 kg CO2e Environmental Issues: - Plastic waste accumulation (90% of toys) - Trend-driven consumption (movie tie-ins) - E-waste from electronic toys and consoles - Toxic materials (phthalates, BPA) - Difficult disassembly for recycling Sustainable Alternatives: - Wooden toys from sustainable sources - Second-hand and toy libraries - Durable, timeless designs - LEGO take-back programs - Games-as-a-service (digital instead of physical) - Sports equipment made from recycled materials """, metadata={"category": "toys_games", "impact_level": "moderate", "plastic_dominant": True} ), # Category 10: Paper Products & Books Document( text=""" Paper Products and Publishing Environmental Impact Paper production accounts for 26% of landfill waste globally. Deforestation for virgin pulp affects biodiversity and carbon sinks. Recycled paper uses 70% less energy than virgin paper production. Printing and transportation add significant emissions. Digital alternatives can reduce carbon footprint by 90%. Product Examples: books, notebooks, paper, cardboard packaging, magazines, newspapers, tissue, office supplies Carbon Footprint Data: - Paperback book: 1 kg CO2e - Notebook (100 pages): 500g CO2e - Cardboard box: 300g CO2e - Magazine: 200g CO2e - Tissue box: 150g CO2e - Office paper (ream): 5 kg CO2e Environmental Issues: - Deforestation (42% of wood harvest for paper) - Energy-intensive pulping and bleaching - Chemical pollution from paper mills - Landfill waste (recyclable but often discarded) - Ink production environmental impact Sustainable Alternatives: - Recycled paper (70% less energy) - Digital books and documents (e-readers) - FSC-certified paper products - Reduced printing (cloud storage) - Reusable notebooks (Rocketbook) - Hemp and bamboo paper """, metadata={"category": "paper_products", "impact_level": "moderate", "deforestation_link": True} ), # Category 11: Beverages Document( text=""" Beverage Industry Environmental Impact Beverage packaging is major waste source globally. Aluminum cans: 170g CO2e each to produce. Glass bottles are heavy, requiring more transport fuel. Coffee pods generate 11 billion units of waste annually. Single-use plastic bottles dominate the market. Product Examples: coffee, tea, soda, beer, wine, juice, bottled water, energy drinks, sports drinks Carbon Footprint Data: - Aluminum can: 170g CO2e - Glass bottle: 200g CO2e - Plastic bottle: 82g CO2e - Coffee pod (single): 50g CO2e - Tetra Pak carton: 100g CO2e - Beer bottle: 300g CO2e Environmental Issues: - Packaging waste (major contributor) - Water extraction affecting local communities - Transportation emissions (heavy liquids) - Single-use culture dominance - Coffee pod waste (non-recyclable) Sustainable Alternatives: - Tap water in reusable bottles - Bulk beverages (kegs, large containers) - Recyclable aluminum (infinitely recyclable) - Loose-leaf tea instead of pods - Local products (reduced transport) - SodaStream for homemade carbonation """, metadata={"category": "beverages", "impact_level": "moderate", "packaging_intensive": True} ), # Category 12: Healthcare & Medical Document( text=""" Healthcare and Medical Products Environmental Impact Healthcare sector contributes 4.4% of global greenhouse gas emissions. Pharmaceutical production is highly chemical and energy-intensive. Single-use medical plastics create massive waste streams. Proper disposal critical as pharmaceuticals pollute water systems. Blister packs and medical packaging often non-recyclable. Product Examples: medications, pills, supplements, bandages, medical devices, prescriptions, vitamins, first aid supplies Carbon Footprint Data: - Pharmaceutical production (per kg): 50 kg CO2e - Blister pack medication: 100g CO2e - Supplement bottle: 200g CO2e - Medical device (average): 5 kg CO2e - Bandage box: 50g CO2e Environmental Issues: - Pharmaceutical pollution in waterways - Single-use medical plastic waste - Chemical-intensive production - Antimicrobial resistance from improper disposal - Non-recyclable packaging (blister packs) Sustainable Alternatives: - Proper medication disposal programs - Reusable medical equipment where safe - Bulk medication packaging - Generic medications (less packaging) - Biodegradable bandages - Pharmaceutical take-back programs """, metadata={"category": "healthcare", "impact_level": "moderate", "chemical_intensive": True} ), # Category 13: Energy Products Document( text=""" Energy Products and Batteries Environmental Impact Battery production involves rare earth mining with massive environmental footprint. Lithium extraction is extremely water-intensive (500,000L per ton). E-waste from chargers and power banks accumulates rapidly. LED bulbs save 75% energy compared to incandescent but contain electronics. Solar panels offset their carbon footprint in 2-4 years of operation. Product Examples: batteries, chargers, light bulbs, solar panels, power banks, extension cords, LED lights Carbon Footprint Data: - AA battery (alkaline): 20g CO2e - Lithium-ion battery (phone): 50g CO2e - Phone charger: 100g CO2e - LED bulb: 50g CO2e - Solar panel (per kW): 500 kg CO2e (offset in 2-4 years) - Power bank: 200g CO2e Environmental Issues: - Rare earth mining (cobalt, lithium) - Water-intensive lithium extraction - E-waste from disposable chargers - Toxic materials in batteries - Energy use in production Sustainable Alternatives: - Rechargeable batteries (500+ cycles) - Solar chargers for small devices - LED bulbs (10x longer lasting) - Energy efficient appliances - Solar panels for home energy - Battery recycling programs """, metadata={"category": "energy_products", "impact_level": "high", "mining_intensive": True} ), # Category 14: Pet Products Document( text=""" Pet Products Industry Environmental Impact Pet food industry generates 64 million tons CO2e per year globally. Meat-based pet food has similar footprint to meat for humans. Plastic pet toys are non-recyclable and short-lived. Cat litter production involves destructive clay mining. Pet waste contributes to methane emissions in landfills. Product Examples: dog food, cat food, pet toys, cat litter, pet beds, collars, leashes, treats Carbon Footprint Data: - Dry dog food (10kg): 20 kg CO2e - Wet cat food (12 cans): 10 kg CO2e - Plastic pet toy: 300g CO2e - Clay cat litter (20kg): 15 kg CO2e - Pet bed: 5 kg CO2e - Dog leash: 500g CO2e Environmental Issues: - Meat-based food = high carbon footprint - Plastic toy waste - Clay mining for litter - Pet waste methane emissions - Overfishing for fish-based food Sustainable Alternatives: - Insect-based or plant-based pet food - Biodegradable poop bags - Natural wood or paper cat litter - Durable, natural material toys - Composting pet waste properly - Recycled material pet beds """, metadata={"category": "pet_products", "impact_level": "moderate", "meat_based": True} ), # Category 15: Construction Materials Document( text=""" Construction Materials Environmental Impact Cement production alone accounts for 8% of global CO2 emissions. Steel production contributes 7% of global greenhouse gas emissions. Deforestation for lumber affects biodiversity and carbon sequestration. Paint releases volatile organic compounds (VOCs) harming air quality. Construction and demolition waste makes up 40% of landfill content. Product Examples: cement, concrete, steel beams, lumber, paint, insulation, drywall, tiles, bricks Carbon Footprint Data: - Cement (per ton): 900 kg CO2e - Concrete (per ton): 410 kg CO2e - Steel (per ton): 1,850 kg CO2e - Lumber (per m³): 100 kg CO2e - Paint (per gallon): 5 kg CO2e - Insulation (per m²): 10 kg CO2e Environmental Issues: - Cement calcination releases massive CO2 - Steel blast furnaces use coal - Illegal logging and deforestation - Paint VOC off-gassing - Construction waste (40% of landfills) - Mining for aggregates Sustainable Alternatives: - Green cement (fly ash, slag cement) - Recycled steel (60% less emissions) - Certified sustainable lumber (FSC) - Low-VOC and zero-VOC paints - Recycled insulation materials - Modular construction (less waste) - Hempcrete and bamboo materials """, metadata={"category": "construction", "impact_level": "critical", "industrial_scale": True} ) ] return knowledge_docs def retrieve_knowledge(self, product_name: str, top_k: int = 2) -> str: """Retrieve relevant environmental knowledge using semantic search""" if not self.index: return "Knowledge base not available" # Create query engine query_engine = self.index.as_query_engine( similarity_top_k=top_k, response_mode="compact" ) # Query for product-specific knowledge query = f"Environmental impact, carbon footprint, issues, and sustainable alternatives for: {product_name}" response = query_engine.query(query) return str(response) def add_document(self, text: str, metadata: Dict = None): """Add new environmental document to index""" doc = Document(text=text, metadata=metadata or {}) self.index.insert(doc) self.index.storage_context.persist() print(f"✅ Added new document to LlamaIndex")