wt002 commited on
Commit
dcf110e
·
verified ·
1 Parent(s): fe46496

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -42,13 +42,18 @@ if not HF_API_TOKEN:
42
 
43
  # --- Global Vector Store and Embeddings ---
44
  try:
 
 
 
 
45
  embeddings = HuggingFaceEmbeddings(model_name=HF_EMBEDDING_MODEL_ID)
46
  logger.info(f"Initialized HuggingFaceEmbeddings with model: {HF_EMBEDDING_MODEL_ID}")
47
  except Exception as e:
48
  logger.error(f"Failed to initialize HuggingFaceEmbeddings: {e}. Please ensure the model_id is correct and dependencies are installed.")
49
  embeddings = None
50
 
51
- vectorstore = DocArrayInMemorySearch(embedding_function=embeddings) if embeddings else None
 
52
  text_splitter = RecursiveCharacterTextSplitter(
53
  chunk_size=1000,
54
  chunk_overlap=200,
@@ -77,8 +82,8 @@ def add_document_to_vector_store(content: str, source: str, metadata: dict = Non
77
  Adds content to the global vector store.
78
  Chunks the content and creates LangChain Documents.
79
  """
80
- if vectorstore is None:
81
- logger.warning("Vector store not initialized. Cannot add document.")
82
  return
83
 
84
  try:
@@ -90,11 +95,26 @@ def add_document_to_vector_store(content: str, source: str, metadata: dict = Non
90
  doc_metadata.update(metadata)
91
  docs.append(Document(page_content=chunk, metadata=doc_metadata))
92
 
93
- vectorstore.add_documents(docs)
 
94
  logger.info(f"Added {len(docs)} chunks from '{source}' to the vector store.")
95
  except Exception as e:
96
  logger.error(f"Error adding document from '{source}' to vector store: {e}")
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  # --- Enhanced Tools ---
99
  class WikiSearchTool(Tool):
100
  """Enhanced Wikipedia search with better formatting and error handling"""
 
42
 
43
  # --- Global Vector Store and Embeddings ---
44
  try:
45
+ # Make sure to import HuggingFaceEmbeddings from the new package
46
+ # if you followed the previous advice to resolve the deprecation warning
47
+ from langchain_huggingface import HuggingFaceEmbeddings # Or keep langchain_community if you haven't migrated yet
48
+
49
  embeddings = HuggingFaceEmbeddings(model_name=HF_EMBEDDING_MODEL_ID)
50
  logger.info(f"Initialized HuggingFaceEmbeddings with model: {HF_EMBEDDING_MODEL_ID}")
51
  except Exception as e:
52
  logger.error(f"Failed to initialize HuggingFaceEmbeddings: {e}. Please ensure the model_id is correct and dependencies are installed.")
53
  embeddings = None
54
 
55
+ # Initialize DocArrayInMemorySearch WITHOUT the embedding_function argument here
56
+ vectorstore = DocArrayInMemorySearch() if embeddings else None # <--- FIXED THIS LINE
57
  text_splitter = RecursiveCharacterTextSplitter(
58
  chunk_size=1000,
59
  chunk_overlap=200,
 
82
  Adds content to the global vector store.
83
  Chunks the content and creates LangChain Documents.
84
  """
85
+ if vectorstore is None or embeddings is None: # Added check for embeddings too
86
+ logger.warning("Vector store or embeddings not initialized. Cannot add document.")
87
  return
88
 
89
  try:
 
95
  doc_metadata.update(metadata)
96
  docs.append(Document(page_content=chunk, metadata=doc_metadata))
97
 
98
+ # Pass the embeddings function here when adding documents
99
+ vectorstore.add_documents(docs, embedding=embeddings) # <--- IMPORTANT: Pass embeddings here
100
  logger.info(f"Added {len(docs)} chunks from '{source}' to the vector store.")
101
  except Exception as e:
102
  logger.error(f"Error adding document from '{source}' to vector store: {e}")
103
 
104
+ # ... (inside ResearchAgent's __call__ method) ...
105
+ def __call__(self, question: str) -> str:
106
+ logger.info(f"Received question: {question[:200]}...")
107
+ try:
108
+ global vectorstore
109
+ if embeddings:
110
+ # Re-initialize vectorstore without embedding_function
111
+ vectorstore = DocArrayInMemorySearch() # <--- FIXED THIS LINE as well
112
+ logger.info("DocArrayInMemorySearch re-initialized for new session.")
113
+ else:
114
+ logger.warning("Embeddings not initialized, cannot re-initialize DocArrayInMemorySearch.")
115
+ return "Error: Embedding model not loaded, cannot process request."
116
+
117
+
118
  # --- Enhanced Tools ---
119
  class WikiSearchTool(Tool):
120
  """Enhanced Wikipedia search with better formatting and error handling"""