DrishtiSharma
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -204,7 +204,6 @@ def run_agent(
|
|
204 |
temperature: float=0.7,
|
205 |
agent_type: Literal["Tool Calling", "ReAct"]="Tool Calling",
|
206 |
) -> Union[str, None]:
|
207 |
-
|
208 |
"""
|
209 |
Generate text based on user queries.
|
210 |
Args:
|
@@ -216,40 +215,42 @@ def run_agent(
|
|
216 |
agent_type: 'Tool Calling' or 'ReAct'
|
217 |
Return:
|
218 |
generated text
|
219 |
-
The chat prompt and message history are stored in
|
220 |
-
st.session_state variables.
|
221 |
"""
|
222 |
|
223 |
try:
|
224 |
-
# Ensure retriever tool is included
|
225 |
if "Retrieval" in st.session_state.tool_names[0]:
|
226 |
if st.session_state.retriever_tool:
|
227 |
-
|
|
|
228 |
tools.append(st.session_state.retriever_tool)
|
229 |
-
|
230 |
else:
|
231 |
-
st.error("Retriever tool is not initialized. Please create a vector store first.")
|
|
|
|
|
|
|
|
|
232 |
|
233 |
-
# Debugging: Print tools being used
|
234 |
-
st.write("**Tools being used by the agent:**", [tool.name for tool in tools])
|
235 |
if "retriever" in [tool.name for tool in tools]:
|
236 |
-
st.
|
237 |
-
|
238 |
-
st.
|
239 |
|
240 |
-
|
241 |
llm = get_chat_model(model, temperature, [StreamHandler(st.empty())])
|
242 |
if llm is None:
|
243 |
-
st.error(f"Unsupported model: {model}", icon="π¨")
|
244 |
return None
|
245 |
|
|
|
246 |
if agent_type == "Tool Calling":
|
247 |
chat_history = st.session_state.history
|
248 |
else:
|
249 |
chat_history = message_history_to_string()
|
250 |
|
251 |
history_query = {"chat_history": chat_history, "input": query}
|
252 |
-
|
253 |
# Generate message content
|
254 |
message_with_no_image = st.session_state.chat_prompt.invoke(history_query)
|
255 |
message_content = message_with_no_image.messages[0].content
|
@@ -286,7 +287,6 @@ def run_agent(
|
|
286 |
return None
|
287 |
|
288 |
|
289 |
-
|
290 |
def openai_create_image(
|
291 |
description: str, model: str="dall-e-3", size: str="1024x1024"
|
292 |
) -> Optional[str]:
|
@@ -379,10 +379,14 @@ def get_vector_store(uploaded_files: List[UploadedFile]) -> Optional[FAISS]:
|
|
379 |
def get_retriever() -> None:
|
380 |
"""
|
381 |
Upload document(s), create a vector store, prepare a retriever tool,
|
382 |
-
save the tool to the variable st.session_state.retriever_tool
|
383 |
"""
|
|
|
|
|
384 |
st.write("")
|
385 |
st.write("**Query Document(s)**")
|
|
|
|
|
386 |
uploaded_files = st.file_uploader(
|
387 |
label="Upload an article",
|
388 |
type=["txt", "pdf", "docx"],
|
@@ -391,29 +395,42 @@ def get_retriever() -> None:
|
|
391 |
key="document_upload_" + str(st.session_state.uploader_key),
|
392 |
)
|
393 |
|
394 |
-
|
395 |
-
|
396 |
-
|
|
|
|
|
|
|
|
|
|
|
397 |
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
retriever = vector_store.as_retriever()
|
404 |
-
st.session_state.retriever_tool = create_retriever_tool(
|
405 |
-
retriever,
|
406 |
-
name="retriever",
|
407 |
-
description="Search uploaded documents for information when queried.",
|
408 |
-
)
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
st.session_state.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
413 |
|
414 |
-
st.success("Retriever tool has been successfully initialized and is ready to use.")
|
415 |
-
else:
|
416 |
-
st.error("Failed to create vector store. Please check the uploaded files.")
|
417 |
|
418 |
|
419 |
|
|
|
204 |
temperature: float=0.7,
|
205 |
agent_type: Literal["Tool Calling", "ReAct"]="Tool Calling",
|
206 |
) -> Union[str, None]:
|
|
|
207 |
"""
|
208 |
Generate text based on user queries.
|
209 |
Args:
|
|
|
215 |
agent_type: 'Tool Calling' or 'ReAct'
|
216 |
Return:
|
217 |
generated text
|
|
|
|
|
218 |
"""
|
219 |
|
220 |
try:
|
221 |
+
# Ensure retriever tool is included when "Retrieval" is selected
|
222 |
if "Retrieval" in st.session_state.tool_names[0]:
|
223 |
if st.session_state.retriever_tool:
|
224 |
+
retriever_tool_name = "retriever" # Ensure naming consistency
|
225 |
+
if retriever_tool_name not in [tool.name for tool in tools]:
|
226 |
tools.append(st.session_state.retriever_tool)
|
227 |
+
st.write(f"β
**{retriever_tool_name} tool has been added successfully.**")
|
228 |
else:
|
229 |
+
st.error("β Retriever tool is not initialized. Please create a vector store first.")
|
230 |
+
return None # Exit early to avoid broken tool usage
|
231 |
+
|
232 |
+
# Debugging: Print final tools list
|
233 |
+
st.write("**Final Tools Being Used:**", [tool.name for tool in tools])
|
234 |
|
|
|
|
|
235 |
if "retriever" in [tool.name for tool in tools]:
|
236 |
+
st.success("β
Retriever tool is confirmed and ready for use.")
|
237 |
+
elif "Retrieval" in st.session_state.tool_names[0]:
|
238 |
+
st.warning("β οΈ 'Retrieval' was selected but the retriever tool is missing!")
|
239 |
|
240 |
+
# Initialize the LLM model
|
241 |
llm = get_chat_model(model, temperature, [StreamHandler(st.empty())])
|
242 |
if llm is None:
|
243 |
+
st.error(f"β Unsupported model: {model}", icon="π¨")
|
244 |
return None
|
245 |
|
246 |
+
# Prepare chat history
|
247 |
if agent_type == "Tool Calling":
|
248 |
chat_history = st.session_state.history
|
249 |
else:
|
250 |
chat_history = message_history_to_string()
|
251 |
|
252 |
history_query = {"chat_history": chat_history, "input": query}
|
253 |
+
|
254 |
# Generate message content
|
255 |
message_with_no_image = st.session_state.chat_prompt.invoke(history_query)
|
256 |
message_content = message_with_no_image.messages[0].content
|
|
|
287 |
return None
|
288 |
|
289 |
|
|
|
290 |
def openai_create_image(
|
291 |
description: str, model: str="dall-e-3", size: str="1024x1024"
|
292 |
) -> Optional[str]:
|
|
|
379 |
def get_retriever() -> None:
|
380 |
"""
|
381 |
Upload document(s), create a vector store, prepare a retriever tool,
|
382 |
+
save the tool to the variable st.session_state.retriever_tool.
|
383 |
"""
|
384 |
+
|
385 |
+
# Section Title
|
386 |
st.write("")
|
387 |
st.write("**Query Document(s)**")
|
388 |
+
|
389 |
+
# File Upload Input
|
390 |
uploaded_files = st.file_uploader(
|
391 |
label="Upload an article",
|
392 |
type=["txt", "pdf", "docx"],
|
|
|
395 |
key="document_upload_" + str(st.session_state.uploader_key),
|
396 |
)
|
397 |
|
398 |
+
# Check if files are uploaded
|
399 |
+
if uploaded_files:
|
400 |
+
# Use a unique button key to avoid duplicate presses
|
401 |
+
if st.button(label="Create the vector store", key=f"create_vector_{st.session_state.uploader_key}"):
|
402 |
+
st.info("Creating the vector store and initializing the retriever tool...")
|
403 |
+
|
404 |
+
# Attempt to create the vector store
|
405 |
+
vector_store = get_vector_store(uploaded_files)
|
406 |
|
407 |
+
if vector_store:
|
408 |
+
uploaded_file_names = [file.name for file in uploaded_files]
|
409 |
+
st.session_state.vector_store_message = (
|
410 |
+
f"Vector store for :blue[[{', '.join(uploaded_file_names)}]] is ready!"
|
411 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
|
413 |
+
# Initialize retriever and create tool
|
414 |
+
retriever = vector_store.as_retriever()
|
415 |
+
st.session_state.retriever_tool = create_retriever_tool(
|
416 |
+
retriever,
|
417 |
+
name="retriever",
|
418 |
+
description="Search uploaded documents for information when queried.",
|
419 |
+
)
|
420 |
+
|
421 |
+
# Add "Retrieval" to the tools list if not already present
|
422 |
+
if "Retrieval" not in st.session_state.tool_names[0]:
|
423 |
+
st.session_state.tool_names[0].append("Retrieval")
|
424 |
+
|
425 |
+
st.success("β
Retriever tool has been successfully initialized and is ready to use.")
|
426 |
+
|
427 |
+
# Debugging output
|
428 |
+
st.write("**Current Tools:**", st.session_state.tool_names[0])
|
429 |
+
else:
|
430 |
+
st.error("β Failed to create vector store. Please check the uploaded files (supported formats: txt, pdf, docx).")
|
431 |
+
else:
|
432 |
+
st.info("Please upload document(s) to create the vector store.")
|
433 |
|
|
|
|
|
|
|
434 |
|
435 |
|
436 |
|