model
¶
Messages
module-attribute
¶
A type alias for the various types of messages that can be passed to a model.
ModelType
¶
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
-
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) -
Preparing Request ([
_format_request][silverlingua.core.templates.model.Model._format_request]): Converts the pre-processed messages into a format suitable for model input. -
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. -
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. -
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]
max_tokens
abstractmethod
property
¶
max_tokens: int
The maximum number of tokens that can be fed to the model at once.
_process_input
¶
Source code in src/SilverLingua/core/templates/model.py
_convert_role
¶
_preprocess
¶
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
_format_request
abstractmethod
¶
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
_standardize_response
abstractmethod
¶
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
_postprocess
abstractmethod
¶
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
_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
_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
_call
¶
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
_acall
async
¶
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
_common_generate_logic
¶
_common_generate_logic(messages: Messages, is_async=False, **kwargs)
Source code in src/SilverLingua/core/templates/model.py
generate
abstractmethod
¶
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
agenerate
abstractmethod
async
¶
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
_common_stream_logic
¶
_common_stream_logic(messages: Messages)
Source code in src/SilverLingua/core/templates/model.py
stream
abstractmethod
¶
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
astream
abstractmethod
async
¶
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.