mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-09-17 23:51:19 +00:00
- Integrate AnthropicChatModel with new AnthropicAsLLM component - Add component formatters for OpenAI and Anthropic chat models - Implement token counter component with estimated token counting - Create base client component for ReMe service communication - Refactor BaseComponent to remove app_context parameter from _start/_close - Update embedding model base class to remove retry logic and use npz cache - Add job component for sequential step execution with BaseJob - Implement step component base class for LLM workflow execution - Enhance application context with proper type annotations - Update component initialization to pass app_context automatically - Remove asyncio dependency from embedding model cache operations
90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
"""Base class for components."""
|
|
|
|
from abc import ABC, abstractmethod
|
|
from typing import TYPE_CHECKING
|
|
|
|
from ..enumeration import ComponentEnum
|
|
from ..utils.logger_utils import get_logger
|
|
|
|
if TYPE_CHECKING:
|
|
from .application_context import ApplicationContext
|
|
|
|
|
|
class BaseComponent(ABC):
|
|
"""Async lifecycle base class with context manager support.
|
|
|
|
Subclasses must implement ``_start`` and ``_close``.
|
|
"""
|
|
|
|
component_type = ComponentEnum.BASE
|
|
|
|
def __init__(
|
|
self,
|
|
name: str | None = None,
|
|
backend: str | None = None,
|
|
app_context: "ApplicationContext | None" = None,
|
|
**kwargs,
|
|
) -> None:
|
|
self.name: str = name or self.__class__.__name__
|
|
self.backend: str | None = backend
|
|
self.app_context: "ApplicationContext | None" = app_context
|
|
self.kwargs: dict = dict(kwargs)
|
|
self.logger = get_logger()
|
|
if hasattr(self.logger, "bind"):
|
|
self.logger = self.logger.bind(component=self.name)
|
|
self._is_started: bool = False
|
|
|
|
@abstractmethod
|
|
async def _start(self) -> None: ...
|
|
|
|
@abstractmethod
|
|
async def _close(self) -> None: ...
|
|
|
|
async def start(self) -> None:
|
|
"""Start the component. No-op if already started."""
|
|
if self._is_started:
|
|
return
|
|
await self._start()
|
|
self._is_started = True
|
|
|
|
async def close(self) -> None:
|
|
"""Close the component. No-op if not started."""
|
|
if not self._is_started:
|
|
return
|
|
try:
|
|
await self._close()
|
|
finally:
|
|
self._is_started = False
|
|
|
|
async def restart(self) -> None:
|
|
"""Close then start."""
|
|
await self.close()
|
|
await self.start()
|
|
|
|
@property
|
|
def is_started(self) -> bool:
|
|
return self._is_started
|
|
|
|
async def __call__(self, **kwargs): ...
|
|
|
|
async def __aenter__(self) -> "BaseComponent":
|
|
await self.start()
|
|
return self
|
|
|
|
async def __aexit__(
|
|
self,
|
|
exc_type: type[BaseException] | None,
|
|
exc_val: BaseException | None,
|
|
exc_tb,
|
|
) -> bool:
|
|
if self._is_started:
|
|
if exc_val is not None:
|
|
try:
|
|
await self._close()
|
|
except BaseException as close_exc:
|
|
raise close_exc from exc_val
|
|
finally:
|
|
self._is_started = False
|
|
else:
|
|
await self.close()
|
|
return False
|