in src/dfcx_scrapi/tools/evaluations.py [0:0]
def pair_tool_calls(self, df: pd.DataFrame) -> pd.DataFrame:
"""Pairs user utterances with indices of relevant tool invocations."""
df["tool_pair"] = pd.Series(dtype="string")
grouped = df.groupby("eval_id")
for _, group in grouped:
tool_indices = []
last_user_utterance_index = None
for index, row in group.iterrows():
if row["action_type"] == "User Utterance":
# Assign accumulated tool indices to
# the *previous* user utterance (if any)
if last_user_utterance_index is not None:
df.loc[last_user_utterance_index, "tool_pair"] = (
str(tool_indices)
)
# Reset for the current user utterance:
tool_indices = []
last_user_utterance_index = index
elif row["action_type"] == "Tool Invocation":
tool_indices.append(index)
# After processing the group, assign any remaining
# tool indices to the last user utterance
if last_user_utterance_index is not None and tool_indices:
df.loc[last_user_utterance_index, "tool_pair"] = (
str(tool_indices)
)
return df