Spaces:
Sleeping
Sleeping
File size: 10,742 Bytes
d14dccf 26539b0 1551d5f 26539b0 d14dccf 26539b0 d14dccf 26539b0 d14dccf 26539b0 d14dccf 26539b0 d14dccf 26539b0 d14dccf 26539b0 d14dccf 1551d5f d14dccf 1551d5f d14dccf 26539b0 d14dccf 1551d5f d14dccf 1551d5f d14dccf 1551d5f d14dccf 26539b0 d14dccf 1551d5f d14dccf 1551d5f d14dccf 26539b0 d14dccf 1551d5f 26539b0 d14dccf 26539b0 1551d5f d14dccf 1551d5f d14dccf 26539b0 d14dccf 26539b0 1249025 d14dccf 1b2b2d6 d14dccf 26539b0 d14dccf 26539b0 3429b66 1551d5f 3429b66 1551d5f d14dccf 3429b66 d14dccf 3429b66 1551d5f 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 1551d5f 3429b66 26539b0 d14dccf 26539b0 d14dccf 26539b0 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf 3429b66 d14dccf feaf702 d14dccf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
"""LangGraph Agent - Complete bypass of problematic vector store"""
import os
import json
from dotenv import load_dotenv
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
from langchain_groq import ChatGroq
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.tools import tool
from supabase.client import Client, create_client
load_dotenv()
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
@tool
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
@tool
def subtract(a: int, b: int) -> int:
"""Subtract two numbers."""
return a - b
@tool
def divide(a: int, b: int) -> int:
"""Divide two numbers."""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
@tool
def modulus(a: int, b: int) -> int:
"""Get the modulus of two numbers."""
return a % b
@tool
def wiki_search(query: str) -> str:
"""Search Wikipedia for a query and return maximum 2 results."""
try:
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
formatted_docs = []
for doc in search_docs:
source = "Wikipedia"
if hasattr(doc, 'metadata') and isinstance(doc.metadata, dict):
source = doc.metadata.get('source', 'Wikipedia')
formatted_docs.append(f"Source: {source}\n{doc.page_content[:1000]}...")
return "\n\n---\n\n".join(formatted_docs)
except Exception as e:
return f"Error searching Wikipedia: {str(e)}"
@tool
def web_search(query: str) -> str:
"""Search the web using Tavily."""
try:
search_tool = TavilySearchResults(max_results=3)
results = search_tool.invoke(query)
if isinstance(results, list):
formatted_results = []
for result in results:
if isinstance(result, dict):
url = result.get('url', 'Unknown')
content = result.get('content', '')[:1000]
formatted_results.append(f"Source: {url}\n{content}...")
return "\n\n---\n\n".join(formatted_results)
return str(results)
except Exception as e:
return f"Error searching web: {str(e)}"
@tool
def arxiv_search(query: str) -> str:
"""Search Arxiv for academic papers."""
try:
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
formatted_docs = []
for doc in search_docs:
source = "ArXiv"
if hasattr(doc, 'metadata') and isinstance(doc.metadata, dict):
source = doc.metadata.get('source', 'ArXiv')
formatted_docs.append(f"Source: {source}\n{doc.page_content[:1000]}...")
return "\n\n---\n\n".join(formatted_docs)
except Exception as e:
return f"Error searching ArXiv: {str(e)}"
# Raw Supabase search function that bypasses LangChain entirely
def raw_supabase_search(query: str, supabase_client):
"""Direct Supabase search without any LangChain components"""
try:
# Simple text-based search using Supabase's built-in functions
# This assumes you have a simple text search function in your database
result = supabase_client.table('documents').select('content').text_search('content', query).limit(1).execute()
if result.data:
return result.data[0]['content']
else:
# Fallback: get any document (for testing)
result = supabase_client.table('documents').select('content').limit(1).execute()
if result.data:
return result.data[0]['content']
return "No documents found in database"
except Exception as e:
return f"Database search error: {str(e)}"
# Alternative: Use simple SQL query
def simple_sql_search(query: str, supabase_client):
"""Simple SQL-based search"""
try:
# Use a simple SQL query to avoid metadata issues
sql_query = f"""
SELECT content
FROM documents
WHERE content ILIKE '%{query}%'
LIMIT 1
"""
result = supabase_client.rpc('execute_sql', {'query': sql_query}).execute()
if result.data:
return result.data[0]['content']
return "No matching documents found"
except Exception as e:
return f"SQL search error: {str(e)}"
# Load system prompt
try:
with open("system_prompt.txt", "r", encoding="utf-8") as f:
system_prompt = f.read()
except FileNotFoundError:
system_prompt = "You are a helpful AI assistant."
sys_msg = SystemMessage(content=system_prompt)
# Initialize Supabase without vector store
supabase_url = "https://ajnakgegqblhwltzkzbz.supabase.co"
supabase_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFqbmFrZ2VncWJsaHdsdHpremJ6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDkyMDgxODgsImV4cCI6MjA2NDc4NDE4OH0.b9RPF-5otedg4yiaQu_uhOgYpXVXd9D_0oR-9cluUjo"
try:
supabase_client = create_client(supabase_url, supabase_key)
except Exception as e:
print(f"Warning: Could not initialize Supabase client: {e}")
supabase_client = None
tools = [
multiply,
add,
subtract,
divide,
modulus,
wiki_search,
web_search,
arxiv_search,
]
def build_graph(provider: str = "groq"):
"""Build the graph without problematic vector store operations"""
if provider == "groq":
llm = ChatGroq(
model="qwen-qwq-32b",
api_key="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",
temperature=0
)
else:
raise ValueError("Invalid provider. Choose 'groq'.")
def retriever(state: MessagesState):
"""Retriever that actually searches based on query"""
try:
query = state["messages"][-1].content.lower()
if supabase_client is None:
return {"messages": [AIMessage(content="I don't have access to my knowledge base right now. Let me help you using my general knowledge or search tools instead. What would you like to know?")]}
print(f"Searching for: {query}") # Debug print
# Try text-based search in the content
try:
# Search for documents containing query terms
result = supabase_client.table('documents').select('content')\
.ilike('content', f'%{query}%')\
.limit(3).execute()
if result.data and len(result.data) > 0:
print(f"Found {len(result.data)} results") # Debug print
# Get the most relevant result
content = result.data[0].get('content', '')
# Look for final answer pattern
if "Final answer :" in content:
answer = content.split("Final answer :")[-1].strip()
else:
# Take relevant portion
answer = content.strip()[:800]
if len(content) > 800:
answer += "..."
return {"messages": [AIMessage(content=answer)]}
else:
print("No matching documents found") # Debug print
except Exception as e:
print(f"Text search failed: {e}")
# Fallback: Instead of returning same document, provide helpful response
return {"messages": [AIMessage(content=f"I couldn't find specific information about '{query}' in my knowledge base. Let me try to help you with my general knowledge, or would you like me to search the web for current information?")]}
except Exception as e:
return {"messages": [AIMessage(content=f"I'm having trouble accessing my knowledge base right now. How can I help you using web search or my general knowledge instead?")]}
# Build simple graph
builder = StateGraph(MessagesState)
builder.add_node("retriever", retriever)
builder.set_entry_point("retriever")
builder.set_finish_point("retriever")
return builder.compile()
# RECOMMENDED: Use this function instead of build_graph()
def build_working_graph(provider: str = "groq"):
"""Build a fully functional graph that actually works for different questions"""
if provider == "groq":
llm = ChatGroq(
model="qwen-qwq-32b",
api_key="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",
temperature=0
)
else:
raise ValueError("Invalid provider.")
llm_with_tools = llm.bind_tools(tools)
def assistant(state: MessagesState):
"""Assistant that can provide different answers for different questions"""
# Add system message to the conversation
messages = [sys_msg] + state["messages"]
response = llm_with_tools.invoke(messages)
return {"messages": [response]}
# Build the graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
builder.set_entry_point("assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "assistant")
return builder.compile()
# Test function
def test_graph():
"""Test the graph builds successfully"""
print("Building working graph (recommended)...")
try:
graph = build_working_graph()
print("β Working graph built successfully!")
return graph
except Exception as e:
print(f"β Working graph failed: {e}")
print("Testing retriever-based graph...")
try:
graph1 = build_graph()
print("β Retriever graph built successfully!")
return graph1
except Exception as e:
print(f"β Retriever graph failed: {e}")
return None
if __name__ == "__main__":
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
graph = test_graph()
messages = [HumanMessage(content=question)]
messages = graph.invoke({"messages": messages})
for m in messages["messages"]:
m.pretty_print() |