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

Code before:
        }

        return [
            {
                "guideline": batch[i],
                "score": relevant_checks_by_index[i]["applies_score"],
                "rationale": relevant_checks_by_index[i]["rationale"],
            }
            for i in relevant_checks_by_index
        ]

    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.

**Objective**: Determine the applicability of each predicate to the concluding 
state of the interaction based on a given set of interactions.

**Task Description**:
1. **Input**:
"""

        if interaction_history:
            prompt += f"""\
    - Interaction Events: ###
    {json_events}
    ###
    """
        else:
            prompt += """\
    - Interaction Context: ###
    You, an AI assistant, 
    are currently engaged in the initial phase of an online session with a user.
    The interaction has just commenced, and no substantial exchange has occurred yet.
    ###
    """

        if context_variables:
            prompt += f"""
    The following is additional information available 
    about the user and the context of the interaction: ###

Review comment (suggestion):
"The concluding state" is bad phrasing. "Concluding" means "final" in this context. The word we need is "latest".

Also, the phrasing "the latest state of the interaction based on a given set of interactions" is introducing ambiguity in the definition of "interaction". Note that ambiguity is one of the worst when dealing with an LLM..!  I suggest to change to "based on a stream of of events".

Code after:
        }

        return [
            {
                "guideline": batch[i],
                "score": relevant_checks_by_index[i]["applies_score"],
                "rationale": relevant_checks_by_index[i]["rationale"],
            }
            for i in relevant_checks_by_index
        ]

    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.

**Objective**: Determine the applicability of each predicate to the latest 
state based on a stream of events.

**Task Description**:
1. **Input**:
"""

        if interaction_history:
            prompt += f"""\
    The following is a list of events describing a back-and-forth
    interaction between you, an AI assistant, and a user: ###
    {json_events}
    ###
    """
        else:
            prompt += """\
    You, an AI assistant, are now present in an online interaction session with a user.
    The session has just started, 
    and the user hasn't said anything yet nor chosen to engage with you.
    """

        if context_variables:
            prompt += f"""
    The following is additional information available 
    about the user and the context of the interaction: ###
    {context_values}