Repository: emcie-co/parlant
File: server/src/emcie/server/engines/alpha/guideline_filter.py (Python)

Code before:
        self._llm_client = make_llm_client("openai")

    async def find_relevant_guidelines(
        self,
        guidelines: Iterable[Guideline],
        context_variables: Iterable[tuple[ContextVariable, ContextVariableValue]],
        interaction_history: Iterable[Event],
    ) -> Iterable[GuidelineProposition]:
        guideline_list = list(guidelines)

        if not guideline_list:
            return []

        batches = self._create_batches(guideline_list, batch_size=5)

        with duration_logger(f"Total guideline filtering ({len(batches)} batches)"):
            batch_tasks = [
                self._process_batch(
                    list(context_variables),
                    list(interaction_history),
                    batch,
                )
                for batch in batches
            ]
            batch_results = await asyncio.gather(*batch_tasks)
            aggregated_proposed_guidelines = sum(batch_results, [])

        return aggregated_proposed_guidelines

    def _create_batches(
        self,
        guidelines: list[Guideline],
        batch_size: int,
    ) -> list[list[Guideline]]:
        batches = []
        batch_count = len(guidelines) // batch_size + 1

        for batch_number in range(batch_count):
            start_offset = batch_number * batch_size
            end_offset = start_offset + batch_size
            batch = guidelines[start_offset:end_offset]
            batches.append(batch)

        return batches

    async def _process_batch(
        self,
        context_variables: list[tuple[ContextVariable, ContextVariableValue]],
        interaction_history: list[Event],
        batch: list[Guideline],
    ) -> list[GuidelineProposition]:

Review comment (suggestion):
General language note: If the class name is "ProposedGuidelines" then a legitimate generic variable name could be either "guidelines" or "proposed_guidelines".
However, the class name is "GuidelinePropositions", therefore the basic noun here is "proposition" rather than "guideline", and the variable name should either be "guideline_propositions" or just "propositions" (which is probably clear enough in this context).

Code after:
        self._llm_client = make_llm_client("openai")

    async def propose_guidelines(
        self,
        guidelines: Iterable[Guideline],
        context_variables: Iterable[tuple[ContextVariable, ContextVariableValue]],
        interaction_history: Iterable[Event],
    ) -> Iterable[GuidelineProposition]:
        guideline_list = list(guidelines)

        if not guideline_list:
            return []

        batches = self._create_batches(guideline_list, batch_size=5)

        with duration_logger(f"Total guideline filtering ({len(batches)} batches)"):
            batch_tasks = [
                self._process_batch(
                    list(context_variables),
                    list(interaction_history),
                    batch,
                )
                for batch in batches
            ]
            batch_results = await asyncio.gather(*batch_tasks)
            propositions = sum(batch_results, [])

        return propositions

    def _create_batches(
        self,
        guidelines: list[Guideline],
        batch_size: int,
    ) -> list[list[Guideline]]:
        batches = []
        batch_count = len(guidelines) // batch_size + 1

        for batch_number in range(batch_count):
            start_offset = batch_number * batch_size
            end_offset = start_offset + batch_size
            batch = guidelines[start_offset:end_offset]
            batches.append(batch)

        return batches

    async def _process_batch(
        self,
        context_variables: list[tuple[ContextVariable, ContextVariableValue]],
        interaction_history: list[Event],
        batch: list[Guideline],
    ) -> list[GuidelineProposition]: