sherpa_ai.models package#
Overview#
The models package provides interfaces and wrappers for language models in Sherpa AI, enabling seamless integration with various LLM providers while adding enhanced functionality like logging and error handling.
Key Components
SherpaBaseChatModel: Core interface for chat-based language models
SherpaBaseModel: Base implementation for all model types
ChatModelWithLogging: Chat model wrapper with integrated logging capabilities
Example Usage#
from sherpa_ai.models.sherpa_base_chat_model import SherpaBaseChatModel
# Initialize a chat model
model = SherpaBaseChatModel(
model_name="gpt-4",
temperature=0.7,
max_tokens=1000
)
# Generate a response to a user query
response = model.generate(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
]
)
print(response)
Submodules#
Module |
Description |
|---|---|
Provides a chat model wrapper with integrated logging capabilities. |
|
Implements the core interface for chat-based language models. |
|
Contains the base implementation for all model types in the system. |
sherpa_ai.models.chat_model_with_logging module#
Chat model with logging functionality for Sherpa AI.
This module provides a wrapper for chat models that adds detailed logging capabilities. It defines the ChatModelWithLogging class which logs all model interactions including inputs, outputs, and model information.
- class sherpa_ai.models.chat_model_with_logging.ChatModelWithLogging(*args, **kwargs)[source]#
Bases:
BaseChatModelChat model wrapper that adds detailed logging functionality.
This class wraps any chat model to add comprehensive logging of all interactions, including input messages, generated responses, and model information.
- llm#
The underlying chat model to wrap.
- Type:
BaseChatModel
Example
>>> base_model = ChatOpenAI() >>> model = ChatModelWithLogging(llm=base_model, logger=custom_logger) >>> response = model.generate([Message("Hello")]) >>> # Logs will include input, output, and model info
- llm: BaseChatModel#
- logger: Logger#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'protected_namespaces': ()}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
sherpa_ai.models.sherpa_base_chat_model module#
Chat model integration module for Sherpa AI.
This module provides chat model integration for the Sherpa AI system. It defines base and OpenAI-specific chat model classes with Sherpa enhancements like usage tracking and verbose logging.
- class sherpa_ai.models.sherpa_base_chat_model.SherpaBaseChatModel(*args, **kwargs)[source]#
Bases:
BaseChatModelBase chat model with Sherpa-specific enhancements.
This class extends the base chat model to add Sherpa-specific functionality, including user-based token usage tracking and verbose logging capabilities.
- user_id#
ID of the user making model requests.
- Type:
Optional[str]
- verbose_logger#
Logger for detailed operation tracking.
- Type:
Example
>>> model = SherpaBaseChatModel(user_id="user123") >>> response = model.generate([Message("Hello")]) >>> print(response.generations[0].text) 'Hi there!'
- user_id: str | None#
- session_id: str | None#
- agent_name: str | None#
- verbose_logger: BaseVerboseLogger#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'protected_namespaces': ()}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sherpa_ai.models.sherpa_base_chat_model.SherpaChatOpenAI(*args, **kwargs)[source]#
Bases:
ChatOpenAIEnhanced OpenAI chat model with Sherpa-specific features.
This class extends the OpenAI chat model to add Sherpa-specific functionality, including user-based token usage tracking and verbose logging capabilities.
- user_id#
ID of the user making model requests.
- Type:
Optional[str]
- verbose_logger#
Logger for detailed operation tracking.
- Type:
Example
>>> model = SherpaChatOpenAI(user_id="user123") >>> response = model.generate([Message("Hello")]) >>> print(response.generations[0].text) 'Hi there!'
- user_id: str | None#
- session_id: str | None#
- agent_name: str | None#
- verbose_logger: BaseVerboseLogger#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'populate_by_name': True, 'protected_namespaces': (), 'validate_by_alias': True, 'validate_by_name': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
sherpa_ai.models.sherpa_base_model module#
Base OpenAI model integration for Sherpa AI.
This module provides the base OpenAI model integration for the Sherpa AI system. It defines the SherpaOpenAI class which extends OpenAI’s chat model with Sherpa-specific functionality like usage tracking.
- class sherpa_ai.models.sherpa_base_model.SherpaOpenAI(*args, **kwargs)[source]#
Bases:
ChatOpenAIEnhanced OpenAI chat model with Sherpa-specific features.
This class extends the OpenAI chat model to add Sherpa-specific functionality, particularly user-based token usage tracking. It maintains compatibility with the base ChatOpenAI interface while adding user tracking capabilities.
- user_id#
ID of the user making model requests.
- Type:
Optional[str]
Example
>>> model = SherpaOpenAI(user_id="user123") >>> response = model.generate("What is the weather?") >>> print(response.generations[0].text) 'The weather is sunny.'
- user_id: str | None#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'populate_by_name': True, 'protected_namespaces': (), 'validate_by_alias': True, 'validate_by_name': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Module contents#
Language model integration module for Sherpa AI.
This module provides language model integration for the Sherpa AI system. It exports the SherpaOpenAI and SherpaChatOpenAI classes which provide interfaces to OpenAI’s language models with Sherpa-specific enhancements.
Example
>>> from sherpa_ai.models import SherpaOpenAI, SherpaChatOpenAI
>>> model = SherpaOpenAI()
>>> chat_model = SherpaChatOpenAI()
>>> response = model.generate("Hello")
>>> chat_response = chat_model.chat("How are you?")
- class sherpa_ai.models.SherpaOpenAI(*args, **kwargs)[source]#
Bases:
ChatOpenAIEnhanced OpenAI chat model with Sherpa-specific features.
This class extends the OpenAI chat model to add Sherpa-specific functionality, particularly user-based token usage tracking. It maintains compatibility with the base ChatOpenAI interface while adding user tracking capabilities.
- user_id#
ID of the user making model requests.
- Type:
Optional[str]
Example
>>> model = SherpaOpenAI(user_id="user123") >>> response = model.generate("What is the weather?") >>> print(response.generations[0].text) 'The weather is sunny.'
- user_id: str | None#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'populate_by_name': True, 'protected_namespaces': (), 'validate_by_alias': True, 'validate_by_name': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class sherpa_ai.models.SherpaChatOpenAI(*args, **kwargs)[source]#
Bases:
ChatOpenAIEnhanced OpenAI chat model with Sherpa-specific features.
This class extends the OpenAI chat model to add Sherpa-specific functionality, including user-based token usage tracking and verbose logging capabilities.
- user_id#
ID of the user making model requests.
- Type:
Optional[str]
- verbose_logger#
Logger for detailed operation tracking.
- Type:
Example
>>> model = SherpaChatOpenAI(user_id="user123") >>> response = model.generate([Message("Hello")]) >>> print(response.generations[0].text) 'Hi there!'
- user_id: str | None#
- session_id: str | None#
- agent_name: str | None#
- verbose_logger: BaseVerboseLogger#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'populate_by_name': True, 'protected_namespaces': (), 'validate_by_alias': True, 'validate_by_name': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].