Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +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 |
|
16 |
# Setup functions
|
17 |
def embed_setup():
|
@@ -84,51 +85,53 @@ if 'chat_history' not in st.query_params:
|
|
84 |
if 'last_response' not in st.query_params:
|
85 |
st.query_params['last_response'] = None
|
86 |
|
87 |
-
# URL input for document ingestion
|
88 |
-
url = st.text_input("Enter URL to crawl and ingest documents:")
|
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 |
-
|
|
|
|
|
|
|
|
|
|
12 |
from llama_index.core.memory import ChatMemoryBuffer
|
13 |
from llama_index.readers.web import FireCrawlWebReader
|
14 |
from llama_index.core import SummaryIndex
|
15 |
+
from streamlit_analytics2 import streamlit_analytics
|
16 |
|
17 |
# Setup functions
|
18 |
def embed_setup():
|
|
|
85 |
if 'last_response' not in st.query_params:
|
86 |
st.query_params['last_response'] = None
|
87 |
|
|
|
|
|
88 |
|
89 |
+
with streamlit_analytics.track():
|
90 |
+
# URL input for document ingestion
|
91 |
+
url = st.text_input("Enter URL to crawl and ingest documents:")
|
92 |
+
|
93 |
+
# Combined Ingest and Setup button
|
94 |
+
if st.button("Ingest and Setup"):
|
95 |
+
if url:
|
96 |
+
with st.spinner("Crawling, ingesting documents, and setting up query engine..."):
|
97 |
+
st.query_params['documents'] = ingest_documents(url)
|
98 |
+
embed_setup()
|
99 |
+
client = qdrant_setup()
|
100 |
+
llm = llm_setup()
|
101 |
+
vector_store = QdrantVectorStore(client=client, collection_name=os.getenv("COLLECTION_NAME"))
|
102 |
+
index = VectorStoreIndex.from_documents(st.query_params['documents'], vector_store=vector_store)
|
103 |
+
st.query_params['chat_engine'] = query_index(index)
|
104 |
+
st.success(f"Documents ingested from {url} and query engine setup completed successfully!")
|
105 |
+
else:
|
106 |
+
st.error("Please enter a URL")
|
107 |
+
|
108 |
+
# Query input
|
109 |
+
query = st.text_input("Enter your query:")
|
110 |
+
|
111 |
+
# Search button
|
112 |
+
if st.button("Search"):
|
113 |
+
if st.query_params['chat_engine'] is None:
|
114 |
+
st.error("Please complete the setup first")
|
115 |
+
elif query:
|
116 |
+
with st.spinner("Searching..."):
|
117 |
+
response = st.query_params['chat_engine'].chat(query)
|
118 |
+
|
119 |
+
# Add the query and response to chat history
|
120 |
+
st.query_params['chat_history'].append(("User", query))
|
121 |
+
st.query_params['chat_history'].append(("Assistant", str(response.response)))
|
122 |
+
|
123 |
+
# Display the most recent response prominently
|
124 |
+
st.subheader("Assistant's Response:")
|
125 |
+
st.write(response.response)
|
126 |
+
else:
|
127 |
+
st.error("Please enter a query")
|
128 |
+
|
129 |
+
# Sidebar for chat history
|
130 |
+
st.sidebar.title("Chat History")
|
131 |
+
for role, message in st.query_params['chat_history']:
|
132 |
+
st.sidebar.text(f"{role}: {message}")
|
133 |
+
|
134 |
+
# Clear chat history button in sidebar
|
135 |
+
if st.sidebar.button("Clear Chat History"):
|
136 |
+
st.query_params['chat_history'] = []
|
137 |
+
st.sidebar.success("Chat history cleared!")
|