-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevaluation_agent.py
More file actions
267 lines (204 loc) · 9.09 KB
/
evaluation_agent.py
File metadata and controls
267 lines (204 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Evaluation Agent for assessing annotation faithfulness.
This agent evaluates how faithfully a HED annotation captures
the original natural language event description.
"""
import logging
import re
from pathlib import Path
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import HumanMessage, SystemMessage
from src.agents.state import HedAnnotationState
from src.utils.json_schema_loader import HedJsonSchemaLoader, load_latest_schema
logger = logging.getLogger(__name__)
class EvaluationAgent:
"""Agent that evaluates the faithfulness of HED annotations.
This agent compares the generated HED annotation against the original
description to assess completeness, accuracy, and semantic fidelity.
Also suggests schema matches for tags that might not exist.
"""
def __init__(self, llm: BaseChatModel, schema_dir: Path | str | None = None) -> None:
"""Initialize the evaluation agent.
Args:
llm: Language model for evaluation
schema_dir: Directory containing JSON schemas
"""
self.llm = llm
self.schema_dir = schema_dir
self.json_schema_loader: HedJsonSchemaLoader | None = None
def _build_system_prompt(self) -> str:
"""Build the system prompt for evaluation.
Returns:
System prompt string
"""
return """You are an expert HED annotation evaluator.
Your task is to assess how faithfully a HED annotation captures the original natural language event description.
## Evaluation Philosophy
- **Be practical, not perfectionist**: Annotations don't need to capture EVERY detail
- **Focus on core event elements**: Event type, main objects, key actions
- **Accept reasonable variations**: Multiple valid ways to annotate the same event
- **Prioritize correctness over completeness**: Better to be accurate than exhaustive
## Evaluation Criteria
### 1. Core Elements (REQUIRED)
- Event type identified (Sensory-event, Agent-action, etc.)
- Main objects/stimuli included
- Key actions captured (if any)
### 2. Semantic Grouping (CRITICAL)
Check that parentheses are used correctly to group related concepts:
**Object properties MUST be grouped together:**
- CORRECT: (Red, Circle) - A single red circle
- WRONG: Red, Circle - Ambiguous; could be two separate things
**Agent-action relationships need proper nesting:**
- CORRECT: Agent-action, ((Human-agent, Experiment-participant), (Press, (Left, Mouse-button)))
- WRONG: Agent-action, Human-agent, Experiment-participant, Press, Left, Mouse-button
**Spatial/relational patterns:**
- CORRECT: ((Red, Circle), (To-left-of, (Green, Square)))
- WRONG: Red, Circle, To-left-of, Green, Square
**Reversibility test**: Can you translate the annotation back to coherent English?
If properties are ungrouped, the meaning becomes ambiguous.
### 3. Important Details (SHOULD HAVE)
- Colors, shapes if explicitly mentioned
- Spatial relationships if specified
- Task role (Experimental-stimulus, Participant-response)
### 4. Optional Enhancements (NICE TO HAVE)
- Fine-grained attributes
- Implicit details
- Additional context
## Decision Guidelines
**ACCEPT if**:
- Core elements are present
- Semantic grouping is correct (properties grouped with objects)
- Can translate back to similar English description
**REFINE if**:
- Missing critical information (event type, main object, key action)
- **Semantic grouping errors** (ungrouped properties, flat structure)
- Contains clear errors or misrepresentations
- Would fail reversibility test (can't translate back)
## Response Format
FAITHFUL: [yes/partial/no]
GROUPING: [correct/needs-improvement]
DECISION: [ACCEPT/REFINE]
FEEDBACK:
- [Brief feedback if refinement needed, especially grouping issues]
"""
def _build_user_prompt(self, description: str, annotation: str) -> str:
"""Build the user prompt for evaluation.
Args:
description: Original natural language description
annotation: Generated HED annotation
Returns:
User prompt string
"""
return f"""Evaluate this HED annotation:
ORIGINAL DESCRIPTION:
{description}
HED ANNOTATION:
{annotation}
Provide a thorough evaluation following the specified format."""
async def evaluate(self, state: HedAnnotationState) -> dict:
"""Evaluate the faithfulness of the current annotation.
Args:
state: Current annotation workflow state
Returns:
State update with evaluation feedback
"""
# Load schema if needed (only if schema_dir provided)
if self.json_schema_loader is None and self.schema_dir is not None:
self.json_schema_loader = load_latest_schema(self.schema_dir)
# Check for potentially invalid tags and suggest matches
annotation = state["current_annotation"]
suggestions = self._check_tags_and_suggest(annotation)
# Build prompts
system_prompt = self._build_system_prompt()
user_prompt = self._build_user_prompt(
state["input_description"],
annotation,
)
# Add tag suggestions if any
if suggestions:
user_prompt += f"\n\n**Tag Suggestions**:\n{suggestions}"
# Generate evaluation
messages = [
SystemMessage(content=system_prompt),
HumanMessage(content=user_prompt),
]
try:
response = await self.llm.ainvoke(messages)
except Exception as e:
logger.error("Evaluation LLM invocation failed: %s", e, exc_info=True)
raise
content = response.content
feedback = content.strip() if isinstance(content, str) else str(content)
# Parse decision with multiple fallbacks
is_faithful = self._parse_decision(feedback)
# Update state
return {
"evaluation_feedback": feedback,
"is_faithful": is_faithful,
"messages": state.get("messages", []) + messages + [response],
}
def _parse_decision(self, feedback: str) -> bool:
"""Parse evaluation decision from LLM feedback.
Args:
feedback: LLM evaluation feedback
Returns:
True if annotation should be accepted, False if needs refinement
"""
feedback_lower = feedback.lower()
# Check for explicit DECISION line
decision_match = re.search(r"decision:\s*(accept|refine)", feedback_lower)
if decision_match:
return decision_match.group(1) == "accept"
# Check for FAITHFUL field - accept "yes" or "partial"
faithful_match = re.search(r"faithful:\s*(yes|partial|no)", feedback_lower)
if faithful_match:
result = faithful_match.group(1)
return result in ["yes", "partial"] # Accept partial as good enough!
# Fallback: look for explicit refine indicators only
refine_indicators = ["refine", "incorrect", "inaccurate", "wrong"]
if any(indicator in feedback_lower for indicator in refine_indicators):
return False
# Default to accept if ambiguous -- avoid unnecessary refinement loops
logger.debug(
"Evaluation parsing: no explicit DECISION/FAITHFUL/refine indicator found; defaulting to ACCEPT"
)
return True
def _check_tags_and_suggest(self, annotation: str) -> str:
"""Check annotation for invalid tags and suggest alternatives.
Args:
annotation: HED annotation string
Returns:
Suggestion text (empty if all tags valid)
"""
if not self.json_schema_loader:
return ""
# Extract tags from annotation (simple tokenization)
# Remove parentheses, split by comma
cleaned = annotation.replace("(", "").replace(")", "")
tags = [t.strip() for t in cleaned.split(",")]
vocabulary = set(self.json_schema_loader.get_vocabulary())
suggestions = []
for tag in tags:
# Skip empty, value placeholders, or column references
if not tag or "#" in tag or "{" in tag:
continue
# Check if tag or its base (before /) is in vocabulary
base_tag = tag.split("/")[0]
if base_tag not in vocabulary:
# Find closest matches
matches = self.json_schema_loader.find_closest_match(base_tag)
if matches:
suggestions.append(
f"- '{base_tag}' not in schema. Did you mean: {', '.join(matches)}?"
)
else:
# Check if it's a valid extension
if "/" in tag:
if self.json_schema_loader.is_extendable(base_tag):
suggestions.append(
f"- '{tag}' uses extension (dataset-specific, non-portable)"
)
else:
suggestions.append(
f"- '{base_tag}' doesn't allow extension. Use schema tag instead."
)
return "\n".join(suggestions) if suggestions else ""