diff --git a/GnosisPages.py b/GnosisPages.py index dd1ce54..06e8eed 100644 --- a/GnosisPages.py +++ b/GnosisPages.py @@ -24,9 +24,11 @@ if "wk_button" not in st.session_state: st.session_state.wk_button = False +if "chroma_db" not in st.session_state: + st.session_state.chroma_db = ChromaDB(openai.api_key) # Build settings -chroma_db = ChromaDB(openai.api_key) +chroma_db = st.session_state.chroma_db collection = settings.build(chroma_db) # Sidebar diff --git a/gnosis/search.py b/gnosis/search.py index 5eb10b0..8a1f344 100644 --- a/gnosis/search.py +++ b/gnosis/search.py @@ -1,6 +1,5 @@ """Search Tool""" - class Search: """Search Tool""" @@ -10,7 +9,18 @@ def __init__(self, chroma_db): def run(self, query: str): """Run the Agent""" + if not self.chroma_db.api_key: + return "No API key set. Please add your OpenAI API key in the sidebar." + collection = self.chroma_db.get_collection("pdf-explainer") + + # get_collection returns a Streamlit object if something went wrong + if not hasattr(collection, "query"): + return "Could not access the collection. Please check your API key." + + if collection.count() == 0: + return "No documents found. Please upload a PDF first." + results = collection.query(query_texts=[query], n_results=5)["documents"][0] return "\n\n---\n\n".join(results)