Data models#
Module defining public data validation models.
- class contextgem.public.data_models.LLMPricing(**data)[source]#
Bases:
_LLMPricing
Represents the pricing details for an LLM.
Defines the cost structure for processing input tokens and generating output tokens, with prices specified per million tokens.
- Variables:
input_per_1m_tokens (StrictFloat) – The cost in currency units for processing 1M input tokens.
output_per_1m_tokens (StrictFloat) – The cost in currency units for generating 1M output tokens.
- Parameters:
- Example:
- LLM pricing definition#
from contextgem import LLMPricing # Create a pricing model for an LLM (openai/o3-mini example) pricing = LLMPricing( input_per_1m_tokens=1.10, # $1.10 per million input tokens output_per_1m_tokens=4.40, # $4.40 per million output tokens ) # LLMPricing objects are immutable try: pricing.input_per_1m_tokens = 0.7 except ValueError as e: print(f"Error when trying to modify pricing: {e}")
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod from_dict(obj_dict)#
Reconstructs an instance of the class from a dictionary representation.
This method deserializes a dictionary containing the object’s attributes and values into a new instance of the class. It handles complex nested structures like aspects, concepts, and extracted items, properly reconstructing each component.
- classmethod from_disk(file_path)#
Loads an instance of the class from a JSON file stored on disk.
This method reads the JSON content from the specified file path and deserializes it into an instance of the class using the from_json method.
- Parameters:
file_path (str | Path) – Path to the JSON file to load (must end with ‘.json’). Can be a string or a Path object.
- Returns:
An instance of the class populated with the data from the file.
- Return type:
Self
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
RuntimeError – If deserialization fails.
- classmethod from_json(json_string)#
Creates an instance of the class from a JSON string representation.
This method deserializes the provided JSON string into a dictionary and uses the from_dict method to construct the class instance. It validates that the class name in the serialized data matches the current class.
- to_dict()#
Transforms the current object into a dictionary representation.
Converts the object to a dictionary that includes: - All public attributes - Special handling for specific public and private attributes
When an LLM or LLM group is serialized, its API credentials and usage/cost stats are removed.
- to_disk(file_path)#
Saves the serialized instance to a JSON file at the specified path.
This method converts the instance to a dictionary representation using to_dict(), then writes it to disk as a formatted JSON file with UTF-8 encoding.
- Parameters:
file_path (str | Path) – Path where the JSON file should be saved (must end with ‘.json’). Can be a string or a Path object.
- Return type:
- Returns:
None
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
RuntimeError – If there’s an error during the file writing process.
- to_json()#
Converts the object to its JSON string representation.
Serializes the object into a JSON-formatted string using the dictionary representation provided by the to_dict() method.
- Returns:
A JSON string representation of the object.
- Return type:
- input_per_1m_tokens: StrictFloat#
- output_per_1m_tokens: StrictFloat#
- class contextgem.public.data_models.RatingScale(*, start=0, end=10)[source]#
Bases:
_RatingScale
Represents a rating scale with defined minimum and maximum values.
Deprecated since version 0.10.0: RatingScale is deprecated and will be removed in v1.0.0. Use a tuple of (start, end) integers instead, e.g. (1, 5) instead of RatingScale(start=1, end=5).
This class defines a numerical scale for rating concepts, with configurable start and end values that determine the valid range for ratings.
- Variables:
start (StrictInt) – The minimum value of the rating scale (inclusive). Must be greater than or equal to 0.
end (StrictInt) – The maximum value of the rating scale (inclusive). Must be greater than 0.
- Parameters:
Initialize RatingScale with deprecation warning.
- classmethod from_dict(obj_dict)#
Reconstructs an instance of the class from a dictionary representation.
This method deserializes a dictionary containing the object’s attributes and values into a new instance of the class. It handles complex nested structures like aspects, concepts, and extracted items, properly reconstructing each component.
- classmethod from_disk(file_path)#
Loads an instance of the class from a JSON file stored on disk.
This method reads the JSON content from the specified file path and deserializes it into an instance of the class using the from_json method.
- Parameters:
file_path (str | Path) – Path to the JSON file to load (must end with ‘.json’). Can be a string or a Path object.
- Returns:
An instance of the class populated with the data from the file.
- Return type:
Self
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
RuntimeError – If deserialization fails.
- classmethod from_json(json_string)#
Creates an instance of the class from a JSON string representation.
This method deserializes the provided JSON string into a dictionary and uses the from_dict method to construct the class instance. It validates that the class name in the serialized data matches the current class.
- to_dict()#
Transforms the current object into a dictionary representation.
Converts the object to a dictionary that includes: - All public attributes - Special handling for specific public and private attributes
When an LLM or LLM group is serialized, its API credentials and usage/cost stats are removed.
- to_disk(file_path)#
Saves the serialized instance to a JSON file at the specified path.
This method converts the instance to a dictionary representation using to_dict(), then writes it to disk as a formatted JSON file with UTF-8 encoding.
- Parameters:
file_path (str | Path) – Path where the JSON file should be saved (must end with ‘.json’). Can be a string or a Path object.
- Return type:
- Returns:
None
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
RuntimeError – If there’s an error during the file writing process.
- to_json()#
Converts the object to its JSON string representation.
Serializes the object into a JSON-formatted string using the dictionary representation provided by the to_dict() method.
- Returns:
A JSON string representation of the object.
- Return type:
- start: StrictInt#
- end: StrictInt#