File size: 4,614 Bytes
ca41776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673a52e
ca41776
 
 
 
 
 
 
 
 
673a52e
ca41776
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
#!/usr/bin/env python3
"""Test the BioFlow Agent API endpoints."""
import requests
import json

BASE_URL = "http://localhost:8000"

def test_health():
    """Test health endpoint."""
    r = requests.get(f"{BASE_URL}/health")
    print(f"Health: {r.status_code}")
    return r.status_code == 200

def test_generate():
    """Test molecule generation endpoint."""
    print("\n[1] Testing /api/agents/generate...")
    
    data = {
        "prompt": "Design a kinase inhibitor for EGFR",
        "mode": "text",
        "num_samples": 3,
    }
    
    r = requests.post(f"{BASE_URL}/api/agents/generate", json=data)
    print(f"   Status: {r.status_code}")
    
    if r.status_code == 200:
        result = r.json()
        print(f"   [OK] Generated: {len(result.get('molecules', []))} molecules")
        for mol in result.get("molecules", [])[:2]:
            smiles = mol.get("smiles", "")[:40]
            print(f"      - {smiles}...")
        return True
    else:
        print(f"   [ERROR] {r.text}")
        return False

def test_validate():
    """Test molecule validation endpoint."""
    print("\n[2] Testing /api/agents/validate...")
    
    data = {
        "smiles": [
            "CC(=O)Oc1ccccc1C(=O)O",  # Aspirin
            "Cn1cnc2c1c(=O)n(C)c(=O)n2C",  # Caffeine
            "CC(=O)Nc1ccc(O)cc1",  # Paracetamol
        ],
    }
    
    r = requests.post(f"{BASE_URL}/api/agents/validate", json=data)
    print(f"   Status: {r.status_code}")
    
    if r.status_code == 200:
        result = r.json()
        summary = result.get("summary", {})
        print(f"   [OK] Validated: {summary.get('total', 0)} molecules")
        print(f"      Passed: {summary.get('passed', 0)}")
        for val in result.get("validations", [])[:2]:
            smiles = val.get("smiles", "")[:25]
            score = val.get("score", 0)
            status = val.get("status", "?")
            print(f"      - {smiles}... score={score:.3f} ({status})")
        return True
    else:
        print(f"   [ERROR] {r.text}")
        return False

def test_rank():
    """Test candidate ranking endpoint."""
    print("\n[3] Testing /api/agents/rank...")
    
    data = {
        "candidates": [
            {"smiles": "CCO", "validation_score": 0.8, "confidence": 0.9},
            {"smiles": "CC", "validation_score": 0.9, "confidence": 0.7},
            {"smiles": "CCC", "validation_score": 0.7, "confidence": 0.8},
        ],
        "top_k": 2,
    }
    
    r = requests.post(f"{BASE_URL}/api/agents/rank", json=data)
    print(f"   Status: {r.status_code}")
    
    if r.status_code == 200:
        result = r.json()
        ranked = result.get("ranked", [])
        print(f"   [OK] Ranked: {len(ranked)} candidates")
        for cand in ranked:
            print(f"      [{cand.get('rank')}] {cand.get('smiles')} score={cand.get('final_score', 0):.3f}")
        return True
    else:
        print(f"   [ERROR] {r.text}")
        return False

def test_workflow():
    """Test full discovery workflow endpoint."""
    print("\n[4] Testing /api/agents/workflow...")
    
    data = {
        "query": "Design an EGFR inhibitor with good oral bioavailability",
        "num_candidates": 5,
        "top_k": 3,
    }
    
    r = requests.post(f"{BASE_URL}/api/agents/workflow", json=data)
    print(f"   Status: {r.status_code}")
    
    if r.status_code == 200:
        result = r.json()
        print(f"   [OK] Workflow status: {result.get('status')}")
        print(f"      Steps: {result.get('steps_completed')}/{result.get('total_steps')}")
        print(f"      Time: {result.get('execution_time_ms', 0):.1f}ms")
        
        top = result.get("top_candidates", [])
        print(f"      Top {len(top)} candidates:")
        for cand in top:
            smiles = cand.get("smiles", "")[:30]
            score = cand.get("final_score", 0)
            print(f"         - {smiles}... (score={score:.3f})")
        return True
    else:
        print(f"   [ERROR] {r.text}")
        return False

if __name__ == "__main__":
    print("=" * 60)
    print("BioFlow Agent API Test")
    print("=" * 60)
    
    # Wait for server
    import time
    for i in range(10):
        try:
            if test_health():
                break
        except:
            print(f"Waiting for server... ({i+1}/10)")
            time.sleep(1)
    else:
        print("[ERROR] Server not available")
        exit(1)
    
    # Run tests
    test_generate()
    test_validate()
    test_rank()
    test_workflow()
    
    print("\n" + "=" * 60)
    print("[OK] Agent API tests complete!")
    print("=" * 60)