Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Pretty print stuff (#402)

Louis Choquel138a0621a205
diff --git a/pipelex/builder/concept/concept_spec.py b/pipelex/builder/concept/concept_spec.py
index 4704ec5b2..98dfee5bd 100644
--- a/pipelex/builder/concept/concept_spec.py
+++ b/pipelex/builder/concept/concept_spec.py
@@ -3,8 +3,9 @@ from datetime import datetime
from typing import Any
from pydantic import ConfigDict, Field, field_validator, model_validator
+from typing_extensions import override
-from pipelex import log
+from pipelex import log, pretty_print
from pipelex.core.concepts.concept_blueprint import (
ConceptBlueprint,
ConceptBlueprintError,
@@ -16,6 +17,7 @@ from pipelex.core.concepts.concept_native import NativeConceptCode
from pipelex.core.concepts.exceptions import ConceptCodeError, ConceptStringOrConceptCodeError
from pipelex.core.domains.domain_blueprint import DomainBlueprint
from pipelex.core.stuffs.structured_content import StructuredContent
+from pipelex.tools.misc.json_utils import remove_none_values_from_dict
from pipelex.tools.misc.string_utils import is_pascal_case, normalize_to_ascii, snake_to_pascal_case
from pipelex.types import Self, StrEnum
@@ -258,3 +260,24 @@ class ConceptSpec(StructuredContent):
converted_structure[field_name] = field_spec.to_blueprint()
return ConceptBlueprint(description=self.description, structure=converted_structure, refines=self.refines)
+
+ @override
+ def pretty_print_content(self, title: str | None = None, number: int | None = None) -> None:
+ the_dict: dict[str, Any] = self.smart_dump()
+ the_dict = remove_none_values_from_dict(data=the_dict)
+ if number:
+ title = f"Concept #{number}: {self.the_concept_code}"
+ else:
+ title = f"Concept: {self.the_concept_code}"
+ if self.refines:
+ title += f" • Refines {self.refines}"
+ the_dict.pop("refines")
+
+ description = self.description
+ the_dict.pop("the_concept_code")
+ the_dict.pop("description")
+ if self.structure:
+ structure = the_dict.pop("structure")
+ pretty_print(structure, title=title, subtitle=description)
+ else:
+ pretty_print(description, title=title)
diff --git a/pipelex/builder/pipe/pipe_signature.py b/pipelex/builder/pipe/pipe_signature.py
index 45c080d0e..815a9ea8b 100644
--- a/pipelex/builder/pipe/pipe_signature.py
+++ b/pipelex/builder/pipe/pipe_signature.py
@@ -2,10 +2,13 @@ from typing import Any
from pydantic import Field, field_validator, model_validator
from pydantic.json_schema import SkipJsonSchema
+from typing_extensions import override
+from pipelex import pretty_print
from pipelex.core.pipes.exceptions import PipeBlueprintError
from pipelex.core.pipes.pipe_blueprint import AllowedPipeCategories, AllowedPipeTypes
from pipelex.core.stuffs.structured_content import StructuredContent
+from pipelex.tools.misc.json_utils import remove_none_values_from_dict
class PipeSignature(StructuredContent):
@@ -61,3 +64,20 @@ class PipeSignature(StructuredContent):
@classmethod
def validate_type(cls, type_value: str) -> AllowedPipeTypes:
return AllowedPipeTypes(type_value)
+
+ @override
+ def pretty_print_content(self, title: str | None = None, number: int | None = None) -> None:
+ the_dict: dict[str, Any] = self.smart_dump()
+ the_dict = remove_none_values_from_dict(data=the_dict)
+ if number:
+ title = f"Pipe signature #{number}: {self.code}"
+ else:
+ title = f"Pipe signature: {self.code}"
+ title += f" • {self.type} -> {self.result}"
+ subtitle = self.description
+ the_dict.pop("code")
+ the_dict.pop("description")
+ the_dict.pop("type")
+ the_dict.pop("pipe_category")
+ the_dict.pop("result")
+ pretty_print(the_dict, title=title, subtitle=subtitle)
diff --git a/pipelex/builder/pipe/pipe_spec.py b/pipelex/builder/pipe/pipe_spec.py
index 1f65409fe..4bac8f6ce 100644
--- a/pipelex/builder/pipe/pipe_spec.py
+++ b/pipelex/builder/pipe/pipe_spec.py
@@ -2,13 +2,15 @@ import re
from typing import Any
from pydantic import Field, field_validator
+from typing_extensions import override
-from pipelex import log
+from pipelex import log, pretty_print
from pipelex.builder.concept.concept_spec import ConceptSpec
from pipelex.core.pipes.exceptions import PipeBlueprintError
from pipelex.core.pipes.pipe_blueprint import AllowedPipeCategories, AllowedPipeTypes, PipeBlueprint
from pipelex.core.pipes.variable_multiplicity import parse_concept_with_multiplicity
from pipelex.core.stuffs.structured_content import StructuredContent
+from pipelex.tools.misc.json_utils import remove_none_values_from_dict
from pipelex.tools.misc.string_utils import is_snake_case, normalize_to_ascii
@@ -129,3 +131,19 @@ class PipeSpec(StructuredContent):
type=self.type,
pipe_category=self.pipe_category,
)
+
+ @override
+ def pretty_print_content(self, title: str | None = None, number: int | None = None) -> None:
+ the_dict: dict[str, Any] = self.smart_dump()
+ the_dict = remove_none_values_from_dict(data=the_dict)
+ if number:
+ title = f"Pipe #{number}: {self.pipe_code}"
+ else:
+ title = f"Pipe: {self.pipe_code}"
+ title += f" • {self.type}"
+ subtitle = self.description
+ the_dict.pop("pipe_code")
+ the_dict.pop("description")
+ the_dict.pop("type")
+ the_dict.pop("pipe_category")
+ pretty_print(the_dict, title=title, subtitle=subtitle)
diff --git a/pipelex/cogt/llm/llm_worker_internal_abstract.py b/pipelex/cogt/llm/llm_worker_internal_abstract.py
index ec02cd435..e9207af5e 100644
--- a/pipelex/cogt/llm/llm_worker_internal_abstract.py
+++ b/pipelex/cogt/llm/llm_worker_internal_abstract.py
@@ -37,7 +37,7 @@ class LLMWorkerInternalAbstract(LLMWorkerAbstract):
@property
@override
def desc(self) -> str:
- return f"LLM-Worker: {self.inference_model.tag}"
+ return self.inference_model.tag
@property
@override
@@ -49,7 +49,7 @@ class LLMWorkerInternalAbstract(LLMWorkerAbstract):
self,
llm_job: LLMJob,
):
- log.info(f"{self.desc} --------------------------------")
+ log.info(f"✨ {self.desc} ✨")
await super()._before_job(llm_job=llm_job)
llm_job.llm_job_before_start(inference_model=self.inference_model)
if get_config().cogt.llm_config.is_dump_text_prompts_enabled:
diff --git a/pipelex/core/stuffs/list_content.py b/pipelex/core/stuffs/list_content.py
index 766f6963b..06faf471c 100644
--- a/pipelex/core/stuffs/list_content.py
+++ b/pipelex/core/stuffs/list_content.py
@@ -78,3 +78,14 @@ class ListContent(StuffContent, Generic[StuffContentType]):
rendered += item.rendered_str(text_format=TextFormat.MARKDOWN)
rendered += "\n"
return rendered
+
+ @override
+ def pretty_print_content(self, title: str | None = None, number: int | None = None) -> None:
+ for item_index, item in enumerate(self.items):
+ item_number = item_index + 1
+ if title:
+ item_title = f"{title} • Item #{item_number}"
+ else:
+ item_title = f"Item #{item_index + 1}"
+ item.pretty_print_content(title=item_title, number=item_number)
+ print()
diff --git a/pipelex/core/stuffs/stuff.py b/pipelex/core/stuffs/stuff.py
index 3139e58ce..112871d66 100644
--- a/pipelex/core/stuffs/stuff.py
+++ b/pipelex/core/stuffs/stuff.py
@@ -193,6 +193,10 @@ Forbidden fields are: 'stuff_name', 'content_class', 'concept_code', 'stuff_code
"""Get content as MermaidContent if applicable."""
return self.content_as(MermaidContent)
+ def pretty_print_stuff(self, title: str | None = None) -> None:
+ title = title or f"{self.stuff_name} ({self.concept.code})"
+ self.content.pretty_print_content(title=title)
+
class DictStuff(CustomBaseModel):
"""Stuff with content as dict[str, Any] instead of StuffContent.
diff --git a/pipelex/core/stuffs/stuff_content.py b/pipelex/core/stuffs/stuff_content.py
index 8460c6271..c281e2131 100644
--- a/pipelex/core/stuffs/stuff_content.py
+++ b/pipelex/core/stuffs/stuff_content.py
@@ -1,10 +1,12 @@
from abc import ABC
-from typing import Any, TypeVar
+from typing import Any, TypeVar, cast
from kajson import kajson
from typing_extensions import override
from pipelex.cogt.templating.templating_style import TextFormat
+from pipelex.tools.misc.json_utils import remove_none_values_from_dict
+from pipelex.tools.misc.pretty import pretty_print
from pipelex.tools.typing.pydantic_utils import CustomBaseModel
StuffContentType = TypeVar("StuffContentType", bound="StuffContent")
@@ -51,3 +53,8 @@ class StuffContent(ABC, CustomBaseModel):
def rendered_json(self) -> str:
return kajson.dumps(self.smart_dump(), indent=4)
+
+ def pretty_print_content(self, title: str | None = None, number: int | None = None) -> None: # noqa: ARG002
+ the_dict: dict[str, Any] = cast("dict[str, Any]", self.smart_dump())
+ the_dict = remove_none_values_from_dict(data=the_dict)
+ pretty_print(the_dict, title=title)
diff --git a/pipelex/pipe_operators/pipe_operator.py b/pipelex/pipe_operators/pipe_operator.py
index b87dd3cb8..dec6d33fb 100644
--- a/pipelex/pipe_operators/pipe_operator.py
+++ b/pipelex/pipe_operators/pipe_operator.py
@@ -3,7 +3,7 @@ from typing import Generic, Literal, TypeVar
from typing_extensions import override
-from pipelex import log, pretty_print, pretty_print_md
+from pipelex import log, pretty_print_md
from pipelex.core.memory.working_memory import WorkingMemory
from pipelex.core.pipes.pipe_abstract import PipeAbstract
from pipelex.core.pipes.pipe_output import PipeOutput
@@ -54,9 +54,13 @@ class PipeOperator(PipeAbstract, Generic[PipeOperatorOutputType]):
output_name=output_name,
)
if isinstance(pipe_output.main_stuff.content, TextContent):
+ print()
pretty_print_md(pipe_output.main_stuff_as_str, title=f"PipeOutput of pipe {self.code}")
+ print()
else:
- pretty_print(pipe_output.main_stuff, title=f"PipeOutput of pipe {self.code}")
+ print()
+ pipe_output.main_stuff.pretty_print_stuff(title=f"PipeOutput of pipe {self.code}: {self.output.code}")
+ print()
case PipeRunMode.DRY:
name = f"Dry run [cyan]{self.class_name}[/cyan]"
indent_level = len(pipe_run_params.pipe_stack) - 1