|
1 | 1 | import copy |
| 2 | +import json |
2 | 3 | import logging |
3 | 4 | import os |
4 | 5 | from dataclasses import dataclass, field |
@@ -502,61 +503,184 @@ def _format_template(self, messages: List[dict], example: dict) -> str: |
502 | 503 | INSTRUCTION_FOLLOWING_PROMPT_EN = self._import_with_fallback( |
503 | 504 | "openjudge.graders.common.instruction_following", |
504 | 505 | "INSTRUCTION_FOLLOWING_PROMPT_EN", |
505 | | - "Evaluate the instruction_following of the response to the query. Query: {query}, Response: {response}", |
| 506 | + "Evaluate the instruction following of the response to the query. Query: {query}, Response: {response}", |
| 507 | + ) |
| 508 | + |
| 509 | + ACTION_ALIGNMENT_PROMPT_EN = self._import_with_fallback( |
| 510 | + "openjudge.graders.agent.action.action_alignment", |
| 511 | + "ACTION_ALIGNMENT_PROMPT_EN", |
| 512 | + "Evaluate the action alignment of the response to the query. Query: {query}, Response: {response}", |
| 513 | + ) |
| 514 | + |
| 515 | + PLAN_FEASIBILITY_PROMPT_EN = self._import_with_fallback( |
| 516 | + "openjudge.graders.agent.plan.plan_feasibility", |
| 517 | + "PLAN_FEASIBILITY_PROMPT_EN", |
| 518 | + "Evaluate the plan feasibility of the response to the query. Query: {query}, Response: {response}", |
| 519 | + ) |
| 520 | + |
| 521 | + REFLECTION_ACCURACY_PROMPT_EN = self._import_with_fallback( |
| 522 | + "openjudge.graders.agent.reflection.reflection_accuracy", |
| 523 | + "REFLECTION_ACCURACY_PROMPT_EN", |
| 524 | + "Evaluate the reflection accuracy of the response to the query. Query: {query}, Response: {response}", |
| 525 | + ) |
| 526 | + |
| 527 | + REFLECTION_OUTCOME_UNDERSTANDING_PROMPT_EN = self._import_with_fallback( |
| 528 | + "openjudge.graders.agent.reflection.reflection_outcome_understanding", |
| 529 | + "REFLECTION_OUTCOME_UNDERSTANDING_PROMPT_EN", |
| 530 | + "Evaluate the reflection outcome understanding of the response to the query. Query: {query}, Response: {response}", |
| 531 | + ) |
| 532 | + |
| 533 | + REFLECTION_PROGRESS_AWARENESS_PROMPT_EN = self._import_with_fallback( |
| 534 | + "openjudge.graders.agent.reflection.reflection_progress_awareness", |
| 535 | + "REFLECTION_PROGRESS_AWARENESS_PROMPT_EN", |
| 536 | + "Evaluate the reflection progress awareness of the response to the query. Query: {query}, Response: {response}", |
| 537 | + ) |
| 538 | + |
| 539 | + TOOL_CALL_ACCURACY_PROMPT_EN = self._import_with_fallback( |
| 540 | + "openjudge.graders.agent.tool.tool_call_accuracy", |
| 541 | + "TOOL_CALL_ACCURACY_PROMPT_EN", |
| 542 | + "Evaluate the tool call accuracy of the response to the query. Query: {query}, Response: {response}", |
| 543 | + ) |
| 544 | + |
| 545 | + TOOL_CALL_SUCCESS_PROMPT_EN = self._import_with_fallback( |
| 546 | + "openjudge.graders.agent.tool.tool_call_success", |
| 547 | + "TOOL_CALL_SUCCESS_PROMPT_EN", |
| 548 | + "Evaluate the tool call success of the response to the query. Query: {query}, Response: {response}", |
| 549 | + ) |
| 550 | + |
| 551 | + TOOL_PARAMETER_CHECK_PROMPT_EN = self._import_with_fallback( |
| 552 | + "openjudge.graders.agent.tool.tool_parameter_check", |
| 553 | + "TOOL_PARAMETER_CHECK_PROMPT_EN", |
| 554 | + "Evaluate the tool parameter check of the response to the query. Query: {query}, Response: {response}", |
| 555 | + ) |
| 556 | + |
| 557 | + TOOL_SELECTION_PROMPT_EN = self._import_with_fallback( |
| 558 | + "openjudge.graders.agent.tool.tool_selection", |
| 559 | + "TOOL_SELECTION_PROMPT_EN", |
| 560 | + "Evaluate the tool selection of the response to the query. Query: {query}, Response: {response}", |
506 | 561 | ) |
507 | 562 |
|
508 | 563 | task_type = example.get("task_type", "unknown") |
509 | 564 |
|
510 | | - if task_type == "correctness": |
| 565 | + if "correctness" in task_type: |
511 | 566 | grader_template = CORRECTNESS_PROMPT_EN |
512 | | - elif task_type == "hallucination": |
| 567 | + elif "hallucination" in task_type: |
513 | 568 | grader_template = HALLUCINATION_PROMPT_EN |
514 | | - elif task_type == "relevance": |
| 569 | + elif "relevance" in task_type: |
515 | 570 | grader_template = RELEVANCE_PROMPT_EN |
516 | | - elif task_type == "harmlessness": |
| 571 | + elif "harmlessness" in task_type: |
517 | 572 | grader_template = HARMFULNESS_PROMPT_EN |
518 | | - elif task_type == "instruction_following": |
| 573 | + elif "instruction_following" in task_type: |
519 | 574 | grader_template = INSTRUCTION_FOLLOWING_PROMPT_EN |
| 575 | + elif "action_alignment" in task_type: |
| 576 | + grader_template = ACTION_ALIGNMENT_PROMPT_EN |
| 577 | + elif "plan_feasibility" in task_type: |
| 578 | + grader_template = PLAN_FEASIBILITY_PROMPT_EN |
| 579 | + elif "reflection_accuracy" in task_type: |
| 580 | + grader_template = REFLECTION_ACCURACY_PROMPT_EN |
| 581 | + elif "reflection_outcome_understanding" in task_type: |
| 582 | + grader_template = REFLECTION_OUTCOME_UNDERSTANDING_PROMPT_EN |
| 583 | + elif "reflection_progress_awareness" in task_type: |
| 584 | + grader_template = REFLECTION_PROGRESS_AWARENESS_PROMPT_EN |
| 585 | + elif "tool_call_accuracy" in task_type: |
| 586 | + grader_template = TOOL_CALL_ACCURACY_PROMPT_EN |
| 587 | + elif "tool_call" in task_type: |
| 588 | + grader_template = TOOL_CALL_SUCCESS_PROMPT_EN |
| 589 | + elif "tool_parameter" in task_type: |
| 590 | + grader_template = TOOL_PARAMETER_CHECK_PROMPT_EN |
| 591 | + elif "tool_selection" in task_type: |
| 592 | + grader_template = TOOL_SELECTION_PROMPT_EN |
520 | 593 | else: |
521 | 594 | # Default to correctness if unknown template |
522 | 595 | pprint(f"task type: {task_type}") |
523 | 596 | raise ValueError( |
524 | 597 | f"Unknown task type: {task_type}. Valid types: correctness, hallucination, relevance, " |
525 | | - f"harmlessness, instruction_following" |
| 598 | + f"harmlessness, instruction_following, action_alignment, plan_feasibility, reflection_accuracy, " |
| 599 | + f"reflection_outcome_understanding, reflection_progress_awareness, " |
| 600 | + f"tool_call_accuracy, tool_call_success, tool_parameter_check, tool_selection, " |
526 | 601 | ) |
527 | 602 | return self._format_grader_template(messages, example, grader_template) |
528 | 603 |
|
529 | 604 | def _format_grader_template(self, messages: List[dict], example: dict, grader_prompt: str) -> str: |
530 | 605 | """Format correctness evaluation template using openjudge prompt.""" |
| 606 | + context = "" |
| 607 | + response = "" |
| 608 | + reference_response = "" |
| 609 | + tool_calls = "" |
| 610 | + tool_definitions = "" |
| 611 | + tool_responses = "" |
| 612 | + observation = "" |
| 613 | + plan = "" |
| 614 | + history = "" |
| 615 | + memory = "" |
| 616 | + action = "" |
| 617 | + reflection = "" |
531 | 618 | if "input" in example and isinstance(example["input"], dict) and "query" in example["input"]: |
532 | 619 | # New JSON format |
533 | 620 | query = example["input"].get("query", "") |
534 | | - context = example["input"].get("context") or "" # Handle null value |
535 | | - reference_response = example["input"].get("reference", "") |
| 621 | + context = example["input"].get("context", "") |
| 622 | + if context: |
| 623 | + if isinstance(context, dict): |
| 624 | + # Extract fields directly if context is already a dictionary |
| 625 | + context = context.get("task_context", "") |
| 626 | + tool_definitions = context.get("tool_definitions", "") |
| 627 | + history = context.get("history", "") |
| 628 | + elif isinstance(context, str): |
| 629 | + try: |
| 630 | + # Attempt to parse JSON string into a dictionary |
| 631 | + parsed_data = json.loads(context) |
| 632 | + |
| 633 | + # Ensure the parsed result is actually a dictionary before accessing keys |
| 634 | + if isinstance(parsed_data, dict): |
| 635 | + context = parsed_data.get("task_context", "") |
| 636 | + tool_definitions = parsed_data.get("tool_definitions", "") |
| 637 | + history = parsed_data.get("history", "") |
| 638 | + |
| 639 | + except (json.JSONDecodeError, TypeError, Exception): |
| 640 | + # If parsing fails, continue without raising an error (keep default values) |
| 641 | + pass |
536 | 642 |
|
537 | | - response = "" |
| 643 | + reference_response = example["input"].get("reference", "") |
538 | 644 | if "answer" in example and isinstance(example["answer"], dict): |
539 | 645 | answer_response = example["answer"].get("response", {}) |
540 | 646 | if isinstance(answer_response, dict): |
541 | 647 | response = answer_response.get("content", "") |
| 648 | + tool_calls = answer_response.get("tool_calls", "") |
| 649 | + tool_responses = answer_response.get("tool_responses", "") |
| 650 | + plan = answer_response.get("plan", "") |
| 651 | + observation = answer_response.get("observation", "") |
| 652 | + memory = answer_response.get("memory", "") |
| 653 | + action = answer_response.get("action", "") |
| 654 | + reflection = answer_response.get("reflection", "") |
542 | 655 | # Also try 'response' field as fallback |
543 | 656 | elif "response" in example and isinstance(example["response"], dict): |
544 | 657 | response = example["response"].get("content", "") |
545 | 658 | else: |
546 | 659 | # Old format - extract from messages |
547 | 660 | query = next((msg["content"] for msg in messages if msg["role"] == "user"), "") |
548 | 661 | response = self._get_response_content(example) |
549 | | - reference_response = None |
550 | | - context = None |
551 | 662 |
|
552 | 663 | instruction = query |
| 664 | + available_tools = tool_definitions |
| 665 | + selected_tools = tool_calls |
553 | 666 | # Replace placeholders in the grader prompt |
554 | 667 | formatted_prompt = grader_prompt.format( |
555 | | - query=query or "", |
556 | | - response=response or "", |
557 | | - reference_response=reference_response or "", |
558 | | - context=str(context) or "", |
559 | | - instruction=instruction or "", |
| 668 | + query=query, |
| 669 | + response=response, |
| 670 | + reference_response=reference_response, |
| 671 | + context=str(context), |
| 672 | + instruction=instruction, |
| 673 | + tool_calls=str(tool_calls), |
| 674 | + tool_definitions=str(tool_definitions), |
| 675 | + tool_responses=str(tool_responses), |
| 676 | + available_tools=str(available_tools), |
| 677 | + selected_tools=str(selected_tools), |
| 678 | + history=history, |
| 679 | + observation=observation, |
| 680 | + plan=plan, |
| 681 | + memory=memory, |
| 682 | + action=action, |
| 683 | + reflection=reflection, |
560 | 684 | ) |
561 | 685 |
|
562 | 686 | return [{"role": "user", "content": formatted_prompt}] |
|
0 commit comments