Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from llama_index.embeddings.gemini import GeminiEmbedding
|
|
| 12 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 13 |
from llama_index.readers.web import FireCrawlWebReader
|
| 14 |
from llama_index.core import SummaryIndex
|
| 15 |
-
import streamlit_analytics2 as streamlit_analytics
|
| 16 |
import time
|
| 17 |
import dotenv
|
| 18 |
|
|
@@ -99,71 +99,71 @@ Be the programmer you've always wanted to be.
|
|
| 99 |
3. Ask any question you want
|
| 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 |
try:
|
| 138 |
chat_engine = query_index(st.session_state['index'])
|
| 139 |
response = chat_engine.chat(st.session_state['query'])
|
| 140 |
except Exception as e:
|
| 141 |
-
st.error(f"
|
| 142 |
-
st.
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
st.sidebar.
|
| 163 |
-
for role, message in st.session_state['chat_history']:
|
| 164 |
-
st.sidebar.text(f"{role}: {message}")
|
| 165 |
-
|
| 166 |
-
# Clear chat history button in sidebar
|
| 167 |
-
if st.sidebar.button("Clear Chat History"):
|
| 168 |
-
st.session_state['chat_history'] = []
|
| 169 |
-
st.sidebar.success("Chat history cleared!")
|
|
|
|
| 12 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 13 |
from llama_index.readers.web import FireCrawlWebReader
|
| 14 |
from llama_index.core import SummaryIndex
|
| 15 |
+
#import streamlit_analytics2 as streamlit_analytics
|
| 16 |
import time
|
| 17 |
import dotenv
|
| 18 |
|
|
|
|
| 99 |
3. Ask any question you want
|
| 100 |
""")
|
| 101 |
|
| 102 |
+
|
| 103 |
+
# URL input for document ingestion
|
| 104 |
+
st.session_state['url'] = st.text_input("Enter URL to crawl and ingest documents (optional):", value=st.session_state['url'])
|
| 105 |
+
|
| 106 |
+
# Collection name input
|
| 107 |
+
st.session_state['collection_name'] = st.text_input("Enter collection name for vector store (compulsory):", value=st.session_state['collection_name'])
|
| 108 |
+
|
| 109 |
+
# Combined Ingest and Setup button
|
| 110 |
+
if st.button("Ingest and Setup"):
|
| 111 |
+
with st.spinner("Setting up query engine..."):
|
| 112 |
+
embed_setup()
|
| 113 |
+
client = qdrant_setup()
|
| 114 |
+
llm = llm_setup()
|
| 115 |
+
vector_store = QdrantVectorStore(client=client, collection_name=st.session_state['collection_name'])
|
| 116 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 117 |
+
|
| 118 |
+
if st.session_state['url']:
|
| 119 |
+
st.session_state['documents'] = ingest_documents(st.session_state['url'])
|
| 120 |
+
st.session_state['index'] = VectorStoreIndex.from_documents(st.session_state['documents'], vector_store=vector_store, storage_context=storage_context)
|
| 121 |
+
st.success(f"Documents ingested from {st.session_state['url']} and query engine setup completed successfully!")
|
| 122 |
+
else:
|
| 123 |
+
st.session_state['index'] = VectorStoreIndex.from_vector_store(vector_store=vector_store, storage_context=storage_context)
|
| 124 |
+
st.success(f"Query engine setup completed successfully using existing collection: {st.session_state['collection_name']}")
|
| 125 |
+
|
| 126 |
+
st.session_state['setup_complete'] = True
|
| 127 |
+
|
| 128 |
+
# Query input
|
| 129 |
+
st.session_state['query'] = st.text_input("Enter your query:", value=st.session_state['query'])
|
| 130 |
+
|
| 131 |
+
# Search button
|
| 132 |
+
if st.button("Search"):
|
| 133 |
+
if not st.session_state['setup_complete']:
|
| 134 |
+
st.error("Please complete the setup first")
|
| 135 |
+
elif st.session_state['query']:
|
| 136 |
+
with st.spinner("Searching..."):
|
| 137 |
+
try:
|
| 138 |
+
chat_engine = query_index(st.session_state['index'])
|
| 139 |
+
response = chat_engine.chat(st.session_state['query'])
|
| 140 |
+
except Exception as e:
|
| 141 |
+
st.error(f"An error occurred: {str(e)}")
|
| 142 |
+
st.info("Retrying in 120 seconds...")
|
| 143 |
+
time.sleep(120)
|
| 144 |
try:
|
| 145 |
chat_engine = query_index(st.session_state['index'])
|
| 146 |
response = chat_engine.chat(st.session_state['query'])
|
| 147 |
except Exception as e:
|
| 148 |
+
st.error(f"Retry failed. Error: {str(e)}")
|
| 149 |
+
st.stop()
|
| 150 |
+
|
| 151 |
+
# Add the query and response to chat history
|
| 152 |
+
st.session_state['chat_history'].append(("User", st.session_state['query']))
|
| 153 |
+
st.session_state['chat_history'].append(("Assistant", str(response.response)))
|
| 154 |
+
|
| 155 |
+
# Display the most recent response prominently
|
| 156 |
+
st.subheader("Assistant's Response:")
|
| 157 |
+
st.write(response.response)
|
| 158 |
+
else:
|
| 159 |
+
st.error("Please enter a query")
|
| 160 |
+
|
| 161 |
+
# Sidebar for chat history
|
| 162 |
+
st.sidebar.title("Chat History")
|
| 163 |
+
for role, message in st.session_state['chat_history']:
|
| 164 |
+
st.sidebar.text(f"{role}: {message}")
|
| 165 |
+
|
| 166 |
+
# Clear chat history button in sidebar
|
| 167 |
+
if st.sidebar.button("Clear Chat History"):
|
| 168 |
+
st.session_state['chat_history'] = []
|
| 169 |
+
st.sidebar.success("Chat history cleared!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|