-
Notifications
You must be signed in to change notification settings - Fork 56
add passing precomputed answers for eval #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
morganmcg1
wants to merge
4
commits into
main
Choose a base branch
from
add_passing_precomputed_answers_for_eval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e467245
update wandbot Evaluation datasets to dataset version with an "index"…
morganmcg1 9c7939d
add readme instructions how to pass precomputed answers
morganmcg1 ceca3ac
add ability to pass precomputed answers json file and refactor eval s…
morganmcg1 337b766
Trial Claude 4 Opus
morganmcg1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import json | ||
| import logging | ||
| from typing import Dict, List, Optional, Any | ||
|
|
||
| import weave | ||
|
|
||
| # From eval.py | ||
| def sanitize_precomputed_item_recursive(item: Any) -> Any: | ||
| """Recursively sanitize an item by converting None values to empty strings.""" | ||
| if isinstance(item, dict): | ||
| return {k: sanitize_precomputed_item_recursive(v) for k, v in item.items()} | ||
| elif isinstance(item, list): | ||
| return [sanitize_precomputed_item_recursive(elem) for elem in item] | ||
| elif item is None: | ||
| return "" | ||
| return item | ||
|
|
||
|
|
||
| def load_and_prepare_precomputed_data( | ||
| file_path: Optional[str], logger: logging.Logger | ||
| ) -> Optional[Dict[str, Dict]]: | ||
| """Loads, sanitizes, and prepares precomputed answers from a JSON file into a map.""" | ||
| if not file_path: | ||
| return None | ||
|
|
||
| logger.info(f"Loading precomputed answers from: {file_path}") | ||
| try: | ||
| with open(file_path, "r") as f: | ||
| loaded_answers_raw = json.load(f) | ||
|
|
||
| if not isinstance(loaded_answers_raw, list): | ||
| raise ValueError("Precomputed answers JSON must be a list of items.") | ||
|
|
||
| loaded_answers_sanitized = [] | ||
| for raw_item in loaded_answers_raw: | ||
| if not isinstance(raw_item, dict): | ||
| raise ValueError(f"Skipping non-dictionary item in precomputed answers: {raw_item}") | ||
| sanitized_item = sanitize_precomputed_item_recursive(raw_item) | ||
| loaded_answers_sanitized.append(sanitized_item) | ||
| logger.debug(f"Sanitized precomputed item: {sanitized_item}") | ||
|
|
||
| precomputed_answers_map = {} | ||
| for i, item in enumerate(loaded_answers_sanitized): | ||
| if not isinstance(item, dict): | ||
| raise ValueError( | ||
| f"Item at original index {i} in precomputed answers (post-sanitization) is not a dictionary." | ||
| ) | ||
|
|
||
| item_index_str: str | ||
| raw_item_index = item.get("index") | ||
|
|
||
| if raw_item_index is None or str(raw_item_index).strip() == "": | ||
| logger.warning( | ||
| f"Item (original index {i}) is missing 'index' or index is empty after sanitization. " | ||
| f"Content: {str(item.get('question', 'N/A'))[:50]+'...'}. Using list index {i} as fallback string key." | ||
| ) | ||
| item_index_str = str(i) | ||
| else: | ||
| item_index_str = str(raw_item_index).strip() | ||
| if not item_index_str: | ||
| logger.warning( | ||
| f"Item (original index {i}) had whitespace-only 'index' after sanitization. " | ||
| f"Content: {str(item.get('question', 'N/A'))[:50]+'...'}. Using list index {i} as fallback string key." | ||
| ) | ||
| item_index_str = str(i) | ||
|
|
||
| if item_index_str in precomputed_answers_map: | ||
| logger.warning( | ||
| f"Duplicate string index '{item_index_str}' found in precomputed answers. " | ||
| f"Overwriting with item from original list at index {i}." | ||
| ) | ||
| precomputed_answers_map[item_index_str] = item | ||
|
|
||
| logger.info( | ||
| f"Loaded {len(precomputed_answers_map)} precomputed answers into map from {len(loaded_answers_sanitized)} sanitized items." | ||
| ) | ||
| return precomputed_answers_map | ||
|
|
||
| except FileNotFoundError: | ||
| logger.error(f"Precomputed answers JSON file not found: {file_path}") | ||
| raise | ||
| except ValueError as e: | ||
| logger.error(f"Invalid format in precomputed answers JSON: {e}") | ||
| raise | ||
| except Exception as e: | ||
| logger.error(f"Failed to load or parse precomputed answers JSON: {e}") | ||
| raise | ||
|
|
||
|
|
||
| def load_and_prepare_dataset_rows( | ||
| dataset_ref_uri: str, is_debug: bool, n_debug_samples: int, logger: logging.Logger | ||
| ) -> List[Dict]: | ||
| """Loads dataset rows from a Weave reference, applies debug sampling, and prepares them for evaluation.""" | ||
| dataset_ref = weave.ref(dataset_ref_uri).get() | ||
| question_rows = dataset_ref.rows | ||
|
|
||
| if is_debug: | ||
| question_rows = question_rows[:n_debug_samples] | ||
|
|
||
| question_rows_for_eval = [] | ||
| for i, row in enumerate(question_rows): | ||
| if not isinstance(row, dict): | ||
| logger.warning(f"Dataset item at original index {i} is not a dictionary, skipping: {row}") | ||
| continue | ||
|
|
||
| dataset_row_index_str: str | ||
| raw_dataset_index = row.get("index") | ||
| if raw_dataset_index is None or str(raw_dataset_index).strip() == "": | ||
| logger.warning( | ||
| f"Dataset item (original list index {i}, question: {str(row.get('question', 'N/A'))[:50] + '...'}) " | ||
| f"is missing 'index' or index is empty. Using list index {i} as fallback string key." | ||
| ) | ||
| dataset_row_index_str = str(i) | ||
| else: | ||
| dataset_row_index_str = str(raw_dataset_index).strip() | ||
|
|
||
| question = row.get("question") | ||
| ground_truth = row.get("answer") | ||
| notes = row.get("notes") | ||
|
|
||
| if question is None: | ||
| logger.warning(f"Dataset item at index {dataset_row_index_str} is missing 'question'. Using empty string.") | ||
| question = "" | ||
| if ground_truth is None: | ||
| logger.warning(f"Dataset item at index {dataset_row_index_str} is missing 'answer'. Using empty string.") | ||
| ground_truth = "" | ||
| if notes is None: | ||
| logger.warning(f"Dataset item at index {dataset_row_index_str} is missing 'notes'. Using empty string.") | ||
| notes = "" | ||
|
|
||
| question_rows_for_eval.append( | ||
| { | ||
| "index": dataset_row_index_str, | ||
| "question": str(question), | ||
| "ground_truth": str(ground_truth), | ||
| "notes": str(notes), | ||
| } | ||
| ) | ||
| return question_rows_for_eval | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider failing fast on duplicate indices
When the same
indexappears twice, the later entry silently overwrites the former.For evaluation reproducibility it’s usually safer to raise, or at least surface a stronger warning, because duplicates often indicate a bug in the pre-computed file.
📝 Committable suggestion
🤖 Prompt for AI Agents