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.
Refactor/simplify choice of llm (#134)
Louis Choquelcff74341269c
diff --git a/pipelex/cogt/image/prompt_image.py b/pipelex/cogt/image/prompt_image.pyindex aafa752c0..df7f3e544 100644--- a/pipelex/cogt/image/prompt_image.py+++ b/pipelex/cogt/image/prompt_image.py@@ -6,10 +6,11 @@ from typing_extensions import overridefrom pipelex.tools.misc.attribute_utils import AttributePolisherfrom pipelex.tools.misc.filetype_utils import FileType, detect_file_type_from_base64, detect_file_type_from_path+from pipelex.tools.typing.pydantic_utils import CustomBaseModel-class PromptImageTypedBytes(BaseModel):- image_bytes: bytes+class PromptImageTypedBytes(CustomBaseModel):+ base_64: bytesfile_type: FileType@@ -37,7 +38,7 @@ class PromptImageUrl(PromptImage):@overridedef __str__(self) -> str:truncated_url = AttributePolisher.get_truncated_value(name="url", value=self.url)- return f"PromptImageUrl(url='{truncated_url}')"+ return f"PromptImageUrl(url='{truncated_url!r}')"@overridedef __format__(self, format_spec: str) -> str:@@ -54,7 +55,7 @@ class PromptImageBytes(PromptImage):def __str__(self) -> str:base_64_str = str(self.base_64)truncated_base_64 = AttributePolisher.get_truncated_value(name="base_64", value=base_64_str)- return f"PromptImageBytes(image_bytes={truncated_base_64})"+ return f"PromptImageBytes(image_bytes={truncated_base_64!r})"@overridedef __repr__(self) -> str:@@ -65,4 +66,4 @@ class PromptImageBytes(PromptImage):return self.__str__()def make_prompt_image_typed_bytes(self) -> PromptImageTypedBytes:- return PromptImageTypedBytes(image_bytes=self.base_64, file_type=self.get_file_type())+ return PromptImageTypedBytes(base_64=self.base_64, file_type=self.get_file_type())diff --git a/pipelex/cogt/llm/llm_models/llm_deck.py b/pipelex/cogt/llm/llm_models/llm_deck.pyindex 1a2a8ff91..0be1ae503 100644--- a/pipelex/cogt/llm/llm_models/llm_deck.py+++ b/pipelex/cogt/llm/llm_models/llm_deck.py@@ -3,7 +3,6 @@ from typing import Dict, Optional, Unionfrom pydantic import field_validator, model_validatorfrom typing_extensions import Self, override-from pipelex import logfrom pipelex.cogt.exceptions import LLMDeckValidatonError, LLMHandleNotFoundError, LLMPresetNotFoundError, LLMSettingsValidationErrorfrom pipelex.cogt.llm.llm_models.llm_deck_abstract import LLMDeckAbstractfrom pipelex.cogt.llm.llm_models.llm_engine_blueprint import LLMEngineBlueprint@@ -51,80 +50,6 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):raise LLMPresetNotFoundError(f"LLM preset '{llm_setting_or_preset_id}' not found in deck")return the_llm_preset- @override- def get_llm_setting_for_text(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- llm_setting: LLMSettingOrPresetId- if replacement := self.llm_choice_overrides.for_text:- log.warning(f"General llm setting override for text, '{self.llm_choice_defaults.for_text}' -> '{replacement}'")- llm_setting = replacement- elif override and (provided_override_for_text := override.for_text):- llm_setting = provided_override_for_text- elif self.llm_choice_defaults.for_text is None:- raise ConfigValidationError("llm_choices.for_text cannot be None")- else:- llm_setting = self.llm_choice_defaults.for_text-- return self.get_llm_setting(llm_setting_or_preset_id=llm_setting)-- @override- def get_llm_setting_for_object(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- log.debug(f"Getting LLM setting for object with provided_override: {override}")- llm_setting: LLMSettingOrPresetId- if replacement := self.llm_choice_overrides.for_object:- log.warning(f"General llm setting override for object, '{self.llm_choice_defaults.for_object}' -> '{replacement}'")- llm_setting = replacement- elif override and (provided_override_for_object := override.for_object):- llm_setting = provided_override_for_object- elif self.llm_choice_defaults.for_object is None:- raise ConfigValidationError("llm_choices.for_object cannot be None")- else:- llm_setting = self.llm_choice_defaults.for_object-- return self.get_llm_setting(llm_setting_or_preset_id=llm_setting)-- @override- def get_llm_setting_for_object_direct(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- log.debug(f"Getting LLM preset for object direct with provided_override: {override}")- llm_setting: LLMSettingOrPresetId- if replacement := self.llm_choice_overrides.for_object_direct:- log.warning(f"General choice override for LLM preset for structured, '{self.llm_choice_defaults.for_object_direct}' -> '{replacement}'")- llm_setting = replacement- elif override and (provided_override_for_object_direct := override.for_object_direct):- llm_setting = provided_override_for_object_direct- elif self.llm_choice_defaults.for_object_direct is None:- raise ConfigValidationError("llm_choices.for_object_direct cannot be None")- else:- llm_setting = self.llm_choice_defaults.for_object_direct-- return self.get_llm_setting(llm_setting_or_preset_id=llm_setting)-- @override- def get_llm_setting_for_object_list(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- llm_setting: LLMSettingOrPresetId- if replacement := self.llm_choice_overrides.for_object_list:- log.warning(f"General choice override for LLM preset for structured, '{self.llm_choice_defaults.for_object_list}' -> '{replacement}'")- llm_setting = replacement- elif override and (provided_override_for_object_list := override.for_object_list):- llm_setting = provided_override_for_object_list- elif self.llm_choice_defaults.for_object_list is None:- raise ConfigValidationError("llm_choices.for_object_list cannot be None")- else:- llm_setting = self.llm_choice_defaults.for_object_list-- return self.get_llm_setting(llm_setting_or_preset_id=llm_setting)-- @override- def get_llm_setting_for_object_list_direct(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- llm_setting: LLMSettingOrPresetId- if override and (provided_override_for_object_list_direct := override.for_object_list_direct):- llm_setting = provided_override_for_object_list_direct- elif self.llm_choice_defaults.for_object_list_direct is None:- raise ConfigValidationError("llm_choices.for_object_list_direct cannot be None")- else:- llm_setting = self.llm_choice_defaults.for_object_list_direct-- return self.get_llm_setting(llm_setting_or_preset_id=llm_setting)-@overridedef find_llm_model(self, llm_handle: str) -> LLMModel:llm_models_provider = get_llm_models_provider()@@ -202,12 +127,6 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):raise ConfigValidationError("llm_choice_defaults.for_text cannot be None")if llm_choice_defaults.for_object is None:raise ConfigValidationError("llm_choice_defaults.for_object cannot be None")- if llm_choice_defaults.for_object_direct is None:- raise ConfigValidationError("llm_choice_defaults.for_object_direct cannot be None")- if llm_choice_defaults.for_object_list is None:- raise ConfigValidationError("llm_choice_defaults.for_object_list cannot be None")- if llm_choice_defaults.for_object_list_direct is None:- raise ConfigValidationError("llm_choice_defaults.for_object_list_direct cannot be None")return llm_choice_defaults@field_validator("llm_choice_overrides", mode="after")@@ -217,12 +136,6 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):value.for_text = Noneif value.for_object == LLM_PRESET_DISABLED:value.for_object = None- if value.for_object_direct == LLM_PRESET_DISABLED:- value.for_object_direct = None- if value.for_object_list == LLM_PRESET_DISABLED:- value.for_object_list = None- if value.for_object_list_direct == LLM_PRESET_DISABLED:- value.for_object_list_direct = Nonereturn valuedef add_llm_name_as_handle_with_defaults(self, llm_name: str):@@ -237,11 +150,6 @@ class LLMDeck(LLMDeckAbstract, ConfigModel):raise LLMHandleNotFoundError(f"llm_handle '{llm_setting.llm_handle}' for llm_preset '{llm_preset_id}' not found in deck")return self- @model_validator(mode="after")- def validate_llm_default_choices(self) -> Self:- self._validate_llm_choices(llm_choices=self.llm_choice_defaults)- return self-@model_validator(mode="after")def validate_llm_setting_overrides(self) -> Self:self._validate_llm_choices(llm_choices=self.llm_choice_overrides)diff --git a/pipelex/cogt/llm/llm_models/llm_deck_abstract.py b/pipelex/cogt/llm/llm_models/llm_deck_abstract.pyindex 40c90d7dc..2fa6835f1 100644--- a/pipelex/cogt/llm/llm_models/llm_deck_abstract.py+++ b/pipelex/cogt/llm/llm_models/llm_deck_abstract.py@@ -6,20 +6,17 @@ from typing_extensions import Selffrom pipelex.cogt.llm.llm_models.llm_engine_blueprint import LLMEngineBlueprintfrom pipelex.cogt.llm.llm_models.llm_model import LLMModel-from pipelex.cogt.llm.llm_models.llm_setting import LLMSetting, LLMSettingChoices, LLMSettingOrPresetId+from pipelex.cogt.llm.llm_models.llm_setting import LLMSetting, LLMSettingChoices, LLMSettingChoicesDefaults, LLMSettingOrPresetIdclass LLMDeckAbstract(ABC):llm_handles: Dict[str, LLMEngineBlueprint] = Field(default_factory=dict)llm_external_handles: List[str] = Field(default_factory=list)llm_presets: Dict[str, LLMSetting] = Field(default_factory=dict)- llm_choice_defaults: LLMSettingChoices+ llm_choice_defaults: LLMSettingChoicesDefaultsllm_choice_overrides: LLMSettingChoices = LLMSettingChoices(for_text=None,for_object=None,- for_object_direct=None,- for_object_list=None,- for_object_list_direct=None,)@abstractmethod@@ -34,26 +31,6 @@ class LLMDeckAbstract(ABC):def get_llm_setting(self, llm_setting_or_preset_id: LLMSettingOrPresetId) -> LLMSetting:pass- @abstractmethod- def get_llm_setting_for_text(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- pass-- @abstractmethod- def get_llm_setting_for_object(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- pass-- @abstractmethod- def get_llm_setting_for_object_direct(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- pass-- @abstractmethod- def get_llm_setting_for_object_list(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- pass-- @abstractmethod- def get_llm_setting_for_object_list_direct(self, override: Optional[LLMSettingChoices] = None) -> LLMSetting:- pass-@abstractmethoddef find_llm_model(self, llm_handle: str) -> LLMModel:passdiff --git a/pipelex/cogt/llm/llm_models/llm_setting.py b/pipelex/cogt/llm/llm_models/llm_setting.pyindex ebb263f96..7fba7e689 100644--- a/pipelex/cogt/llm/llm_models/llm_setting.py+++ b/pipelex/cogt/llm/llm_models/llm_setting.py@@ -55,12 +55,14 @@ class LLMSetting(ConfigModel):LLMSettingOrPresetId = Union[LLMSetting, str]+class LLMSettingChoicesDefaults(ConfigModel):+ for_text: LLMSettingOrPresetId+ for_object: LLMSettingOrPresetId++class LLMSettingChoices(ConfigModel):for_text: Optional[LLMSettingOrPresetId]for_object: Optional[LLMSettingOrPresetId]- for_object_direct: Optional[LLMSettingOrPresetId]- for_object_list: Optional[LLMSettingOrPresetId]- for_object_list_direct: Optional[LLMSettingOrPresetId]def list_used_presets(self) -> Set[str]:return set(@@ -69,9 +71,6 @@ class LLMSettingChoices(ConfigModel):for setting in [self.for_text,self.for_object,- self.for_object_direct,- self.for_object_list,- self.for_object_list_direct,]if isinstance(setting, str)]@@ -82,14 +81,8 @@ class LLMSettingChoices(ConfigModel):cls,for_text: Optional[LLMSettingOrPresetId] = None,for_object: Optional[LLMSettingOrPresetId] = None,- for_object_direct: Optional[LLMSettingOrPresetId] = None,- for_object_list: Optional[LLMSettingOrPresetId] = None,- for_object_list_direct: Optional[LLMSettingOrPresetId] = None,) -> Self:return cls(for_text=for_text,for_object=for_object,- for_object_direct=for_object_direct,- for_object_list=for_object_list,- for_object_list_direct=for_object_list_direct,)diff --git a/pipelex/libraries/llm_deck/base_llm_deck.toml b/pipelex/libraries/llm_deck/base_llm_deck.tomlindex f64fbba24..64b2d2545 100644--- a/pipelex/libraries/llm_deck/base_llm_deck.toml+++ b/pipelex/libraries/llm_deck/base_llm_deck.toml@@ -77,6 +77,3 @@ llm_to_extract_tables = { llm_handle = "best-claude", temperature = 0.1 }[llm_choice_defaults]for_text = "cheap_llm_for_text"for_object = "cheap_llm_for_object"-for_object_direct = "cheap_llm_for_object"-for_object_list = "cheap_llm_for_object"-for_object_list_direct = "cheap_llm_for_object"diff --git a/pipelex/libraries/llm_deck/overrides.toml b/pipelex/libraries/llm_deck/overrides.tomlindex 8541de740..76598a632 100644--- a/pipelex/libraries/llm_deck/overrides.toml+++ b/pipelex/libraries/llm_deck/overrides.toml@@ -7,6 +7,3 @@[llm_choice_overrides]for_text = "disabled"for_object = "disabled"-for_object_direct = "disabled"-for_object_list = "disabled"-for_object_list_direct = "disabled"diff --git a/pipelex/pipe_operators/pipe_llm.py b/pipelex/pipe_operators/pipe_llm.pyindex 228729a19..c9289b45e 100644--- a/pipelex/pipe_operators/pipe_llm.py+++ b/pipelex/pipe_operators/pipe_llm.py@@ -7,7 +7,7 @@ from pipelex import logfrom pipelex.cogt.content_generation.content_generator_dry import ContentGeneratorDryfrom pipelex.cogt.content_generation.content_generator_protocol import ContentGeneratorProtocolfrom pipelex.cogt.llm.llm_models.llm_deck_check import check_llm_setting_with_deck-from pipelex.cogt.llm.llm_models.llm_setting import LLMSetting, LLMSettingChoices+from pipelex.cogt.llm.llm_models.llm_setting import LLMSetting, LLMSettingChoices, LLMSettingOrPresetIdfrom pipelex.cogt.llm.llm_prompt import LLMPromptfrom pipelex.cogt.llm.llm_prompt_factory_abstract import LLMPromptFactoryAbstractfrom pipelex.config import StaticValidationReaction, get_config@@ -194,26 +194,6 @@ class PipeLLM(PipeOperator):for llm_setting in self.llm_choices.list_used_presets():check_llm_setting_with_deck(llm_setting_or_preset_id=llm_setting)- @property- def llm_setting_main(self) -> LLMSetting:- return get_llm_deck().get_llm_setting_for_text(override=self.llm_choices)-- @property- def llm_setting_for_object(self) -> LLMSetting:- return get_llm_deck().get_llm_setting_for_object(override=self.llm_choices)-- @property- def llm_setting_for_object_direct(self) -> LLMSetting:- return get_llm_deck().get_llm_setting_for_object_direct(override=self.llm_choices)-- @property- def llm_setting_for_object_list(self) -> LLMSetting:- return get_llm_deck().get_llm_setting_for_object_list(override=self.llm_choices)-- @property- def llm_setting_for_object_list_direct(self) -> LLMSetting:- return get_llm_deck().get_llm_setting_for_object_list_direct(override=self.llm_choices)-@overridedef required_variables(self) -> Set[str]:required_variables: Set[str] = set()@@ -265,15 +245,38 @@ class PipeLLM(PipeOperator):else:log.verbose(f"{self.class_name} generate a single '{output_concept_code}' (class '{output_concept.structure_class_name}')")- if not self.pipe_llm_prompt.prompting_style and (- llm_model := get_llm_deck().find_optional_llm_model(llm_handle=self.llm_setting_main.llm_handle)- ):+ # Collect what LLM settings we have for this particular PipeLLM+ llm_for_text_choice: Optional[LLMSettingOrPresetId] = None+ llm_for_object_choice: Optional[LLMSettingOrPresetId] = None+ if self.llm_choices:+ llm_for_text_choice = self.llm_choices.for_text+ llm_for_object_choice = self.llm_choices.for_object++ # Choice of main LLM for text first from this PipeLLM setting (self.llm_choices)+ # or from the llm_choice_overrides or fallback on the llm_choice_defaults+ llm_setting_or_preset_id_for_text: LLMSettingOrPresetId = (+ llm_for_text_choice or get_llm_deck().llm_choice_overrides.for_text or get_llm_deck().llm_choice_defaults.for_text+ )+ llm_setting_main: LLMSetting = get_llm_deck().get_llm_setting(llm_setting_or_preset_id=llm_setting_or_preset_id_for_text)++ # Choice of main LLM for object from this PipeLLM setting (self.llm_choices)+ # OR FROM THE llm_for_text_choice (if any)+ # then fallback on the llm_choice_overrides or llm_choice_defaults+ llm_setting_or_preset_id_for_object: LLMSettingOrPresetId = (+ llm_for_object_choice+ or llm_for_text_choice+ or get_llm_deck().llm_choice_overrides.for_object+ or get_llm_deck().llm_choice_defaults.for_object+ )+ llm_setting_for_object: LLMSetting = get_llm_deck().get_llm_setting(llm_setting_or_preset_id=llm_setting_or_preset_id_for_object)++ if not self.pipe_llm_prompt.prompting_style and (llm_model := get_llm_deck().find_optional_llm_model(llm_handle=llm_setting_main.llm_handle)):llm_family = llm_model.llm_family- if self.llm_setting_main.prompting_target:- log.dev(f"prompting_target for '{self.llm_setting_main.llm_handle}' from setting: {self.llm_setting_main}")+ if llm_setting_main.prompting_target:+ log.dev(f"prompting_target for '{llm_setting_main.llm_handle}' from setting: {llm_setting_main}")else:- log.dev(f"prompting_target for '{self.llm_setting_main.llm_handle}' from llm_family: {llm_family}")- prompting_target = self.llm_setting_main.prompting_target or llm_family.prompting_target+ log.dev(f"prompting_target for '{llm_setting_main.llm_handle}' from llm_family: {llm_family}")+ prompting_target = llm_setting_main.prompting_target or llm_family.prompting_targetself.pipe_llm_prompt.prompting_style = get_config().pipelex.prompting_config.get_prompting_style(prompting_target=prompting_target,)@@ -310,7 +313,7 @@ class PipeLLM(PipeOperator):generated_text: str = await content_generator.make_llm_text(job_metadata=job_metadata,llm_prompt_for_text=llm_prompt_1,- llm_setting_main=self.llm_setting_main,+ llm_setting_main=llm_setting_main,)the_content = TextContent(@@ -376,6 +379,8 @@ class PipeLLM(PipeOperator):is_multiple_output=is_multiple_output,fixed_nb_output=fixed_nb_output,output_class_name=output_concept.structure_class_name,+ llm_setting_main=llm_setting_main,+ llm_setting_for_object=llm_setting_for_object,llm_prompt_1=llm_prompt_1,llm_prompt_2_factory=llm_prompt_2_factory,content_generator=content_generator,@@ -404,6 +409,8 @@ class PipeLLM(PipeOperator):is_multiple_output: bool,fixed_nb_output: Optional[int],output_class_name: str,+ llm_setting_main: LLMSetting,+ llm_setting_for_object: LLMSetting,llm_prompt_1: LLMPrompt,llm_prompt_2_factory: Optional[LLMPromptFactoryAbstract],content_generator: ContentGeneratorProtocol,@@ -429,9 +436,9 @@ class PipeLLM(PipeOperator):job_metadata=job_metadata,object_class=content_class,llm_prompt_for_text=llm_prompt_1,- llm_setting_main=self.llm_setting_main,+ llm_setting_main=llm_setting_main,llm_prompt_factory_for_object_list=llm_prompt_2_factory,- llm_setting_for_object_list=self.llm_setting_for_object_list,+ llm_setting_for_object_list=llm_setting_for_object,nb_items=fixed_nb_output,)else:@@ -442,7 +449,7 @@ class PipeLLM(PipeOperator):job_metadata=job_metadata,object_class=content_class,llm_prompt_for_object_list=llm_prompt_1,- llm_setting_for_object_list=self.llm_setting_for_object_list_direct,+ llm_setting_for_object_list=llm_setting_for_object,nb_items=fixed_nb_output,)@@ -459,9 +466,9 @@ class PipeLLM(PipeOperator):job_metadata=job_metadata,object_class=content_class,llm_prompt_for_text=llm_prompt_1,- llm_setting_main=self.llm_setting_main,+ llm_setting_main=llm_setting_main,llm_prompt_factory_for_object=llm_prompt_2_factory,- llm_setting_for_object=self.llm_setting_for_object,+ llm_setting_for_object=llm_setting_for_object,)else:# We're generating a single object directly@@ -471,7 +478,7 @@ class PipeLLM(PipeOperator):job_metadata=job_metadata,object_class=content_class,llm_prompt_for_object=llm_prompt_1,- llm_setting_for_object=self.llm_setting_for_object_direct,+ llm_setting_for_object=llm_setting_for_object,)the_content = generated_objectdiff --git a/pipelex/pipe_operators/pipe_llm_factory.py b/pipelex/pipe_operators/pipe_llm_factory.pyindex 991d5c715..f3a3bd2c3 100644--- a/pipelex/pipe_operators/pipe_llm_factory.py+++ b/pipelex/pipe_operators/pipe_llm_factory.py@@ -30,9 +30,6 @@ class PipeLLMBlueprint(PipeBlueprint):llm: Optional[LLMSettingOrPresetId] = Nonellm_to_structure: Optional[LLMSettingOrPresetId] = None- llm_to_structure_direct: Optional[LLMSettingOrPresetId] = None- llm_to_structure_list: Optional[LLMSettingOrPresetId] = None- llm_to_structure_list_direct: Optional[LLMSettingOrPresetId] = Nonestructuring_method: Optional[StructuringMethod] = Noneprompt_template_to_structure: Optional[str] = None@@ -134,9 +131,6 @@ class PipeLLMFactory(PipeSpecificFactoryProtocol[PipeLLMBlueprint, PipeLLM]):llm_choices = LLMSettingChoices(for_text=pipe_blueprint.llm,for_object=pipe_blueprint.llm_to_structure,- for_object_direct=pipe_blueprint.llm_to_structure_direct,- for_object_list=pipe_blueprint.llm_to_structure_list,- for_object_list_direct=pipe_blueprint.llm_to_structure_list_direct,)# output_multiplicity defaults to False for PipeLLM so unless it's run with explicit demand for multiple outputs,diff --git a/pipelex/plugins/anthropic/anthropic_factory.py b/pipelex/plugins/anthropic/anthropic_factory.pyindex 6e44689dd..4cd56931a 100644--- a/pipelex/plugins/anthropic/anthropic_factory.py+++ b/pipelex/plugins/anthropic/anthropic_factory.py@@ -84,7 +84,7 @@ class AnthropicFactory:"source": {"type": "base64","media_type": mime, # type: ignore- "data": prepped_image.image_bytes.decode("utf-8"),+ "data": prepped_image.base_64.decode("utf-8"),}, # type: ignore}elif isinstance(prepped_image, str): # pyright: ignore[reportUnnecessaryIsInstance]@@ -127,7 +127,7 @@ class AnthropicFactory:"source": {"type": "base64","media_type": mime, # type: ignore- "data": prepped_image.image_bytes.decode("utf-8"),+ "data": prepped_image.base_64.decode("utf-8"),}, # type: ignore}elif isinstance(prepped_image, str): # pyright: ignore[reportUnnecessaryIsInstance]@@ -141,7 +141,6 @@ class AnthropicFactory:}else:raise AnthropicFactoryError(f"Unsupported PromptImageTypedBytesOrUrl type: '{type(prepped_image).__name__}'")- log.debug(image_block_param_in_loop)images_block_params.append(image_block_param_in_loop)content: List[Union[TextBlockParam, ImageBlockParam]] = images_block_params + [text_block_param]@@ -170,7 +169,7 @@ class AnthropicFactory:typed_bytes_or_url = prompt_image.urlelif isinstance(prompt_image, PromptImagePath):b64 = await load_binary_as_base64_async(prompt_image.file_path)- typed_bytes_or_url = PromptImageTypedBytes(image_bytes=b64, file_type=prompt_image.get_file_type())+ typed_bytes_or_url = PromptImageTypedBytes(base_64=b64, file_type=prompt_image.get_file_type())else:raise AnthropicFactoryError(f"Unsupported PromptImage type: '{type(prompt_image).__name__}'")return typed_bytes_or_urldiff --git a/pipelex/tools/misc/attribute_utils.py b/pipelex/tools/misc/attribute_utils.pyindex eec35c15a..8cfec1008 100644--- a/pipelex/tools/misc/attribute_utils.py+++ b/pipelex/tools/misc/attribute_utils.py@@ -1,4 +1,4 @@-from typing import Any, ClassVar+from typing import Any, ClassVar, Unionclass AttributePolisher:@@ -13,21 +13,30 @@ class AttributePolisher:return value[:max_length] + cls.truncate_suffixreturn value+ @classmethod+ def _truncate_bytes(cls, value: bytes, max_length: int) -> bytes:+ """Truncate a bytes to the specified maximum length and append the truncate suffix."""+ if len(value) > max_length:+ return value[:max_length] + cls.truncate_suffix.encode("utf-8")+ return value+@classmethoddef should_truncate(cls, name: str, value: Any) -> bool:- if not isinstance(value, str):+ if not isinstance(value, (str, bytes)):return Falseif name == "base_64" and len(value) > cls.base_64_truncate_length:return True- elif name == "url" and value.startswith("data:image/") and len(value) > cls.url_truncate_length:+ elif name == "url" and isinstance(value, str) and value.startswith("data:image/") and len(value) > cls.url_truncate_length:return Truereturn False@classmethod- def get_truncated_value(cls, name: str, value: str) -> str:+ def get_truncated_value(cls, name: str, value: Union[str, bytes]) -> Union[str, bytes]:"""Get the truncated value based on the field name and value type."""- if name == "base_64":+ if isinstance(value, bytes):+ return cls._truncate_bytes(value, cls.base_64_truncate_length)+ elif name == "base_64":return cls._truncate_string(value, cls.base_64_truncate_length)elif name == "url" and value.startswith("data:image/"):return cls._truncate_string(value, cls.url_truncate_length)diff --git a/pipelex/tools/misc/markdown_utils.py b/pipelex/tools/misc/markdown_utils.pyindex f55a2afd0..c0695ed8e 100644--- a/pipelex/tools/misc/markdown_utils.py+++ b/pipelex/tools/misc/markdown_utils.py@@ -57,7 +57,7 @@ def convert_to_markdown(data: Any, level: int = 1, is_pretty: bool = False, key:# If it's a string with multiple lines, just output them as-is.str_value = str(data)if key and AttributePolisher.should_truncate(name=key, value=str_value):- return AttributePolisher.get_truncated_value(name=key, value=str_value)+ return str(AttributePolisher.get_truncated_value(name=key, value=str_value))return str_valueelif data is None: