|
| 1 | +import uvicorn |
| 2 | +from fastapi import APIRouter, Body, FastAPI |
| 3 | +from fastapi.routing import APIRoute |
| 4 | + |
| 5 | +app = FastAPI() |
| 6 | +router = APIRouter(tags=["bank"]) |
| 7 | + |
| 8 | + |
| 9 | +@router.get("/user_profile_retrieve", description="A tool for querying user profile") |
| 10 | +def user_profile_retrieve( |
| 11 | + query: str = Body(description="query"), |
| 12 | + user_pin: str = Body(description="SystemArg.agent_pin"), |
| 13 | + agent_pin: str = Body(description="SystemArg.user_pin"), |
| 14 | +): |
| 15 | + user_profile_dict = { |
| 16 | + "001": "Arlen, a student, likes music", |
| 17 | + "002": "Tom, a programmer, likes sports", |
| 18 | + } |
| 19 | + portrait = user_profile_dict.get(user_pin, "Nothing") |
| 20 | + return f"The current user profile is: {portrait}" |
| 21 | + |
| 22 | + |
| 23 | +@router.post("/user_profile_deposit", description="A tool for updating user profile") |
| 24 | +def user_profile_deposit( |
| 25 | + content: str = Body(description="content"), |
| 26 | + user_pin: str = Body(description="SystemArg.agent_pin"), |
| 27 | + agent_pin: str = Body(description="SystemArg.user_pin"), |
| 28 | +) -> str: |
| 29 | + print(agent_pin, user_pin, content) |
| 30 | + return "updated user_profile" |
| 31 | + |
| 32 | + |
| 33 | +app.include_router(router) |
| 34 | + |
| 35 | + |
| 36 | +@app.get("/list_banks") |
| 37 | +def list_banks(): |
| 38 | + return get_banks_from_router(router) |
| 39 | + |
| 40 | + |
| 41 | +def get_banks_from_router(router: APIRouter): |
| 42 | + banks = [] |
| 43 | + for route in router.routes: |
| 44 | + if isinstance(route, APIRoute) and "bank" in getattr(route, "tags", []): |
| 45 | + description = route.description |
| 46 | + input_schema = {"type": "object", "properties": {}, "required": []} |
| 47 | + for param in route.dependant.query_params + route.dependant.body_params: |
| 48 | + param_type = param.type_ |
| 49 | + # Type conversion (simple implementation) |
| 50 | + if param_type is str: |
| 51 | + t = "string" |
| 52 | + elif param_type is int: |
| 53 | + t = "integer" |
| 54 | + elif param_type is float: |
| 55 | + t = "number" |
| 56 | + elif param_type is bool: |
| 57 | + t = "boolean" |
| 58 | + else: |
| 59 | + t = "string" |
| 60 | + input_schema["properties"][param.name] = { |
| 61 | + "type": t, |
| 62 | + "description": param.field_info.description or "", |
| 63 | + } |
| 64 | + if param.required: |
| 65 | + input_schema["required"].append(param.name) |
| 66 | + banks.append( |
| 67 | + { |
| 68 | + "name": route.endpoint.__name__, |
| 69 | + "endpoint": route.path, |
| 70 | + "methods": route.methods, |
| 71 | + "description": description, |
| 72 | + "inputSchema": input_schema, |
| 73 | + } |
| 74 | + ) |
| 75 | + return banks |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + uvicorn.run(app, host="127.0.0.1", port=8090) |
0 commit comments