""" AI Vectorization Engine (The Brain) This script demonstrates how to pass the scraped data into OpenAI to create Vectors, which are then stored in Pinecone for semantic search. """ import os import json # pip install openai pinecone-client # from openai import OpenAI # from pinecone import Pinecone # INITIALIZE CLIENTS (Requires API Keys) # openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY")) # index = pc.Index("real-estate-egypt") def vectorize_properties(json_file): print("[*] Loading raw properties...") try: with open(json_file, 'r', encoding='utf-8') as f: properties = json.load(f) except FileNotFoundError: print("[!] raw_properties.json not found. Run scraper first.") return vectors_to_upsert = [] print("[*] Generating AI Embeddings via OpenAI...") for idx, prop in enumerate(properties): # 1. Create a rich text representation of the property for the AI to understand ai_description = f"{prop['title']}. It is located in {prop['location']}. The price is {prop['price']}." # 2. Call OpenAI Embedding API (Mocked for demonstration) print(f" -> Vectorizing: {prop['title']}") """ # REAL CODE: response = openai_client.embeddings.create( input=ai_description, model="text-embedding-3-small" ) vector_data = response.data[0].embedding """ vector_data = [0.015, -0.022, 0.045] # Mock 1536-dimensional vector # 3. Prepare payload for Pinecone Vector Database vectors_to_upsert.append({ "id": f"prop_{idx}", "values": vector_data, "metadata": { "title": prop['title'], "price": prop['price'], "location": prop['location'], "image": prop['image'] } }) # 4. Upsert to Pinecone print(f"[*] Uploading {len(vectors_to_upsert)} vectors to Pinecone...") """ # REAL CODE: index.upsert(vectors=vectors_to_upsert) """ print("[+] Architecture Phase 2 Complete. Properties are now searchable via Semantic Search!") def search_smart_match(user_query): """ Example of how a user's quiz results are searched """ print(f"\n[*] User searches for: '{user_query}'") print("[*] Vectorizing user query...") # Convert query to vector, then search Pinecone for Cosine Similarity matches. print("[+] Returning top 10 AI matches...") if __name__ == "__main__": # Create a dummy json file to test the script dummy_data = [{"title": "شقة في زايد", "location": "Sheikh Zayed", "price": "5,000,000", "image": ""}] with open('raw_properties.json', 'w', encoding='utf-8') as f: json.dump(dummy_data, f) vectorize_properties('raw_properties.json') search_smart_match("I want a quiet place near good schools in Zayed under 6M")