|
| 1 | +"""MuSR: Testing the Limits of Chain-of-thought with Multistep Soft Reasoning |
| 2 | +
|
| 3 | +Zayne Sprague, Xi Ye, Kaj Bostrom, Swarat Chaudhuri, and Greg Durrett. |
| 4 | +https://arxiv.org/abs/2310.16049 |
| 5 | +
|
| 6 | +# Example: Eval MuSR with team_allocation and CoT+ |
| 7 | +inspect eval musr.py -T domain=team_allocation -T prompt_technique=cot+ |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +import ast |
| 12 | +from typing import Any, Dict, Literal |
| 13 | + |
| 14 | +from inspect_ai import Task, task |
| 15 | +from inspect_ai.dataset import Sample, hf_dataset |
| 16 | +from inspect_ai.scorer import choice |
| 17 | +from inspect_ai.solver import multiple_choice, system_message |
| 18 | + |
| 19 | +from inspect_evals.musr.prompts import ( |
| 20 | + COT_PLUS_PROMPT, |
| 21 | + COT_PROMPT, |
| 22 | + MURDER_MYSTERIES_EXAMPLE, |
| 23 | + MURDER_MYSTERIES_HINT, |
| 24 | + OBJECT_PLACEMENTS_EXAMPLE, |
| 25 | + OBJECT_PLACEMENTS_HINT, |
| 26 | + REGULAR_PROMPT, |
| 27 | + SYSTEM_PROMPT, |
| 28 | + TEAM_ALLOCATION_EXAMPLE, |
| 29 | + TEAM_ALLOCATION_HINT, |
| 30 | +) |
| 31 | + |
| 32 | +DomainType = Literal["murder_mysteries", "object_placements", "team_allocation"] |
| 33 | +PromptTechniqueType = Literal["regular", "cot", "cot+"] |
| 34 | + |
| 35 | +DEFAULT_DOMAIN: DomainType = "murder_mysteries" |
| 36 | +DEFAULT_PROMPT_TECHNIQUE: PromptTechniqueType = "regular" |
| 37 | +DEFAULT_EXAMPLE_COUNT = 0 |
| 38 | + |
| 39 | + |
| 40 | +@task |
| 41 | +def musr( |
| 42 | + domain: DomainType = DEFAULT_DOMAIN, |
| 43 | + prompt_technique: PromptTechniqueType = DEFAULT_PROMPT_TECHNIQUE, |
| 44 | + example_count: int = DEFAULT_EXAMPLE_COUNT, |
| 45 | +) -> Task: |
| 46 | + """Inspect task implementing the MuSR benchmark. |
| 47 | +
|
| 48 | + Args: |
| 49 | + domain (Literal["murder_mysteries", "object_placements", "team_allocation"]): Which domain in the dataset to evaluate. |
| 50 | + Defaults to "murder_mysteries". |
| 51 | + prompt_technique (Literal["regular", "cot", "cot+"]): The prompt technique to use. "regular" includes only the narrative |
| 52 | + and the question. "cot" uses chain-of-thought prompting. "cot+" includes a hint. Defaults to "regular". |
| 53 | + example_count (int): Number of solved examples to include at the beginning of each prompt. Defaults to 0. Currently only supports 1 example. |
| 54 | + """ |
| 55 | + prompt = get_domain_prompt(domain, prompt_technique, example_count) |
| 56 | + |
| 57 | + dataset = hf_dataset( |
| 58 | + path="TAUR-Lab/MuSR", |
| 59 | + split=domain, |
| 60 | + sample_fields=record_to_sample, |
| 61 | + shuffle=True, |
| 62 | + auto_id=True, |
| 63 | + ) |
| 64 | + |
| 65 | + return Task( |
| 66 | + dataset=dataset, |
| 67 | + solver=[ |
| 68 | + system_message(SYSTEM_PROMPT), |
| 69 | + multiple_choice(template=prompt), |
| 70 | + ], |
| 71 | + scorer=choice(), |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def get_domain_prompt( |
| 76 | + domain: DomainType = DEFAULT_DOMAIN, |
| 77 | + prompt_technique: PromptTechniqueType = DEFAULT_PROMPT_TECHNIQUE, |
| 78 | + example_count: int = DEFAULT_EXAMPLE_COUNT, |
| 79 | +) -> str: |
| 80 | + domain_info = { |
| 81 | + "murder_mysteries": { |
| 82 | + "hint": MURDER_MYSTERIES_HINT, |
| 83 | + "example": MURDER_MYSTERIES_EXAMPLE, |
| 84 | + }, |
| 85 | + "object_placements": { |
| 86 | + "hint": OBJECT_PLACEMENTS_HINT, |
| 87 | + "example": OBJECT_PLACEMENTS_EXAMPLE, |
| 88 | + }, |
| 89 | + "team_allocation": { |
| 90 | + "hint": TEAM_ALLOCATION_HINT, |
| 91 | + "example": TEAM_ALLOCATION_EXAMPLE, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + if domain not in domain_info: |
| 96 | + raise ValueError( |
| 97 | + "Unknown domain. Valid domains are murder_mysteries (default), object_placements, and team_allocation" |
| 98 | + ) |
| 99 | + |
| 100 | + if prompt_technique == "regular": |
| 101 | + prompt = REGULAR_PROMPT |
| 102 | + elif prompt_technique == "cot": |
| 103 | + prompt = COT_PROMPT |
| 104 | + elif prompt_technique == "cot+": |
| 105 | + prompt = COT_PLUS_PROMPT.format(hint=domain_info[domain]["hint"]) |
| 106 | + else: |
| 107 | + raise ValueError( |
| 108 | + "Unknown prompt technique. Valid prompt techniques are regular (default), cot, and cot+." |
| 109 | + ) |
| 110 | + |
| 111 | + if example_count > 1: |
| 112 | + raise ValueError(">1 examples currently not supported") |
| 113 | + if example_count == 1: |
| 114 | + return f'Here is an example of solving the task:\n\n{domain_info[domain]["example"]}\n\nThis is the end of the example. The real task is below.\n\n---\n\n{prompt}' |
| 115 | + else: |
| 116 | + return prompt |
| 117 | + |
| 118 | + |
| 119 | +def record_to_sample(record: Dict[str, Any]) -> Sample: |
| 120 | + return Sample( |
| 121 | + input=f'{record["narrative"]}\n\n{record["question"]}', |
| 122 | + choices=ast.literal_eval(record["choices"]), |
| 123 | + target=chr(ord("A") + int(record["answer_index"])), |
| 124 | + ) |
0 commit comments