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

Code before:
            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]:
        prompt = self._format_prompt(
            context_variables=context_variables,
            interaction_history=interaction_history,
            guidelines=batch,
        )

        with duration_logger("Guideline batch filtering"):
            llm_response = await self._generate_llm_response(prompt)

        proposed_guidelines_json = json.loads(llm_response)["checks"]

        logger.debug(f"Guideline filter batch result: {proposed_guidelines_json}")
        proposed_guidelines = [
            GuidelineProposition(
                guideline=batch[int(r["predicate_number"]) - 1],
                score=r["applies_score"],
                rationale=r["rationale"],
            )
            for r in proposed_guidelines_json
            if r["applies_score"] >= 8
        ]

        return proposed_guidelines

    def _format_prompt(
        self,
        context_variables: list[tuple[ContextVariable, ContextVariableValue]],
        interaction_history: list[Event],
        guidelines: list[Guideline],
    ) -> str:
        json_events = events_to_json(interaction_history)
        context_values = context_variables_to_json(context_variables)
        predicates = "\n".join(f"{i}) {g.predicate}" for i, g in enumerate(guidelines, start=1))
        prompt = """
### Definition of Predicate Relevance Evaluation:
This process involves assessing the relevance of predefined predicates 
to the last known state of an interaction between an AI assistant and a user.


Review comment (suggestion):
Rename this and others (in tests as well) to `guideline_propositions` or even simply `propositions` if the context is clear enough

Code after:
            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]:
        prompt = self._format_prompt(
            context_variables=context_variables,
            interaction_history=interaction_history,
            guidelines=batch,
        )

        with duration_logger("Guideline batch filtering"):
            llm_response = await self._generate_llm_response(prompt)

        propositions_json = json.loads(llm_response)["checks"]

        logger.debug(f"Guideline filter batch result: {propositions_json}")
        propositions = [
            GuidelineProposition(
                guideline=batch[int(p["predicate_number"]) - 1],
                score=p["applies_score"],
                rationale=p["rationale"],
            )
            for p in propositions_json
            if p["applies_score"] >= 7
        ]

        return propositions

    def _format_prompt(
        self,
        context_variables: list[tuple[ContextVariable, ContextVariableValue]],
        interaction_history: list[Event],
        guidelines: list[Guideline],
    ) -> str:
        json_events = events_to_json(interaction_history)
        context_values = context_variables_to_json(context_variables)
        predicates = "\n".join(f"{i}) {g.predicate}" for i, g in enumerate(guidelines, start=1))
        prompt = """
### Definition of Predicate Relevance Evaluation:
This process involves assessing the relevance of predefined predicates 
to the last known state of an interaction between an AI assistant and a user.