decorators#

Functions

trace_generator_call([attributes, save_dir, ...])

Decorator to trace generator predictions in a task component, especially failed ones.

trace_generator_states([attributes, ...])

Decorator to trace generators in a task component.

trace_generator_states(attributes: List[str] | None = None, save_dir: str | None = './traces/', project_name: str | None = None, filename: str | None = None)[source]#

Decorator to trace generators in a task component.

It dynamically attaches a GeneratorLogger to the target generator attribute and logs the prompt states of the generator. You can use it on any component that has attributes pointing to a generator object.

Parameters:
  • attributes (List[str], Optional) – The list of attributes that point to the generator objects. If not provided, it will automatically detect the attributes that are instances of Generator.

  • filepath (str, Optional) – The path to the directory where the trace file will be saved. Default is “./traces/”.

  • filename (str, Optional) – The name of the trace file. If not provided, it will be “{class_name}_generator_trace.json”.

Examples:

from adalflow.tracing import trace_generator_states

# Define a class and apply the decorator
@trace_generator_states()
class TestGenerator:
    def __init__(self):
        super().__init__()
        prompt_kwargs = {"input_str": "world"}
        self.generator = Generator(
            model_client=OpenAIClient(),
            prompt_kwargs=prompt_kwargs,
            model_kwargs={"model": "gpt-3.5-turbo"},
        )
# now you will see log files in the ./traces/ with a filename like TestGenerator_generator_trace.json
# If you update the prompt templates or the prompt_kwargs, it will be logged in the file.
trace_generator_call(attributes: List[str] | None = None, save_dir: str | None = './traces/', error_only: bool = True)[source]#

Decorator to trace generator predictions in a task component, especially failed ones.

This decorator is a wrapper around the generator call method. It logs the generator call by reading its GeneratorOutput and logs the call if the output is an error.

Parameters:
  • attributes (List[str]) – The list of attributes that point to the generator objects.

  • save_dir (str) – The path to the directory where the trace file will be saved.

  • error_only (bool) – If True, only log the calls that have an error. Default is True.

Examples:

from adalflow.tracing import trace_generator_call
@trace_generator_call()
class TestGenerator:
    def __init__(self):
        super().__init__()
        prompt_kwargs = {"input_str": "world"}
        self.generator = Generator(
            model_client=OpenAIClient(),
            prompt_kwargs=prompt_kwargs,
            model_kwargs={"model": "gpt-3.5-turbo"},
        )
# now you will see log files in the ./traces/ with a filename like TestGenerator_generator_call.jsonl
# If the generator call has an error, it will be logged in the file.

If you want to decorate a component(such as LLMRetriever) from the library where you do not have access to the source code, you can do it like this:

from adalflow.components.retriever import LLMRetriever

# Define a subclass and apply the decorator
@trace_generator_call(save_dir=...)
class LoggedLLMRetriever(LLMRetriever):
    pass
retriever = LoggedLLMRetriever(...)

You can access the logger via TestGenerator.generator_call_logger if you want to access call records in the code.