wt002 commited on
Commit
1c46468
·
verified ·
1 Parent(s): 73c986b

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +30 -19
agent.py CHANGED
@@ -429,31 +429,42 @@ embedding_model = HuggingFaceEmbeddings(
429
  # -----------------------------
430
  # Create FAISS index and save it
431
  # -----------------------------
432
- try:
433
- # Make sure to pass the directory, not the file path
434
- index_dir = "/home/wendy/my_hf_agent_course_projects/faiss_index"
435
- vector_store = FAISS.load_local(index_dir, embedding_model, allow_dangerous_deserialization=True)
436
- except Exception as e:
437
- print(f"Error loading index: {str(e)}")
438
- # Fallback to rebuilding the index if loading fails
439
- vector_store = FAISS.from_documents(docs, embedding_model)
440
 
441
- # -----------------------------
442
- # Load FAISS index properly
443
- # -----------------------------
444
- # Ensure the directory path is correct and points to the index file directly
445
- index_path = "/home/wendy/my_hf_agent_course_projects/faiss_index"
446
- try:
447
- loaded_store = FAISS.load_local(index_path, embedding_model, allow_dangerous_deserialization=True)
448
- print("FAISS index loaded successfully")
449
- except Exception as e:
450
- print(f"Error loading FAISS index: {str(e)}")
 
 
 
 
 
 
 
 
 
 
451
 
 
 
 
452
 
453
  # -----------------------------
454
  # Create LangChain Retriever Tool
455
  # -----------------------------
456
- retriever = loaded_store.as_retriever()
457
 
458
  question_retriever_tool = create_retriever_tool(
459
  retriever=retriever,
 
429
  # -----------------------------
430
  # Create FAISS index and save it
431
  # -----------------------------
432
+ class ChatState(TypedDict):
433
+ messages: Annotated[
434
+ List[str],
435
+ gr.State(render=False),
436
+ "Stores chat history"
437
+ ]
 
 
438
 
439
+ def initialize_vector_store():
440
+ embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
441
+ index_path = "/home/wendy/my_hf_agent_course_projects/faiss_index"
442
+
443
+ if os.path.exists(os.path.join(index_path, "index.faiss")):
444
+ try:
445
+ return FAISS.load_local(
446
+ index_path,
447
+ embedding_model,
448
+ allow_dangerous_deserialization=True
449
+ )
450
+ except Exception as e:
451
+ print(f"Error loading index: {e}")
452
+
453
+ # Fallback: Create new index
454
+ print("Building new vector store...")
455
+ docs = [...] # Your document loading logic here
456
+ vector_store = FAISS.from_documents(docs, embedding_model)
457
+ vector_store.save_local(index_path)
458
+ return vector_store
459
 
460
+ # Initialize at module level
461
+ loaded_store = initialize_vector_store()
462
+ retriever = loaded_store.as_retriever()
463
 
464
  # -----------------------------
465
  # Create LangChain Retriever Tool
466
  # -----------------------------
467
+ #retriever = loaded_store.as_retriever()
468
 
469
  question_retriever_tool = create_retriever_tool(
470
  retriever=retriever,