Skip to content

model

Messages module-attribute

A type alias for the various types of messages that can be passed to a model.

ModelType

Bases: Enum

CHAT class-attribute instance-attribute

CHAT = 0

EMBEDDING class-attribute instance-attribute

EMBEDDING = 1

CODE class-attribute instance-attribute

CODE = 2

Model

Bases: BaseModel, ABC

Abstract class for all Large Language Models.

This class outlines a standardized lifecycle for interacting with LLMs, aimed at ensuring a consistent process for message trimming, pre-processing, preparing requests for the model, invoking the model, standardizing the response, and post-processing. The lifecycle is as follows:

Lifecycle
  1. Pre-processing ([_preprocess][silverlingua.core.templates.model.Model._preprocess]): Performs any necessary transformations or adjustments to the messages prior to trimming or preparing them for model input. (Optional)

  2. Preparing Request ([_format_request][silverlingua.core.templates.model.Model._format_request]): Converts the pre-processed messages into a format suitable for model input.

  3. Model Invocation ([_call][silverlingua.core.templates.model.Model._call] or [_acall][silverlingua.core.templates.model.Model._acall]): Feeds the prepared input to the LLM and retrieves the raw model output. There should be both synchronous and asynchronous versions available.

  4. Standardizing Response ([_standardize_response][silverlingua.core.templates.model.Model._standardize_response]): Transforms the raw model output into a consistent response format suitable for further processing or delivery.

  5. Post-processing ([_postprocess][silverlingua.core.templates.model.Model._postprocess]): Performs any final transformations or adjustments to the standardized responses, making them ready for delivery. (Optional)

Subclasses should implement each of the non-optional lifecycle steps in accordance with the specific requirements and behaviors of the target LLM.

See also
  • [Agent][silverlingua.core.templates.agent.Agent]
  • [Idearium][silverlingua.core.organisms.idearium.Idearium]
  • [Notion][silverlingua.core.molecules.notion.Notion]

model_config class-attribute instance-attribute

model_config = ConfigDict(frozen=True)

max_response class-attribute instance-attribute

max_response: int = Field(default=0)

api_key instance-attribute

api_key: str

name instance-attribute

name: str

role instance-attribute

role: Type[ChatRole]

type instance-attribute

type: ModelType

llm instance-attribute

llm: Callable

llm_async instance-attribute

llm_async: Callable

can_stream instance-attribute

can_stream: bool

tokenizer instance-attribute

tokenizer: Tokenizer

max_tokens abstractmethod property

max_tokens: int

The maximum number of tokens that can be fed to the model at once.

_process_input

_process_input(messages: Messages) -> Idearium
Source code in src/SilverLingua/core/templates/model.py
def _process_input(self, messages: Messages) -> Idearium:
    if isinstance(messages, str):
        notions = [Notion(content=messages, role=self.role.HUMAN)]
    elif isinstance(messages, Notion):
        notions = [messages]
    elif isinstance(messages, Idearium):
        return messages  # Already an Idearium, no need to convert
    elif isinstance(messages, list):
        notions = [
            (
                Notion(content=msg, role=self.role.HUMAN)
                if isinstance(msg, str)
                else msg
            )
            for msg in messages
        ]
    else:
        raise ValueError("Invalid input type for messages")

    return Idearium(self.tokenizer, self.max_tokens, notions)

_convert_role

_convert_role(role: ChatRole) -> str

Converts the standard ChatRole to the model-specific role.

Source code in src/SilverLingua/core/templates/model.py
def _convert_role(self, role: ChatRole) -> str:
    """
    Converts the standard ChatRole to the model-specific role.
    """
    return str(self.role[role.name].value)

_preprocess

_preprocess(messages: List[Notion]) -> List[Notion]

Preprocesses the List of Notions, applying any effects necessary before being prepped for input into an API.

This is a lifecycle method that is called by the generate method.

Source code in src/SilverLingua/core/templates/model.py
def _preprocess(self, messages: List[Notion]) -> List[Notion]:
    """
    Preprocesses the List of `Notions`, applying any effects necessary
    before being prepped for input into an API.

    This is a lifecycle method that is called by the `generate` method.
    """
    return [
        Notion(msg.content, self._convert_role(msg.chat_role), msg.persistent)
        for msg in messages
    ]

_format_request abstractmethod

_format_request(messages: List[Notion], *args, **kwargs) -> Union[str, object]

Formats the List of Notions into a format suitable for model input.

This is a lifecycle method that is called by the generate method.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
def _format_request(
    self, messages: List[Notion], *args, **kwargs
) -> Union[str, object]:
    """
    Formats the List of `Notions` into a format suitable for model input.

    This is a lifecycle method that is called by the `generate` method.
    """
    pass

_standardize_response abstractmethod

_standardize_response(response: Union[object, str, List[any]], *args, **kwargs) -> List[Notion]

Standardizes the raw response from the model into a List of Notions.

This is a lifecycle method that is called by the generate method.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
def _standardize_response(
    self, response: Union[object, str, List[any]], *args, **kwargs
) -> List[Notion]:
    """
    Standardizes the raw response from the model into a List of Notions.

    This is a lifecycle method that is called by the `generate` method.
    """
    pass

_postprocess abstractmethod

_postprocess(response: List[Notion], *args, **kwargs) -> List[Notion]

Postprocesses the response from the model, applying any final effects before being returned.

This is a lifecycle method that is called by the generate method.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
def _postprocess(self, response: List[Notion], *args, **kwargs) -> List[Notion]:
    """
    Postprocesses the response from the model, applying any final effects
    before being returned.

    This is a lifecycle method that is called by the `generate` method.
    """
    pass

_retry_call abstractmethod

_retry_call(input: Union[str, object, List[any]], e: Exception, api_call: Callable, retries: int = 0) -> Union[str, object]

Retry logic for API calls used by _common_call_logic.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
def _retry_call(
    self,
    input: Union[str, object, List[any]],
    e: Exception,
    api_call: Callable,
    retries: int = 0,
) -> Union[str, object]:
    """
    Retry logic for API calls used by `_common_call_logic`.
    """
    pass

_common_call_logic

_common_call_logic(input: Union[str, object, List[any]], api_call: Callable, retries: int = 0) -> Union[str, object]
Source code in src/SilverLingua/core/templates/model.py
def _common_call_logic(
    self,
    input: Union[str, object, List[any]],
    api_call: Callable,
    retries: int = 0,
) -> Union[str, object]:
    if input is None:
        raise ValueError("No input provided.")

    try:
        out = api_call(messages=input)
        return out
    except Exception as e:
        logger.error(f"Error calling LLM API: {e}")
        if retries >= 3:
            raise e

        return self._retry_call(input, e, api_call, retries=retries)

_call

_call(input: Union[str, object, List[any]], retries: int = 0, **kwargs) -> object

Calls the model with the given input and returns the raw response.

Should behave exactly as _acall does, but synchronously.

This is a lifecycle method that is called by the generate method.

Source code in src/SilverLingua/core/templates/model.py
def _call(
    self, input: Union[str, object, List[any]], retries: int = 0, **kwargs
) -> object:
    """
    Calls the model with the given input and returns the raw response.

    Should behave exactly as `_acall` does, but synchronously.

    This is a lifecycle method that is called by the `generate` method.
    """

    def api_call(**kwargs_):
        return self.llm(**kwargs_, **kwargs)

    return self._common_call_logic(input, api_call, retries)

_acall async

_acall(input: Union[str, object, List[any]], retries: int = 0, **kwargs) -> object

Calls the model with the given input and returns the raw response asynchronously.

Should behave exactly as _call does, but asynchronously.

This is a lifecycle method that is called by the agenerate method.

Source code in src/SilverLingua/core/templates/model.py
async def _acall(
    self, input: Union[str, object, List[any]], retries: int = 0, **kwargs
) -> object:
    """
    Calls the model with the given input and returns the
    raw response asynchronously.

    Should behave exactly as `_call` does, but asynchronously.

    This is a lifecycle method that is called by the `agenerate` method.
    """

    async def api_call(**kwargs_):
        return await self.llm_async(**kwargs_, **kwargs)

    result = self._common_call_logic(input, api_call, retries)
    if asyncio.iscoroutine(result):
        return await result
    return result

_common_generate_logic

_common_generate_logic(messages: Messages, is_async=False, **kwargs)
Source code in src/SilverLingua/core/templates/model.py
def _common_generate_logic(
    self,
    messages: Messages,
    is_async=False,
    **kwargs,
):
    if messages is None:
        raise ValueError("No messages provided.")

    call_method = self._acall if is_async else self._call

    idearium = self._process_input(messages)
    input = self._format_request(self._preprocess(idearium))

    if is_async:

        async def call():
            response = await call_method(input, **kwargs)
            output = self._standardize_response(response)
            return self._postprocess(output)

        return call()
    else:
        response = call_method(input, **kwargs)
        output = self._standardize_response(response)
        return self._postprocess(output)

generate abstractmethod

generate(messages: Messages, *args, **kwargs) -> List[Notion]

Calls the model with the given messages and returns the response.

Messages can be any of: string, list of strings, Notion, list of Notions, or Idearium.

This is the primary method for generating responses from the model, and is responsible for calling all of the lifecycle methods.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
def generate(
    self,
    messages: Messages,
    *args,
    **kwargs,
) -> List[Notion]:
    """
    Calls the model with the given messages and returns the response.

    Messages can be any of:
    string, list of strings, Notion, list of Notions, or Idearium.

    This is the primary method for generating responses from the model,
    and is responsible for calling all of the lifecycle methods.
    """
    pass

agenerate abstractmethod async

agenerate(messages: Messages, *args, **kwargs) -> List[Notion]

Calls the model with the given messages and returns the response asynchronously.

Messages can be any of: string, list of strings, Notion, list of Notions, or Idearium.

This is the primary method for generating async responses from the model, and is responsible for calling all of the lifecycle methods.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
async def agenerate(
    self,
    messages: Messages,
    *args,
    **kwargs,
) -> List[Notion]:
    """
    Calls the model with the given messages and returns the response
    asynchronously.

    Messages can be any of:
    string, list of strings, Notion, list of Notions, or Idearium.

    This is the primary method for generating async responses from the model,
    and is responsible for calling all of the lifecycle methods.
    """
    pass

_common_stream_logic

_common_stream_logic(messages: Messages)
Source code in src/SilverLingua/core/templates/model.py
def _common_stream_logic(self, messages: Messages):
    if messages is None:
        raise ValueError("No messages provided.")

    if not self.can_stream:
        raise ValueError(
            "This model does not support streaming. "
            + "Please use the `generate` method instead."
        )

    idearium = self._process_input(messages)
    input = self._format_request(self._preprocess(idearium))
    return input

stream abstractmethod

stream(messages: Messages, *args, **kwargs) -> Generator[Notion, Any, None]

Streams the model with the given messages and returns the response, one token at a time.

Messages can be any of: string, list of strings, Notion, list of Notions, or Idearium.

If the model cannot be streamed, this will raise an exception.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
def stream(
    self, messages: Messages, *args, **kwargs
) -> Generator[Notion, Any, None]:
    """
    Streams the model with the given messages and returns the response,
    one token at a time.

    Messages can be any of:
    string, list of strings, Notion, list of Notions, or Idearium.

    If the model cannot be streamed, this will raise an exception.
    """
    pass

astream abstractmethod async

astream(messages: Messages, *args, **kwargs) -> Generator[Notion, Any, None]

Streams the model with the given messages and returns the response, one token at a time, asynchronously.

Messages can be any of: string, list of strings, Notion, list of Notions, or Idearium.

If the model cannot be streamed, this will raise an exception.

Source code in src/SilverLingua/core/templates/model.py
@abstractmethod
async def astream(
    self, messages: Messages, *args, **kwargs
) -> Generator[Notion, Any, None]:
    """
    Streams the model with the given messages and returns the response,
    one token at a time, asynchronously.

    Messages can be any of:
    string, list of strings, Notion, list of Notions, or Idearium.

    If the model cannot be streamed, this will raise an exception.
    """
    pass