component#

Base building block for building LLM task pipelines. It handles states recursively, such as training, components, parameters recursively along with serialization and deserialization.

Functions

fun_to_component(fun)

Helper function to convert a function into a Component with its own class name.

Classes

Component(*args, **kwargs)

Base class for all LLM task pipeline components.

FunComponent([fun, afun])

Component that wraps a function.

class Component(*args, **kwargs)[source]#

Bases: object

Base class for all LLM task pipeline components.

Such as Prompt, ModelClient, Embedder, Retriever, Generator, etc. Your task pipeline should subclass this.

Components can also contain other Components, allowing to nest them in a tree structure. You can assign the subcomponents as regular attributes:

Component supports three modes: - Training mode: train() When turned on, any component __call__ will use forward and backward for in-context training.

When turned off, the __call__ should only use call and acall for inference.

  • Tracing mode: trace() When turned on, the component will accumulate input, output, and backpropagate the eval score to Parameter of Demo type.

  • Teacher mode: use_teacher() When turned on, the component will accumulates the demos to the _trace`s, otherwise, it will be saved in `_student_traces.

Example:

from adalflow.core import Component, Generator
from adalflow.components.model_client import OpenAIClient

template_doc = r"<SYS> You are a doctor </SYS> User: {{input_str}}"

class DocQA(Component):
    def __init__(self):
        super(DocQA, self).__init__()
        self.doc = Generator(
            template=template_doc,
            model_client=OpenAIClient(),
            model_kwargs={"model": "gpt-3.5-turbo"},
        )

    def call(self, query: str) -> str:
        return self.doc(query).data

# instantiate the component
doc_qa = DocQA()
print(doc_qa)

The print will be:

DocQA(
(doc): Generator(
    model_kwargs={'model': 'gpt-3.5-turbo'}, model_type=ModelType.LLM
    (system_prompt): Prompt(template: <SYS> You are a doctor </SYS> User: {{input_str}}, prompt_variables: ['input_str'])
    (model_client): OpenAIClient()
)
)

We follow the same design pattern as PyTorch’s nn.Module. Instead of working with Tensor and Parameter to train models with weights and biases, our component works with any data, Parameter that can be any data type for LLM in-context learning, from manual to auto prompt engineering. Besides, (1) instead of forward and backward functions, we have call and acall functions for sync and async calls. (2) we provide to_dict to handle serialization of the whole component states on top of state_dict.

We purposely avoid using the name “Module” to avoid confusion with PyTorch’s nn.Module. As we consider ‘Component’ to be an extension to ‘Module’ as if you use a local llm model for the Generator, you might need the ‘Module’ within the ‘Component’.

training: bool#
teacher_mode: bool = False#
tracing: bool = False#
name: str = 'Component'#
use_teacher(mode: bool = True)[source]#

Sets the component in teacher mode.

trace(mode: bool = True)[source]#

Sets the component in tracing mode.This signal will be used in forward and backward to accumulate input and output.

train(mode: bool = True)[source]#

Sets the component in training mode.

eval()[source]#

Sets the component in evaluation mode.

is_picklable() bool[source]#

Test if the given object is picklable.

Parameters:

obj – The object to test.

Returns:

True if the object is picklable, False otherwise.

Return type:

bool

pickle_to_file(filepath: str) None[source]#

Pickle the component to a file.

classmethod load_from_pickle(filepath: str) None[source]#

Load the component from a file.

to_dict(exclude: List[str] | None = None) Dict[str, Any][source]#

Converts the component to a dictionary object for serialization, including more states of the component than state_dict.

Each data if of format: {“type”: type, “data”: data}

classmethod from_dict(data: Dict[str, Any]) T[source]#

Create an instance from previously serialized data using to_dict() method.

classmethod from_config(config: Dict[str, Any]) T[source]#

Create an instance of the component from a configuration dictionary.

classmethod load_state_pickle(filepath: str) Component[source]#

Load the state of the component from a file using pickle.

register_parameter(name: str, param: Parameter | None = None) None[source]#
parameters(recursive: bool = True) Iterable[Parameter][source]#

Returns an iterator over module parameters.

Parameters:

recursive (bool) – if True, then yields parameters of this component and all subcomponents. Otherwise, yields only parameters that are direct members of this component.

Yields:

Parameter – module parameter

Examples

>>> for param in model.parameters():
>>>     print(param)
named_parameters(prefix: str = '', recursive: bool = True, remove_duplicate: bool = True) Iterable[Tuple[str, Parameter]][source]#

Returns an iterator over componenet parameters, yielding both the name of the parameter as well as the parameter itself.

Parameters:
  • prefix (str) – prefix to prepend to all parameter names.

  • recursive (bool) – if True, then yields parameters of this component and all subcomponents. Otherwise, yields only parameters that are direct members of this component. are direct members of this component.

  • remove_duplicate (bool) – if True, then yields only unique parameters.

Yields:

Tuple[str, Parameter] – Tuple containing the name and parameter

Examples

>>> for name, param in model.named_parameters():
>>>     print(name, param)
call(*args, **kwargs)[source]#
async acall(*args, **kwargs)[source]#

API call, file io.

add_component(name: str, component: Component | None) None[source]#

Add a child component to the current component.

register_component(name: str, component: Component | None) None[source]#

Alias for add_component

get_subcomponent(name: str) Component | None[source]#
named_children(grad_component_only: bool = False) Iterable[Tuple[str, Component]][source]#

Returns an iterator over immediate children components.

children(grad_component_only: bool = False) Iterable[Component][source]#

Returns an iterator over immediate children components.

components(grad_component_only: bool = False) Iterable[Component][source]#

Returns an iterator over all components in the Module.

named_components(memo: Set[Component] | None = None, prefix: str = '', remove_duplicate: bool = True, grad_component_only: bool = False)[source]#

Return an iterator over all components in the pipeline, yielding both the name of the component as well as the component itself as a tuple.

This can be used to represent the state of the component in a dictionary format. :param memo: a memo to store the set of components already added to the result :type memo: Optional[Set[“Component”]] :param prefix: a prefix to prepend to all component names :type prefix: str :param remove_duplicate: if True, then yields only unique components :type remove_duplicate: bool

Yields:

Tuple[str, “Component”] – Tuple containing the name and component

Example

>>> qa = Generator(template="User {{input}}", model_client=GroqAPIClient(), model_kwargs={"model": "llama3-8b-8192"})
>>> for idx, c in enumerate(qa.named_components()):
...     print(f"{idx} -> {c}")

0 -> (‘’, Generator( model_kwargs={‘model’: ‘llama3-8b-8192’}, model_type=ModelType.LLM (system_prompt): Prompt(template: User: {{input}}, prompt_variables: [‘input’]) (model_client): GroqAPIClient() )) 1 -> (‘system_prompt’, Prompt(template: User: {{input}}, prompt_variables: [‘input’])) 2 -> (‘model_client’, GroqAPIClient())

state_dict(destination: Dict[str, Any] | None = None, prefix: str | None = '') Dict[str, Any][source]#

Returns a dictionary containing references to the whole state of the component.

Parameters are included for now.

..note:

The returned object is a shallow copy. It cantains references to the component’s parameters and subcomponents.

Parameters:
  • destination (Dict[str, Any]) – If provided, the state of component will be copied into it.

  • returned. (an OrderedDict will be created and)

  • Othersie

  • returned.

  • prefix (str) – a prefix to add to the keys in the state_dict.

Returns:

a dictionary containing the state of the component.

Return type:

Dict[str, Any]

load_state_dict(state_dict: Mapping[str, Any], strict: bool = True)[source]#

Copy parameters from state_dict into this component and its descendants.

If strict is True, then the keys of state_dict must exactly match the keys returned by this component’s state_dict() function.

class FunComponent(fun: Callable | None = None, afun: Callable | None = None)[source]#

Bases: Component

Component that wraps a function.

Parameters:

fun (Callable) – The function to be wrapped.

Examples:

function = lambda x: x + 1 fun_component = FunComponent(function) print(fun_component(1)) # 2

call(*args, **kwargs)[source]#
fun_to_component(fun) FunComponent[source]#

Helper function to convert a function into a Component with its own class name.

Can be used as both a decorator and a function.

Parameters:

fun (Callable) – The function to be wrapped.

Returns:

The component that wraps the function.

Return type:

FunComponent

Examples: 1. As a decorator:

>>> @fun_to_component
>>> def my_function(x):
>>>     return x + 1
>>> # is equivalent to
>>> class MyFunctionComponent(FunComponent):
>>>     def __init__(self):
>>>         super().__init__(my_function)
  1. As a function:
    >>> my_function_component = fun_to_component(my_function)