|
1 | | -class APIBackend: |
2 | | - """abstract""" |
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import Any, Dict, List, Optional, Tuple, Union |
| 3 | + |
| 4 | + |
| 5 | +class APIBackend(ABC): |
| 6 | + """Abstract base class for LLM API backends""" |
| 7 | + |
| 8 | + @abstractmethod |
| 9 | + def build_chat_session( |
| 10 | + self, conversation_id: Optional[str] = None, session_system_prompt: Optional[str] = None |
| 11 | + ) -> Any: |
| 12 | + """Create a new chat session""" |
| 13 | + pass |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def build_messages_and_create_chat_completion( |
| 17 | + self, |
| 18 | + user_prompt: str, |
| 19 | + system_prompt: Optional[str] = None, |
| 20 | + former_messages: Optional[List[Any]] = None, |
| 21 | + chat_cache_prefix: str = "", |
| 22 | + shrink_multiple_break: bool = False, |
| 23 | + *args: Any, |
| 24 | + **kwargs: Any, |
| 25 | + ) -> str: |
| 26 | + """Build messages and get chat completion""" |
| 27 | + pass |
| 28 | + |
| 29 | + @abstractmethod |
| 30 | + def create_embedding( |
| 31 | + self, input_content: Union[str, List[str]], *args: Any, **kwargs: Any |
| 32 | + ) -> Union[List[Any], Any]: |
| 33 | + """Create embeddings for input text""" |
| 34 | + pass |
| 35 | + |
| 36 | + @abstractmethod |
| 37 | + def build_messages_and_calculate_token( |
| 38 | + self, |
| 39 | + user_prompt: str, |
| 40 | + system_prompt: Optional[str], |
| 41 | + former_messages: Optional[List[Dict[str, Any]]] = None, |
| 42 | + *, |
| 43 | + shrink_multiple_break: bool = False, |
| 44 | + ) -> int: |
| 45 | + """Build messages and calculate their token count""" |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +# TODO: seperate cache layer. try to be tranparent. |
| 50 | +class CachedAPIBackend(APIBackend): |
| 51 | + ... |
| 52 | + # @abstractmethod |
| 53 | + # def none_cache_function ... |
0 commit comments