-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
585 lines (480 loc) · 24.7 KB
/
agent.py
File metadata and controls
585 lines (480 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import os
import streamlit as st
import json
import time
import re
from agno.agent import Agent
from agno.run.agent import RunOutput
from agno.models.google import Gemini
from agno.models.openai import OpenAIChat
from dotenv import load_dotenv
from firecrawl import FirecrawlApp
from pydantic import BaseModel, Field
from typing import List, Optional
# Load environment variables
load_dotenv()
# API keys - must be set in environment variables
DEFAULT_FIRECRAWL_API_KEY = os.getenv("FIRECRAWL_API_KEY")
DEFAULT_OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# Pydantic schemas
class PropertyDetails(BaseModel):
address: str = Field(description="Full property address")
price: Optional[str] = Field(description="Property price")
bedrooms: Optional[str] = Field(description="Number of bedrooms")
bathrooms: Optional[str] = Field(description="Number of bathrooms")
square_feet: Optional[str] = Field(description="Square footage")
property_type: Optional[str] = Field(description="Type of property")
description: Optional[str] = Field(description="Property description")
features: Optional[List[str]] = Field(description="Property features")
images: Optional[List[str]] = Field(description="Property image URLs")
agent_contact: Optional[str] = Field(description="Agent contact information")
listing_url: Optional[str] = Field(description="Original listing URL")
class PropertyListing(BaseModel):
properties: List[PropertyDetails] = Field(description="List of properties found")
total_count: int = Field(description="Total number of properties found")
source_website: str = Field(description="Website where properties were found")
class DirectFirecrawlAgent:
"""Agent with direct Firecrawl integration for property search"""
def __init__(self, firecrawl_api_key: str, openai_api_key: str, model_id: str = "gpt-4o"):
self.agent = Agent(
model=OpenAIChat(id=model_id, api_key=openai_api_key),
markdown=True,
description="I am a real estate expert who helps find and analyze properties based on user preferences."
)
self.firecrawl = FirecrawlApp(api_key=firecrawl_api_key)
def find_properties_direct(self, city: str, state: str, user_criteria: dict, selected_websites: list) -> dict:
"""Direct Firecrawl integration for property search"""
city_formatted = city.replace(' ', '-').lower()
state_upper = state.upper() if state else ''
# Create URLs for selected websites
state_lower = state.lower() if state else ''
city_trulia = city.replace(' ', '_') # Trulia uses underscores for spaces
search_urls = {
"Zillow": f"https://www.zillow.com/homes/for_sale/{city_formatted}-{state_upper}/",
"Realtor.com": f"https://www.realtor.com/realestateandhomes-search/{city_formatted}_{state_upper}/pg-1",
"Trulia": f"https://www.trulia.com/{state_upper}/{city_trulia}/",
"Homes.com": f"https://www.homes.com/homes-for-sale/{city_formatted}-{state_lower}/"
}
# Filter URLs based on selected websites
urls_to_search = [url for site, url in search_urls.items() if site in selected_websites]
print(f"Selected websites: {selected_websites}")
print(f"URLs to search: {urls_to_search}")
if not urls_to_search:
return {"error": "No websites selected"}
# Create comprehensive prompt with specific schema guidance
prompt = f"""You are extracting property listings from real estate websites. Extract EVERY property listing you can find on the page.
USER SEARCH CRITERIA:
- Budget: {user_criteria.get('budget_range', 'Any')}
- Property Type: {user_criteria.get('property_type', 'Any')}
- Bedrooms: {user_criteria.get('bedrooms', 'Any')}
- Bathrooms: {user_criteria.get('bathrooms', 'Any')}
- Min Square Feet: {user_criteria.get('min_sqft', 'Any')}
- Special Features: {user_criteria.get('special_features', 'Any')}
EXTRACTION INSTRUCTIONS:
1. Find ALL property listings on the page (usually 20-40 per page)
2. For EACH property, extract these fields:
- address: Full street address (required)
- price: Listed price with $ symbol (required)
- bedrooms: Number of bedrooms (required)
- bathrooms: Number of bathrooms (required)
- square_feet: Square footage if available
- property_type: House/Condo/Townhouse/Apartment etc.
- description: Brief property description if available
- listing_url: Direct link to property details if available
- agent_contact: Agent name/phone if visible
3. CRITICAL REQUIREMENTS:
- Extract AT MOST 2 properties if they exist on the page
- Do NOT skip properties even if some fields are missing
- Use "Not specified" for missing optional fields
- Ensure address and price are always filled
- Look for property cards, listings, search results
4. RETURN FORMAT:
- Return JSON with "properties" array containing all extracted properties
- Each property should be a complete object with all available fields
- Set "total_count" to the number of properties extracted
- Set "source_website" to the main website name (Zillow/Realtor/Trulia/Homes)
EXTRACT EVERY VISIBLE PROPERTY LISTING - DO NOT LIMIT TO JUST A FEW!
"""
try:
# Direct Firecrawl call - using correct API format
print(f"Calling Firecrawl with {len(urls_to_search)} URLs")
raw_response = self.firecrawl.extract(
urls_to_search,
prompt=prompt,
schema=PropertyListing.model_json_schema()
)
print("Raw Firecrawl Response:", raw_response)
if hasattr(raw_response, 'success') and raw_response.success:
# Handle Firecrawl response object
properties = raw_response.data.get('properties', []) if hasattr(raw_response, 'data') else []
total_count = raw_response.data.get('total_count', 0) if hasattr(raw_response, 'data') else 0
print(f"Response data keys: {list(raw_response.data.keys()) if hasattr(raw_response, 'data') else 'No data'}")
elif isinstance(raw_response, dict) and raw_response.get('success'):
# Handle dictionary response
properties = raw_response['data'].get('properties', [])
total_count = raw_response['data'].get('total_count', 0)
print(f"Response data keys: {list(raw_response['data'].keys())}")
else:
properties = []
total_count = 0
print(f"Response failed or unexpected format: {type(raw_response)}")
print(f"Extracted {len(properties)} properties from {total_count} total found")
# Debug: Print first property if available
if properties:
print(f"First property sample: {properties[0]}")
return {
'success': True,
'properties': properties,
'total_count': len(properties),
'source_websites': selected_websites
}
else:
# Enhanced error message with debugging info
error_msg = f"""No properties extracted despite finding {total_count} listings.
POSSIBLE CAUSES:
1. Website structure changed - extraction schema doesn't match
2. Website blocking or requiring interaction (captcha, login)
3. Properties don't match specified criteria too strictly
4. Extraction prompt needs refinement for this website
SUGGESTIONS:
- Try different websites (Zillow, Realtor.com, Trulia, Homes.com)
- Broaden search criteria (Any bedrooms, Any type, etc.)
- Check if website requires specific user interaction
Debug Info: Found {total_count} listings but extraction returned empty array."""
return {"error": error_msg}
except Exception as e:
return {"error": f"Firecrawl extraction failed: {str(e)}"}
def create_sequential_agents(llm, user_criteria):
"""Create agents for sequential manual execution"""
property_search_agent = Agent(
name="Property Search Agent",
model=llm,
instructions="""
You are a property search expert. Your role is to find and extract property listings.
WORKFLOW:
1. SEARCH FOR PROPERTIES:
- Use the provided Firecrawl data to extract property listings
- Focus on properties matching user criteria
- Extract detailed property information
2. EXTRACT PROPERTY DATA:
- Address, price, bedrooms, bathrooms, square footage
- Property type, features, listing URLs
- Agent contact information
3. PROVIDE STRUCTURED OUTPUT:
- List properties with complete details
- Include all listing URLs
- Rank by match quality to user criteria
IMPORTANT:
- Focus ONLY on finding and extracting property data
- Do NOT provide market analysis or valuations
- Your output will be used by other agents for analysis
""",
)
market_analysis_agent = Agent(
name="Market Analysis Agent",
model=llm,
instructions="""
You are a market analysis expert. Provide CONCISE market insights.
REQUIREMENTS:
- Keep analysis brief and to the point
- Focus on key market trends only
- Provide 2-3 bullet points per area
- Avoid repetition and lengthy explanations
COVER:
1. Market Condition: Buyer's/seller's market, price trends
2. Key Neighborhoods: Brief overview of areas where properties are located
3. Investment Outlook: 2-3 key points about investment potential
FORMAT: Use bullet points and keep each section under 100 words.
""",
)
property_valuation_agent = Agent(
name="Property Valuation Agent",
model=llm,
instructions="""
You are a property valuation expert. Provide CONCISE property assessments.
REQUIREMENTS:
- Keep each property assessment brief (2-3 sentences max)
- Focus on key points only: value, investment potential, recommendation
- Avoid lengthy analysis and repetition
- Use bullet points for clarity
FOR EACH PROPERTY, PROVIDE:
1. Value Assessment: Fair price, over/under priced
2. Investment Potential: High/Medium/Low with brief reason
3. Key Recommendation: One actionable insight
FORMAT:
- Use bullet points
- Keep each property under 50 words
- Focus on actionable insights only
""",
)
return property_search_agent, market_analysis_agent, property_valuation_agent
def run_sequential_analysis(city, state, user_criteria, selected_websites, firecrawl_api_key, openai_api_key, update_callback):
"""Run agents sequentially with manual coordination"""
# Initialize agents
llm = OpenAIChat(id="gpt-4o", api_key=openai_api_key)
property_search_agent, market_analysis_agent, property_valuation_agent = create_sequential_agents(llm, user_criteria)
# Step 1: Property Search with Direct Firecrawl Integration
update_callback(0.2, "Searching properties...", "🔍 Property Search Agent: Finding properties...")
direct_agent = DirectFirecrawlAgent(
firecrawl_api_key=firecrawl_api_key,
openai_api_key=openai_api_key,
model_id="gpt-4o"
)
properties_data = direct_agent.find_properties_direct(
city=city,
state=state,
user_criteria=user_criteria,
selected_websites=selected_websites
)
if "error" in properties_data:
return f"Error in property search: {properties_data['error']}"
properties = properties_data.get('properties', [])
if not properties:
return "No properties found matching your criteria."
update_callback(0.4, "Properties found", f"✅ Found {len(properties)} properties")
# Step 2: Market Analysis
update_callback(0.5, "Analyzing market...", "📊 Market Analysis Agent: Analyzing market trends...")
market_analysis_prompt = f"""
Provide CONCISE market analysis for these properties:
PROPERTIES: {len(properties)} properties in {city}, {state}
BUDGET: {user_criteria.get('budget_range', 'Any')}
Give BRIEF insights on:
• Market condition (buyer's/seller's market)
• Key neighborhoods where properties are located
• Investment outlook (2-3 bullet points max)
Keep each section under 100 words. Use bullet points.
"""
market_result: RunOutput = market_analysis_agent.run(market_analysis_prompt)
market_analysis = market_result.content
update_callback(0.7, "Market analysis complete", "✅ Market analysis completed")
# Step 3: Property Valuation
update_callback(0.8, "Evaluating properties...", "💰 Property Valuation Agent: Evaluating properties...")
# Create detailed property list for valuation
properties_for_valuation = []
for i, prop in enumerate(properties, 1):
if isinstance(prop, dict):
prop_data = {
'number': i,
'address': prop.get('address', 'Address not available'),
'price': prop.get('price', 'Price not available'),
'property_type': prop.get('property_type', 'Type not available'),
'bedrooms': prop.get('bedrooms', 'Not specified'),
'bathrooms': prop.get('bathrooms', 'Not specified'),
'square_feet': prop.get('square_feet', 'Not specified')
}
else:
prop_data = {
'number': i,
'address': getattr(prop, 'address', 'Address not available'),
'price': getattr(prop, 'price', 'Price not available'),
'property_type': getattr(prop, 'property_type', 'Type not available'),
'bedrooms': getattr(prop, 'bedrooms', 'Not specified'),
'bathrooms': getattr(prop, 'bathrooms', 'Not specified'),
'square_feet': getattr(prop, 'square_feet', 'Not specified')
}
properties_for_valuation.append(prop_data)
valuation_prompt = f"""
Provide CONCISE property assessments for each property. Use the EXACT format shown below:
USER BUDGET: {user_criteria.get('budget_range', 'Any')}
PROPERTIES TO EVALUATE:
{json.dumps(properties_for_valuation, indent=2)}
For EACH property, provide assessment in this EXACT format:
**Property [NUMBER]: [ADDRESS]**
• Value: [Fair price/Over priced/Under priced] - [brief reason]
• Investment Potential: [High/Medium/Low] - [brief reason]
• Recommendation: [One actionable insight]
REQUIREMENTS:
- Start each assessment with "**Property [NUMBER]:**"
- Keep each property assessment under 50 words
- Analyze ALL {len(properties)} properties individually
- Use bullet points as shown
"""
valuation_result: RunOutput = property_valuation_agent.run(valuation_prompt)
property_valuations = valuation_result.content
update_callback(0.9, "Valuation complete", "✅ Property valuations completed")
# Step 4: Final Synthesis
update_callback(0.95, "Synthesizing results...", "🤖 Synthesizing final recommendations...")
# Debug: Check properties structure
print(f"Properties type: {type(properties)}")
print(f"Properties length: {len(properties)}")
if properties:
print(f"First property type: {type(properties[0])}")
print(f"First property: {properties[0]}")
# Format properties for better display
properties_display = ""
for i, prop in enumerate(properties, 1):
# Handle both dict and object access
if isinstance(prop, dict):
address = prop.get('address', 'Address not available')
price = prop.get('price', 'Price not available')
prop_type = prop.get('property_type', 'Type not available')
bedrooms = prop.get('bedrooms', 'Not specified')
bathrooms = prop.get('bathrooms', 'Not specified')
square_feet = prop.get('square_feet', 'Not specified')
agent_contact = prop.get('agent_contact', 'Contact not available')
description = prop.get('description', 'No description available')
listing_url = prop.get('listing_url', '#')
else:
# Handle object access
address = getattr(prop, 'address', 'Address not available')
price = getattr(prop, 'price', 'Price not available')
prop_type = getattr(prop, 'property_type', 'Type not available')
bedrooms = getattr(prop, 'bedrooms', 'Not specified')
bathrooms = getattr(prop, 'bathrooms', 'Not specified')
square_feet = getattr(prop, 'square_feet', 'Not specified')
agent_contact = getattr(prop, 'agent_contact', 'Contact not available')
description = getattr(prop, 'description', 'No description available')
listing_url = getattr(prop, 'listing_url', '#')
properties_display += f"""
### Property {i}: {address}
**Price:** {price}
**Type:** {prop_type}
**Bedrooms:** {bedrooms} | **Bathrooms:** {bathrooms}
**Square Feet:** {square_feet}
**Agent Contact:** {agent_contact}
**Description:** {description}
**Listing URL:** [View Property]({listing_url})
---
"""
final_synthesis = f"""
# 🏠 Property Listings Found
**Total Properties:** {len(properties)} properties matching your criteria
{properties_display}
---
# 📊 Market Analysis & Investment Insights
{market_analysis}
---
# 💰 Property Valuations & Recommendations
{property_valuations}
---
# 🔗 All Property Links
"""
# Extract and add property links
all_text = f"{json.dumps(properties, indent=2)} {market_analysis} {property_valuations}"
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', all_text)
if urls:
final_synthesis += "\n### Available Property Links:\n"
for i, url in enumerate(set(urls), 1):
final_synthesis += f"{i}. {url}\n"
update_callback(1.0, "Analysis complete", "🎉 Complete analysis ready!")
# Return structured data for better UI display
return {
'properties': properties,
'market_analysis': market_analysis,
'property_valuations': property_valuations,
'markdown_synthesis': final_synthesis,
'total_properties': len(properties)
}
def extract_property_valuation(property_valuations, property_number, property_address):
"""Extract valuation for a specific property from the full analysis"""
if not property_valuations:
return None
# Split by property sections - look for the formatted property headers
sections = property_valuations.split('**Property')
# Look for the specific property number
for section in sections:
if section.strip().startswith(f"{property_number}:"):
# Add back the "**Property" prefix and clean up
clean_section = f"**Property{section}".strip()
# Remove any extra asterisks at the end
clean_section = clean_section.replace('**', '**').replace('***', '**')
return clean_section
# Fallback: look for property number mentions in any format
all_sections = property_valuations.split('\n\n')
for section in all_sections:
if (f"Property {property_number}" in section or
f"#{property_number}" in section):
return section
# Last resort: try to match by address
for section in all_sections:
if any(word in section.lower() for word in property_address.lower().split()[:3] if len(word) > 2):
return section
# If no specific match found, return indication that analysis is not available
return f"**Property {property_number} Analysis**\n• Analysis: Individual assessment not available\n• Recommendation: Review general market analysis in the Market Analysis tab"
def display_properties_professionally(properties, market_analysis, property_valuations, total_properties):
"""Display properties in a clean, professional UI using Streamlit components"""
# Header with key metrics
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Properties Found", total_properties)
with col2:
# Calculate average price
prices = []
for p in properties:
price_str = p.get('price', '') if isinstance(p, dict) else getattr(p, 'price', '')
if price_str and price_str != 'Price not available':
try:
price_num = ''.join(filter(str.isdigit, str(price_str)))
if price_num:
prices.append(int(price_num))
except:
pass
avg_price = f"${sum(prices) // len(prices):,}" if prices else "N/A"
st.metric("Average Price", avg_price)
with col3:
types = {}
for p in properties:
t = p.get('property_type', 'Unknown') if isinstance(p, dict) else getattr(p, 'property_type', 'Unknown')
types[t] = types.get(t, 0) + 1
most_common = max(types.items(), key=lambda x: x[1])[0] if types else "N/A"
st.metric("Most Common Type", most_common)
# Create tabs for different views
tab1, tab2, tab3 = st.tabs(["🏠 Properties", "📊 Market Analysis", "💰 Valuations"])
with tab1:
for i, prop in enumerate(properties, 1):
# Extract property data
data = {k: prop.get(k, '') if isinstance(prop, dict) else getattr(prop, k, '')
for k in ['address', 'price', 'property_type', 'bedrooms', 'bathrooms', 'square_feet', 'description', 'listing_url']}
with st.container():
# Property header with number and price
col1, col2 = st.columns([3, 1])
with col1:
st.subheader(f"#{i} 🏠 {data['address']}")
with col2:
st.metric("Price", data['price'])
# Property details with right-aligned button
col1, col2, col3 = st.columns([2, 2, 1])
with col1:
st.markdown(f"**Type:** {data['property_type']}")
st.markdown(f"**Beds/Baths:** {data['bedrooms']}/{data['bathrooms']}")
st.markdown(f"**Area:** {data['square_feet']}")
with col2:
with st.expander("💰 Investment Analysis"):
# Extract property-specific valuation from the full analysis
property_valuation = extract_property_valuation(property_valuations, i, data['address'])
if property_valuation:
st.markdown(property_valuation)
else:
st.info("Investment analysis not available for this property")
with col3:
if data['listing_url'] and data['listing_url'] != '#':
st.markdown(
f"""
<div style="height: 100%; display: flex; align-items: center; justify-content: flex-end;">
<a href="{data['listing_url']}" target="_blank"
style="text-decoration: none; padding: 0.5rem 1rem;
background-color: #0066cc; color: white;
border-radius: 6px; font-size: 0.9em; font-weight: 500;">
Property Link
</a>
</div>
""",
unsafe_allow_html=True
)
st.divider()
with tab2:
st.subheader("📊 Market Analysis")
if market_analysis:
for section in market_analysis.split('\n\n'):
if section.strip():
st.markdown(section)
else:
st.info("No market analysis available")
with tab3:
st.subheader("💰 Investment Analysis")
if property_valuations:
for section in property_valuations.split('\n\n'):
if section.strip():
st.markdown(section)
else:
st.info("No valuation data available")