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.

Build command first WIP

Louis Choquel1686a5283e50
diff --git a/pipelex/cli/_cli.py b/pipelex/cli/_cli.py
index e5234f56b..8ca16df2e 100644
--- a/pipelex/cli/_cli.py
+++ b/pipelex/cli/_cli.py
@@ -6,6 +6,7 @@ from typer.core import TyperGroup
from typing_extensions import override
from pipelex.cli.commands import init_app, migrate_app, show_app, validate_app
+from pipelex.cli.commands.build_cmd import build_app
class PipelexCLI(TyperGroup):
@@ -36,3 +37,4 @@ app.add_typer(init_app, name="init", help="Initialization commands")
app.add_typer(validate_app, name="validate", help="Validation and dry-run commands")
app.add_typer(show_app, name="show", help="Show and list commands")
app.add_typer(migrate_app, name="migrate", help="Migration commands")
+app.add_typer(build_app, name="build", help="Build artifacts like pipeline blueprints")
diff --git a/pipelex/cli/commands/build_cmd.py b/pipelex/cli/commands/build_cmd.py
new file mode 100644
index 000000000..1f19bddb5
--- /dev/null
+++ b/pipelex/cli/commands/build_cmd.py
@@ -0,0 +1,97 @@
+from __future__ import annotations
+
+import asyncio
+from typing import Annotated, Optional
+
+import typer
+
+from pipelex import pretty_print
+from pipelex.exceptions import PipelexCLIError
+from pipelex.libraries.pipeline_blueprint import PipelineLibraryBlueprint
+from pipelex.pipelex import Pipelex
+from pipelex.pipeline.execute import execute_pipeline
+from pipelex.tools.misc.file_utils import load_text_from_path
+
+
+def _load_requirements_text(requirements: Optional[str], requirements_file: Optional[str]) -> str:
+ if requirements_file:
+ return load_text_from_path(requirements_file)
+ if not requirements:
+ raise PipelexCLIError("You must provide requirements text via --requirements or a file via --file")
+ return requirements
+
+
+async def _generate_blueprint_async(requirements_text: str) -> PipelineLibraryBlueprint:
+ pipe_output = await execute_pipeline(
+ pipe_code="generate_pipeline_blueprint",
+ input_memory={
+ "requirements": requirements_text,
+ },
+ )
+ return pipe_output.main_stuff_as(content_type=PipelineLibraryBlueprint)
+
+
+def do_build_blueprint(
+ relative_config_folder_path: str,
+ requirements: Optional[str],
+ requirements_file: Optional[str],
+ output_path: Optional[str],
+) -> None:
+ # Initialize Pipelex (loads libraries and pipes)
+ Pipelex.make(relative_config_folder_path=relative_config_folder_path, from_file=False)
+
+ # Get requirements text
+ try:
+ requirements_text = _load_requirements_text(requirements=requirements, requirements_file=requirements_file)
+ except Exception as exc:
+ raise PipelexCLIError(f"Failed to load requirements: {exc}") from exc
+
+ # Generate blueprint
+ blueprint = asyncio.run(_generate_blueprint_async(requirements_text=requirements_text))
+
+ # Save or display result
+ if output_path:
+ blueprint.save_to_file(output_path)
+ typer.echo(f"✅ Blueprint saved to '{output_path}'")
+ else:
+ pretty_print(blueprint, title="Pipeline Blueprint")
+ pretty_print(blueprint.to_toml_dict(), title="Pipeline Blueprint (TOML)")
+
+
+# Typer group for build commands
+build_app = typer.Typer(help="Build artifacts like pipeline blueprints")
+
+
+@build_app.command("blueprint")
+def build_blueprint_cmd(
+ requirements: Annotated[
+ Optional[str],
+ typer.Option("--requirements", "-r", help="Requirements text to generate the pipeline blueprint from"),
+ ] = None,
+ requirements_file: Annotated[
+ Optional[str],
+ typer.Option("--file", "-f", help="Path to a file containing the requirements text"),
+ ] = None,
+ output_path: Annotated[
+ Optional[str],
+ typer.Option("--output", "-o", help="Path to save the generated TOML blueprint (optional)"),
+ ] = None,
+ relative_config_folder_path: Annotated[
+ str,
+ typer.Option(
+ "--config-folder-path",
+ "-c",
+ help="Relative path to the config folder path (libraries)",
+ ),
+ ] = "./pipelex_libraries",
+) -> None:
+ try:
+ do_build_blueprint(
+ relative_config_folder_path=relative_config_folder_path,
+ requirements=requirements,
+ requirements_file=requirements_file,
+ output_path=output_path,
+ )
+ except Exception as exc:
+ typer.echo(f"❌ Error: {exc}")
+ raise typer.Exit(1)
diff --git a/pipelex/cli/commands/init_cmd.py b/pipelex/cli/commands/init_cmd.py
index bb64fe69e..22a942796 100644
--- a/pipelex/cli/commands/init_cmd.py
+++ b/pipelex/cli/commands/init_cmd.py
@@ -25,7 +25,7 @@ def do_init_libraries(directory: str = ".", overwrite: bool = False) -> None:
else:
typer.echo(f"✅ Successfully initialized pipelex libraries at '{target_path}' (only created non-existing files)")
except Exception as exc:
- raise PipelexCLIError(f"Failed to initialize libraries at '{directory}': {exc}")
+ raise PipelexCLIError(f"Failed to initialize libraries at '{directory}': {exc}") from exc
def do_init_config(reset: bool = False) -> None:
@@ -41,7 +41,7 @@ def do_init_config(reset: bool = False) -> None:
shutil.copy2(pipelex_template_path, target_config_path)
typer.echo(f"Created pipelex.toml at {target_config_path}")
except Exception as exc:
- raise PipelexCLIError(f"Failed to create pipelex.toml: {exc}")
+ raise PipelexCLIError(f"Failed to create pipelex.toml: {exc}") from exc
# Typer group for init commands
diff --git a/pipelex/cli/commands/show_cmd.py b/pipelex/cli/commands/show_cmd.py
index 0c3f5c2c3..e4f0ffc15 100644
--- a/pipelex/cli/commands/show_cmd.py
+++ b/pipelex/cli/commands/show_cmd.py
@@ -20,7 +20,7 @@ def do_show_config() -> None:
title=f"Pipelex configuration for project: {config_manager.get_project_name()}",
)
except Exception as exc:
- raise PipelexConfigError(f"Error loading configuration: {exc}")
+ raise PipelexConfigError(f"Error loading configuration: {exc}") from exc
def do_list_pipes(relative_config_folder_path: str = "pipelex_libraries") -> None:
@@ -30,7 +30,7 @@ def do_list_pipes(relative_config_folder_path: str = "pipelex_libraries") -> Non
try:
get_pipe_provider().pretty_list_pipes()
except Exception as exc:
- raise PipelexCLIError(f"Failed to list pipes: {exc}")
+ raise PipelexCLIError(f"Failed to list pipes: {exc}") from exc
def do_show_pipe(pipe_code: str, relative_config_folder_path: str = "./pipelex_libraries") -> None:
diff --git a/pipelex/pipe_operators/pipe_llm_factory.py b/pipelex/pipe_operators/pipe_llm_factory.py
index d5c3ce043..69d6d420c 100644
--- a/pipelex/pipe_operators/pipe_llm_factory.py
+++ b/pipelex/pipe_operators/pipe_llm_factory.py
@@ -14,6 +14,7 @@ from pipelex.pipe_operators.pipe_jinja2_factory import PipeJinja2Factory
from pipelex.pipe_operators.pipe_llm import PipeLLM, StructuringMethod
from pipelex.pipe_operators.pipe_llm_prompt import PipeLLMPrompt
from pipelex.tools.templating.jinja2_errors import Jinja2TemplateError
+from pipelex.tools.templating.template_provider_abstract import TemplateNotFoundError
from pipelex.tools.typing.validation_utils import has_more_than_one_among_attributes_from_lists
@@ -100,12 +101,16 @@ class PipeLLMFactory(PipeSpecificFactoryProtocol[PipeLLMBlueprint, PipeLLM]):
error_msg += "The prompt template is not provided."
raise PipeDefinitionError(error_msg) from exc
elif pipe_blueprint.prompt is None and pipe_blueprint.prompt_name is None:
- # no jinja2 provided, no verbatim name, no fixed text, let's use the pipe code as jinja2 name
- user_pipe_jinja2 = PipeJinja2(
- code="adhoc_for_user_prompt",
- domain=domain_code,
- jinja2_name=pipe_code,
- )
+ # no jinja2 provided, no verbatim name, no fixed text, let's try and use the pipe code as jinja2 name
+ try:
+ user_pipe_jinja2 = PipeJinja2(
+ code="adhoc_for_user_prompt",
+ domain=domain_code,
+ jinja2_name=pipe_code,
+ )
+ except TemplateNotFoundError as exc:
+ error_msg = f"Jinja2 template not found for pipe '{pipe_code}' in domain '{domain_code}': {exc}."
+ raise PipeDefinitionError(error_msg) from exc
user_images: List[str] = []
if pipe_blueprint.inputs: