Aspects#
Module for handling document aspects.
This module provides the Aspect class, which represents a defined area or topic within a document that requires focused attention. Aspects are used to identify and extract specific subjects or themes from documents according to predefined criteria.
Aspects can be associated with concepts, reference paragraphs and sentences from the source document, and can be configured with different LLM roles for extraction and reasoning tasks.
The module integrates with the broader ContextGem framework for document analysis and information extraction.
- class contextgem.public.aspects.Aspect(**data)[source]#
Bases:
_AssignedInstancesProcessor
,_ExtractedItemsAttributeProcessor
,_RefParasAndSentsAttrituteProcessor
Represents an aspect with associated metadata, sub-aspects, concepts, and logic for validation.
An aspect is a defined area or topic within a document that requires focused attention. Each aspect corresponds to a specific subject or theme described in the task.
- Variables:
name – The name of the aspect. Required, non-empty string.
description – A detailed description of the aspect. Required, non-empty string.
concepts – A list of concepts associated with the aspect. These concepts must be unique in both name and description and cannot include concepts with vision LLM roles.
llm_role – The role of the LLM responsible for aspect extraction. Default is “extractor_text”. Valid roles are “extractor_text” and “reasoner_text”.
reference_depth – The structural depth of references (paragraphs or sentences). Defaults to “paragraphs”. Affects the structure of
extracted_items
.add_justifications – Whether the LLM will output justification for each extracted item. Inherited from base class. Defaults to False.
justification_depth – The level of detail for justifications. Inherited from base class. Defaults to “brief”.
justification_max_sents – Maximum number of sentences in a justification. Inherited from base class. Defaults to 2.
- Parameters:
- Example:
- Aspect definition#
from contextgem import Aspect # Define an aspect focused on termination clauses termination_aspect = Aspect( name="Termination provisions", description="Contract termination conditions, notice requirements, and severance terms.", reference_depth="sentences", add_justifications=True, justification_depth="comprehensive", )
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.
- name: NonEmptyStr#
- description: NonEmptyStr#
- aspects: list[Aspect]#
- concepts: list[_Concept]#
- llm_role: LLMRoleAspect#
- reference_depth: ReferenceDepth#
- add_aspects(aspects)#
Adds aspects to the existing aspects list of an instance and returns the updated instance. This method ensures that the provided aspects are deeply copied to avoid any unintended state modification of the original reusable aspects.
- add_concepts(concepts)#
Adds a list of new concepts to the existing concepts attribute of the instance. This method ensures that the provided list of concepts is deep-copied to prevent unintended side effects from modifying the input list outside of this method.
- Parameters:
concepts (list[_Concept]) – A list of concepts to be added. It will be deep-copied before being added to the instance’s concepts attribute.
- Returns:
Returns the instance itself after the modification.
- Return type:
Self
- clone()#
Creates and returns a deep copy of the current instance.
- Return type:
typing.Self
- Returns:
A deep copy of the current instance.
- property extracted_items: list[_ExtractedItem]#
Provides access to extracted items.
- Returns:
A list containing the extracted items as _ExtractedItem objects.
- Return type:
list[_ExtractedItem]
- 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 to the JSON file to load (must end with ‘.json’).
- 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’.
OSError – If there’s an error reading the file.
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.
- get_aspect_by_name(name)#
Finds and returns an aspect with the specified name from the list of available aspects, if the instance has aspects attribute.
- Parameters:
name (str) – The name of the aspect to find.
- Returns:
The aspect with the specified name.
- Return type:
- Raises:
ValueError – If no aspect with the specified name is found.
- get_aspects_by_names(names)#
Retrieve a list of Aspect objects corresponding to the provided list of names.
- get_concept_by_name(name)#
Retrieves a concept from the list of concepts based on the provided name, if the instance has concepts attribute.
- Parameters:
name (str) – The name of the concept to search for.
- Returns:
The _Concept object with the specified name.
- Return type:
_Concept
- Raises:
ValueError – If no concept with the specified name is found.
- get_concepts_by_names(names)#
Retrieve a list of _Concept objects corresponding to the provided list of names.
- property reference_paragraphs: list[Paragraph]#
Provides access to the instance’s reference paragraphs, assigned during extraction.
- property reference_sentences: list[Sentence]#
Provides access to the instance’s reference sentences, assigned during extraction.
- remove_all_aspects()#
Removes all aspects from the instance and returns the updated instance.
This method clears the aspects attribute of the instance by resetting it to an empty list. It returns the same instance, allowing for method chaining.
- Return type:
typing.Self
- Returns:
The updated instance with all aspects removed
- remove_all_concepts()#
Removes all concepts from the instance and returns the updated instance.
This method clears the concepts attribute of the instance by resetting it to an empty list. It returns the same instance, allowing for method chaining.
- Return type:
typing.Self
- Returns:
The updated instance with all concepts removed
- remove_all_instances()#
Removes all assigned instances from the object and resets them as empty lists. Returns the modified instance.
- Returns:
The modified object with all assigned instances removed.
- Return type:
Self
- remove_aspect_by_name(name)#
Removes an aspect from the assigned aspects by its name.
- Parameters:
name (str) – The name of the aspect to be removed
- Returns:
Updated instance with the aspect removed.
- Return type:
Self
- remove_aspects_by_names(names)#
Removes multiple aspects from an object based on the provided list of names.
- remove_concept_by_name(name)#
Removes a concept from the assigned concepts by its name.
- Parameters:
name (str) – The name of the concept to be removed
- Returns:
Updated instance with the concept removed.
- Return type:
Self
- remove_concepts_by_names(names)#
Removes concepts from the object by their names.
- 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 where the JSON file should be saved (must end with ‘.json’).
- Return type:
- Returns:
None
- Raises:
ValueError – If the file path doesn’t end with ‘.json’.
IOError – 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:
- add_justifications: StrictBool#
- justification_depth: JustificationDepth#
- justification_max_sents: StrictInt#
- custom_data: dict#