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
import json
|
2 |
+
import psycopg2
|
3 |
+
import streamlit as st
|
4 |
+
import openai
|
5 |
+
from decimal import Decimal
|
6 |
+
|
7 |
+
PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
|
8 |
+
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
9 |
+
INDEX_NAME = 'realvest-data-v2'
|
10 |
+
EMBEDDING_MODEL = "text-embedding-ada-002" # OpenAI's best embeddings as of Apr 2023
|
11 |
+
MAX_LENGTH_DESC = 200
|
12 |
+
MATCH_SCORE_THR = 0.0
|
13 |
+
TOP_K = 20
|
14 |
+
|
15 |
+
def net_operating(rent, tax_rate, price):
|
16 |
+
|
17 |
+
#Takes input as monthly mortgage amount and monthly rental amount
|
18 |
+
#Uses managment expense, amount for repairs, vacancy ratio
|
19 |
+
#Example input: net_operating(1000,1,400,200)
|
20 |
+
#879.33
|
21 |
+
#1000 - 16.67 (tax) - 100 (managment) - 4 (repairs)
|
22 |
+
|
23 |
+
mortgage_amt = mortgage_monthly(price,20,3)
|
24 |
+
prop_managment = rent * 0.10
|
25 |
+
prop_tax = (price * (tax_rate/100)/12)
|
26 |
+
prop_repairs = (price * 0.02)/12
|
27 |
+
vacancy = (rent*0.02)
|
28 |
+
#These sections are a list of all the expenses used and formulas for each
|
29 |
+
|
30 |
+
net_income = rent - prop_managment - prop_tax - prop_repairs - vacancy - mortgage_amt
|
31 |
+
#Summing up expenses
|
32 |
+
output = [prop_managment, prop_tax, prop_repairs, vacancy, net_income]
|
33 |
+
|
34 |
+
|
35 |
+
return output
|
36 |
+
|
37 |
+
def down_payment(price,percent):
|
38 |
+
#This function takes the price and the downpayment rate and returns the downpayment amount
|
39 |
+
#Ie down_payment(100,20) returns 20
|
40 |
+
amt_down = price*(percent/100)
|
41 |
+
return(amt_down)
|
42 |
+
|
43 |
+
def mortgage_monthly(price,years,percent):
|
44 |
+
|
45 |
+
|
46 |
+
#This implements an approach to finding a monthly mortgage amount from the purchase price,
|
47 |
+
#years and percent.
|
48 |
+
#Sample input: (300000,20,4) = 2422
|
49 |
+
#
|
50 |
+
|
51 |
+
|
52 |
+
percent = percent /100
|
53 |
+
down = down_payment(price,20)
|
54 |
+
loan = price - down
|
55 |
+
months = years*12
|
56 |
+
interest_monthly = percent/12
|
57 |
+
interest_plus = interest_monthly + 1
|
58 |
+
exponent = (interest_plus)**(-1*months)
|
59 |
+
subtract = 1 - exponent
|
60 |
+
division = interest_monthly / subtract
|
61 |
+
payment = division * loan
|
62 |
+
|
63 |
+
|
64 |
+
return(payment)
|
65 |
+
|
66 |
+
#to do
|
67 |
+
def price_mine(pid):
|
68 |
+
#Currently this function takes an input of a URL and returns the listing prices
|
69 |
+
#The site it mines is remax
|
70 |
+
#The input must be a string input, we can reformat the input to force this to work
|
71 |
+
#Next we use regex to remove space and commas and dollar signs
|
72 |
+
#need to get from a product id to a price
|
73 |
+
prices = 0
|
74 |
+
prices = float(prices)
|
75 |
+
|
76 |
+
return prices
|
77 |
+
|
78 |
+
|
79 |
+
def cap_rate(monthly_income, price):
|
80 |
+
#This function takes net income, and price and calculates the cap rate
|
81 |
+
#
|
82 |
+
cap_rate = ((monthly_income*12) / price)*100
|
83 |
+
|
84 |
+
return cap_rate
|
85 |
+
|
86 |
+
|
87 |
+
def cash_on_cash(monthly_income, down_payment):
|
88 |
+
cash_return = ((monthly_income*12)/down_payment)*100
|
89 |
+
return cash_return
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
def query_postgresql(
|
94 |
+
query: str,
|
95 |
+
database: str,
|
96 |
+
user: str,
|
97 |
+
password: str,
|
98 |
+
host: str,
|
99 |
+
port: str,
|
100 |
+
named_columns: bool=True
|
101 |
+
):
|
102 |
+
|
103 |
+
conn = psycopg2.connect(
|
104 |
+
database=database,
|
105 |
+
user=user,
|
106 |
+
password=password,
|
107 |
+
host=host,
|
108 |
+
port=port
|
109 |
+
)
|
110 |
+
|
111 |
+
cur = conn.cursor()
|
112 |
+
cur.execute(query)
|
113 |
+
rows = cur.fetchall()
|
114 |
+
|
115 |
+
if named_columns:
|
116 |
+
column_names = [desc[0] for desc in cur.description]
|
117 |
+
return [ dict(zip(column_names, r)) for r in rows ]
|
118 |
+
|
119 |
+
return rows
|
120 |
+
|
121 |
+
def query_postgresql_realvest(query: str, named_columns: bool=True):
|
122 |
+
import streamlit as st
|
123 |
+
POSTGRESQL_REALVEST_USER = st.secrets["POSTGRESQL_REALVEST_USER"]
|
124 |
+
POSTGRESQL_REALVEST_PSWD = st.secrets["POSTGRESQL_REALVEST_PSWD"]
|
125 |
+
|
126 |
+
return query_postgresql(
|
127 |
+
query,
|
128 |
+
database="realvest",
|
129 |
+
user=POSTGRESQL_REALVEST_USER,
|
130 |
+
password=POSTGRESQL_REALVEST_PSWD,
|
131 |
+
host="realvest.cdb5lmqrlgu5.us-east-2.rds.amazonaws.com",
|
132 |
+
port="5432",
|
133 |
+
named_columns=named_columns
|
134 |
+
)
|
135 |
+
|
136 |
+
def summarize_products(products: str) -> str:
|
137 |
+
"""
|
138 |
+
Input:
|
139 |
+
products = [
|
140 |
+
{text information of product#1},
|
141 |
+
{text information of product#2},
|
142 |
+
{text information of product#3},
|
143 |
+
]
|
144 |
+
|
145 |
+
Output:
|
146 |
+
summary = "{summary of all products}"
|
147 |
+
"""
|
148 |
+
NEW_LINE = '\n'
|
149 |
+
prompt = f"""
|
150 |
+
You are a highly experienced commercial asset investor. You are writing an investement analysis report based on the data provided. You tends to be critical as your goal is to sift through and to identify promising oppurtunities. Try to use the following instructions to write it.
|
151 |
+
For Executive Summary:
|
152 |
+
Brief overview of the property, location, asking price, and the potential investment opportunity.
|
153 |
+
Property Description: Detailed description of the property, including number of units, age of the building, renovations, amenities, and other notable features.
|
154 |
+
|
155 |
+
for Demographics Analysis: based on the location, come up with basic population, income, age, crime, tax, traffic information.
|
156 |
+
|
157 |
+
for Market Analysis: Detailed analysis of the local market, including demographics, competition, growth projections, and other relevant factors. Make sure to call out the specific category of the business and its typical range of multipliers.
|
158 |
+
|
159 |
+
for Financial Analysis: Detailed review of the property's financials, look into the finance_info and description, including the key metrics like CAP, COC, GRM, NIM, DCR, ER per Unit, Price per Unit, GOI, NOI, Total Debt Service, Cash Flow, ROI, IRR, Equity Buildup Rate, BER, and LTV. Each metric should be calculated and explained. Given the specific business category, provide the key metrics specific to this category. Export in a format that is in a table. Most important information: cash flow, cap rate and EBITDA
|
160 |
+
|
161 |
+
for Risk Assessment: Detailed analysis of potential risks, including economic, industry, and location-specific risks. Call out specific risks that are relevant to the property.
|
162 |
+
|
163 |
+
for Follow up questions: come up with questions and to do list with contact info.
|
164 |
+
|
165 |
+
for Conclusion and Recommendation: Final thoughts on the investment opportunity, including whether it meets your investment criteria and objectives.
|
166 |
+
|
167 |
+
Here is the input data in JSON format, try to parse out the metrics and location information, if the data is not include, try to use your own knoweldge and expertise to fill in.
|
168 |
+
|
169 |
+
|
170 |
+
{products}
|
171 |
+
|
172 |
+
|
173 |
+
Please write a insightful summary (display as Markup) and try your best to fill in data and what you know with the following format:
|
174 |
+
|
175 |
+
#Investment Sample Report:
|
176 |
+
|
177 |
+
##Executive Summary:
|
178 |
+
|
179 |
+
##Demographics Analysis:
|
180 |
+
|
181 |
+
##Market Analysis:
|
182 |
+
|
183 |
+
##Financial Analysis:
|
184 |
+
|
185 |
+
##Risk Assessment:
|
186 |
+
|
187 |
+
##Follow up questions:
|
188 |
+
|
189 |
+
##Conclusion and Recommendation:
|
190 |
+
|
191 |
+
"""
|
192 |
+
#print(f"prompt: {prompt}")
|
193 |
+
|
194 |
+
openai.api_key = OPENAI_API_KEY
|
195 |
+
completion = openai.ChatCompletion.create(
|
196 |
+
model="gpt-4",
|
197 |
+
messages=[
|
198 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
199 |
+
{"role": "user", "content": prompt}
|
200 |
+
]
|
201 |
+
)
|
202 |
+
|
203 |
+
summary = completion.choices[0].message
|
204 |
+
return summary
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
|
209 |
+
if __name__ == "__main__":
|
210 |
+
query = """
|
211 |
+
select productid, category, established_year name, alternatename,
|
212 |
+
offers,asking_price, cash_flow, finance_info, rent, listing_profile, description
|
213 |
+
from main_products
|
214 |
+
limit 1
|
215 |
+
"""
|
216 |
+
|
217 |
+
results = query_postgresql_realvest(query)
|
218 |
+
#loop through results and cancatenate each row into a json string
|
219 |
+
|
220 |
+
|
221 |
+
# Assuming results are a list of tuples
|
222 |
+
|
223 |
+
for row in results:
|
224 |
+
print(row['productid'])
|
225 |
+
str_data = ""
|
226 |
+
str_data += json.dumps({k: str(v) if isinstance(v, Decimal) else v for k, v in row.items()})
|
227 |
+
|
228 |
+
summary = ""
|
229 |
+
summary = summarize_products(str_data)
|
230 |
+
print(summary['content'])
|