Spaces:
Runtime error
Runtime error
File size: 13,359 Bytes
835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 7343388 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 |
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set protobuf implementation to avoid C++ extension issues
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
# Load keys from environment
hf_token = os.getenv("HUGGINGFACE_INFERENCE_TOKEN")
serper_api_key = os.getenv("SERPER_API_KEY")
# ---- Imports ----
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition, ToolNode
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain_community.vectorstores import Chroma
from langchain_core.documents import Document
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.tools import tool
from langchain.tools.retriever import create_retriever_tool
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.schema import Document
import json
import requests
from typing import List, Dict, Any
import re
import math
from datetime import datetime
# ---- Enhanced Tools ----
@tool
def multiply(a: float, b: float) -> float:
"""Multiply two numbers"""
return a * b
@tool
def add(a: float, b: float) -> float:
"""Add two numbers"""
return a + b
@tool
def subtract(a: float, b: float) -> float:
"""Subtract two numbers"""
return a - b
@tool
def divide(a: float, b: float) -> float:
"""Divide two numbers"""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
@tool
def modulus(a: int, b: int) -> int:
"""Calculate modulus of two integers"""
return a % b
@tool
def power(a: float, b: float) -> float:
"""Calculate a raised to the power of b"""
return a ** b
@tool
def square_root(a: float) -> float:
"""Calculate square root of a number"""
return math.sqrt(a)
@tool
def factorial(n: int) -> int:
"""Calculate factorial of a number"""
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
if n == 0 or n == 1:
return 1
result = 1
for i in range(2, n + 1):
result *= i
return result
@tool
def gcd(a: int, b: int) -> int:
"""Calculate greatest common divisor"""
while b:
a, b = b, a % b
return a
@tool
def lcm(a: int, b: int) -> int:
"""Calculate least common multiple"""
return abs(a * b) // gcd(a, b)
@tool
def percentage(part: float, whole: float) -> float:
"""Calculate percentage"""
return (part / whole) * 100
@tool
def compound_interest(principal: float, rate: float, time: float, n: int = 1) -> float:
"""Calculate compound interest"""
return principal * (1 + rate/n) ** (n * time)
@tool
def wiki_search(query: str) -> str:
"""Search Wikipedia for information"""
try:
search_docs = WikipediaLoader(query=query, load_max_docs=3).load()
if not search_docs:
return "No Wikipedia results found."
formatted = "\n\n---\n\n".join([
f'<Document source="{doc.metadata.get("source", "Wikipedia")}" title="{doc.metadata.get("title", "Unknown")}"/>\n{doc.page_content[:2000]}\n</Document>'
for doc in search_docs
])
return formatted
except Exception as e:
return f"Wikipedia search error: {str(e)}"
@tool
def web_search(query: str) -> str:
"""Search the web using Tavily"""
try:
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
if not search_docs:
return "No web search results found."
formatted = "\n\n---\n\n".join([
f'<Document source="{doc.get("url", "Unknown")}" title="{doc.get("title", "Unknown")}"/>\n{doc.get("content", "")[:2000]}\n</Document>'
for doc in search_docs
])
return formatted
except Exception as e:
return f"Web search error: {str(e)}"
@tool
def arxiv_search(query: str) -> str:
"""Search ArXiv for academic papers"""
try:
search_docs = ArxivLoader(query=query, load_max_docs=2).load()
if not search_docs:
return "No ArXiv results found."
formatted = "\n\n---\n\n".join([
f'<Document source="{doc.metadata.get("source", "ArXiv")}" title="{doc.metadata.get("Title", "Unknown")}"/>\n{doc.page_content[:1500]}\n</Document>'
for doc in search_docs
])
return formatted
except Exception as e:
return f"ArXiv search error: {str(e)}"
@tool
def serper_search(query: str) -> str:
"""Enhanced web search using Serper API"""
if not serper_api_key:
return "Serper API key not available"
try:
url = "https://google.serper.dev/search"
payload = json.dumps({
"q": query,
"num": 5
})
headers = {
'X-API-KEY': serper_api_key,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
results = response.json()
if 'organic' not in results:
return "No search results found"
formatted = "\n\n---\n\n".join([
f'<Document source="{result.get("link", "Unknown")}" title="{result.get("title", "Unknown")}"/>\n{result.get("snippet", "")}\n</Document>'
for result in results['organic'][:3]
])
return formatted
except Exception as e:
return f"Serper search error: {str(e)}"
# ---- Embedding & Vector Store Setup ----
def setup_vector_store():
try:
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
# Check if metadata.jsonl exists and load it
if os.path.exists('metadata.jsonl'):
json_QA = []
with open('metadata.jsonl', 'r') as jsonl_file:
for line in jsonl_file:
if line.strip(): # Skip empty lines
json_QA.append(json.loads(line))
if json_QA:
documents = [
Document(
page_content=f"Question: {sample.get('Question', '')}\n\nFinal answer: {sample.get('Final answer', '')}",
metadata={"source": sample.get("task_id", "unknown")}
)
for sample in json_QA if sample.get('Question') and sample.get('Final answer')
]
if documents:
vector_store = Chroma.from_documents(
documents=documents,
embedding=embeddings,
persist_directory="./chroma_db",
collection_name="my_collection"
)
vector_store.persist()
print(f"Vector store created with {len(documents)} documents")
return vector_store
# Create empty vector store if no data
vector_store = Chroma(
embedding_function=embeddings,
persist_directory="./chroma_db",
collection_name="my_collection"
)
print("Empty vector store created")
return vector_store
except Exception as e:
print(f"Vector store setup error: {e}")
# Return a dummy vector store function
return None
vector_store = setup_vector_store()
@tool
def similar_question_search(query: str) -> str:
"""Search for similar questions in the knowledge base"""
if not vector_store:
return "Vector store not available"
try:
matched_docs = vector_store.similarity_search(query, 3)
if not matched_docs:
return "No similar questions found"
formatted = "\n\n---\n\n".join([
f'<Document source="{doc.metadata.get("source", "Unknown")}" />\n{doc.page_content[:1000]}\n</Document>'
for doc in matched_docs
])
return formatted
except Exception as e:
return f"Similar question search error: {str(e)}"
# ---- Enhanced System Prompt ----
system_prompt = """
You are an expert assistant capable of solving complex questions using available tools. You have access to:
1. Mathematical tools: add, subtract, multiply, divide, modulus, power, square_root, factorial, gcd, lcm, percentage, compound_interest
2. Search tools: wiki_search, web_search, arxiv_search, serper_search, similar_question_search
IMPORTANT INSTRUCTIONS:
1. Break down complex questions into smaller steps
2. Use tools systematically to gather information and perform calculations
3. For mathematical problems, show your work step by step
4. For factual questions, search for current and accurate information
5. Cross-reference information from multiple sources when possible
6. Be precise with numbers - avoid rounding unless necessary
When providing your final answer, use this exact format:
FINAL ANSWER: [YOUR ANSWER]
Rules for the final answer:
- Numbers: Use plain digits without commas, units, or symbols (unless specifically requested)
- Strings: Use exact names without articles or abbreviations
- Lists: Comma-separated values following the above rules
- Be concise and accurate
Think step by step and use the available tools to ensure accuracy.
"""
sys_msg = SystemMessage(content=system_prompt)
# ---- Enhanced Tool List ----
tools = [
# Math tools
multiply, add, subtract, divide, modulus, power, square_root,
factorial, gcd, lcm, percentage, compound_interest,
# Search tools
wiki_search, web_search, arxiv_search, serper_search, similar_question_search
]
# ---- Graph Definition ----
def build_graph(provider: str = "huggingface"):
"""Build the agent graph with improved HuggingFace model"""
if provider == "huggingface":
# Use a more capable model from HuggingFace
try:
# Try with a well-supported model first
endpoint = HuggingFaceEndpoint(
repo_id="google/flan-t5-base", # This model works well with the current setup
temperature=0.1,
huggingfacehub_api_token=hf_token,
max_new_tokens=512,
task="text2text-generation"
)
llm = ChatHuggingFace(llm=endpoint)
except Exception as e:
print(f"Failed to initialize google/flan-t5-base: {e}")
# Fallback to another model
try:
endpoint = HuggingFaceEndpoint(
repo_id="microsoft/DialoGPT-medium",
temperature=0.1,
huggingfacehub_api_token=hf_token,
max_new_tokens=512
)
llm = ChatHuggingFace(llm=endpoint)
except Exception as e2:
print(f"Failed to initialize DialoGPT-medium: {e2}")
# Final fallback
endpoint = HuggingFaceEndpoint(
repo_id="bigscience/bloom-560m",
temperature=0.1,
huggingfacehub_api_token=hf_token,
max_new_tokens=256
)
llm = ChatHuggingFace(llm=endpoint)
else:
raise ValueError("Only 'huggingface' provider is supported in this version.")
llm_with_tools = llm.bind_tools(tools)
def assistant(state: MessagesState):
"""Enhanced assistant node with better error handling"""
try:
messages = state["messages"]
response = llm_with_tools.invoke(messages)
return {"messages": [response]}
except Exception as e:
print(f"Assistant error: {e}")
# Fallback response
fallback_msg = HumanMessage(content=f"I encountered an error: {str(e)}. Let me try a simpler approach.")
return {"messages": [fallback_msg]}
def retriever(state: MessagesState):
"""Enhanced retriever with better context injection"""
messages = state["messages"]
user_query = messages[-1].content if messages else ""
# Try to find similar questions
context_messages = [sys_msg]
if vector_store:
try:
similar = vector_store.similarity_search(user_query, k=2)
if similar:
context_msg = HumanMessage(
content=f"Here are similar questions for context:\n\n{similar[0].page_content}"
)
context_messages.append(context_msg)
except Exception as e:
print(f"Retriever error: {e}")
return {"messages": context_messages + messages}
# Build the graph
builder = StateGraph(MessagesState)
builder.add_node("retriever", retriever)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges
builder.add_edge(START, "retriever")
builder.add_edge("retriever", "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "assistant")
return builder.compile() |