DSPy: The framework for programming—not prompting—language models
Python
feat: add file output support to inspect_history and fix return type (#9321)
Blake Leddenc1083e2cfb15
diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.pyindex e8b893a09..d9dc9db47 100644--- a/dspy/clients/base_lm.py+++ b/dspy/clients/base_lm.py@@ -1,6 +1,6 @@import datetimeimport uuid-from typing import Any+from typing import Any, TextIOfrom dspy.dsp.utils import settingsfrom dspy.utils.callback import with_callbacks@@ -160,8 +160,8 @@ class BaseLM:return new_instance- def inspect_history(self, n: int = 1):- return pretty_print_history(self.history, n)+ def inspect_history(self, n: int = 1, file: "TextIO | None" = None) -> None:+ pretty_print_history(self.history, n, file=file)def update_history(self, entry):if settings.disable_history:@@ -283,6 +283,13 @@ class BaseLM:return [result]-def inspect_history(n: int = 1):- """The global history shared across all LMs."""- return pretty_print_history(GLOBAL_HISTORY, n)+def inspect_history(n: int = 1, file: "TextIO | None" = None) -> None:+ """The global history shared across all LMs.++ Args:+ n: Number of recent entries to display. Defaults to 1.+ file: An optional file-like object to write output to. When+ provided, ANSI color codes are automatically disabled.+ Defaults to `None` (prints to stdout).+ """+ pretty_print_history(GLOBAL_HISTORY, n, file=file)diff --git a/dspy/primitives/module.py b/dspy/primitives/module.pyindex e1a5f8d15..55db3c76d 100644--- a/dspy/primitives/module.py+++ b/dspy/primitives/module.py@@ -1,6 +1,6 @@import inspectimport logging-from typing import Any+from typing import Any, TextIOfrom dspy.dsp.utils.settings import settingsfrom dspy.predict.parallel import Parallel@@ -250,7 +250,7 @@ class Module(BaseModule, metaclass=ProgramMeta):set_attribute_by_name(self, name, func(predictor))return self- def inspect_history(self, n: int = 1):+ def inspect_history(self, n: int = 1, file: "TextIO | None" = None) -> None:"""Display the LM call history for this module.Prints a formatted view of the most recent language model calls@@ -260,11 +260,11 @@ class Module(BaseModule, metaclass=ProgramMeta):Args:n: The number of recent history entries to display.Defaults to 1.-- Returns:- The formatted history output.+ file: An optional file-like object to write output to. When+ provided, ANSI color codes are automatically disabled.+ Defaults to `None` (prints to stdout)."""- return pretty_print_history(self.history, n)+ pretty_print_history(self.history, n, file=file)def batch(self,diff --git a/dspy/utils/inspect_history.py b/dspy/utils/inspect_history.pyindex 07934157f..46aebad1c 100644--- a/dspy/utils/inspect_history.py+++ b/dspy/utils/inspect_history.py@@ -1,35 +1,57 @@-def _green(text: str, end: str = "\n"):- return "\x1b[32m" + str(text).lstrip() + "\x1b[0m" + end+from __future__ import annotations+import sys+from typing import Any, TextIO-def _red(text: str, end: str = "\n"):- return "\x1b[31m" + str(text) + "\x1b[0m" + end+def _green(text: str, end: str = "\n", *, use_colors: bool = True) -> str:+ if use_colors:+ return "\x1b[32m" + str(text).lstrip() + "\x1b[0m" + end+ return str(text).lstrip() + end-def _blue(text: str, end: str = "\n"):- return "\x1b[34m" + str(text) + "\x1b[0m" + end+def _red(text: str, end: str = "\n", *, use_colors: bool = True) -> str:+ if use_colors:+ return "\x1b[31m" + str(text) + "\x1b[0m" + end+ return str(text) + end-def pretty_print_history(history, n: int = 1):- """Prints the last n prompts and their completions."""++def _blue(text: str, end: str = "\n", *, use_colors: bool = True) -> str:+ if use_colors:+ return "\x1b[34m" + str(text) + "\x1b[0m" + end+ return str(text) + end+++def pretty_print_history(history: list[dict[str, Any]], n: int = 1, file: TextIO | None = None) -> None:+ """Print the last n prompts and their completions.++ Args:+ history: The history list to print from.+ n: Number of recent entries to display. Defaults to 1.+ file: An optional file-like object to write output to (must have a+ `.write()` method). When provided, ANSI color codes are+ automatically disabled. Defaults to `None` (prints to stdout).+ """+ out = file or sys.stdout+ use_colors = file is Nonefor item in history[-n:]:messages = item["messages"] or [{"role": "user", "content": item["prompt"]}]outputs = item["outputs"]timestamp = item.get("timestamp", "Unknown time")- print("\n\n\n")- print("\x1b[34m" + f"[{timestamp}]" + "\x1b[0m" + "\n")+ print("\n\n\n", file=out)+ print(_blue(f"[{timestamp}]", use_colors=use_colors), file=out)for msg in messages:- print(_red(f"{msg['role'].capitalize()} message:"))+ print(_red(f"{msg['role'].capitalize()} message:", use_colors=use_colors), file=out)if isinstance(msg["content"], str):- print(msg["content"].strip())+ print(msg["content"].strip(), file=out)else:if isinstance(msg["content"], list):for c in msg["content"]:if c["type"] == "text":- print(c["text"].strip())+ print(c["text"].strip(), file=out)elif c["type"] == "image_url":image_str = ""if "base64" in c["image_url"].get("url", ""):@@ -40,36 +62,36 @@ def pretty_print_history(history, n: int = 1):)else:image_str = f"<image_url: {c['image_url']['url']}>"- print(_blue(image_str.strip()))+ print(_blue(image_str.strip(), use_colors=use_colors), file=out)elif c["type"] == "input_audio":audio_format = c["input_audio"]["format"]len_audio = len(c["input_audio"]["data"])audio_str = f"<audio format='{audio_format}' base64-encoded, length={len_audio}>"- print(_blue(audio_str.strip()))+ print(_blue(audio_str.strip(), use_colors=use_colors), file=out)elif c["type"] == "file" or c["type"] == "input_file":- file = c.get("file", c.get("input_file", {}))- filename = file.get("filename", "")- file_id = file.get("file_id", "")- file_data = file.get("file_data", "")+ file_info = c.get("file", c.get("input_file", {}))+ filename = file_info.get("filename", "")+ file_id = file_info.get("file_id", "")+ file_data = file_info.get("file_data", "")file_str = f"<file: name:{filename}, id:{file_id}, data_length:{len(file_data)}>"- print(_blue(file_str.strip()))- print("\n")+ print(_blue(file_str.strip(), use_colors=use_colors), file=out)+ print("\n", file=out)if isinstance(outputs[0], dict):if outputs[0]["text"]:- print(_red("Response:"))- print(_green(outputs[0]["text"].strip()))+ print(_red("Response:", use_colors=use_colors), file=out)+ print(_green(outputs[0]["text"].strip(), use_colors=use_colors), file=out)if outputs[0].get("tool_calls"):- print(_red("Tool calls:"))+ print(_red("Tool calls:", use_colors=use_colors), file=out)for tool_call in outputs[0]["tool_calls"]:- print(_green(f"{tool_call['function']['name']}: {tool_call['function']['arguments']}"))+ print(_green(f"{tool_call['function']['name']}: {tool_call['function']['arguments']}", use_colors=use_colors), file=out)else:- print(_red("Response:"))- print(_green(outputs[0].strip()))+ print(_red("Response:", use_colors=use_colors), file=out)+ print(_green(outputs[0].strip(), use_colors=use_colors), file=out)if len(outputs) > 1:choices_text = f" \t (and {len(outputs) - 1} other completions)"- print(_red(choices_text, end=""))+ print(_red(choices_text, end="", use_colors=use_colors), file=out)- print("\n\n\n")+ print("\n\n\n", file=out)