Skip to content

registry ¤

Registry for LLMs and Operators.

Classes:

Name Description
LLMRegistry

Registry for LLMs.

OperatorRegistry

Registry for Operators.

Functions:

Name Description
register_llm

A decorator that registers an LLM class to the LLM registry.

register_operator

A decorator that registers an operator class to the operator registry.

Attributes:

Name Type Description
llm_registry

The global LLM registry.

operator_registry

The global Operator registry.

llm_registry module-attribute ¤

llm_registry = LLMRegistry()

The global LLM registry.

operator_registry module-attribute ¤

operator_registry = OperatorRegistry()

The global Operator registry.

LLMRegistry ¤

LLMRegistry()

Registry for LLMs. The registry will have a nested structure: {provider: {model: llm_cls}} But it will also support a direct provider-to-llm_cls mapping for generic LLMs. But it will also support a direct provider-to-llm_cls mapping for generic LLMs.

Methods:

Name Description
register

Registers an LLM.

resolve

Resolves an LLM. If the model is not specified, the default model of the given provider will be used.

Source code in src/declarai/operators/registry.py
19
20
def __init__(self):
    self._registry = defaultdict(dict)

register ¤

register(
    provider: str,
    llm_cls: Type[LLM],
    model: Optional[str] = "default",
)

Registers an LLM. Args: provider: the name of the LLM provider. llm_cls: the LLM class to register. model: the specific model name.

Returns:

Type Description

None

Source code in src/declarai/operators/registry.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def register(
    self, provider: str, llm_cls: Type[LLM], model: Optional[str] = "default"
):
    """
    Registers an LLM.
    Args:
        provider: the name of the LLM provider.
        llm_cls: the LLM class to register.
        model: the specific model name.

    Returns:
        None

    """
    self._registry[provider][model] = llm_cls

resolve ¤

resolve(
    provider: str,
    model: Optional[str] = None,
    **kwargs: Optional[str]
) -> LLM

Resolves an LLM. If the model is not specified, the default model of the given provider will be used. Args: provider: the name of the LLM provider. model: the specific model name. **kwargs: Additional keyword arguments to pass to the LLM initialization.

Returns:

Type Description
LLM

An LLM instance.

Source code in src/declarai/operators/registry.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def resolve(self, provider: str, model: Optional[str] = None, **kwargs) -> LLM:
    """
    Resolves an LLM. If the model is not specified, the default model of the given provider will be used.
    Args:
        provider: the name of the LLM provider.
        model: the specific model name.
        **kwargs: Additional keyword arguments to pass to the LLM initialization.

    Returns:
        An LLM instance.
    """
    if not model:
        model = "default"
    provider_registry = self._registry.get(provider, {})

    llm_cls = provider_registry.get(model)
    if not llm_cls:
        # If the specific model isn't found, fall back to the default.
        llm_cls = provider_registry.get("default")

    if not llm_cls:
        raise NotImplementedError(
            f"LLMProvider : {provider} or model: {model} not implemented"
        )
    try:
        return llm_cls(model=model, **kwargs)
    except TypeError:
        return llm_cls(**kwargs)

OperatorRegistry ¤

OperatorRegistry()

Registry for Operators. The registry will have a nested structure: {provider: {operator_type: {model: operator_cls}}} It will support a direct provider-to-operator_cls mapping for generic Operators.

Methods:

Name Description
register

Registers an operator.

resolve

Resolves an operator.

Source code in src/declarai/operators/registry.py
88
89
def __init__(self):
    self._registry = defaultdict(lambda: defaultdict(dict))

register ¤

register(
    provider: str,
    operator_type: str,
    operator_cls: Type[BaseOperator],
    model: str = "default",
)

Registers an operator.

Parameters:

Name Type Description Default
provider str

the name of the operator provider.

required
operator_type str

the type of the operator.

required
operator_cls Type[BaseOperator]

the operator class to register.

required
model str

the specific model name that the operator is registered to.

'default'

Returns:

Type Description

None

Source code in src/declarai/operators/registry.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def register(
    self,
    provider: str,
    operator_type: str,
    operator_cls: Type[BaseOperator],
    model: str = "default",
):
    """
    Registers an operator.

    Args:
        provider: the name of the operator provider.
        operator_type: the type of the operator.
        operator_cls: the operator class to register.
        model: the specific model name that the operator is registered to.

    Returns:
        None

    """
    self._registry[provider][operator_type][model] = operator_cls

resolve ¤

resolve(
    llm_instance: LLM, operator_type: str
) -> Type[BaseOperator]

Resolves an operator.

Parameters:

Name Type Description Default
llm_instance LLM

An LLM instance.

required
operator_type str

A string representing the type of the operator.

required

Returns:

Type Description
Type[BaseOperator]

An operator class.

Source code in src/declarai/operators/registry.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def resolve(self, llm_instance: LLM, operator_type: str) -> Type[BaseOperator]:
    """
    Resolves an operator.

    Args:
        llm_instance: An LLM instance.
        operator_type: A string representing the type of the operator.

    Returns:
        An operator class.

    """
    default_model = "default"
    provider = llm_instance.provider
    operator_registry = self._registry.get(provider, {})

    specific_operator_registry = operator_registry.get(operator_type, {})
    operator_cls = specific_operator_registry.get(llm_instance.model)

    if not operator_cls:
        # If the specific model isn't found, fall back to the default.
        operator_cls = specific_operator_registry.get(default_model)

    if not operator_cls:
        raise NotImplementedError(
            f"Operator type : {operator_type} for provider: {provider} or model: {llm_instance.model} not implemented"
        )

    return operator_cls

register_llm ¤

register_llm(
    provider: str, model: Optional[str] = "default"
)

A decorator that registers an LLM class to the LLM registry.

Parameters:

Name Type Description Default
provider str

the name of the LLM provider.

required
model Optional[str]

the specific model name.

'default'

Returns:

Type Description

A decorator that registers the decorated LLM class to the LLM registry.

Source code in src/declarai/operators/registry.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def register_llm(provider: str, model: Optional[str] = "default"):
    """
    A decorator that registers an LLM class to the LLM registry.

    Args:
        provider: the name of the LLM provider.
        model: the specific model name.

    Returns:
        A decorator that registers the decorated LLM class to the LLM registry.
    """

    def decorator(cls):
        llm_registry.register(provider, cls, model)
        return cls

    return decorator

register_operator ¤

register_operator(
    provider: str,
    operator_type: str,
    model: Optional[str] = "default",
)

A decorator that registers an operator class to the operator registry.

Parameters:

Name Type Description Default
provider str

the name of the operator provider.

required
operator_type str

the string representing the type of the operator.

required
model Optional[str]

the specific model name.

'default'

Returns:

Type Description

A decorator that registers the decorated operator class to the operator registry.

Source code in src/declarai/operators/registry.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def register_operator(
    provider: str, operator_type: str, model: Optional[str] = "default"
):
    """
    A decorator that registers an operator class to the operator registry.

    Args:
        provider: the name of the operator provider.
        operator_type: the string representing the type of the operator.
        model: the specific model name.

    Returns:
        A decorator that registers the decorated operator class to the operator registry.

    """

    def decorator(cls):
        operator_registry.register(provider, operator_type, cls, model)
        return cls

    return decorator