File size: 4,955 Bytes
1637cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script to verify the fixes for list handling and DuckDuckGo integration
"""

import os
import sys

# Add current directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Set test API key
os.environ["ANTHROPIC_API_KEY"] = "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"

def test_clean_answer_with_lists():
    """Test that _clean_answer now handles lists properly"""
    print("=" * 60)
    print("Testing _clean_answer with different input types")
    print("=" * 60)
    
    try:
        from app import LangGraphAgent
        
        # Create a test agent
        agent = LangGraphAgent(os.environ["ANTHROPIC_API_KEY"])
        
        # Test cases that previously caused errors
        test_inputs = [
            "Normal string answer",
            ["This", "was", "a", "list"],  # This caused the error!
            {"answer": "dict input"},
            42,
            ["The answer is:", "42"],
            None,
        ]
        
        for test_input in test_inputs:
            print(f"\nInput: {test_input} (type: {type(test_input)})")
            try:
                result = agent._clean_answer(test_input)
                print(f"βœ… Success: '{result}'")
            except AttributeError as e:
                print(f"❌ AttributeError: {e}")
            except Exception as e:
                print(f"❌ Other error: {type(e).__name__}: {e}")
                
    except Exception as e:
        print(f"Failed to import or create agent: {e}")

def test_web_search_without_serpapi():
    """Test that web search works with DuckDuckGo"""
    print("\n" + "=" * 60)
    print("Testing DuckDuckGo web search (no API key needed)")
    print("=" * 60)
    
    try:
        from app import web_search
        
        # Test queries
        queries = [
            "Python programming",
            "Current president of France",
            "What is 2 + 2",
        ]
        
        for query in queries:
            print(f"\nSearching for: '{query}'")
            try:
                result = web_search(query, max_results=3)
                print(f"βœ… Search successful!")
                print(f"Result preview: {result[:200]}...")
            except Exception as e:
                print(f"❌ Search failed: {e}")
                
    except Exception as e:
        print(f"Failed to import web_search: {e}")

def test_tool_input_handling():
    """Test that all tools handle list inputs"""
    print("\n" + "=" * 60)
    print("Testing tool input handling")
    print("=" * 60)
    
    try:
        from app import calculator, python_executor, analyze_reversed_text
        
        # Test with list inputs
        test_cases = [
            ("calculator", calculator, ["2", "+", "2"]),
            ("python_executor", python_executor, ["print('Hello')", "print('World')"]),
            ("analyze_reversed_text", analyze_reversed_text, ["hello", "world"]),
        ]
        
        for tool_name, tool_func, list_input in test_cases:
            print(f"\nTesting {tool_name} with list input: {list_input}")
            try:
                result = tool_func(list_input)
                print(f"βœ… Success: {result[:100]}...")
            except AttributeError as e:
                print(f"❌ AttributeError: {e}")
            except Exception as e:
                print(f"❌ Other error: {type(e).__name__}: {e}")
                
    except Exception as e:
        print(f"Failed to import tools: {e}")

def test_gaia_question():
    """Test with an actual GAIA-like question"""
    print("\n" + "=" * 60)
    print("Testing with GAIA-like question")
    print("=" * 60)
    
    try:
        from app import BasicAgent
        
        # Create agent
        agent = BasicAgent()
        if agent.agent is None:
            agent.set_api_key(os.environ["ANTHROPIC_API_KEY"])
        
        # Test question
        question = "What is the capital of France?"
        
        print(f"Question: {question}")
        print("Running agent...")
        
        try:
            answer = agent(question)
            print(f"βœ… Answer: {answer}")
        except Exception as e:
            print(f"❌ Error: {type(e).__name__}: {e}")
            
    except Exception as e:
        print(f"Failed to test agent: {e}")

if __name__ == "__main__":
    print("GAIA Agent Fix Verification Tests")
    print("=" * 80)
    
    # Run all tests
    test_clean_answer_with_lists()
    test_web_search_without_serpapi()
    test_tool_input_handling()
    test_gaia_question()
    
    print("\n" + "=" * 80)
    print("Test Summary:")
    print("1. _clean_answer should now handle lists without 'lower' error")
    print("2. Web search should work with DuckDuckGo (no API key)")
    print("3. All tools should handle list inputs gracefully")
    print("4. Agent should provide clean, concise answers")