Spaces:
Build error
Build error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -13,13 +13,15 @@ from langchain_community.document_loaders import ArxivLoader
|
|
| 13 |
from langchain_community.vectorstores import SupabaseVectorStore
|
| 14 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 15 |
from langchain_core.tools import tool
|
| 16 |
-
|
| 17 |
from supabase.client import Client, create_client
|
| 18 |
# --- langchain create_retriever_tool fallback (paste near other imports) ---
|
|
|
|
| 19 |
import traceback
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
| 23 |
HAS_CREATE_RETRIEVER_TOOL = True
|
| 24 |
except Exception:
|
| 25 |
HAS_CREATE_RETRIEVER_TOOL = False
|
|
@@ -55,18 +57,18 @@ except Exception:
|
|
| 55 |
text = d.get("page_content") or d.get("text") or str(d)
|
| 56 |
else:
|
| 57 |
text = str(d)
|
| 58 |
-
|
|
|
|
| 59 |
# return compact result
|
| 60 |
return "\n\n".join(t for t in out_texts if t)
|
| 61 |
|
| 62 |
def create_retriever_tool(retriever, name: str = "retriever", description: str = ""):
|
| 63 |
"""
|
| 64 |
-
Minimal drop-in fallback
|
| 65 |
-
|
| 66 |
"""
|
| 67 |
return _SimpleRetrieverTool(retriever, name=name, description=description)
|
| 68 |
|
| 69 |
-
|
| 70 |
load_dotenv()
|
| 71 |
|
| 72 |
@tool
|
|
@@ -182,7 +184,7 @@ vector_store = SupabaseVectorStore(
|
|
| 182 |
table_name="documents",
|
| 183 |
query_name="match_documents_langchain",
|
| 184 |
)
|
| 185 |
-
|
| 186 |
retriever=vector_store.as_retriever(),
|
| 187 |
name="Question Search",
|
| 188 |
description="A tool to retrieve similar questions from a vector store.",
|
|
@@ -190,6 +192,7 @@ create_retriever_tool = create_retriever_tool(
|
|
| 190 |
|
| 191 |
|
| 192 |
|
|
|
|
| 193 |
tools = [
|
| 194 |
multiply,
|
| 195 |
add,
|
|
|
|
| 13 |
from langchain_community.vectorstores import SupabaseVectorStore
|
| 14 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 15 |
from langchain_core.tools import tool
|
| 16 |
+
|
| 17 |
from supabase.client import Client, create_client
|
| 18 |
# --- langchain create_retriever_tool fallback (paste near other imports) ---
|
| 19 |
+
# NOTE: removed the unconditional import that caused ModuleNotFoundError.
|
| 20 |
import traceback
|
| 21 |
|
| 22 |
try:
|
| 23 |
+
# Prefer the real helper if available
|
| 24 |
+
from langchain.tools.retriever import create_retriever_tool # type: ignore
|
| 25 |
HAS_CREATE_RETRIEVER_TOOL = True
|
| 26 |
except Exception:
|
| 27 |
HAS_CREATE_RETRIEVER_TOOL = False
|
|
|
|
| 57 |
text = d.get("page_content") or d.get("text") or str(d)
|
| 58 |
else:
|
| 59 |
text = str(d)
|
| 60 |
+
if text:
|
| 61 |
+
out_texts.append(text.strip())
|
| 62 |
# return compact result
|
| 63 |
return "\n\n".join(t for t in out_texts if t)
|
| 64 |
|
| 65 |
def create_retriever_tool(retriever, name: str = "retriever", description: str = ""):
|
| 66 |
"""
|
| 67 |
+
Minimal drop-in fallback returning an object with .run(query).
|
| 68 |
+
Replace with the real langchain helper later once you pin the package.
|
| 69 |
"""
|
| 70 |
return _SimpleRetrieverTool(retriever, name=name, description=description)
|
| 71 |
|
|
|
|
| 72 |
load_dotenv()
|
| 73 |
|
| 74 |
@tool
|
|
|
|
| 184 |
table_name="documents",
|
| 185 |
query_name="match_documents_langchain",
|
| 186 |
)
|
| 187 |
+
retriever_tool = create_retriever_tool(
|
| 188 |
retriever=vector_store.as_retriever(),
|
| 189 |
name="Question Search",
|
| 190 |
description="A tool to retrieve similar questions from a vector store.",
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
+
|
| 196 |
tools = [
|
| 197 |
multiply,
|
| 198 |
add,
|