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.pyindex 4704ec5b2..98dfee5bd 100644--- a/pipelex/builder/concept/concept_spec.py+++ b/pipelex/builder/concept/concept_spec.py@@ -3,8 +3,9 @@ from datetime import datetimefrom typing import Anyfrom pydantic import ConfigDict, Field, field_validator, model_validator+from typing_extensions import override-from pipelex import log+from pipelex import log, pretty_printfrom pipelex.core.concepts.concept_blueprint import (ConceptBlueprint,ConceptBlueprintError,@@ -16,6 +17,7 @@ from pipelex.core.concepts.concept_native import NativeConceptCodefrom pipelex.core.concepts.exceptions import ConceptCodeError, ConceptStringOrConceptCodeErrorfrom pipelex.core.domains.domain_blueprint import DomainBlueprintfrom pipelex.core.stuffs.structured_content import StructuredContent+from pipelex.tools.misc.json_utils import remove_none_values_from_dictfrom pipelex.tools.misc.string_utils import is_pascal_case, normalize_to_ascii, snake_to_pascal_casefrom 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.pyindex 45c080d0e..815a9ea8b 100644--- a/pipelex/builder/pipe/pipe_signature.py+++ b/pipelex/builder/pipe/pipe_signature.py@@ -2,10 +2,13 @@ from typing import Anyfrom pydantic import Field, field_validator, model_validatorfrom pydantic.json_schema import SkipJsonSchema+from typing_extensions import override+from pipelex import pretty_printfrom pipelex.core.pipes.exceptions import PipeBlueprintErrorfrom pipelex.core.pipes.pipe_blueprint import AllowedPipeCategories, AllowedPipeTypesfrom pipelex.core.stuffs.structured_content import StructuredContent+from pipelex.tools.misc.json_utils import remove_none_values_from_dictclass PipeSignature(StructuredContent):@@ -61,3 +64,20 @@ class PipeSignature(StructuredContent):@classmethoddef 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.pyindex 1f65409fe..4bac8f6ce 100644--- a/pipelex/builder/pipe/pipe_spec.py+++ b/pipelex/builder/pipe/pipe_spec.py@@ -2,13 +2,15 @@ import refrom typing import Anyfrom pydantic import Field, field_validator+from typing_extensions import override-from pipelex import log+from pipelex import log, pretty_printfrom pipelex.builder.concept.concept_spec import ConceptSpecfrom pipelex.core.pipes.exceptions import PipeBlueprintErrorfrom pipelex.core.pipes.pipe_blueprint import AllowedPipeCategories, AllowedPipeTypes, PipeBlueprintfrom pipelex.core.pipes.variable_multiplicity import parse_concept_with_multiplicityfrom pipelex.core.stuffs.structured_content import StructuredContent+from pipelex.tools.misc.json_utils import remove_none_values_from_dictfrom 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.pyindex 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@overridedef 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.pyindex 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.pyindex 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.pyindex 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, castfrom kajson import kajsonfrom typing_extensions import overridefrom 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_printfrom pipelex.tools.typing.pydantic_utils import CustomBaseModelStuffContentType = 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.pyindex 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, TypeVarfrom typing_extensions import override-from pipelex import log, pretty_print, pretty_print_md+from pipelex import log, pretty_print_mdfrom pipelex.core.memory.working_memory import WorkingMemoryfrom pipelex.core.pipes.pipe_abstract import PipeAbstractfrom 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