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.
Moved settings for structure prompt suffixe and instructor dumps to cogt config
Louis Choquelc8ffaf195036
diff --git a/docs/pages/configuration/config-technical/cogt-config.md b/docs/pages/configuration/config-technical/cogt-config.mdindex ea0d9e368..8896096ab 100644--- a/docs/pages/configuration/config-technical/cogt-config.md+++ b/docs/pages/configuration/config-technical/cogt-config.md@@ -31,6 +31,7 @@ Configuration for all Language Model interactions:```toml[cogt.llm_config]default_max_images = 100 # Maximum number of images in prompts+is_structure_prompt_enabled = false# Job configuration[cogt.llm_config.llm_job_config]@@ -172,6 +173,7 @@ is_auto_setup_preset_ocr = true[cogt.llm_config]default_max_images = 100+is_structure_prompt_enabled = false[cogt.llm_config.llm_job_config]is_streaming_enabled = falsediff --git a/pipelex/cogt/config_cogt.py b/pipelex/cogt/config_cogt.pyindex 478e108e3..adcaed1c1 100644--- a/pipelex/cogt/config_cogt.py+++ b/pipelex/cogt/config_cogt.py@@ -21,12 +21,15 @@ class ImgGenConfig(ConfigModel):class InstructorConfig(ConfigModel):is_openai_structured_output_enabled: bool+ is_dump_kwargs_enabled: bool+ is_dump_response_enabled: bool+ is_dump_error_enabled: boolclass LLMConfig(ConfigModel):instructor_config: InstructorConfigllm_job_config: LLMJobConfig-+ is_structure_prompt_enabled: booldefault_max_images: intdiff --git a/pipelex/pipe_operators/img_gen/pipe_img_gen.py b/pipelex/pipe_operators/img_gen/pipe_img_gen.pyindex be9036289..d4d3684c8 100644--- a/pipelex/pipe_operators/img_gen/pipe_img_gen.py+++ b/pipelex/pipe_operators/img_gen/pipe_img_gen.py@@ -138,44 +138,20 @@ class PipeImgGen(PipeOperator[PipeImgGenOutput]):default_reaction = static_validation_config.default_reactionreactions = static_validation_config.reactions# check that we have either an img_gen_prompt passed as attribute or as a single text input+ nb_inputs = self.inputs.nb_inputsif self.img_gen_prompt:- if self.inputs.items:- msg = "img_gen_prompt_var_name must be None if img_gen_prompt is provided"+ if nb_inputs > 0:+ msg = "There must be no inputs if img_gen_prompt is provided"raise PipeDefinitionError(msg)# we're good with the prompt provided as attributereturn- candidate_prompt_var_names: list[str] = []- for input_name, requirement in self.inputs.items:- log.debug(f"Validating input '{input_name}' with concept code '{requirement.concept.code}'")- if concept_provider.is_compatible(- tested_concept=requirement.concept,- wanted_concept=ConceptFactory.make_native_concept(native_concept_data=NATIVE_CONCEPTS_DATA[NativeConceptEnum.TEXT]),- ):- self.img_gen_prompt_var_name = input_name- candidate_prompt_var_names.append(input_name)- else:- inadequate_input_concept_error = StaticValidationError(- error_type=StaticValidationErrorType.INADEQUATE_INPUT_CONCEPT,- domain=self.domain,- pipe_code=self.code,- variable_names=[input_name],- provided_concept_code=requirement.concept.code,- explanation="Only a text input can be provided for image gen prompt",- )- match reactions.get(StaticValidationErrorType.INADEQUATE_INPUT_CONCEPT, default_reaction):- case StaticValidationReaction.IGNORE:- pass- case StaticValidationReaction.LOG:- log.error(inadequate_input_concept_error.desc())- case StaticValidationReaction.RAISE:- raise inadequate_input_concept_error- if len(candidate_prompt_var_names) > 1:+ if nb_inputs > 1:too_many_candidate_inputs_error = StaticValidationError(error_type=StaticValidationErrorType.TOO_MANY_CANDIDATE_INPUTS,domain=self.domain,pipe_code=self.code,- variable_names=candidate_prompt_var_names,+ variable_names=self.inputs.variables,explanation="Only one text input can be provided for image gen prompt",)match reactions.get(StaticValidationErrorType.TOO_MANY_CANDIDATE_INPUTS, default_reaction):@@ -185,7 +161,7 @@ class PipeImgGen(PipeOperator[PipeImgGenOutput]):log.error(too_many_candidate_inputs_error.desc())case StaticValidationReaction.RAISE:raise too_many_candidate_inputs_error- elif len(candidate_prompt_var_names) == 0:+ elif nb_inputs < 1:missing_input_var_error = StaticValidationError(error_type=StaticValidationErrorType.MISSING_INPUT_VARIABLE,domain=self.domain,@@ -199,8 +175,32 @@ class PipeImgGen(PipeOperator[PipeImgGenOutput]):log.error(missing_input_var_error.desc())case StaticValidationReaction.RAISE:raise missing_input_var_error++ # We have confirmed right above that we have exactly one input+ # get input_name, requirement from single item in inputs+ input_name, requirement = self.inputs.items[0]+ log.debug(f"Validating input '{input_name}' with concept code '{requirement.concept.code}'")+ if concept_provider.is_compatible(+ tested_concept=requirement.concept,+ wanted_concept=ConceptFactory.make_native_concept(native_concept_data=NATIVE_CONCEPTS_DATA[NativeConceptEnum.TEXT]),+ ):+ self.img_gen_prompt_var_name = input_nameelse:- self.img_gen_prompt_var_name = candidate_prompt_var_names[0]+ inadequate_input_concept_error = StaticValidationError(+ error_type=StaticValidationErrorType.INADEQUATE_INPUT_CONCEPT,+ domain=self.domain,+ pipe_code=self.code,+ variable_names=[input_name],+ provided_concept_code=requirement.concept.code,+ explanation="For PipeImgGen you must provide a text input or a concept that refines text",+ )+ match reactions.get(StaticValidationErrorType.INADEQUATE_INPUT_CONCEPT, default_reaction):+ case StaticValidationReaction.IGNORE:+ pass+ case StaticValidationReaction.LOG:+ log.error(inadequate_input_concept_error.desc())+ case StaticValidationReaction.RAISE:+ raise inadequate_input_concept_error@overrideasync def _run_operator_pipe(diff --git a/pipelex/pipe_operators/llm/pipe_llm.py b/pipelex/pipe_operators/llm/pipe_llm.pyindex da33d67d0..10cfab882 100644--- a/pipelex/pipe_operators/llm/pipe_llm.py+++ b/pipelex/pipe_operators/llm/pipe_llm.py@@ -388,10 +388,12 @@ class PipeLLM(PipeOperator[PipeLLMOutput]):else:llm_prompt_2_factory = None- output_structure_prompt: str | None = PipeLLM.get_output_structure_prompt(- concept_string=pipe_run_params.dynamic_output_concept_code or output_concept.concept_string,- is_with_preliminary_text=is_with_preliminary_text,- )+ output_structure_prompt: str | None = None+ if get_config().cogt.llm_config.is_structure_prompt_enabled:+ output_structure_prompt = PipeLLM.get_output_structure_prompt(+ concept_string=pipe_run_params.dynamic_output_concept_code or output_concept.concept_string,+ is_with_preliminary_text=is_with_preliminary_text,+ )llm_prompt_1_for_object = await self.llm_prompt_spec.make_llm_prompt(output_concept_string=output_concept.concept_string,context_provider=working_memory,@@ -528,7 +530,6 @@ class PipeLLM(PipeOperator[PipeLLMOutput]):@staticmethoddef get_output_structure_prompt(concept_string: str, is_with_preliminary_text: bool) -> str | None:- return Noneconcept = get_required_concept(concept_string=concept_string)class_name = concept.structure_class_nameoutput_class = get_class_registry().get_class(class_name)diff --git a/pipelex/pipe_operators/ocr/pipe_ocr.py b/pipelex/pipe_operators/ocr/pipe_ocr.pyindex 1d9838d8e..9b2c94c47 100644--- a/pipelex/pipe_operators/ocr/pipe_ocr.py+++ b/pipelex/pipe_operators/ocr/pipe_ocr.py@@ -111,8 +111,6 @@ class PipeOcr(PipeOperator[PipeOcrOutput]):# We have confirmed right above that we have exactly one input# get input_name, requirement from single item in inputsinput_name, requirement = self.inputs.items[0]- log.debug(f"{input_name=}")- log.debug(f"{requirement=}")log.debug(f"Validating input '{input_name}' with concept code '{requirement.concept.code}'")if concept_provider.is_compatible(tested_concept=requirement.concept,@@ -133,7 +131,7 @@ class PipeOcr(PipeOperator[PipeOcrOutput]):pipe_code=self.code,variable_names=[input_name],provided_concept_code=requirement.concept.code,- explanation="For OCR you must provide either a pdf or an image or a concept that refines one of them",+ explanation="For PipeOcr you must provide either a pdf or an image or a concept that refines one of them",)match reactions.get(StaticValidationErrorType.INADEQUATE_INPUT_CONCEPT, default_reaction):case StaticValidationReaction.IGNORE:diff --git a/pipelex/pipelex.toml b/pipelex/pipelex.tomlindex 352b8b794..3df6c9fb3 100644--- a/pipelex/pipelex.toml+++ b/pipelex/pipelex.toml@@ -87,9 +87,13 @@ is_auto_setup_preset_ocr = true[cogt.llm_config]default_max_images = 100+is_structure_prompt_enabled = false[cogt.llm_config.instructor_config]is_openai_structured_output_enabled = false+is_dump_kwargs_enabled = true+is_dump_response_enabled = false+is_dump_error_enabled = false[cogt.llm_config.llm_job_config]max_retries = 3@@ -234,4 +238,3 @@ structure_field_ordering = ["type", "definition", "choices", "required"][pipelex.plx_config.pipes]field_ordering = ["type", "definition", "inputs", "output"]-diff --git a/pipelex/plugins/anthropic/anthropic_llm_worker.py b/pipelex/plugins/anthropic/anthropic_llm_worker.pyindex af0ffcf57..bc533de71 100644--- a/pipelex/plugins/anthropic/anthropic_llm_worker.py+++ b/pipelex/plugins/anthropic/anthropic_llm_worker.py@@ -4,23 +4,21 @@ import instructorfrom anthropic import AsyncAnthropic, AsyncAnthropicBedrock, omitfrom typing_extensions import override-from pipelex import log, pretty_print+from pipelex import logfrom pipelex.cogt.exceptions import LLMCompletionError, SdkTypeErrorfrom pipelex.cogt.llm.llm_job import LLMJobfrom pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstractfrom pipelex.cogt.llm.structured_output import StructureMethodfrom pipelex.cogt.model_backends.model_spec import InferenceModelSpec+from pipelex.config import get_configfrom pipelex.plugins.anthropic.anthropic_exceptions import AnthropicWorkerConfigurationErrorfrom pipelex.plugins.anthropic.anthropic_factory import AnthropicFactory, AnthropicSdkVariantfrom pipelex.reporting.reporting_protocol import ReportingProtocol+from pipelex.tools.misc.instructor_utils import dump_error, dump_kwargs, dump_responsefrom pipelex.tools.typing.pydantic_utils import BaseModelTypeVarfrom pipelex.types import StrEnum-def dump_kwargs(*_: Any, **kwargs: Any) -> None:- pretty_print(kwargs, title="Instructor about to send to Anthropic")--class AnthropicExtraField(StrEnum):CLAUDE_4_TOKENS_LIMIT = "claude_4_tokens_limit"@@ -79,7 +77,13 @@ class AnthropicLLMWorker(LLMWorkerInternalAbstract):else:self.instructor_for_objects = instructor.from_anthropic(client=sdk_instance)- self.instructor_for_objects.on(hook_name="completion:kwargs", handler=dump_kwargs)+ instructor_config = get_config().cogt.llm_config.instructor_config+ if instructor_config.is_dump_kwargs_enabled:+ self.instructor_for_objects.on(hook_name="completion:kwargs", handler=dump_kwargs)+ if instructor_config.is_dump_response_enabled:+ self.instructor_for_objects.on(hook_name="completion:response", handler=dump_response)+ if instructor_config.is_dump_error_enabled:+ self.instructor_for_objects.on(hook_name="completion:error", handler=dump_error)########################################################## Instance methodsdiff --git a/pipelex/plugins/openai/openai_llm_worker.py b/pipelex/plugins/openai/openai_llm_worker.pyindex 26907e0e0..6fd8b2b04 100644--- a/pipelex/plugins/openai/openai_llm_worker.py+++ b/pipelex/plugins/openai/openai_llm_worker.py@@ -9,30 +9,20 @@ if TYPE_CHECKING:from openai.types.chat import ChatCompletionMessagefrom typing_extensions import override-from pipelex import log, pretty_print+from pipelex import logfrom pipelex.cogt.exceptions import LLMCompletionError, LLMModelNotFoundError, SdkTypeErrorfrom pipelex.cogt.llm.llm_job import LLMJobfrom pipelex.cogt.llm.llm_worker_internal_abstract import LLMWorkerInternalAbstractfrom pipelex.cogt.llm.structured_output import StructureMethodfrom pipelex.cogt.model_backends.model_constraints import ModelConstraintsfrom pipelex.cogt.model_backends.model_spec import InferenceModelSpec+from pipelex.config import get_configfrom pipelex.plugins.openai.openai_factory import OpenAIFactoryfrom pipelex.reporting.reporting_protocol import ReportingProtocol+from pipelex.tools.misc.instructor_utils import dump_error, dump_kwargs, dump_responsefrom pipelex.tools.typing.pydantic_utils import BaseModelTypeVar-def dump_kwargs(*_: Any, **kwargs: Any) -> None:- pretty_print(kwargs, title="Instructor about to send to OpenAI")---def dump_response(response: Any) -> None:- pretty_print(response, title="Instructor response from OpenAI")---def dump_error(error: Exception) -> None:- pretty_print(error, title="Instructor error from OpenAI")--class OpenAILLMWorker(LLMWorkerInternalAbstract):def __init__(self,@@ -60,9 +50,13 @@ class OpenAILLMWorker(LLMWorkerInternalAbstract):else:self.instructor_for_objects = instructor.from_openai(client=sdk_instance)- self.instructor_for_objects.on(hook_name="completion:kwargs", handler=dump_kwargs)- self.instructor_for_objects.on(hook_name="completion:response", handler=dump_response)- self.instructor_for_objects.on(hook_name="completion:error", handler=dump_error)+ instructor_config = get_config().cogt.llm_config.instructor_config+ if instructor_config.is_dump_kwargs_enabled:+ self.instructor_for_objects.on(hook_name="completion:kwargs", handler=dump_kwargs)+ if instructor_config.is_dump_response_enabled:+ self.instructor_for_objects.on(hook_name="completion:response", handler=dump_response)+ if instructor_config.is_dump_error_enabled:+ self.instructor_for_objects.on(hook_name="completion:error", handler=dump_error)#########################################################@overridediff --git a/pipelex/tools/misc/instructor_utils.py b/pipelex/tools/misc/instructor_utils.pynew file mode 100644index 000000000..b64340270--- /dev/null+++ b/pipelex/tools/misc/instructor_utils.py@@ -0,0 +1,15 @@+from typing import Any++from pipelex import pretty_print+++def dump_kwargs(*_: Any, **kwargs: Any) -> None:+ pretty_print(kwargs, title="Instructor about to send to LLM provider")+++def dump_response(response: Any) -> None:+ pretty_print(response, title="Instructor response from LLM provider")+++def dump_error(error: Exception) -> None:+ pretty_print(error, title="Instructor error from LLM provider")