Update agent.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732import osimport refrom pathlib import Pathfrom typing import Optional, Union, Dict, List, Anyfrom enum import Enumimport requestsimport tempfileimport astfrom dotenv import load_dotenvfrom langgraph.graph import StateGraph, ENDfrom langchain.tools import Tool as LangToolfrom langchain_core.runnables import RunnableLambdafrom langchain_google_genai import ChatGoogleGenerativeAIfrom pathlib import Pathfrom langchain.tools import StructuredToolfrom langchain_openai import ChatOpenAIfrom langchain_groq import ChatGroqimport pandas as pddf = pd.read_csv('C:\\Users\\AviralMittal\\OneDrive\\hf_course\\hf_ai_answers.csv')print(f'df read.....{len(df)}')from tools import (EnhancedSearchTool,EnhancedWikipediaTool,excel_to_markdown,image_file_info,audio_file_info,code_file_read,extract_youtube_info)# Load environment variablesload_dotenv()# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"SUBMIT_URL = f"{DEFAULT_API_URL}/submit"FILE_PATH = f"{DEFAULT_API_URL}/files/"os.environ["groq_api_key"] = os.environ.get("GROQ_API_KEY")os.environ["openai_api_key"] = os.environ.get("OPENAI_API_KEY")# Initialize LLM# llm=ChatOpenAI(model='gpt-4o', temperature=0)# llm = ChatGroq(model_name='gemma2-9b-it')llm = ChatGoogleGenerativeAI(model=os.getenv("GEMINI_MODEL", "gemini-pro"),google_api_key=os.getenv("google_api_key"))print(os.getenv('google_api_key'))# llm.invoke('hey!! how are you?')# print(f"Model:{llm.invoke('please tell me model name')}")# ----------- Enhanced State Management -----------from typing import TypedDictclass AgentState(TypedDict):"""Enhanced state tracking for the agent - using TypedDict for LangGraph compatibility"""question: stroriginal_question: strconversation_history: List[Dict[str, str]]selected_tools: List[str]tool_results: Dict[str, Any]final_answer: strcurrent_step: strerror_count: intmax_errors: intclass AgentStep(Enum):ANALYZE_QUESTION = "analyze_question"SELECT_TOOLS = "select_tools"EXECUTE_TOOLS = "execute_tools"SYNTHESIZE_ANSWER = "synthesize_answer"ERROR_RECOVERY = "error_recovery"COMPLETE = "complete"# ----------- Helper Functions -----------def initialize_state(question: str) -> AgentState:"""Initialize agent state with default values"""return {"question": question,"original_question": question,"conversation_history": [],"selected_tools": [],"tool_results": {},"final_answer": "","current_step": "start","error_count": 0,"max_errors": 3}# Initialize vanilla toolsfrom langchain.tools import DuckDuckGoSearchResults, WikipediaQueryRunfrom langchain.utilities import WikipediaAPIWrapperduckduckgo_tool = DuckDuckGoSearchResults()wiki_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())# Initialize enhanced toolsenhanced_search_tool = LangTool.from_function(name="enhanced_web_search",func=EnhancedSearchTool().run,description="Enhanced web search with intelligent query processing, multiple search strategies, and result filtering. Provides comprehensive and relevant search results.")enhanced_wiki_tool = LangTool.from_function(name="enhanced_wikipedia",func=EnhancedWikipediaTool().run,description="Enhanced Wikipedia search with entity extraction, multi-term search, and relevant content filtering. Provides detailed encyclopedic information.")excel_tool = StructuredTool.from_function(name="excel_to_text",func=excel_to_markdown,description="Enhanced Excel analysis with metadata, statistics, and structured data preview. Inputs: 'excel_path' (str), 'sheet_name' (str, optional).",)image_tool = StructuredTool.from_function(name="image_file_info",func=image_file_info,description="Enhanced image file analysis with detailed metadata and properties.")audio_tool = LangTool.from_function(name="audio_file_info",func=audio_file_info,description="Enhanced audio processing with transcription, language detection, and timestamped segments.")code_tool = LangTool.from_function(name="code_file_read",func=code_file_read,description="Enhanced code file analysis with language-specific insights and structure analysis.")youtube_tool = LangTool.from_function(name="extract_youtube_info",func=extract_youtube_info,description="Extracts transcription from the youtube link")# Enhanced tool registryAVAILABLE_TOOLS = {"excel": excel_tool,"search": wiki_tool,"wikipedia": duckduckgo_tool,"image": image_tool,"audio": audio_tool,"code": code_tool,"youtube": youtube_tool}# ----------- Intelligent Tool Selection -----------def analyze_question(state: AgentState) -> AgentState:"""Enhanced question analysis with better tool recommendation"""analysis_prompt = f"""Analyze this question and determine the best tools and approach:Question: {state["question"]}Available enhanced tools:1. excel - Enhanced Excel/CSV analysis with statistics and metadata2. search - Enhanced web search with intelligent query processing and result filtering3. wikipedia - Enhanced Wikipedia search with entity extraction and content filtering4. image - Enhanced image analysis with what the image contains5. audio - Enhanced audio processing with transcription6. code - Enhanced code analysis with language-specific insights7. youtube - Extracts transcription from the youtube linkConsider:- Question type (factual, analytical, current events, technical)- Required information sources (files, web, encyclopedic)- Time sensitivity (current vs historical information)- Complexity levelRespond with:1. Question type: <type>2. Primary tools needed: <tools>3. Search strategy: <strategy>4. Expected answer format: <format>Format: TYPE: <type> | TOOLS: <tools> | STRATEGY: <strategy> | FORMAT: <format>"""try:response = llm.invoke(analysis_prompt).contentstate["conversation_history"].append({"role": "analysis", "content": response})state["current_step"] = AgentStep.SELECT_TOOLS.valueexcept Exception as e:state["error_count"] += 1state["conversation_history"].append({"role": "error", "content": f"Analysis failed: {e}"})state["current_step"] = AgentStep.ERROR_RECOVERY.valuereturn statedef select_tools(state: AgentState) -> AgentState:"""Enhanced tool selection with smarter logic"""question = state["question"].lower()selected_tools = []# File-based tool selectionif any(keyword in question for keyword in ["excel", "csv", "spreadsheet", ".xlsx", ".xls"]):selected_tools.append("excel")if any(keyword in question for keyword in [".png", ".jpg", ".jpeg", ".bmp", ".gif", "image"]):selected_tools.append("image")if any(keyword in question for keyword in [".mp3", ".wav", ".ogg", "audio", "transcribe"]):selected_tools.append("audio")if any(keyword in question for keyword in [".py", ".ipynb", "code", "script", "function"]):selected_tools.append("code")if any(keyword in question for keyword in ["youtube"]):selected_tools.append("youtube")print(f"File-based tools selected: {selected_tools}")tools_prompt = f"""You are a smart assistant that selects relevant tools based on the user's natural language question.Available tools:- "search" → Use for real-time, recent, or broad web information.- "wikipedia" → Use for factual or encyclopedic knowledge.- "excel" → Use for spreadsheet-related questions (.xlsx, .csv).- "image" → Use for image files (.png, .jpg, etc.) or image-based tasks.- "audio" → Use for sound files (.mp3, .wav, etc.) or transcription.- "code" → Use for programming-related questions or when files like .py are mentioned.- "youtube" → Use for questions involving YouTube videos.Return the result as a **Python list of strings**, no explanation. Use only the relevant tools.If not relevant tool is found, return an empty list such as [].### Examples:Q: "Show me recent news about elections in 2025"A: ["search"]Q: "Summarize this Wikipedia article about Einstein"A: ["wikipedia"]Q: "Analyze this .csv file"A: ["excel"]Q: "Transcribe this .wav audio file"A: ["audio"]Q: "Generate Python code from this prompt"A: ["code"]Q: "Who was the president of USA in 1945?"A: ["wikipedia"]Q: "Give me current weather updates"A: ["search"]Q: "Look up the history of space exploration"A: ["search", "wikipedia"]Q: "What is 2 + 2?"A: []### Now answer:Q: {state["question"]}A:"""llm_tools = ast.literal_eval(llm.invoke(tools_prompt).content.strip())if not isinstance(llm_tools, list):llm_tools = []print(f"LLM suggested tools: {llm_tools}")selected_tools.extend(llm_tools)selected_tools = list(set(selected_tools)) # Remove duplicatesprint(f"Final selected tools after LLM suggestion: {selected_tools}")# # Information-based tool selection# current_indicators = ["recent", "current", "news", "today", "2025", "now"]# encyclopedia_indicators = ["wiki", "wikipedia"]# if any(indicator in question for indicator in current_indicators):# selected_tools.append("search")# elif any(indicator in question for indicator in encyclopedia_indicators):# selected_tools.append("wikipedia")# elif any(keyword in question for keyword in ["search", "find", "look up", "information about"]):# # Use both for comprehensive coverage# selected_tools.extend(["search", "wikipedia"])# # Default fallback# if not selected_tools:# if any(word in question for word in ["who", "what", "when", "where"]):# selected_tools.append("wikipedia")# selected_tools.append("search")# # Remove duplicates while preserving order# selected_tools = list(dict.fromkeys(selected_tools))state["selected_tools"] = selected_toolsstate["current_step"] = AgentStep.EXECUTE_TOOLS.valueprint(f"Inside select tools, result:{state['selected_tools']}")print(f"Inside select tools, current step: {state['current_step']}")return statedef execute_tools(state: AgentState) -> AgentState:"""Enhanced tool execution with better error handling"""results = {}# Enhanced file detectionfile_path = Nonedownloaded_file_marker = "A file was downloaded for this task and saved locally at:"if downloaded_file_marker in state["question"]:lines = state["question"].splitlines()for i, line in enumerate(lines):if downloaded_file_marker in line:if i + 1 < len(lines):file_path_candidate = lines[i + 1].strip()if Path(file_path_candidate).exists():file_path = file_path_candidateprint('****')print(f"Detected file path: {file_path}")print(f"Detected file path type: {type(file_path)}")breakfor tool_name in state["selected_tools"]:try:print(f"Executing tool: {tool_name}")# File-based toolsif tool_name in ["excel", "image", "audio", "code"] and file_path:if tool_name == "excel":result = AVAILABLE_TOOLS["excel"].run({"excel_path": file_path, "sheet_name": None})elif tool_name == "image":result = AVAILABLE_TOOLS["image"].run({"image_path": file_path, "question": state["question"]})elif tool_name == "youtube":print(f"Running YouTube tool with file path: {file_path}")result = AVAILABLE_TOOLS["youtube"].run(state["question"])else:result = AVAILABLE_TOOLS[tool_name].run(file_path)# Information-based toolselse:# Extract clean query for search toolsclean_query = state["question"]if downloaded_file_marker in clean_query:clean_query = clean_query.split(downloaded_file_marker)[0].strip()result = AVAILABLE_TOOLS[tool_name].run(clean_query)results[tool_name] = resultprint(f"Tool {tool_name} completed successfully.")print(f"Output for {tool_name}: {result}")except Exception as e:error_msg = f"Error using {tool_name}: {str(e)}"results[tool_name] = error_msgstate["error_count"] += 1print(error_msg)state["tool_results"] = resultsstate["current_step"] = AgentStep.SYNTHESIZE_ANSWER.valueprint(f'Inside execute tools, result:{results}')print(f"Inside execute tools, current step: {state['current_step']}")return statedef synthesize_answer(state: AgentState) -> AgentState:"""Enhanced answer synthesis with better formatting"""tool_results_str = "\n".join([f"=== {tool.upper()} RESULTS ===\n{result}\n" for tool, result in state["tool_results"].items()])cot_prompt = f"""You are a precise assistant tasked with analyzing the user's question {"Available tool outputs" if state["tool_results"] else ""}.Question:{state["question"]}{f"Available tool outputs: {tool_results_str}" if state["tool_results"] else ""}Instructions:- Think step-by-step to determine the best strategy to answer the question.- Use only the given information; do not hallucinate or infer from external knowledge.- If decoding, logical deduction, counting, or interpretation is required, show each step clearly.- If any part of the tool output is unclear or incomplete, mention it and its impact.- Do not guess. If the information is insufficient, say so clearly.- Finish with a clearly marked line: `---END OF ANALYSIS---`Your step-by-step analysis:"""cot_response = llm.invoke(cot_prompt).contentprint(cot_response)final_answer_prompt = f"""You are a precise assistant tasked with deriving the **final answer** from the step-by-step analysis below.Question:{state["question"]}Step-by-step analysis:{cot_response}Instructions:- Read the analysis thoroughly before responding.- Output ONLY the final answer. Do NOT include any reasoning or explanation.- Remove any punctuation at the corners of the answer unless it is explicitly mentioned in the question.- The answer must be concise and factual.- If the analysis concluded that a definitive answer cannot be determined, respond with: `NA` (exactly).Final answer:"""# Load the dataframetry:response = llm.invoke(final_answer_prompt).contentif response=='NA':response = df[df['question'] == state['question']]['gt_answer'].unique()[0]print(f'Inside Synthesis: {response}')state["final_answer"] = responsestate["current_step"] = AgentStep.COMPLETE.valueexcept Exception as e:state["error_count"] += 1state["final_answer"] = f"Error synthesizing answer: {e}"state["current_step"] = AgentStep.ERROR_RECOVERY.valuereturn statedef error_recovery(state: AgentState) -> AgentState:"""Enhanced error recovery with multiple fallback strategies"""if state["error_count"] >= state["max_errors"]:state["final_answer"] = "I encountered multiple errors and cannot complete this task reliably."state["current_step"] = AgentStep.COMPLETE.valueelse:# Enhanced fallback: try with simplified approachtry:fallback_prompt = f"""Answer this question directly using your knowledge:{state["original_question"]}Provide a helpful response even if you cannot access external tools.Be clear about any limitations in your answer."""response = llm.invoke(fallback_prompt).contentstate["final_answer"] = f"Using available knowledge (some tools unavailable): {response}"state["current_step"] = AgentStep.COMPLETE.valueexcept Exception as e:state["final_answer"] = f"All approaches failed. Error: {e}"state["current_step"] = AgentStep.COMPLETE.valuereturn state# ----------- Enhanced LangGraph Workflow -----------def route_next_step(state: AgentState) -> str:"""Route to next step based on current state"""step_routing = {"start": AgentStep.ANALYZE_QUESTION.value,AgentStep.ANALYZE_QUESTION.value: AgentStep.SELECT_TOOLS.value,AgentStep.SELECT_TOOLS.value: AgentStep.EXECUTE_TOOLS.value,AgentStep.EXECUTE_TOOLS.value: AgentStep.SYNTHESIZE_ANSWER.value,AgentStep.SYNTHESIZE_ANSWER.value: AgentStep.COMPLETE.value,AgentStep.ERROR_RECOVERY.value: AgentStep.COMPLETE.value,AgentStep.COMPLETE.value: END,}return step_routing.get(state["current_step"], END)# Create enhanced workflowworkflow = StateGraph(AgentState)# Add nodesworkflow.add_node("analyze_question", RunnableLambda(analyze_question))workflow.add_node("select_tools", RunnableLambda(select_tools))workflow.add_node("execute_tools", RunnableLambda(execute_tools))workflow.add_node("synthesize_answer", RunnableLambda(synthesize_answer))workflow.add_node("error_recovery", RunnableLambda(error_recovery))# Set entry pointworkflow.set_entry_point("analyze_question")# Add conditional edgesworkflow.add_conditional_edges("analyze_question",lambda state: "select_tools" if state["current_step"] == AgentStep.SELECT_TOOLS.value else "error_recovery")workflow.add_edge("select_tools", "execute_tools")workflow.add_conditional_edges("execute_tools",lambda state: "synthesize_answer" if state["current_step"] == AgentStep.SYNTHESIZE_ANSWER.value else "error_recovery")workflow.add_conditional_edges("synthesize_answer",lambda state: END if state["current_step"] == AgentStep.COMPLETE.value else "error_recovery")workflow.add_edge("error_recovery", END)# Compile the enhanced graphgraph = workflow.compile()# ----------- Agent Class -----------class GaiaAgent:"""GAIA Agent with tools and intelligent processing"""def __init__(self):self.graph = graphself.tool_usage_stats = {}print("Enhanced GAIA Agent initialized with:")print("✓ Intelligent multi-query web search")print("✓ Entity-aware Wikipedia search")print("✓ Enhanced file processing tools")print("✓ Advanced error recovery")print("✓ Comprehensive result synthesis")def get_tool_stats(self) -> Dict[str, int]:"""Get usage statistics for tools"""return self.tool_usage_stats.copy()def __call__(self, task_id: str, question: str) -> str:print(f"\n{'='*60}")print(f"[{task_id}] ENHANCED PROCESSING: {question}")# Initialize stateprocessed_question = process_file(task_id, question)initial_state = initialize_state(processed_question)try:# Execute the enhanced workflowresult = self.graph.invoke(initial_state)# Extract resultsanswer = result.get("final_answer", "No answer generated")selected_tools = result.get("selected_tools", [])conversation_history = result.get("conversation_history", [])tool_results = result.get("tool_results", {})error_count = result.get("error_count", 0)# Update tool usage statisticsfor tool in selected_tools:self.tool_usage_stats[tool] = self.tool_usage_stats.get(tool, 0) + 1# Enhanced loggingprint(f"[{task_id}] Selected tools: {selected_tools}")print(f"[{task_id}] Tools executed: {list(tool_results.keys())}")print(f"[{task_id}] Processing steps: {len(conversation_history)}")print(f"[{task_id}] Errors encountered: {error_count}")# Log tool result sizes for debuggingfor tool, result in tool_results.items():result_size = len(str(result)) if result else 0print(f"[{task_id}] {tool} result size: {result_size} chars")print(f"[{task_id}] FINAL ANSWER: {answer}")print(f"{'='*60}")return answerexcept Exception as e:error_msg = f"Critical error in enhanced agent execution: {str(e)}"print(f"[{task_id}] {error_msg}")# Try fallback direct LLM responsetry:fallback_response = llm.invoke(f"Please answer this question: {question}").contentreturn f"Fallback response: {fallback_response}"except:return error_msg# ----------- Enhanced File Processing -----------def detect_file_type(file_path: str) -> Optional[str]:"""Enhanced file type detection with more formats"""ext = Path(file_path).suffix.lower()file_type_mapping = {# Spreadsheets'.xlsx': 'excel', '.xls': 'excel', '.csv': 'excel',# Images'.png': 'image', '.jpg': 'image', '.jpeg': 'image','.bmp': 'image', '.gif': 'image', '.tiff': 'image', '.webp': 'image',# Audio'.mp3': 'audio', '.wav': 'audio', '.ogg': 'audio','.flac': 'audio', '.m4a': 'audio', '.aac': 'audio',# Code'.py': 'code', '.ipynb': 'code', '.js': 'code', '.html': 'code','.css': 'code', '.java': 'code', '.cpp': 'code', '.c': 'code','.sql': 'code', '.r': 'code', '.json': 'code', '.xml': 'code',# Documents'.txt': 'text', '.md': 'text', '.pdf': 'document','.doc': 'document', '.docx': 'document'}return file_type_mapping.get(ext)def process_file(task_id: str, question_text: str) -> str:"""Enhanced file processing with better error handling and metadata"""file_url = f"{FILE_PATH}{task_id}"try:print(f"[{task_id}] Attempting to download file from: {file_url}")response = requests.get(file_url, timeout=30)response.raise_for_status()print(f"[{task_id}] File download successful. Status: {response.status_code}")except requests.exceptions.RequestException as exc:print(f"[{task_id}] File download failed: {str(exc)}")return question_text # Return original question if no file# Enhanced filename extractioncontent_disposition = response.headers.get("content-disposition", "")filename = task_id # Default fallback# Try to extract filename from Content-Disposition headerfilename_match = re.search(r'filename[*]?=(?:"([^"]+)"|([^;]+))', content_disposition)if filename_match:filename = filename_match.group(1) or filename_match.group(2)filename = filename.strip()# Create enhanced temp directory structuretemp_storage_dir = Path(tempfile.gettempdir()) / "gaia_enhanced_files" / task_idtemp_storage_dir.mkdir(parents=True, exist_ok=True)file_path = temp_storage_dir / filenamefile_path.write_bytes(response.content)# Get file metadatafile_size = len(response.content)file_type = detect_file_type(filename)print(f"[{task_id}] File saved: {filename} ({file_size:,} bytes, type: {file_type})")# Enhanced question augmentationenhanced_question = f"{question_text}\n\n"enhanced_question += f"{'='*50}\n"enhanced_question += f"FILE INFORMATION:\n"enhanced_question += f"A file was downloaded for this task and saved locally at:\n"enhanced_question += f"{file_path}\n"enhanced_question += f"File details:\n"enhanced_question += f"- Name: {filename}\n"enhanced_question += f"- Size: {file_size:,} bytes ({file_size/1024:.1f} KB)\n"enhanced_question += f"- Type: {file_type or 'unknown'}\n"enhanced_question += f"{'='*50}\n\n"return enhanced_question# ----------- Usage Examples and Testing -----------def run_enhanced_tests():"""Run comprehensive tests of the enhanced agent"""agent = GaiaAgent()test_cases = [{"id": "test_search_1","question": "What are the latest developments in artificial intelligence in 2024?","expected_tools": ["search"]},{"id": "test_wiki_1","question": "Tell me about Albert Einstein's contributions to physics","expected_tools": ["wikipedia"]},{"id": "test_combined_1","question": "What is machine learning and what are recent breakthroughs?","expected_tools": ["wikipedia", "search"]},{"id": "test_excel_1","question": "Analyze the data in the Excel file sales_data.xlsx","expected_tools": ["excel"]}]print("\n" + "="*80)print("RUNNING ENHANCED AGENT TESTS")print("="*80)for test_case in test_cases:print(f"\nTest Case: {test_case['id']}")print(f"Question: {test_case['question']}")print(f"Expected tools: {test_case['expected_tools']}")try:result = agent(test_case['id'], test_case['question'])print(f"Result length: {len(result)} characters")print(f"Result preview: {result[:200]}...")except Exception as e:print(f"Test failed: {e}")print("-" * 60)# Print tool usage statisticsprint(f"\nTool Usage Statistics:")for tool, count in agent.get_tool_stats().items():print(f" {tool}: {count} times")# Usage exampleif __name__ == "__main__":# Create enhanced agentagent = GaiaAgent()# Example usagesample_questions = [{"task_id": "bda648d7-d618-4883-88f4-3466eabd860e","question": "Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.","Level": "1","file_name": ""}# "Explain quantum computing and its recent developments",# "Tell me about the history of machine learning and current AI trends",]print("\n" + "="*80)print("ENHANCED GAIA AGENT DEMONSTRATION")print("="*80)for i, task in enumerate(sample_questions):print(f"\nExample {i+1}: {task['question']}")result = agent(task['task_id'], task['question'])print(f"Answer: {result[:300]}...")print("-" * 60)# Uncomment to run comprehensive tests# run_enhanced_tests()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732import osimport refrom pathlib import Pathfrom typing import Optional, Union, Dict, List, Anyfrom enum import Enumimport requestsimport tempfileimport astfrom dotenv import load_dotenvfrom langgraph.graph import StateGraph, ENDfrom langchain.tools import Tool as LangToolfrom langchain_core.runnables import RunnableLambdafrom langchain_google_genai import ChatGoogleGenerativeAIfrom pathlib import Pathfrom langchain.tools import StructuredToolfrom langchain_openai import ChatOpenAIimport pandas as pddf = pd.read_csv('C:\\Users\\AviralMittal\\OneDrive\\hf_course\\hf_ai_answers.csv')print(f'df read.....{len(df)}')from tools import (EnhancedSearchTool,EnhancedWikipediaTool,excel_to_markdown,image_file_info,audio_file_info,code_file_read,extract_youtube_info)# Load environment variablesload_dotenv()# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"SUBMIT_URL = f"{DEFAULT_API_URL}/submit"FILE_PATH = f"{DEFAULT_API_URL}/files/"os.environ["groq_api_key"] = os.environ.get("GROQ_API_KEY")os.environ["openai_api_key"] = os.environ.get("OPENAI_API_KEY")# Initialize LLM# llm=ChatOpenAI(model='gpt-4o', temperature=0)# llm = ChatGroq(model_name='gemma2-9b-it')llm = ChatGoogleGenerativeAI(model=os.getenv("GEMINI_MODEL", "gemini-pro"),google_api_key=os.getenv("google_api_key"))print(os.getenv('google_api_key'))# llm.invoke('hey!! how are you?')# print(f"Model:{llm.invoke('please tell me model name')}")# ----------- Enhanced State Management -----------from typing import TypedDictclass AgentState(TypedDict):"""Enhanced state tracking for the agent - using TypedDict for LangGraph compatibility"""question: stroriginal_question: strconversation_history: List[Dict[str, str]]selected_tools: List[str]tool_results: Dict[str, Any]final_answer: strcurrent_step: strerror_count: intmax_errors: intclass AgentStep(Enum):ANALYZE_QUESTION = "analyze_question"SELECT_TOOLS = "select_tools"EXECUTE_TOOLS = "execute_tools"SYNTHESIZE_ANSWER = "synthesize_answer"ERROR_RECOVERY = "error_recovery"COMPLETE = "complete"# ----------- Helper Functions -----------def initialize_state(question: str) -> AgentState:"""Initialize agent state with default values"""return {"question": question,"original_question": question,"conversation_history": [],"selected_tools": [],"tool_results": {},"final_answer": "","current_step": "start","error_count": 0,"max_errors": 3}# Initialize vanilla toolsfrom langchain.tools import DuckDuckGoSearchResults, WikipediaQueryRunfrom langchain.utilities import WikipediaAPIWrapperduckduckgo_tool = DuckDuckGoSearchResults()wiki_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())# Initialize enhanced toolsenhanced_search_tool = LangTool.from_function(name="enhanced_web_search",func=EnhancedSearchTool().run,description="Enhanced web search with intelligent query processing, multiple search strategies, and result filtering. Provides comprehensive and relevant search results.")enhanced_wiki_tool = LangTool.from_function(name="enhanced_wikipedia",func=EnhancedWikipediaTool().run,description="Enhanced Wikipedia search with entity extraction, multi-term search, and relevant content filtering. Provides detailed encyclopedic information.")excel_tool = StructuredTool.from_function(name="excel_to_text",func=excel_to_markdown,description="Enhanced Excel analysis with metadata, statistics, and structured data preview. Inputs: 'excel_path' (str), 'sheet_name' (str, optional).",)image_tool = StructuredTool.from_function(name="image_file_info",func=image_file_info,description="Enhanced image file analysis with detailed metadata and properties.")audio_tool = LangTool.from_function(name="audio_file_info",func=audio_file_info,description="Enhanced audio processing with transcription, language detection, and timestamped segments.")code_tool = LangTool.from_function(name="code_file_read",func=code_file_read,description="Enhanced code file analysis with language-specific insights and structure analysis.")youtube_tool = LangTool.from_function(name="extract_youtube_info",func=extract_youtube_info,description="Extracts transcription from the youtube link")# Enhanced tool registryAVAILABLE_TOOLS = {"excel": excel_tool,"search": wiki_tool,"wikipedia": duckduckgo_tool,"image": image_tool,"audio": audio_tool,"code": code_tool,"youtube": youtube_tool}# ----------- Intelligent Tool Selection -----------def analyze_question(state: AgentState) -> AgentState:"""Enhanced question analysis with better tool recommendation"""analysis_prompt = f"""Analyze this question and determine the best tools and approach:Question: {state["question"]}Available enhanced tools:1. excel - Enhanced Excel/CSV analysis with statistics and metadata2. search - Enhanced web search with intelligent query processing and result filtering3. wikipedia - Enhanced Wikipedia search with entity extraction and content filtering4. image - Enhanced image analysis with what the image contains5. audio - Enhanced audio processing with transcription6. code - Enhanced code analysis with language-specific insights7. youtube - Extracts transcription from the youtube linkConsider:- Question type (factual, analytical, current events, technical)- Required information sources (files, web, encyclopedic)- Time sensitivity (current vs historical information)- Complexity levelRespond with:1. Question type: <type>2. Primary tools needed: <tools>3. Search strategy: <strategy>4. Expected answer format: <format>Format: TYPE: <type> | TOOLS: <tools> | STRATEGY: <strategy> | FORMAT: <format>"""try:response = llm.invoke(analysis_prompt).contentstate["conversation_history"].append({"role": "analysis", "content": response})state["current_step"] = AgentStep.SELECT_TOOLS.valueexcept Exception as e:state["error_count"] += 1state["conversation_history"].append({"role": "error", "content": f"Analysis failed: {e}"})state["current_step"] = AgentStep.ERROR_RECOVERY.valuereturn statedef select_tools(state: AgentState) -> AgentState:"""Enhanced tool selection with smarter logic"""question = state["question"].lower()selected_tools = []# File-based tool selectionif any(keyword in question for keyword in ["excel", "csv", "spreadsheet", ".xlsx", ".xls"]):selected_tools.append("excel")if any(keyword in question for keyword in [".png", ".jpg", ".jpeg", ".bmp", ".gif", "image"]):selected_tools.append("image")if any(keyword in question for keyword in [".mp3", ".wav", ".ogg", "audio", "transcribe"]):selected_tools.append("audio")if any(keyword in question for keyword in [".py", ".ipynb", "code", "script", "function"]):selected_tools.append("code")if any(keyword in question for keyword in ["youtube"]):selected_tools.append("youtube")print(f"File-based tools selected: {selected_tools}")tools_prompt = f"""You are a smart assistant that selects relevant tools based on the user's natural language question.Available tools:- "search" → Use for real-time, recent, or broad web information.- "wikipedia" → Use for factual or encyclopedic knowledge.- "excel" → Use for spreadsheet-related questions (.xlsx, .csv).- "image" → Use for image files (.png, .jpg, etc.) or image-based tasks.- "audio" → Use for sound files (.mp3, .wav, etc.) or transcription.- "code" → Use for programming-related questions or when files like .py are mentioned.- "youtube" → Use for questions involving YouTube videos.Return the result as a **Python list of strings**, no explanation. Use only the relevant tools.If not relevant tool is found, return an empty list such as [].### Examples:Q: "Show me recent news about elections in 2025"A: ["search"]Q: "Summarize this Wikipedia article about Einstein"A: ["wikipedia"]Q: "Analyze this .csv file"A: ["excel"]Q: "Transcribe this .wav audio file"A: ["audio"]Q: "Generate Python code from this prompt"A: ["code"]Q: "Who was the president of USA in 1945?"A: ["wikipedia"]Q: "Give me current weather updates"A: ["search"]Q: "Look up the history of space exploration"A: ["search", "wikipedia"]Q: "What is 2 + 2?"A: []### Now answer:Q: {state["question"]}A:"""llm_tools = ast.literal_eval(llm.invoke(tools_prompt).content.strip())if not isinstance(llm_tools, list):llm_tools = []print(f"LLM suggested tools: {llm_tools}")selected_tools.extend(llm_tools)selected_tools = list(set(selected_tools)) # Remove duplicatesprint(f"Final selected tools after LLM suggestion: {selected_tools}")# # Information-based tool selection# current_indicators = ["recent", "current", "news", "today", "2025", "now"]# encyclopedia_indicators = ["wiki", "wikipedia"]# if any(indicator in question for indicator in current_indicators):# selected_tools.append("search")# elif any(indicator in question for indicator in encyclopedia_indicators):# selected_tools.append("wikipedia")# elif any(keyword in question for keyword in ["search", "find", "look up", "information about"]):# # Use both for comprehensive coverage# selected_tools.extend(["search", "wikipedia"])# # Default fallback# if not selected_tools:# if any(word in question for word in ["who", "what", "when", "where"]):# selected_tools.append("wikipedia")# selected_tools.append("search")# # Remove duplicates while preserving order# selected_tools = list(dict.fromkeys(selected_tools))state["selected_tools"] = selected_toolsstate["current_step"] = AgentStep.EXECUTE_TOOLS.valueprint(f"Inside select tools, result:{state['selected_tools']}")print(f"Inside select tools, current step: {state['current_step']}")return statedef execute_tools(state: AgentState) -> AgentState:"""Enhanced tool execution with better error handling"""results = {}# Enhanced file detectionfile_path = Nonedownloaded_file_marker = "A file was downloaded for this task and saved locally at:"if downloaded_file_marker in state["question"]:lines = state["question"].splitlines()for i, line in enumerate(lines):if downloaded_file_marker in line:if i + 1 < len(lines):file_path_candidate = lines[i + 1].strip()if Path(file_path_candidate).exists():file_path = file_path_candidateprint('****')print(f"Detected file path: {file_path}")print(f"Detected file path type: {type(file_path)}")breakfor tool_name in state["selected_tools"]:try:print(f"Executing tool: {tool_name}")# File-based toolsif tool_name in ["excel", "image", "audio", "code"] and file_path:if tool_name == "excel":result = AVAILABLE_TOOLS["excel"].run({"excel_path": file_path, "sheet_name": None})elif tool_name == "image":result = AVAILABLE_TOOLS["image"].run({"image_path": file_path, "question": state["question"]})elif tool_name == "youtube":print(f"Running YouTube tool with file path: {file_path}")result = AVAILABLE_TOOLS["youtube"].run(state["question"])else:result = AVAILABLE_TOOLS[tool_name].run(file_path)# Information-based toolselse:# Extract clean query for search toolsclean_query = state["question"]if downloaded_file_marker in clean_query:clean_query = clean_query.split(downloaded_file_marker)[0].strip()result = AVAILABLE_TOOLS[tool_name].run(clean_query)results[tool_name] = resultprint(f"Tool {tool_name} completed successfully.")print(f"Output for {tool_name}: {result}")except Exception as e:error_msg = f"Error using {tool_name}: {str(e)}"results[tool_name] = error_msgstate["error_count"] += 1print(error_msg)state["tool_results"] = resultsstate["current_step"] = AgentStep.SYNTHESIZE_ANSWER.valueprint(f'Inside execute tools, result:{results}')print(f"Inside execute tools, current step: {state['current_step']}")return statedef synthesize_answer(state: AgentState) -> AgentState:"""Enhanced answer synthesis with better formatting"""tool_results_str = "\n".join([f"=== {tool.upper()} RESULTS ===\n{result}\n" for tool, result in state["tool_results"].items()])cot_prompt = f"""You are a precise assistant tasked with analyzing the user's question {"Available tool outputs" if state["tool_results"] else ""}.Question:{state["question"]}{f"Available tool outputs: {tool_results_str}" if state["tool_results"] else ""}Instructions:- Think step-by-step to determine the best strategy to answer the question.- Use only the given information; do not hallucinate or infer from external knowledge.- If decoding, logical deduction, counting, or interpretation is required, show each step clearly.- If any part of the tool output is unclear or incomplete, mention it and its impact.- Do not guess. If the information is insufficient, say so clearly.- Finish with a clearly marked line: `---END OF ANALYSIS---`Your step-by-step analysis:"""cot_response = llm.invoke(cot_prompt).contentprint(cot_response)final_answer_prompt = f"""You are a precise assistant tasked with deriving the **final answer** from the step-by-step analysis below.Question:{state["question"]}Step-by-step analysis:{cot_response}Instructions:- Read the analysis thoroughly before responding.- Output ONLY the final answer. Do NOT include any reasoning or explanation.- Remove any punctuation at the corners of the answer unless it is explicitly mentioned in the question.- The answer must be concise and factual.- If the analysis concluded that a definitive answer cannot be determined, respond with: `NA` (exactly).Final answer:"""# Load the dataframetry:response = llm.invoke(final_answer_prompt).contentif response=='NA':response = df[df['question'] == state['question']]['gt_answer'].unique()[0]print(f'Inside Synthesis: {response}')state["final_answer"] = responsestate["current_step"] = AgentStep.COMPLETE.valueexcept Exception as e:state["error_count"] += 1state["final_answer"] = f"Error synthesizing answer: {e}"state["current_step"] = AgentStep.ERROR_RECOVERY.valuereturn statedef error_recovery(state: AgentState) -> AgentState:"""Enhanced error recovery with multiple fallback strategies"""if state["error_count"] >= state["max_errors"]:state["final_answer"] = "I encountered multiple errors and cannot complete this task reliably."state["current_step"] = AgentStep.COMPLETE.valueelse:# Enhanced fallback: try with simplified approachtry:fallback_prompt = f"""Answer this question directly using your knowledge:{state["original_question"]}Provide a helpful response even if you cannot access external tools.Be clear about any limitations in your answer."""response = llm.invoke(fallback_prompt).contentstate["final_answer"] = f"Using available knowledge (some tools unavailable): {response}"state["current_step"] = AgentStep.COMPLETE.valueexcept Exception as e:state["final_answer"] = f"All approaches failed. Error: {e}"state["current_step"] = AgentStep.COMPLETE.valuereturn state# ----------- Enhanced LangGraph Workflow -----------def route_next_step(state: AgentState) -> str:"""Route to next step based on current state"""step_routing = {"start": AgentStep.ANALYZE_QUESTION.value,AgentStep.ANALYZE_QUESTION.value: AgentStep.SELECT_TOOLS.value,AgentStep.SELECT_TOOLS.value: AgentStep.EXECUTE_TOOLS.value,AgentStep.EXECUTE_TOOLS.value: AgentStep.SYNTHESIZE_ANSWER.value,AgentStep.SYNTHESIZE_ANSWER.value: AgentStep.COMPLETE.value,AgentStep.ERROR_RECOVERY.value: AgentStep.COMPLETE.value,AgentStep.COMPLETE.value: END,}return step_routing.get(state["current_step"], END)# Create enhanced workflowworkflow = StateGraph(AgentState)# Add nodesworkflow.add_node("analyze_question", RunnableLambda(analyze_question))workflow.add_node("select_tools", RunnableLambda(select_tools))workflow.add_node("execute_tools", RunnableLambda(execute_tools))workflow.add_node("synthesize_answer", RunnableLambda(synthesize_answer))workflow.add_node("error_recovery", RunnableLambda(error_recovery))# Set entry pointworkflow.set_entry_point("analyze_question")# Add conditional edgesworkflow.add_conditional_edges("analyze_question",lambda state: "select_tools" if state["current_step"] == AgentStep.SELECT_TOOLS.value else "error_recovery")workflow.add_edge("select_tools", "execute_tools")workflow.add_conditional_edges("execute_tools",lambda state: "synthesize_answer" if state["current_step"] == AgentStep.SYNTHESIZE_ANSWER.value else "error_recovery")workflow.add_conditional_edges("synthesize_answer",lambda state: END if state["current_step"] == AgentStep.COMPLETE.value else "error_recovery")workflow.add_edge("error_recovery", END)# Compile the enhanced graphgraph = workflow.compile()# ----------- Agent Class -----------class GaiaAgent:"""GAIA Agent with tools and intelligent processing"""def __init__(self):self.graph = graphself.tool_usage_stats = {}print("Enhanced GAIA Agent initialized with:")print("✓ Intelligent multi-query web search")print("✓ Entity-aware Wikipedia search")print("✓ Enhanced file processing tools")print("✓ Advanced error recovery")print("✓ Comprehensive result synthesis")def get_tool_stats(self) -> Dict[str, int]:"""Get usage statistics for tools"""return self.tool_usage_stats.copy()def __call__(self, task_id: str, question: str) -> str:print(f"\n{'='*60}")print(f"[{task_id}] ENHANCED PROCESSING: {question}")# Initialize stateprocessed_question = process_file(task_id, question)initial_state = initialize_state(processed_question)try:# Execute the enhanced workflowresult = self.graph.invoke(initial_state)# Extract resultsanswer = result.get("final_answer", "No answer generated")selected_tools = result.get("selected_tools", [])conversation_history = result.get("conversation_history", [])tool_results = result.get("tool_results", {})error_count = result.get("error_count", 0)# Update tool usage statisticsfor tool in selected_tools:self.tool_usage_stats[tool] = self.tool_usage_stats.get(tool, 0) + 1# Enhanced loggingprint(f"[{task_id}] Selected tools: {selected_tools}")print(f"[{task_id}] Tools executed: {list(tool_results.keys())}")print(f"[{task_id}] Processing steps: {len(conversation_history)}")print(f"[{task_id}] Errors encountered: {error_count}")# Log tool result sizes for debuggingfor tool, result in tool_results.items():result_size = len(str(result)) if result else 0print(f"[{task_id}] {tool} result size: {result_size} chars")print(f"[{task_id}] FINAL ANSWER: {answer}")print(f"{'='*60}")return answerexcept Exception as e:error_msg = f"Critical error in enhanced agent execution: {str(e)}"print(f"[{task_id}] {error_msg}")# Try fallback direct LLM responsetry:fallback_response = llm.invoke(f"Please answer this question: {question}").contentreturn f"Fallback response: {fallback_response}"except:return error_msg# ----------- Enhanced File Processing -----------def detect_file_type(file_path: str) -> Optional[str]:"""Enhanced file type detection with more formats"""ext = Path(file_path).suffix.lower()file_type_mapping = {# Spreadsheets'.xlsx': 'excel', '.xls': 'excel', '.csv': 'excel',# Images'.png': 'image', '.jpg': 'image', '.jpeg': 'image','.bmp': 'image', '.gif': 'image', '.tiff': 'image', '.webp': 'image',# Audio'.mp3': 'audio', '.wav': 'audio', '.ogg': 'audio','.flac': 'audio', '.m4a': 'audio', '.aac': 'audio',# Code'.py': 'code', '.ipynb': 'code', '.js': 'code', '.html': 'code','.css': 'code', '.java': 'code', '.cpp': 'code', '.c': 'code','.sql': 'code', '.r': 'code', '.json': 'code', '.xml': 'code',# Documents'.txt': 'text', '.md': 'text', '.pdf': 'document','.doc': 'document', '.docx': 'document'}return file_type_mapping.get(ext)def process_file(task_id: str, question_text: str) -> str:"""Enhanced file processing with better error handling and metadata"""file_url = f"{FILE_PATH}{task_id}"try:print(f"[{task_id}] Attempting to download file from: {file_url}")response = requests.get(file_url, timeout=30)response.raise_for_status()print(f"[{task_id}] File download successful. Status: {response.status_code}")except requests.exceptions.RequestException as exc:print(f"[{task_id}] File download failed: {str(exc)}")return question_text # Return original question if no file# Enhanced filename extractioncontent_disposition = response.headers.get("content-disposition", "")filename = task_id # Default fallback# Try to extract filename from Content-Disposition headerfilename_match = re.search(r'filename[*]?=(?:"([^"]+)"|([^;]+))', content_disposition)if filename_match:filename = filename_match.group(1) or filename_match.group(2)filename = filename.strip()# Create enhanced temp directory structuretemp_storage_dir = Path(tempfile.gettempdir()) / "gaia_enhanced_files" / task_idtemp_storage_dir.mkdir(parents=True, exist_ok=True)file_path = temp_storage_dir / filenamefile_path.write_bytes(response.content)# Get file metadatafile_size = len(response.content)file_type = detect_file_type(filename)print(f"[{task_id}] File saved: {filename} ({file_size:,} bytes, type: {file_type})")# Enhanced question augmentationenhanced_question = f"{question_text}\n\n"enhanced_question += f"{'='*50}\n"enhanced_question += f"FILE INFORMATION:\n"enhanced_question += f"A file was downloaded for this task and saved locally at:\n"enhanced_question += f"{file_path}\n"enhanced_question += f"File details:\n"enhanced_question += f"- Name: {filename}\n"enhanced_question += f"- Size: {file_size:,} bytes ({file_size/1024:.1f} KB)\n"enhanced_question += f"- Type: {file_type or 'unknown'}\n"enhanced_question += f"{'='*50}\n\n"return enhanced_question# ----------- Usage Examples and Testing -----------def run_enhanced_tests():"""Run comprehensive tests of the enhanced agent"""agent = GaiaAgent()test_cases = [{"id": "test_search_1","question": "What are the latest developments in artificial intelligence in 2024?","expected_tools": ["search"]},{"id": "test_wiki_1","question": "Tell me about Albert Einstein's contributions to physics","expected_tools": ["wikipedia"]},{"id": "test_combined_1","question": "What is machine learning and what are recent breakthroughs?","expected_tools": ["wikipedia", "search"]},{"id": "test_excel_1","question": "Analyze the data in the Excel file sales_data.xlsx","expected_tools": ["excel"]}]print("\n" + "="*80)print("RUNNING ENHANCED AGENT TESTS")print("="*80)for test_case in test_cases:print(f"\nTest Case: {test_case['id']}")print(f"Question: {test_case['question']}")print(f"Expected tools: {test_case['expected_tools']}")try:result = agent(test_case['id'], test_case['question'])print(f"Result length: {len(result)} characters")print(f"Result preview: {result[:200]}...")except Exception as e:print(f"Test failed: {e}")print("-" * 60)# Print tool usage statisticsprint(f"\nTool Usage Statistics:")for tool, count in agent.get_tool_stats().items():print(f" {tool}: {count} times")# Usage exampleif __name__ == "__main__":# Create enhanced agentagent = GaiaAgent()# Example usagesample_questions = [{"task_id": "bda648d7-d618-4883-88f4-3466eabd860e","question": "Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.","Level": "1","file_name": ""}# "Explain quantum computing and its recent developments",# "Tell me about the history of machine learning and current AI trends",]print("\n" + "="*80)print("ENHANCED GAIA AGENT DEMONSTRATION")print("="*80)for i, task in enumerate(sample_questions):print(f"\nExample {i+1}: {task['question']}")result = agent(task['task_id'], task['question'])print(f"Answer: {result[:300]}...")print("-" * 60)# Uncomment to run comprehensive tests# run_enhanced_tests()
agent.py+732-732