from time import sleep
import streamlit as st
import openai
import pinecone
from postgres_db import query_postgresql_realvest
PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
INDEX_NAME = 'realvest-data-v2'
EMBEDDING_MODEL = "text-embedding-ada-002" # OpenAI's best embeddings as of Apr 2023
MAX_LENGTH_DESC = 200
MATCH_SCORE_THR = 0.0
TOP_K = 20
def query_pinecone(xq, top_k: int=3, include_metadata: bool=True, sleep_time: int=10):
MAX_TRIALS = 5
trial = 0
out = None
while (out is None) and (trial < MAX_TRIALS):
try:
out = st.session_state['index'].query(xq, top_k=top_k, include_metadata=include_metadata)
return out
except pinecone.core.exceptions.PineconeProtocolError as err:
print(f"Error, sleep! {err}")
sleep(sleep_time)
trial = trial + 1
return out
def sort_dict_by_value(d: dict, ascending: bool=True):
"""
Sort dictionary {k1: v1, k2: v2} by its value. The output is
a sorted list of tuples [(k1, v1), (k2, v2)]
"""
return sorted(d.items(), key=lambda x: x[1], reverse=not ascending)
# initialize connection to pinecone (get API key at app.pinecone.io)
from tenacity import retry, stop_after_attempt, wait_fixed
pinecone.init(
api_key=PINECONE_API_KEY,
environment="us-central1-gcp" # may be different, check at app.pinecone.io
)
@retry(stop=stop_after_attempt(5), wait=wait_fixed(15))
def setup_pinecone_index():
try:
print("Attempting to set up Pinecone index...") # add this line
if "index" not in st.session_state:
st.session_state['index'] = pinecone.Index(INDEX_NAME)
return st.session_state['index']
except AttributeError as e:
print("Caught an AttributeError:", e)
raise # Re-raise the exception so that tenacity can catch it and retry
except Exception as e: # add this block
print("Caught an unexpected exception:", e)
raise
def init_session_state():
try:
st.session_state['index'] = setup_pinecone_index()
# stats = test_pinecone()
except Exception as e:
print("Failed to set up Pinecone index after several attempts. Error:", e)
if 'display_results' not in st.session_state:
st.session_state['display_results'] = False
if 'count_checked' not in st.session_state:
st.session_state['count_checked'] = 0
def callback_count_checked():
st.session_state['count_checked'] = 0
st.session_state['checked_boxes'] = []
for key in list(st.session_state.keys()):
if (key.split('__')[0] == 'cb_compare') and (st.session_state[key] == True):
st.session_state['count_checked'] += 1
st.session_state['checked_boxes'].append(key)
def summarize_products(products: list) -> str:
"""
Input:
products = [
{text information of product#1},
{text information of product#2},
{text information of product#3},
]
Output:
summary = "{summary of all products}"
"""
NEW_LINE = '\n'
prompt = f"""
Based on the product information below, please read and try to understand it.
{ f"{NEW_LINE*2}---{NEW_LINE*2}".join(products) }
Please write a concise and insightful summary table (display as HTML) to compare the products for investors, which should inlcude but not limited to:
- description
- category
- asking price
- location
- potential profit margin
"""
print(f"prompt: {prompt}")
openai.api_key = OPENAI_API_KEY
completion = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
summary = completion.choices[0].message
return summary
### Main
# st.set_page_config(layout="centered")
css='''
'''
st.markdown(css, unsafe_allow_html=True)
hide_footer_style = """