fabricatio_core.models.generic

This module defines generic classes for models in the Fabricatio library, providing a foundation for various model functionalities.

Classes

Base

Base class for all models with Pydantic configuration.

Display

Class that provides formatted JSON representation utilities.

Named

Class that includes a name attribute.

Described

Class that includes a description attribute.

Titled

Class that includes a title attribute.

WithBriefing

Class that combines naming and description attributes with briefing generation.

WithDependency

Class that manages file dependencies.

Vectorizable

Class that prepares the vectorization of the model.

ScopedConfig

Configuration holder with hierarchical fallback mechanism.

EmbeddingScopedConfig

Configuration for embedding-related settings.

RerankerScopedConfig

Configuration for reranker-related settings.

LLMScopedConfig

Configuration for LLM-related settings.

UnsortGenerate

Class that provides a reverse JSON schema of the model.

WithFormatedJsonSchema

Class that provides a formatted JSON schema of the model.

CreateJsonObjPrompt

Class that provides a prompt for creating a JSON object.

InstantiateFromString

Class that provides a method to instantiate the class from a string.

ProposedAble

Class that provides a method to propose a JSON object based on the requirement.

Language

Class that provides a language attribute.

SketchedAble

Class that provides a method to scratch the object.

Module Contents

class fabricatio_core.models.generic.Base(/, **data: Any)[source]

Bases: pydantic.BaseModel, abc.ABC

Base class for all models with Pydantic configuration.

This class sets up the basic Pydantic configuration for all models in the Fabricatio library. The model_config uses use_attribute_docstrings=True to ensure field descriptions are pulled from the attribute’s docstring instead of the default Pydantic behavior.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fabricatio_core.models.generic.Display(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that provides formatted JSON representation utilities.

Provides methods to generate both pretty-printed and compact JSON representations of the model. Used for debugging and logging purposes.

display() str[source]

Generate pretty-printed JSON representation.

Returns:

JSON string with 1-level indentation for readability

Return type:

str

compact() str[source]

Generate compact JSON representation.

Returns:

Minified JSON string without whitespace

Return type:

str

static seq_display(seq: Iterable[Display], compact: bool = False) str[source]

Generate formatted display for sequence of Display objects.

Parameters:
  • seq (Iterable[Display]) – Sequence of objects to display

  • compact (bool) – Use compact format instead of pretty print

Returns:

Combined display output with boundary markers

Return type:

str

class fabricatio_core.models.generic.Named(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that includes a name attribute.

This class adds a name attribute to models, which is intended to be a unique identifier.

name: str

The name of this object,briefly and conclusively.

class fabricatio_core.models.generic.Described(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that includes a description attribute.

This class adds a description attribute to models, providing additional context or information.

description: str

A comprehensive description of this object, including its purpose, scope, and context. This should clearly explain what this object is about, why it exists, and in what situations it applies. The description should be detailed enough to provide full understanding of this object’s intent and application.

class fabricatio_core.models.generic.Titled(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that includes a title attribute.

title: str

The title of this object, make it professional and concise.No prefixed heading number should be included.

class fabricatio_core.models.generic.WithBriefing(/, **data: Any)[source]

Bases: Named, Described, abc.ABC

Class that combines naming and description attributes with briefing generation.

This class inherits from both Named and Described classes to provide a combined interface that includes both name and description attributes. It also provides automatic briefing generation by combining these two attributes.

property briefing: str

Get the briefing of the object.

Returns:

The briefing of the object.

Return type:

str

__eq__(other: object) bool[source]

Check if two roles are equal.

__hash__() int[source]

Get the hash value of the role.

class fabricatio_core.models.generic.WithDependency(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that manages file dependencies.

This class includes methods to manage file dependencies required for reading or writing.

dependencies: List[str] = None

The file dependencies which is needed to read or write to meet a specific requirement, a list of file paths.

add_dependency[P: str | pathlib.Path](dependency: P | List[P]) Self[source]

Add a file dependency to the task.

Parameters:

dependency (str | Path | List[str | Path]) – The file dependency to add to the task.

Returns:

The current instance of the task.

Return type:

Self

remove_dependency[P: str | pathlib.Path](dependency: P | List[P]) Self[source]

Remove a file dependency from the task.

Parameters:

dependency (str | Path | List[str | Path]) – The file dependency to remove from the task.

Returns:

The current instance of the task.

Return type:

Self

clear_dependencies() Self[source]

Clear all file dependencies from the task.

Returns:

The current instance of the task.

Return type:

Self

override_dependencies[P: str | pathlib.Path](dependencies: List[P] | P) Self[source]

Override the file dependencies of the task.

Parameters:

dependencies (List[str | Path] | str | Path) – The file dependencies to override the task’s dependencies.

Returns:

The current instance of the task.

Return type:

Self

read_dependency[T](idx: int = -1, reader: Callable[[str], T] = lambda p: ...) T[source]

Read the content of a file dependency.

Parameters:
  • idx (int) – Index of the dependency to read. Defaults to -1 (last dependency).

  • reader (Callable[[str], T]) – Function to use for reading the file.

Returns:

The content of the file read using the provided reader function.

Return type:

T

property dependencies_prompt: str

Generate a prompt for the task based on the file dependencies.

Returns:

The generated prompt for the task.

Return type:

str

class fabricatio_core.models.generic.Vectorizable[source]

Bases: abc.ABC

Class that prepares the vectorization of the model.

This class includes methods to prepare the model for vectorization, ensuring it fits within a specified token length.

prepare_vectorization() str[source]

Prepare the vectorization of the model.

Returns:

The prepared vectorization of the model.

Return type:

str

Raises:

ValueError – If the chunk exceeds the maximum sequence length.

class fabricatio_core.models.generic.ScopedConfig(/, **data: Any)[source]

Bases: Base, abc.ABC

Configuration holder with hierarchical fallback mechanism.

fallback_to(other: ScopedConfig | Any, exclude: Set[str] | None = None) Self[source]

Merge configuration values with fallback priority.

Copies non-null values from ‘other’ to self where current values are None.

Parameters:
  • other (ScopedConfig) – Configuration to fallback to

  • exclude (Optional[Set[str]]) – Field names to exclude from fallback

Returns:

Current instance with merged values

Return type:

Self

hold_to(others: ScopedConfig | Any | Iterable[ScopedConfig | Any], exclude: Set[str] | None = None) Self[source]

Propagate non-null values to other configurations.

Copies current non-null values to target configurations where they are None.

Parameters:
  • others (ScopedConfig|Iterable) – Target configurations to update

  • exclude (Optional[Set[str]]) – Field names to exclude from propagation

Returns:

Current instance unchanged

Return type:

Self

class fabricatio_core.models.generic.EmbeddingScopedConfig(/, **data: Any)[source]

Bases: ScopedConfig

Configuration for embedding-related settings.

embedding_send_to: str | None = None

The LLM model name.

embedding_no_cache: bool = False

Whether to disable caching for embeddings.

embedding_ndim: int | None = None

The dimensionality of the output embeddings. Must match between search and store.

class fabricatio_core.models.generic.RerankerScopedConfig(/, **data: Any)[source]

Bases: ScopedConfig

Configuration for reranker-related settings.

reranker_send_to: str | None = None

The group name of which the requests will be sent.

reranker_no_cache: bool | None = None

Whether to disable caching for the reranker.

class fabricatio_core.models.generic.LLMScopedConfig(/, **data: Any)[source]

Bases: ScopedConfig

Configuration for LLM-related settings.

llm_send_to: str | None = None

The group name of which the requests will be sent.

llm_top_p: pydantic.NonNegativeFloat | None = None

The top p of the LLM model.

llm_temperature: pydantic.NonNegativeFloat | None = None

The temperature of the LLM model.

llm_stream: bool | None = None

Whether to stream the LLM model’s response.

llm_max_completion_tokens: pydantic.PositiveInt | None = None

The maximum number of tokens to generate.

llm_presence_penalty: pydantic.PositiveFloat | None = None

The presence penalty of the LLM model.

llm_frequency_penalty: pydantic.PositiveFloat | None = None

The frequency penalty of the LLM model.

llm_no_cache: bool | None = None

Whether to disable caching for the LLM model.

class fabricatio_core.models.generic.UnsortGenerate(by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE, union_format: Literal['any_of', 'primitive_type_array'] = 'any_of')[source]

Bases: pydantic.json_schema.GenerateJsonSchema

Class that provides a reverse JSON schema of the model.

This class overrides the sorting behavior of the JSON schema generation to maintain the original order.

sort(value: pydantic.json_schema.JsonSchemaValue, parent_key: str | None = None) pydantic.json_schema.JsonSchemaValue[source]

Not sort.

Parameters:
  • value (JsonSchemaValue) – The JSON schema value to sort.

  • parent_key (str | None) – The parent key of the JSON schema value.

Returns:

The JSON schema value without sorting.

Return type:

JsonSchemaValue

class fabricatio_core.models.generic.WithFormatedJsonSchema(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that provides a formatted JSON schema of the model.

This class includes a method to generate a formatted JSON schema of the model.

classmethod formated_json_schema() str[source]

Get the JSON schema of the model in a formatted string.

Returns:

The JSON schema of the model in a formatted string.

Return type:

str

class fabricatio_core.models.generic.CreateJsonObjPrompt(/, **data: Any)[source]

Bases: WithFormatedJsonSchema, abc.ABC

Class that provides a prompt for creating a JSON object.

This class includes a method to create a prompt for creating a JSON object based on the model’s schema and a requirement.

classmethod create_json_prompt(requirement: List[str]) List[str][source]
classmethod create_json_prompt(requirement: str) str

Create the prompt for creating a JSON object with given requirement.

Parameters:

requirement (str | List[str]) – The requirement for the JSON object.

Returns:

The prompt for creating a JSON object with given requirement.

Return type:

str | List[str]

class fabricatio_core.models.generic.InstantiateFromString(/, **data: Any)[source]

Bases: Base, abc.ABC

Class that provides a method to instantiate the class from a string.

This class includes a method to instantiate the class from a JSON string representation.

classmethod instantiate_from_string(string: str) Self | None[source]

Instantiate the class from a string.

Parameters:

string (str) – The string to instantiate the class from.

Returns:

The instance of the class or None if the string is not valid.

Return type:

Self | None

class fabricatio_core.models.generic.ProposedAble(/, **data: Any)[source]

Bases: CreateJsonObjPrompt, InstantiateFromString, abc.ABC

Class that provides a method to propose a JSON object based on the requirement.

This class combines the functionality to create a prompt for a JSON object and instantiate it from a string.

class fabricatio_core.models.generic.Language[source]

Class that provides a language attribute.

property language: str[source]

Get the language of the object.

class fabricatio_core.models.generic.SketchedAble(/, **data: Any)[source]

Bases: ProposedAble, Display, abc.ABC

Class that provides a method to scratch the object.

This class combines the functionality to propose a JSON object, instantiate it from a string, and display it.