Spaces:
Sleeping
Sleeping
File size: 12,609 Bytes
c23121a |
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 |
"""langgraph ReAct LLAMA instruct agent"""
from dotenv import load_dotenv
import os
from typing import TypedDict, List, Dict, Any, Optional
from langchain_tavily import TavilySearch
from langchain_core.tools import tool
import requests
from urllib.parse import urlparse
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition,ToolNode
from langchain_core.messages import SystemMessage, HumanMessage
from langchain.schema import HumanMessage, SystemMessage
import json
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
from langchain.agents import initialize_agent
from langchain.agents.agent_types import AgentType
import pandas as pd
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
import sympy
from sympy import sympify
load_dotenv()
@tool
def arvix_search(query: str) -> str:
"""
Search Arxiv for a query and return up to 3 results.
Args:
query: The search query.
Returns:
A string with formatted Arxiv search results (truncated to 1000 chars each).
"""
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
for doc in search_docs
]
)
return formatted_search_docs
@tool
def wiki_search(query: str) -> str:
"""
Search Wikipedia for a query and return up to 2 formatted results.
Args:
query: The search query.
Returns:
A string with formatted Wikipedia search results.
"""
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
for doc in search_docs
]
)
return formatted_search_docs
@tool
def analyze_excel_file(input_str: str) -> str:
"""
Analyze an Excel file using pandas and answer a question about it.
Args:
input_str: JSON string with fields:
- file_path: Path to the Excel file
- query: A question about the file contents (optional)
Returns:
A summary of the file contents or an error message.
"""
try:
import json
import pandas as pd
# Parse JSON input
data = json.loads(input_str)
file_path = data.get("file_path")
query = data.get("query")
if not file_path:
return "Error: 'file_path' is required."
# Read the Excel file (all sheets)
xls = pd.ExcelFile(file_path)
sheet_names = xls.sheet_names
result = f"Excel file loaded with sheets: {', '.join(sheet_names)}.\n\n"
# Analyze the first sheet as default
df = pd.read_excel(xls, sheet_name=sheet_names[0])
result += f"First sheet '{sheet_names[0]}' loaded with {len(df)} rows and {len(df.columns)} columns.\n"
result += f"Columns: {', '.join(df.columns)}\n\n"
result += "Summary statistics:\n"
result += str(df.describe(include='all'))
if query:
result += f"\n\nQuery: {query} (No advanced query handling implemented yet.)"
return result
except json.JSONDecodeError:
return "Error: Input must be a valid JSON string with 'file_path' and optional 'query'."
except Exception as e:
return f"Error analyzing Excel file: {str(e)}"
@tool
def web_search(query: str) -> str:
"""
Perform a web search using Tavily and return the result.
"""
try:
search = TavilySearch()
result = search.invoke(query)
if isinstance(result, dict) and "results" in result:
docs = result["results"]
return "\n\n---\n\n".join(
[f"{doc['title']}\n{doc['url']}\n{doc['content']}" for doc in docs]
)
else:
return f"Error: Unexpected Tavily response format: {result}"
except Exception as e:
return f"Error using TavilySearch: {str(e)}"
@tool
def analyze_csv_file(input_str: str) -> str:
"""
Analyze a CSV file using pandas and answer a question about it.
Args:
input_str: JSON string with fields:
- file_path: Path to the CSV file
- query: A question about the file contents
Returns:
A basic analysis of the file or an error message
"""
try:
# Parse the JSON string
data = json.loads(input_str)
file_path = data.get("file_path")
query = data.get("query")
if not file_path:
return "Error: 'file_path' is required."
# Read the CSV
df = pd.read_csv(file_path)
# Basic metadata
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
result += f"Columns: {', '.join(df.columns)}\n\n"
result += "Summary statistics:\n"
result += str(df.describe(include='all', datetime_is_numeric=True))
# Optionally handle a query (not implemented in detail here)
if query:
result += f"\n\nQuery: {query} (No logic implemented yet to answer it.)"
return result
except json.JSONDecodeError:
return "Error: Input must be a valid JSON string with 'file_path' and optional 'query'."
except Exception as e:
return f"Error analyzing CSV file: {str(e)}"
@tool
def download_file_from_url(input_str: str) -> str:
"""
Downloads a file from a URL and saves it in the 'saved_files' directory.
Args:
input_str (str): A JSON string with keys:
- "url": the URL to download from (required)
- "filename": optional filename to save as
Returns:
A message indicating success and file path, or an error message.
"""
try:
# Parse the input string
data = json.loads(input_str)
url = data.get("url")
filename = data.get("filename", None)
if not url:
return "Error: 'url' is required in the input JSON."
# Create directory if not exists
new_dir = os.path.join(os.getcwd(), "saved_files")
os.makedirs(new_dir, exist_ok=True)
# Generate filename if not provided
if not filename:
path = urlparse(url).path
filename = os.path.basename(path) or f"downloaded_{os.urandom(4).hex()}"
filepath = os.path.join(new_dir, filename)
# Download the file
response = requests.get(url, stream=True)
response.raise_for_status()
# Save the file
with open(filepath, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return f"File downloaded to {filepath}. You can now process this file."
except json.JSONDecodeError:
return "Error: Invalid JSON input. Expected format: {\"url\": \"...\", \"filename\": \"optional_name\"}"
except Exception as e:
return f"Error: {str(e)}"
@tool
def find_file_for_question(input_str: str) -> str:
"""
Constructs a multimodal question prompt for the agent to answer.
Args:
input_str (str): JSON string with keys:
- task_id: ID of the file
- question: The actual question
- file_name: (optional) file name, if image is involved
Returns:
A full natural language prompt that includes the file URL if needed.
"""
try:
data = json.loads(input_str)
task_id = data.get("task_id")
question = data.get("question")
file_name = data.get("file_name")
if not task_id or not question:
return "Error: Missing 'task_id' or 'question' in input."
prompt = question
if file_name:
file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
prompt += f"\n\nImage file to consider: {file_url}"
return prompt
except json.JSONDecodeError:
return "Error: Invalid input. Provide JSON with 'task_id', 'question', and optional 'file_name'."
except Exception as e:
return f"Error: {str(e)}"
@tool
def calculate_math_expression(expr: str) -> str:
"""
Evaluate a symbolic math expression (e.g., algebraic, numeric, or arithmetic).
Use this tool if the input is a math expression like '2 + 3*sqrt(4)', 'sin(pi/2)', or '3 ** 2'.
Input:
A raw string expression. Example: '2 + 3 * sqrt(4)'
Returns:
A float result as a string if successful,
otherwise a string with the error message.
"""
try:
result = sympify(expr)
# Check if the result is an actual sympy object with evalf
if hasattr(result, "evalf"):
return str(result.evalf())
else:
return str(result) # Already a number or something that can't be evaluated further
except Exception as e:
return f"Error: {str(e)}"
class AgentState(TypedDict):
messages: str # The original input question
attachments: Dict[str, Any] # Attachments (e.g., images, files) related to the question
context: List[Dict] # Retrieved context (e.g., search results, documents)
reasoning: List[str] # Step-by-step reasoning traces
partial_answer: Optional[str] # Intermediate answer (if multi-step)
final_answer: Optional[str] # Final answer to return
tools_used: List[str] # Track which tools were called (for debugging)
tools = [
find_file_for_question,
analyze_excel_file,
analyze_csv_file,
web_search,
arvix_search,
wiki_search,
download_file_from_url,
calculate_math_expression]
# Build graph function
def build_graph():
"""Build the graph"""
llm = HuggingFaceEndpoint(
repo_id="meta-llama/Llama-4-Scout-17B-16E-Instruct",
temperature= 0,
provider="novita",
)
chat_model = ChatHuggingFace(llm=llm)
agent = initialize_agent(
tools=tools,
llm=chat_model,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
handle_parsing_errors=True
)
def assistant(state: AgentState):
system_prompt = f"""
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
"""
sys_msg = SystemMessage(content= system_prompt)
return {
"messages": [agent.invoke({"input": [sys_msg] + state["messages"]})],
}
builder = StateGraph(AgentState)
# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message requires a tool, route to tools
# Otherwise, provide a direct response
tools_condition,
)
builder.add_edge("tools", "assistant")
return builder.compile()
if __name__ == "__main__":
#test the agent with a sample question
question = "what was the first university in the world?"
messages = [HumanMessage(content=question)]
output = build_graph().invoke({"messages": messages})
#print out the response
for entry in output["messages"]:
for msg in entry["input"]:
if isinstance(msg, HumanMessage):
print("🧑 Human:", msg.content)
elif isinstance(msg, SystemMessage):
print("⚙️ System:", msg.content)
print("🤖 Output:", entry["output"])
print("-" * 50)
|