Skip to content

declarai ¤

Main interface for declarai.

Decorates the package functionalities and serve as the main interface for the user.

Classes:

Name Description
Declarai

Context manager for the Declarai root interface.

Functions:

Name Description
azure_openai

Sets up a Declarai context for the Azure OpenAI provider.

magic

This is an empty method used as a potential replacement for using the docstring for passing

openai

Sets up a Declarai context for the OpenAI provider.

register_llm

Registers an LLM.

register_operator

Registers an operator.

Declarai ¤

Declarai(provider: str, model: str, **kwargs: str)

Context manager for the Declarai root interface. This class is responsible for setting up tasks and initializing experimental features provided by Declarai.

Parameters:

Name Type Description Default
provider str

The provider name.

required
model str

The model name.

required
**kwargs

Additional keyword arguments passed to the LLM resolver.

{}

Attributes:

Name Type Description
llm LLM

Resolved LLM.

task Callable

A decorator for task creation.

experimental

A namespace for experimental features.

experimental.chat Callable

A decorator for chat operators.

Source code in src/declarai/declarai.py
138
139
140
141
142
143
144
145
def __init__(self, provider: str, model: str, **kwargs):
    self.llm = resolve_llm(provider, model, **kwargs)
    self.task = TaskDecorator(self.llm).task

    class Experimental:
        chat = ChatDecorator(self.llm).chat

    self.experimental = Experimental

azure_openai ¤

azure_openai(
    deployment_name: str,
    azure_openai_key: str = None,
    azure_openai_api_base: str = None,
    api_version: str = None,
    headers: dict = None,
    timeout: int = None,
    stream: bool = None,
    request_timeout: int = None,
) -> Declarai

Sets up a Declarai context for the Azure OpenAI provider.

Parameters:

Name Type Description Default
deployment_name str

Name of the deployment.

required
azure_openai_key str

Azure OpenAI key.

None
azure_openai_api_base str

Base API URL for Azure OpenAI.

None
api_version str

API version.

None
headers dict

Additional headers for the request.

None
timeout int

Timeout for the request.

None
stream bool

Whether to stream the response.

None
request_timeout int

Request timeout duration.

None

Returns:

Name Type Description
DeclaraiContext Declarai

Initialized Declarai context.

Source code in src/declarai/declarai.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
def azure_openai(
    deployment_name: str,
    azure_openai_key: str = None,
    azure_openai_api_base: str = None,
    api_version: str = None,
    headers: dict = None,
    timeout: int = None,
    stream: bool = None,
    request_timeout: int = None,
) -> Declarai:
    """
    Sets up a Declarai context for the Azure OpenAI provider.

    Args:
        deployment_name (str): Name of the deployment.
        azure_openai_key (str, optional): Azure OpenAI key.
        azure_openai_api_base (str, optional): Base API URL for Azure OpenAI.
        api_version (str, optional): API version.
        headers (dict, optional): Additional headers for the request.
        timeout (int, optional): Timeout for the request.
        stream (bool, optional): Whether to stream the response.
        request_timeout (int, optional): Request timeout duration.

    Returns:
        DeclaraiContext: Initialized Declarai context.
    """
    return Declarai(
        provider=ProviderAzureOpenai,
        model=deployment_name,
        azure_openai_key=azure_openai_key,
        azure_openai_api_base=azure_openai_api_base,
        api_version=api_version,
        headers=headers,
        timeout=timeout,
        stream=stream,
        request_timeout=request_timeout,
    )

magic ¤

magic(
    return_name: Optional[str] = None,
    *,
    task_desc: Optional[str] = None,
    input_desc: Optional[Dict[str, str]] = None,
    output_desc: Optional[str] = None,
    **kwargs: Optional[str]
) -> Any

This is an empty method used as a potential replacement for using the docstring for passing parameters to the LLM builder. It can also serve as a fake use of arguments in the defined functions as to simplify handling of lint rules for llms functions.

Example
@openai.task
def add(a: int, b: int) -> int:
    return magic(a, b)
Source code in src/declarai/declarai.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def magic(
    return_name: Optional[str] = None,
    *,
    task_desc: Optional[str] = None,
    input_desc: Optional[Dict[str, str]] = None,
    output_desc: Optional[str] = None,
    **kwargs
) -> Any:
    """
    This is an empty method used as a potential replacement for using the docstring for passing
    parameters to the LLM builder. It can also serve as a fake use of arguments in the defined
    functions as to simplify handling of lint rules for llms functions.

    Example:
        ```py
        @openai.task
        def add(a: int, b: int) -> int:
            return magic(a, b)
        ```
    """
    pass

openai ¤

openai(
    model: ModelsOpenai,
    version: str = None,
    openai_token: str = None,
    headers: dict = None,
    timeout: int = None,
    stream: bool = None,
    request_timeout: int = None,
) -> Declarai

Sets up a Declarai context for the OpenAI provider.

Parameters:

Name Type Description Default
model ModelsOpenai

The model to be used.

required
version str

Model version.

None
openai_token str

OpenAI authentication token.

None
headers dict

Additional headers for the request.

None
timeout int

Timeout for the request.

None
stream bool

Whether to stream the response.

None
request_timeout int

Request timeout duration.

None

Returns:

Name Type Description
Declarai Declarai

Initialized Declarai context.

Source code in src/declarai/declarai.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def openai(
    model: ModelsOpenai,
    version: str = None,
    openai_token: str = None,
    headers: dict = None,
    timeout: int = None,
    stream: bool = None,
    request_timeout: int = None,
) -> Declarai:
    """
    Sets up a Declarai context for the OpenAI provider.

    Args:
        model (ModelsOpenai): The model to be used.
        version (str, optional): Model version.
        openai_token (str, optional): OpenAI authentication token.
        headers (dict, optional): Additional headers for the request.
        timeout (int, optional): Timeout for the request.
        stream (bool, optional): Whether to stream the response.
        request_timeout (int, optional): Request timeout duration.

    Returns:
        Declarai: Initialized Declarai context.
    """
    return Declarai(
        provider=ProviderOpenai,
        model=model,
        version=version,
        openai_token=openai_token,
        headers=headers,
        timeout=timeout,
        stream=stream,
        request_timeout=request_timeout,
    )

register_llm ¤

register_llm(
    provider: str, llm_cls: Type[LLM], model: str = None
)

Registers an LLM. Args: provider: Name of the LLM provider. model: Specific model name (optional). llm_cls: The LLM class to register.

Source code in src/declarai/declarai.py
223
224
225
226
227
228
229
230
231
def register_llm(provider: str, llm_cls: Type[LLM], model: str = None):
    """
    Registers an LLM.
    Args:
        provider: Name of the LLM provider.
        model: Specific model name (optional).
        llm_cls: The LLM class to register.
    """
    llm_registry.register(provider=provider, llm_cls=llm_cls, model=model)

register_operator ¤

register_operator(
    provider: str,
    operator_type: str,
    operator_cls: Type[BaseOperator],
    model: str = None,
)

Registers an operator. Args: provider: Name of the LLM provider. operator_type: The type of operator (e.g., "chat", "task"). operator_cls: The operator class to register. model: Specific model name

Source code in src/declarai/declarai.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def register_operator(
    provider: str,
    operator_type: str,
    operator_cls: Type[BaseOperator],
    model: str = None,
):
    """
    Registers an operator.
    Args:
        provider: Name of the LLM provider.
        operator_type: The type of operator (e.g., "chat", "task").
        operator_cls: The operator class to register.
        model: Specific model name
    """
    operator_registry.register(
        provider=provider,
        operator_type=operator_type,
        model=model,
        operator_cls=operator_cls,
    )