Spaces:
Runtime error
Runtime error
File size: 13,485 Bytes
835936b 695f802 835936b 8d36e0e 835936b 8d36e0e 835936b 8d36e0e 835936b 695f802 835936b 8d36e0e 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 835936b 695f802 8d36e0e 835936b 695f802 8d36e0e 695f802 8d36e0e 835936b 695f802 835936b 695f802 8d36e0e 695f802 8d36e0e 835936b 695f802 835936b 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 835936b 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 835936b 695f802 835936b 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 835936b 695f802 835936b 695f802 835936b 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 835936b 8d36e0e 835936b 695f802 8d36e0e 695f802 8d36e0e 835936b 695f802 8d36e0e 695f802 8d36e0e 7343388 8d36e0e 835936b 8d36e0e 835936b 8d36e0e 835936b 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 835936b 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 8d36e0e 695f802 835936b 8d36e0e 835936b 695f802 8d36e0e 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
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 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, AIMessage
from langchain_core.tools import tool
from langchain_core.language_models.base import BaseLanguageModel
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
# Custom HuggingFace LLM wrapper
class SimpleHuggingFaceLLM(BaseLanguageModel):
def __init__(self, repo_id: str, hf_token: str):
super().__init__()
self.repo_id = repo_id
self.hf_token = hf_token
self.api_url = f"https://api-inference.huggingface.co/models/{repo_id}"
self.headers = {"Authorization": f"Bearer {hf_token}"}
def _generate(self, messages, stop=None, run_manager=None, **kwargs):
# Convert messages to a single prompt
if isinstance(messages, list):
prompt = messages[-1].content if messages else ""
else:
prompt = str(messages)
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": 512,
"temperature": 0.1,
"return_full_text": False
}
}
try:
response = requests.post(self.api_url, headers=self.headers, json=payload)
if response.status_code == 200:
result = response.json()
if isinstance(result, list) and len(result) > 0:
generated_text = result[0].get('generated_text', '')
else:
generated_text = str(result)
from langchain_core.outputs import LLMResult, Generation
return LLMResult(generations=[[Generation(text=generated_text)]])
else:
return LLMResult(generations=[[Generation(text=f"Error: {response.status_code}")]])
except Exception as e:
return LLMResult(generations=[[Generation(text=f"Error: {str(e)}")]])
def invoke(self, input, config=None, **kwargs):
if isinstance(input, list):
prompt = input[-1].content if input else ""
else:
prompt = str(input)
result = self._generate(prompt)
generated_text = result.generations[0][0].text
return AIMessage(content=generated_text)
@property
def _llm_type(self):
return "huggingface_custom"
# ---- 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 calculate_average(numbers: str) -> float:
"""Calculate average of comma-separated numbers"""
try:
nums = [float(x.strip()) for x in numbers.split(',')]
return sum(nums) / len(nums)
except:
return 0.0
@tool
def wiki_search(query: str) -> str:
"""Search Wikipedia for information"""
try:
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
if not search_docs:
return "No Wikipedia results found."
formatted = "\n\n---\n\n".join([
f'Wikipedia: {doc.metadata.get("title", "Unknown")}\n{doc.page_content[:1500]}'
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=2).invoke(query=query)
if not search_docs:
return "No web search results found."
formatted = "\n\n---\n\n".join([
f'Web: {doc.get("title", "Unknown")}\n{doc.get("content", "")[:1500]}'
for doc in search_docs
])
return formatted
except Exception as e:
return f"Web search error: {str(e)}"
@tool
def simple_calculation(expression: str) -> str:
"""Safely evaluate simple mathematical expressions"""
try:
# Remove any non-mathematical characters for safety
safe_chars = set('0123456789+-*/.() ')
if not all(c in safe_chars for c in expression):
return "Invalid characters in expression"
# Evaluate the expression
result = eval(expression)
return str(result)
except Exception as e:
return f"Calculation error: {str(e)}"
# ---- Embedding & Vector Store Setup ----
def setup_vector_store():
try:
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-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
try:
json_QA.append(json.loads(line))
except:
continue
if json_QA:
documents = []
for sample in json_QA:
if sample.get('Question') and sample.get('Final answer'):
doc = Document(
page_content=f"Question: {sample['Question']}\n\nAnswer: {sample['Final answer']}",
metadata={"source": sample.get("task_id", "unknown")}
)
documents.append(doc)
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 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 "No similar questions available"
try:
matched_docs = vector_store.similarity_search(query, k=2)
if not matched_docs:
return "No similar questions found"
formatted = "\n\n".join([
f'Similar Q&A:\n{doc.page_content[:800]}'
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 that can solve various types of questions using available tools.
Available tools:
- Math: add, subtract, multiply, divide, modulus, power, square_root, factorial, gcd, lcm, percentage, compound_interest, calculate_average, simple_calculation
- Search: wiki_search, web_search, similar_question_search
Instructions:
1. Read the question carefully
2. Break down complex problems into steps
3. Use appropriate tools to gather information or perform calculations
4. Think step by step and show your reasoning
5. Provide accurate, concise answers
IMPORTANT: Always end your response with:
FINAL ANSWER: [your answer here]
For the final answer:
- Numbers: Use plain digits (no commas, units, or symbols unless requested)
- Text: Use exact names without articles
- Lists: Comma-separated values
Think carefully and use tools when needed.
"""
sys_msg = SystemMessage(content=system_prompt)
# ---- Tool List ----
tools = [
# Math tools
multiply, add, subtract, divide, modulus, power, square_root,
factorial, gcd, lcm, percentage, compound_interest, calculate_average, simple_calculation,
# Search tools
wiki_search, web_search, similar_question_search
]
# ---- Graph Definition ----
def build_graph(provider: str = "huggingface"):
"""Build the agent graph with custom HuggingFace integration"""
if provider == "huggingface":
# Use custom HuggingFace LLM with fallback models
models_to_try = [
"google/flan-t5-base",
"microsoft/DialoGPT-medium",
"bigscience/bloom-560m"
]
llm = None
for model_id in models_to_try:
try:
llm = SimpleHuggingFaceLLM(repo_id=model_id, hf_token=hf_token)
print(f"Successfully initialized model: {model_id}")
break
except Exception as e:
print(f"Failed to initialize {model_id}: {e}")
continue
if llm is None:
raise ValueError("Failed to initialize any HuggingFace model")
else:
raise ValueError("Only 'huggingface' provider is supported")
# Simple tool binding simulation
def llm_with_tools(messages):
return llm.invoke(messages)
def assistant(state: MessagesState):
"""Assistant node with enhanced error handling"""
try:
messages = state["messages"]
response = llm_with_tools(messages)
return {"messages": [response]}
except Exception as e:
print(f"Assistant error: {e}")
fallback_response = AIMessage(content="I encountered an error processing your request. Let me try a simpler approach.")
return {"messages": [fallback_response]}
def retriever(state: MessagesState):
"""Enhanced retriever with context injection"""
messages = state["messages"]
user_query = messages[-1].content if messages else ""
context_messages = [sys_msg]
# Add similar question context if available
if vector_store:
try:
similar = vector_store.similarity_search(user_query, k=1)
if similar:
context_msg = HumanMessage(
content=f"Here's a similar example:\n{similar[0].page_content[:500]}"
)
context_messages.append(context_msg)
except Exception as e:
print(f"Retriever error: {e}")
return {"messages": context_messages + messages}
# Build simplified graph (without complex tool routing for now)
builder = StateGraph(MessagesState)
builder.add_node("retriever", retriever)
builder.add_node("assistant", assistant)
# Simple linear flow
builder.add_edge(START, "retriever")
builder.add_edge("retriever", "assistant")
return builder.compile() |