|
| 1 | +from typing import Literal |
| 2 | + |
| 3 | +import tiktoken |
| 4 | +from inspect_ai import Task, task |
| 5 | +from inspect_ai.model import get_model |
| 6 | +from inspect_ai.solver import ( |
| 7 | + Generate, |
| 8 | + Solver, |
| 9 | + TaskState, |
| 10 | + generate, |
| 11 | + prompt_template, |
| 12 | + solver, |
| 13 | +) |
| 14 | +from tiktoken.core import Encoding |
| 15 | + |
| 16 | +from inspect_evals.niah.utils.dataset_generation import ( |
| 17 | + ExpParams, |
| 18 | + Haystack, |
| 19 | + generate_full_context, |
| 20 | + get_data_from_hf, |
| 21 | +) |
| 22 | +from inspect_evals.niah.utils.prompting import ( |
| 23 | + MAIN_PROMPT, |
| 24 | + QUESTION_PROMPT, |
| 25 | + create_question_prompt, |
| 26 | +) |
| 27 | +from inspect_evals.niah.utils.sample_generation import ( |
| 28 | + generate_samples, |
| 29 | + needle_into_haystack, |
| 30 | + sample_df_to_dataset, |
| 31 | +) |
| 32 | +from inspect_evals.niah.utils.scoring import ( |
| 33 | + custom_model_graded_qa_with_history_scorer, |
| 34 | + return_metadata_variable_as_history, |
| 35 | +) |
| 36 | +from inspect_evals.niah.utils.text_utils import get_model_or_default |
| 37 | + |
| 38 | +# Define a token buffer for max context to avoid potential edge case issues with long outputs |
| 39 | +TOKEN_BUFFER = 100 |
| 40 | + |
| 41 | + |
| 42 | +@task |
| 43 | +def niah( |
| 44 | + min_context: int = 10000, |
| 45 | + max_context: int = 120000, |
| 46 | + n_contexts: int = 15, |
| 47 | + n_positions: int = 15, |
| 48 | + start_buffer: int = 0, |
| 49 | + end_buffer: int = 0, |
| 50 | + n_needles: int = 1, |
| 51 | + sample_method: Literal["fixed", "sequential", "random"] = "fixed", |
| 52 | + fixed_index: int = 0, |
| 53 | + n_runs: int = 1, |
| 54 | +) -> Task: |
| 55 | + """ |
| 56 | + Inspect Task implementation for NIAH (Needle in a Haystack). |
| 57 | +
|
| 58 | + This function generates a task that evaluates the model on a dataset with varying context sizes and needle positions. |
| 59 | + Needles are inserted into the context to measure the model's ability to retrieve relevant information. |
| 60 | +
|
| 61 | + Args: |
| 62 | + min_context (int): Minimum context length to evaluate. Default is 10000. |
| 63 | + max_context (int): Maximum context length to evaluate. Default is 120000. |
| 64 | + n_contexts (int): The number of contexts to evaluate. Default is 15. |
| 65 | + n_positions (int): The number of positions to evaluate for a given context length. Default is 15. |
| 66 | + start_buffer (int): Buffer at the top of the context to avoid placing needles. Default is 0. |
| 67 | + end_buffer (int): Buffer at the bottom of the context to avoid placing needles. Default is 0. |
| 68 | + n_needles (int): The number of needles to sample. Default is 1. |
| 69 | + sample_method (Literal["fixed", "random"]): Method for sampling the needles. |
| 70 | + If "fixed", a single specific needle index is used for all trials. |
| 71 | + If "random", a new needle is randomly sampled for each trial. |
| 72 | + If "sequential", needles are sequentially sampled across trials. |
| 73 | + Default is "fixed". |
| 74 | + fixed_index (int): The index of the needle to use when `sample_method` is "fixed" or |
| 75 | + the index of the starting position when `sample_method` is "sequential". Default is 0. |
| 76 | + n_runs (int): The number of runs per set of experimental parameters. Default is 1. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + Task: A Task object containing the dataset, the solver configuration, and a custom scorer with metadata handling. |
| 80 | + """ |
| 81 | + # Get the active model |
| 82 | + model = get_model() |
| 83 | + |
| 84 | + # Use default model name for tokenisation if no tokeniser found for current model |
| 85 | + tokeniser_model_name = get_model_or_default(model.name) |
| 86 | + |
| 87 | + # Create an encoder for given model |
| 88 | + enc = tiktoken.encoding_for_model(tokeniser_model_name) |
| 89 | + |
| 90 | + # Import OpenCompass 'Needle in a Haystack' dataset from HF |
| 91 | + hf_data = get_data_from_hf() |
| 92 | + |
| 93 | + # Generate ExpParams object for storing experimental parameters. |
| 94 | + exp_params = ExpParams( |
| 95 | + min_context=min_context, |
| 96 | + max_context=max_context, |
| 97 | + n_contexts=n_contexts, |
| 98 | + n_positions=n_positions, |
| 99 | + start_buffer=start_buffer, |
| 100 | + end_buffer=end_buffer, |
| 101 | + n_needles=n_needles, |
| 102 | + sample_method=sample_method, |
| 103 | + fixed_index=fixed_index, |
| 104 | + n_runs=n_runs, |
| 105 | + main_prompt=MAIN_PROMPT, |
| 106 | + question_prompt=QUESTION_PROMPT, |
| 107 | + token_buffer=TOKEN_BUFFER, |
| 108 | + model_name=model.name, |
| 109 | + ) |
| 110 | + |
| 111 | + # Generate the haystack for the largest context length required. Smaller context lengths will trim haystack. |
| 112 | + haystack = generate_full_context(hf_data, exp_params, enc) |
| 113 | + |
| 114 | + # Generate a DataFrame with Sample information |
| 115 | + samples_df = generate_samples(hf_data, haystack, exp_params, enc) |
| 116 | + |
| 117 | + # Convert Sample DataFrame to Dataset |
| 118 | + dataset = sample_df_to_dataset(samples_df) |
| 119 | + |
| 120 | + # Return the Task |
| 121 | + return Task( |
| 122 | + dataset=dataset, |
| 123 | + solver=[ |
| 124 | + add_to_haystack( |
| 125 | + haystack, enc |
| 126 | + ), # Take needle and other information from Sample to generate combined haystack and needle text. |
| 127 | + prompt_template(MAIN_PROMPT), |
| 128 | + generate(), |
| 129 | + ], |
| 130 | + # Custom wrapper used to allow for grouped scoring and parsing metadata to scorer |
| 131 | + scorer=custom_model_graded_qa_with_history_scorer( |
| 132 | + include_history=return_metadata_variable_as_history, |
| 133 | + ), |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +@solver |
| 138 | +def add_to_haystack(haystack: Haystack, enc: Encoding) -> Solver: |
| 139 | + """ |
| 140 | + Custom solver function. |
| 141 | +
|
| 142 | + Inserts a specified prompt (needle) into a larger context (haystack) string based on provided Sample metadata parameters |
| 143 | +
|
| 144 | + Args: |
| 145 | + haystack (Haystack): Haystack object containing complete context (haystack) in which the needle will be embedded. |
| 146 | + enc (Encoding): The tokeniser encoding object, used to convert text to tokens. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + Solver: An asynchronous solver function that takes `TaskState` and `Generate` |
| 150 | + and returns an updated `TaskState` with prompt text. |
| 151 | + """ |
| 152 | + |
| 153 | + async def solve(state: TaskState, generate: Generate) -> TaskState: |
| 154 | + prompt = state.user_prompt |
| 155 | + metadata = state.metadata |
| 156 | + full_context = needle_into_haystack( |
| 157 | + haystack.encoded_context, |
| 158 | + prompt.text, |
| 159 | + metadata["haystack_length"], |
| 160 | + metadata["position"], |
| 161 | + enc, |
| 162 | + ) |
| 163 | + prompt.text = create_question_prompt(full_context, metadata["needle_question"]) |
| 164 | + return state |
| 165 | + |
| 166 | + return solve |
0 commit comments