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.
Fix a Pretty print and httpx bugs (#419)
Louis Choquel9d8eabf35a80
diff --git a/docs/pages/build-reliable-ai-workflows-with-pipelex/pipe-operators/PipeLLM.md b/docs/pages/build-reliable-ai-workflows-with-pipelex/pipe-operators/PipeLLM.mdindex 5cc4ab036..b0a173695 100644--- a/docs/pages/build-reliable-ai-workflows-with-pipelex/pipe-operators/PipeLLM.md+++ b/docs/pages/build-reliable-ai-workflows-with-pipelex/pipe-operators/PipeLLM.md@@ -28,7 +28,7 @@ description = "Describe an image"inputs = { image = "Image" }output = "VisualDescription"prompt = """-Describe the provided image in great detail.+Describe the provided image in great detail: $image"""```@@ -46,7 +46,7 @@ description = "Analyze wedding photo"inputs = { wedding_photo = "Photo" }output = "PhotoAnalysis"prompt = """-Analyze this wedding photo and describe the key moments captured.+Analyze this wedding photo and describe the key moments captured: $wedding_photo"""```@@ -61,7 +61,7 @@ description = "Analyze the visual layout of a page"inputs = { "page_content.page_view" = "Image" }output = "LayoutAnalysis"prompt = """-Analyze the visual layout and design elements of this page.+Analyze the visual layout and design elements of this page: $page_content.page_viewFocus on typography, spacing, and overall composition."""```@@ -86,7 +86,7 @@ inputs = {}output = "ImageComparison"prompt = """-Compare these two images and describe their similarities and differences.+Compare these two images and describe their similarities and differences: $first_image and $second_image"""```@@ -106,7 +106,7 @@ output = "DocumentAnalysis"prompt = """Given this context: $context-Analyze the document page shown in the image and explain how it relates to the provided context.+Analyze the document page shown in the image and explain how it relates to the provided context: $document.page_view"""```@@ -125,7 +125,7 @@ Analyze the document page shown in the image and explain how it relates to the p| `model` | string or table | Specifies the LLM choice by name, setting, or preset to use. | No || `model_to_structure` | string or table | Specifies the LLM choice by name, setting, or preset to use for structuring after preliminary text generation. | No || `system_prompt` | string | A system-level prompt to guide the LLM's behavior (e.g., "You are a helpful assistant"). Can be inline text or a reference to a template file (`"file:path/to/prompt.md"`). | No |-| `prompt_template` | string | A template for the user prompt. Use `$` for inline variables (e.g., `$topic`) and `@` to insert the content of an entire input (e.g., `@text_to_summarize`). **Note**: Do not use `@` or `$` for image variables. | No |+| `prompt` | string | A template for the user prompt. Use `$` for inline variables (e.g., `$topic`) and `@` to insert the content of an entire input (e.g., `@text_to_summarize`). Image variables should also be tagged with `$` or `@`. | No || `structuring_method` | string | The method for generating structured output. Can be `direct` or `preliminary_text`. Defaults to the global configuration. | No |### Output Multiplicity@@ -171,7 +171,7 @@ Write a four-line poem about pipes.### Text-to-Text Example-This pipe summarizes an input text, using a `prompt_template` to inject the input.+This pipe summarizes an input text, using a `prompt` to inject the input.```plx[pipe.summarize_text]@@ -199,7 +199,7 @@ description = "Extract table data from an image"inputs = { image = "TableScreenshot" }output = "TableData"prompt = """-Extract the table data from this image and format it as a structured table.+Extract the table data from this image and format it as a structured table: $image"""```diff --git a/docs/pages/cookbook-examples/extract-proof-of-purchase.md b/docs/pages/cookbook-examples/extract-proof-of-purchase.mdindex e79a230c8..aa458a2c9 100644--- a/docs/pages/cookbook-examples/extract-proof-of-purchase.md+++ b/docs/pages/cookbook-examples/extract-proof-of-purchase.md@@ -61,7 +61,7 @@ model = "llm_for_img_to_text"structuring_method = "preliminary_text"system_prompt = """You are a multimodal LLM, expert at converting images into perfect markdown."""prompt = """-You are given an image of a proof of purchase.+You are given an image of a proof of purchase: $page_content.page_viewYour role is to convert the image into perfect markdown.To help you do so, you are given the text extracted from the page by an OCR model.diff --git a/docs/pages/cookbook-examples/extract-table.md b/docs/pages/cookbook-examples/extract-table.mdindex a7f68ebf5..182acf30e 100644--- a/docs/pages/cookbook-examples/extract-table.md+++ b/docs/pages/cookbook-examples/extract-table.md@@ -78,7 +78,7 @@ description = "Review an HTML table"inputs = { table_screenshot = "TableScreenshot", html_table = "HtmlTable" }output = "HtmlTable"prompt = """-Your role is to correct an html_table to make sure that it matches the one in the provided image.+Your role is to correct an html_table to make sure that it matches the one in the provided image: $table_screenshot@html_tablediff --git a/docs/pages/cookbook-examples/invoice-extractor.md b/docs/pages/cookbook-examples/invoice-extractor.mdindex 38653df1a..233e2c687 100644--- a/docs/pages/cookbook-examples/invoice-extractor.md+++ b/docs/pages/cookbook-examples/invoice-extractor.md@@ -82,7 +82,7 @@ inputs = { "invoice_page.page_view" = "Image", invoice_details = "InvoiceDetailsoutput = "Invoice"model = "llm_to_extract_invoice"prompt = """-Extract invoice information from this invoice:+Extract invoice information from this invoice: $invoice_page.page_viewThe category of this invoice is: $invoice_details.category.diff --git a/pipelex/cogt/model_backends/backend_library.py b/pipelex/cogt/model_backends/backend_library.pyindex 3f1dc15e8..9e956e421 100644--- a/pipelex/cogt/model_backends/backend_library.py+++ b/pipelex/cogt/model_backends/backend_library.py@@ -97,8 +97,9 @@ class InferenceBackendLibrary(RootModel[InferenceBackendLibraryRoot]):) from var_fallback_pattern_excexcept VarNotFoundError as var_not_found_exc:msg = (- f"Variable substitution failed due to a 'variable not found' error in file '{backends_library_path}':"- f"\n{var_not_found_exc}\nRun mode: '{runtime_manager.run_mode}'"+ f"Variable substitution failed due to a 'variable not found' error in file '{backends_library_path}':\n"+ f"Backend name: '{backend_name}', Variable name: '{var_not_found_exc.var_name}'\n"+ f"{var_not_found_exc}\nRun mode: '{runtime_manager.run_mode}'")raise InferenceBackendCredentialsError(error_type=InferenceBackendCredentialsErrorType.VAR_NOT_FOUND,diff --git a/pipelex/core/stuffs/stuff_content.py b/pipelex/core/stuffs/stuff_content.pyindex c281e2131..962532aa1 100644--- a/pipelex/core/stuffs/stuff_content.py+++ b/pipelex/core/stuffs/stuff_content.py@@ -1,11 +1,11 @@from abc import ABC-from typing import Any, TypeVar, cast+from typing import Any, TypeVarfrom 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.json_utils import remove_none_valuesfrom pipelex.tools.misc.pretty import pretty_printfrom pipelex.tools.typing.pydantic_utils import CustomBaseModel@@ -55,6 +55,5 @@ class StuffContent(ABC, CustomBaseModel):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)+ json_content = remove_none_values(json_content=self.smart_dump())+ pretty_print(json_content, title=title)diff --git a/pipelex/plugins/openai/openai_factory.py b/pipelex/plugins/openai/openai_factory.pyindex fccc3265f..6543ae78e 100644--- a/pipelex/plugins/openai/openai_factory.py+++ b/pipelex/plugins/openai/openai_factory.py@@ -48,6 +48,12 @@ class OpenAIFactory:msg = f"Plugin '{plugin}' is not supported by OpenAIFactory"raise OpenAIFactoryError(msg) from exc+ # We have a workaround here:+ # OpenAI can be used without any API key (for instance when pointing to local Ollama) but the SDK,+ # as it is, raises if there is not API key (api_key is None and there is not env var).+ # But it works fine with an empty string.+ api_key = backend.api_key or ""+the_client: openai.AsyncOpenAImatch sdk_variant:case OpenAISdkVariant.AZURE_OPENAI:@@ -57,20 +63,19 @@ class OpenAIFactory:raise OpenAIFactoryError(msg)the_client = openai.AsyncAzureOpenAI(azure_endpoint=backend.endpoint,- api_key=backend.api_key,+ api_key=api_key,api_version=backend.get_extra_config(AzureExtraField.API_VERSION),)-case OpenAISdkVariant.OPENAI:log.verbose(f"Making AsyncOpenAI client with endpoint: {backend.endpoint}")the_client = openai.AsyncOpenAI(- api_key=backend.api_key,+ api_key=api_key,base_url=backend.endpoint,)case OpenAISdkVariant.OPENAI_ALT_IMG_GEN:log.verbose(f"Making AsyncOpenAI client with endpoint: {backend.endpoint}")the_client = openai.AsyncOpenAI(- api_key=backend.api_key,+ api_key=api_key,base_url=backend.endpoint,)diff --git a/pipelex/tools/misc/file_fetch_utils.py b/pipelex/tools/misc/file_fetch_utils.pyindex cb295dbe2..8612ef0a9 100644--- a/pipelex/tools/misc/file_fetch_utils.py+++ b/pipelex/tools/misc/file_fetch_utils.py@@ -1,12 +1,15 @@import httpxfrom httpx import Response+from pipelex.tools.misc.http_utils import get_user_agent+async def fetch_file_from_url_httpx_async(url: str,request_timeout: int | None = None,) -> bytes:- async with httpx.AsyncClient() as client:+ user_agent = get_user_agent()+ async with httpx.AsyncClient(headers={"User-Agent": user_agent}) as client:response: Response = await client.get(url,timeout=request_timeout,@@ -21,7 +24,8 @@ def fetch_file_from_url_httpx(url: str,request_timeout: int | None = None,) -> bytes:- with httpx.Client() as client:+ user_agent = get_user_agent()+ with httpx.Client(headers={"User-Agent": user_agent}) as client:response: Response = client.get(url,timeout=request_timeout,diff --git a/pipelex/tools/misc/http_utils.py b/pipelex/tools/misc/http_utils.pynew file mode 100644index 000000000..7c994eb61--- /dev/null+++ b/pipelex/tools/misc/http_utils.py@@ -0,0 +1,8 @@+from pipelex.tools.misc.package_utils import get_package_version+from pipelex.urls import URLs+++def get_user_agent() -> str:+ version = get_package_version()+ homepage_url = URLs.homepage+ return f"Pipelex/{version} ({homepage_url})"diff --git a/pipelex/urls.py b/pipelex/urls.pyindex adec05ebf..7d90529a1 100644--- a/pipelex/urls.py+++ b/pipelex/urls.py@@ -1,3 +1,7 @@class URLs:+ homepage = "https://pipelex.com"+ repository = "https://github.com/Pipelex/pipelex"+ documentation = "https://docs.pipelex.com/"+ changelog = "https://docs.pipelex.com/changelog/"discord = "https://go.pipelex.com/discord"pipe_func_docs = "https://docs.pipelex.com/pages/build-reliable-ai-workflows-with-pipelex/pipe-operators/PipeFunc"