|
| 1 | +""" |
| 2 | +Test the API feature using JSONPlaceholder - a free fake REST API for testing. |
| 3 | +
|
| 4 | +This example demonstrates: |
| 5 | +1. Creating an API manually with endpoints |
| 6 | +2. Loading an API from an OpenAPI spec URL |
| 7 | +3. Using the API with an agent |
| 8 | +4. Making requests through the agent |
| 9 | +""" |
| 10 | + |
| 11 | +from dotenv import load_dotenv |
| 12 | + |
| 13 | +from agentle.agents.agent import Agent |
| 14 | +from agentle.agents.apis.api import API |
| 15 | +from agentle.agents.apis.endpoint import Endpoint |
| 16 | +from agentle.agents.apis.endpoint_parameter import EndpointParameter |
| 17 | +from agentle.agents.apis.http_method import HTTPMethod |
| 18 | +from agentle.agents.apis.parameter_location import ParameterLocation |
| 19 | +from agentle.agents.apis.primitive_schema import PrimitiveSchema |
| 20 | +from agentle.generations.providers.google.google_generation_provider import ( |
| 21 | + GoogleGenerationProvider, |
| 22 | +) |
| 23 | + |
| 24 | +load_dotenv(override=True) |
| 25 | + |
| 26 | + |
| 27 | +async def test_manual_api(): |
| 28 | + """Test creating an API manually.""" |
| 29 | + print("\n" + "=" * 70) |
| 30 | + print("TEST 1: Manual API Creation") |
| 31 | + print("=" * 70) |
| 32 | + |
| 33 | + # Create API manually |
| 34 | + api = API( |
| 35 | + name="JSONPlaceholder", |
| 36 | + description="Free fake REST API for testing and prototyping", |
| 37 | + base_url="https://jsonplaceholder.typicode.com", |
| 38 | + endpoints=[], |
| 39 | + ) |
| 40 | + |
| 41 | + # Test various path patterns to ensure function names are valid |
| 42 | + test_endpoints = [ |
| 43 | + # Normal path |
| 44 | + Endpoint( |
| 45 | + name="get_posts", |
| 46 | + description="Get all posts", |
| 47 | + path="/posts", |
| 48 | + method=HTTPMethod.GET, |
| 49 | + ), |
| 50 | + # Path with parameter |
| 51 | + Endpoint( |
| 52 | + name="get_post", |
| 53 | + description="Get a specific post by ID", |
| 54 | + path="/posts/{id}", |
| 55 | + method=HTTPMethod.GET, |
| 56 | + parameters=[ |
| 57 | + EndpointParameter( |
| 58 | + name="id", |
| 59 | + description="Post ID", |
| 60 | + parameter_schema=PrimitiveSchema(type="integer"), |
| 61 | + location=ParameterLocation.PATH, |
| 62 | + required=True, |
| 63 | + ) |
| 64 | + ], |
| 65 | + ), |
| 66 | + # Path with dashes (should be converted to underscores) |
| 67 | + Endpoint( |
| 68 | + name="get_user_posts", |
| 69 | + description="Get posts by user", |
| 70 | + path="/users/{userId}/posts", |
| 71 | + method=HTTPMethod.GET, |
| 72 | + parameters=[ |
| 73 | + EndpointParameter( |
| 74 | + name="userId", |
| 75 | + description="User ID", |
| 76 | + parameter_schema=PrimitiveSchema(type="integer"), |
| 77 | + location=ParameterLocation.PATH, |
| 78 | + required=True, |
| 79 | + ) |
| 80 | + ], |
| 81 | + ), |
| 82 | + ] |
| 83 | + |
| 84 | + for endpoint in test_endpoints: |
| 85 | + api.add_endpoint(endpoint) |
| 86 | + |
| 87 | + # Create agent with the API |
| 88 | + agent = Agent( |
| 89 | + name="Blog Assistant", |
| 90 | + generation_provider=GoogleGenerationProvider( |
| 91 | + use_vertex_ai=True, project="unicortex", location="global" |
| 92 | + ), |
| 93 | + model="gemini-2.5-flash", |
| 94 | + instructions="You are a helpful assistant that can fetch blog posts. When asked about posts, use the available tools.", |
| 95 | + apis=[api], |
| 96 | + ) |
| 97 | + |
| 98 | + # Test the agent |
| 99 | + result = agent.run("Get me post with ID 1") |
| 100 | + print(f"\n✅ Manual API test passed!") |
| 101 | + print(f"Response preview: {result.text[:100]}...") |
| 102 | + |
| 103 | + return result |
| 104 | + |
| 105 | + |
| 106 | +async def test_openapi_spec(): |
| 107 | + """Test loading an API from an OpenAPI spec.""" |
| 108 | + print("\n" + "=" * 70) |
| 109 | + print("TEST 2: OpenAPI Spec Loading") |
| 110 | + print("=" * 70) |
| 111 | + |
| 112 | + try: |
| 113 | + # Load a real OpenAPI spec from a public API |
| 114 | + # Using PetStore API as it's a standard example |
| 115 | + api = await API.from_openapi_spec( |
| 116 | + "https://petstore3.swagger.io/api/v3/openapi.json", |
| 117 | + name="PetStore", |
| 118 | + base_url_override="https://petstore3.swagger.io/api/v3", # Override the relative base URL |
| 119 | + include_operations=["getPetById"], # Only include one simple operation |
| 120 | + ) |
| 121 | + |
| 122 | + print(f"✅ Loaded API: {api.name}") |
| 123 | + print(f" Base URL: {api.base_url}") |
| 124 | + print(f" Endpoints: {len(api.endpoints)}") |
| 125 | + |
| 126 | + # Print endpoint names to verify they're valid |
| 127 | + for endpoint in api.endpoints: |
| 128 | + print(f" - {endpoint.name}: {endpoint.method.value} {endpoint.path}") |
| 129 | + |
| 130 | + # Create agent with the API |
| 131 | + agent = Agent( |
| 132 | + name="Pet Store Assistant", |
| 133 | + generation_provider=GoogleGenerationProvider( |
| 134 | + use_vertex_ai=True, project="unicortex", location="global" |
| 135 | + ), |
| 136 | + model="gemini-2.5-flash", |
| 137 | + instructions="You are a pet store assistant. You can look up pets by ID.", |
| 138 | + apis=[api], |
| 139 | + ) |
| 140 | + |
| 141 | + # Test the agent with a specific pet ID that should exist |
| 142 | + result = agent.run("Get information about pet with ID 1") |
| 143 | + print(f"\n✅ OpenAPI spec test passed!") |
| 144 | + print(f"Response preview: {result.text[:100]}...") |
| 145 | + |
| 146 | + return result |
| 147 | + |
| 148 | + except Exception as e: |
| 149 | + print(f"⚠️ OpenAPI spec test skipped: {e}") |
| 150 | + import traceback |
| 151 | + |
| 152 | + traceback.print_exc() |
| 153 | + return None |
| 154 | + |
| 155 | + |
| 156 | +async def main(): |
| 157 | + """Run all tests.""" |
| 158 | + print("\n🧪 Testing API Feature") |
| 159 | + print("=" * 70) |
| 160 | + |
| 161 | + # Test 1: Manual API creation |
| 162 | + await test_manual_api() |
| 163 | + |
| 164 | + # Test 2: OpenAPI spec loading |
| 165 | + await test_openapi_spec() |
| 166 | + |
| 167 | + print("\n" + "=" * 70) |
| 168 | + print("✅ All API tests completed successfully!") |
| 169 | + print("=" * 70) |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + import asyncio |
| 174 | + |
| 175 | + asyncio.run(main()) |
0 commit comments