Expose
What it is
- An abstract base class (ABC) defining a contract for components that can be exposed:
- as LangChain tools (for agent use)
- as FastAPI routes (for HTTP access)
Public API
Class: Expose
Abstract interface; implement both methods in concrete subclasses.
-
as_tools(self) -> list[BaseTool]- Purpose: Return a list of LangChain tools representing the component’s functionality.
- Notes:
BaseToolis only imported for typing underTYPE_CHECKING.
-
as_api(self, router: APIRouter, route_name: str = "", name: str = "", description: str = "", description_stream: str = "", tags: list[str | Enum] | None = []) -> None- Purpose: Register FastAPI routes on the provided
APIRouterto expose the component over HTTP. - Parameters:
router: FastAPIAPIRouterto register routes onroute_name,name,description,description_stream: optional metadata stringstags: optional list of tags (strorEnum) for FastAPI route tagging (default is[]in signature)
- Purpose: Register FastAPI routes on the provided
Configuration/Dependencies
- Depends on
fastapi.APIRouter. - Type hints refer to
langchain_core.tools.BaseTool(imported only when type-checking).
Usage
from fastapi import APIRouter
from naas_abi_core.utils.Expose import Expose
class MyFeature(Expose):
def as_tools(self):
return [] # return a list of BaseTool instances
def as_api(self, router: APIRouter, route_name: str = "", name: str = "",
description: str = "", description_stream: str = "", tags=None):
@router.get(f"/{route_name or 'my-feature'}", tags=tags or [])
def endpoint():
return {"ok": True}
router = APIRouter()
MyFeature().as_api(router, route_name="health")Caveats
- Both methods are abstract; instantiating
Exposedirectly is not possible. tagsin the abstract method signature defaults to[](a mutable default). Implementations may want to useNoneand normalize to a list internally.