Skip to content

Agent History

Bases: BoostedMagic

Source code in pydantic_ai_kernel/magics/agent_history_magic.py
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class AgentHistoryMagic(BoostedMagic):

    @boosted_option(
        "-f",
        "--formatter",
        default=None,
        help="Formatter for the output. Must be supported by jupyter frontend. All formatter can be set by environment variable PYDANTIC_AI_KERNEL_FORMATTER",
        choices=out_formatter_list,
        completer=lambda w, r: complete_from_list(out_formatter_list, w, r),
    )
    def line_agent_history(self, formatter: str | None = None) -> None:
        """
        %agent_history : displays the agent history

        Examples :
        -------
            • `%agent_history --formatter md` : markdown display of agent history

        """
        # Use kernel's configured formatter if not overridden
        env_formatter = getattr(self.kernel, "formatter", "text")
        if formatter is None:
            formatter = env_formatter
        self.kernel: PydanticAIBaseKernel
        self.evaluate = False
        history: list[ModelMessage] = self.kernel.agent_history

        structured_messages = structured_message_display(history)

        if formatter == "json":
            self.kernel.Print(
                ModelMessagesTypeAdapter.dump_json(history, indent=4).decode("utf-8")
            )
            return

        for title, content in structured_messages:
            if formatter == "md":
                if self.kernel.use_widget:
                    self.kernel.Display(
                        widgets.Accordion(
                            children=[
                                widgets.HTML(
                                    value=content,
                                    placeholder="",
                                    description="",
                                )
                            ],
                            titles=[title],
                        )
                    )
                else:
                    self.kernel.Display({"text/markdown": f"### {title}  \n{content}"})
            elif formatter == "text":
                self.kernel.Print(f"  {title} :\n" + put_text_in_box(content, 4))
            elif formatter == "terminal":
                self.kernel.Print(
                    f"  \033[0;32m{title}\033[0m :\n" + put_text_in_box(content, 4)
                )

line_agent_history(formatter=None)

%agent_history : displays the agent history

Examples :

• `%agent_history --formatter md` : markdown display of agent history
Source code in pydantic_ai_kernel/magics/agent_history_magic.py
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@boosted_option(
    "-f",
    "--formatter",
    default=None,
    help="Formatter for the output. Must be supported by jupyter frontend. All formatter can be set by environment variable PYDANTIC_AI_KERNEL_FORMATTER",
    choices=out_formatter_list,
    completer=lambda w, r: complete_from_list(out_formatter_list, w, r),
)
def line_agent_history(self, formatter: str | None = None) -> None:
    """
    %agent_history : displays the agent history

    Examples :
    -------
        • `%agent_history --formatter md` : markdown display of agent history

    """
    # Use kernel's configured formatter if not overridden
    env_formatter = getattr(self.kernel, "formatter", "text")
    if formatter is None:
        formatter = env_formatter
    self.kernel: PydanticAIBaseKernel
    self.evaluate = False
    history: list[ModelMessage] = self.kernel.agent_history

    structured_messages = structured_message_display(history)

    if formatter == "json":
        self.kernel.Print(
            ModelMessagesTypeAdapter.dump_json(history, indent=4).decode("utf-8")
        )
        return

    for title, content in structured_messages:
        if formatter == "md":
            if self.kernel.use_widget:
                self.kernel.Display(
                    widgets.Accordion(
                        children=[
                            widgets.HTML(
                                value=content,
                                placeholder="",
                                description="",
                            )
                        ],
                        titles=[title],
                    )
                )
            else:
                self.kernel.Display({"text/markdown": f"### {title}  \n{content}"})
        elif formatter == "text":
            self.kernel.Print(f"  {title} :\n" + put_text_in_box(content, 4))
        elif formatter == "terminal":
            self.kernel.Print(
                f"  \033[0;32m{title}\033[0m :\n" + put_text_in_box(content, 4)
            )