Prat0 commited on
Commit
2a104f1
·
verified ·
1 Parent(s): 55c15b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -24
app.py CHANGED
@@ -14,6 +14,11 @@ 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
 
18
  # Initialize session state
19
  if 'setup_complete' not in st.session_state:
@@ -24,8 +29,15 @@ if 'chat_history' not in st.session_state:
24
  st.session_state['chat_history'] = []
25
  if 'index' not in st.session_state:
26
  st.session_state['index'] = None
 
 
 
 
 
 
27
 
28
  os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
 
29
 
30
  # Setup functions
31
  def embed_setup():
@@ -81,58 +93,65 @@ st.title("Talk to Software Documentation")
81
 
82
  st.markdown("""
83
  This tool allows you to chat with software documentation. Here's how to use it:
84
- 1. Enter the URL of the documentation you want to chat about.
85
- 2. Click the "Ingest and Setup" button to crawl the documentation and set up the query engine.
86
- 3. Once setup is complete, enter your query in the text box.
87
- 4. Click "Search" to get a response based on the documentation.
88
- 5. View your chat history in the sidebar.
 
89
  """)
90
 
91
  with streamlit_analytics.track():
92
  # URL input for document ingestion
93
- url = st.text_input("Enter URL to crawl and ingest documents:")
 
 
 
94
 
95
  # Combined Ingest and Setup button
96
  if st.button("Ingest and Setup"):
97
- if url:
98
- with st.spinner("Crawling, ingesting documents, and setting up query engine..."):
99
- st.session_state['documents'] = ingest_documents(url)
100
- embed_setup()
101
- client = qdrant_setup()
102
- llm = llm_setup()
103
- vector_store = QdrantVectorStore(client=client, collection_name="testdoc1")
104
- index = VectorStoreIndex.from_documents(st.session_state['documents'], vector_store=vector_store)
105
- st.session_state['index'] = index
106
- st.session_state['setup_complete'] = True
107
- st.success(f"Documents ingested from {url} and query engine setup completed successfully!")
108
- else:
109
- st.error("Please enter a URL")
 
 
 
110
 
111
  # Query input
112
- query = st.text_input("Enter your query:(please click on the search button, do not just press enter)")
113
 
114
  # Search button
115
  if st.button("Search"):
116
  if not st.session_state['setup_complete']:
117
  st.error("Please complete the setup first")
118
- elif query:
119
  with st.spinner("Searching..."):
120
  try:
121
  chat_engine = query_index(st.session_state['index'])
122
- response = chat_engine.chat(query)
123
  except Exception as e:
124
  st.error(f"An error occurred: {str(e)}")
125
  st.info("Retrying in 120 seconds...")
126
  time.sleep(120)
127
  try:
128
  chat_engine = query_index(st.session_state['index'])
129
- response = chat_engine.chat(query)
130
  except Exception as e:
131
  st.error(f"Retry failed. Error: {str(e)}")
132
  st.stop()
133
 
134
  # Add the query and response to chat history
135
- st.session_state['chat_history'].append(("User", query))
136
  st.session_state['chat_history'].append(("Assistant", str(response.response)))
137
 
138
  # Display the most recent response prominently
 
14
  from llama_index.core import SummaryIndex
15
  import streamlit_analytics2 as streamlit_analytics
16
  import time
17
+ import dotenv
18
+
19
+ dotenv.load_dotenv()
20
+ # Set page config
21
+ #st.set_page_config(page_title="Talk to Software Documentation", page_icon="📚", layout="wide")
22
 
23
  # Initialize session state
24
  if 'setup_complete' not in st.session_state:
 
29
  st.session_state['chat_history'] = []
30
  if 'index' not in st.session_state:
31
  st.session_state['index'] = None
32
+ if 'url' not in st.session_state:
33
+ st.session_state['url'] = ""
34
+ if 'collection_name' not in st.session_state:
35
+ st.session_state['collection_name'] = ""
36
+ if 'query' not in st.session_state:
37
+ st.session_state['query'] = ""
38
 
39
  os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
40
+ os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
41
 
42
  # Setup functions
43
  def embed_setup():
 
93
 
94
  st.markdown("""
95
  This tool allows you to chat with software documentation. Here's how to use it:
96
+ 1. Enter the URL of the documentation you want to chat about (optional if using an existing collection).
97
+ 2. Enter the collection name for the vector store.
98
+ 3. Click the "Ingest and Setup" button to crawl the documentation (if URL provided) and set up the query engine.
99
+ 4. Once setup is complete, enter your query in the text box.
100
+ 5. Click "Search" to get a response based on the documentation.
101
+ 6. View your chat history in the sidebar.
102
  """)
103
 
104
  with streamlit_analytics.track():
105
  # URL input for document ingestion
106
+ st.session_state['url'] = st.text_input("Enter URL to crawl and ingest documents (optional):", value=st.session_state['url'])
107
+
108
+ # Collection name input
109
+ st.session_state['collection_name'] = st.text_input("Enter collection name for vector store:", value=st.session_state['collection_name'])
110
 
111
  # Combined Ingest and Setup button
112
  if st.button("Ingest and Setup"):
113
+ with st.spinner("Setting up query engine..."):
114
+ embed_setup()
115
+ client = qdrant_setup()
116
+ llm = llm_setup()
117
+ vector_store = QdrantVectorStore(client=client, collection_name=st.session_state['collection_name'])
118
+ storage_context = StorageContext.from_defaults(vector_store=vector_store)
119
+
120
+ if st.session_state['url']:
121
+ st.session_state['documents'] = ingest_documents(st.session_state['url'])
122
+ st.session_state['index'] = VectorStoreIndex.from_documents(st.session_state['documents'], vector_store=vector_store, storage_context=storage_context)
123
+ st.success(f"Documents ingested from {st.session_state['url']} and query engine setup completed successfully!")
124
+ else:
125
+ st.session_state['index'] = VectorStoreIndex.from_vector_store(vector_store=vector_store, storage_context=storage_context)
126
+ st.success(f"Query engine setup completed successfully using existing collection: {st.session_state['collection_name']}")
127
+
128
+ st.session_state['setup_complete'] = True
129
 
130
  # Query input
131
+ st.session_state['query'] = st.text_input("Enter your query:", value=st.session_state['query'])
132
 
133
  # Search button
134
  if st.button("Search"):
135
  if not st.session_state['setup_complete']:
136
  st.error("Please complete the setup first")
137
+ elif st.session_state['query']:
138
  with st.spinner("Searching..."):
139
  try:
140
  chat_engine = query_index(st.session_state['index'])
141
+ response = chat_engine.chat(st.session_state['query'])
142
  except Exception as e:
143
  st.error(f"An error occurred: {str(e)}")
144
  st.info("Retrying in 120 seconds...")
145
  time.sleep(120)
146
  try:
147
  chat_engine = query_index(st.session_state['index'])
148
+ response = chat_engine.chat(st.session_state['query'])
149
  except Exception as e:
150
  st.error(f"Retry failed. Error: {str(e)}")
151
  st.stop()
152
 
153
  # Add the query and response to chat history
154
+ st.session_state['chat_history'].append(("User", st.session_state['query']))
155
  st.session_state['chat_history'].append(("Assistant", str(response.response)))
156
 
157
  # Display the most recent response prominently