|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from scipy import optimize |
| 5 | + |
| 6 | + |
| 7 | +class Task(Enum): |
| 8 | + RANKING = 0 |
| 9 | + ANSWER = 1 |
| 10 | + PROMPT = 2 |
| 11 | + VOTE = 3 |
| 12 | + |
| 13 | + |
| 14 | +def task_selection( |
| 15 | + num_ranking_tasks: int, current_prompts: int, target_num_prompts: int, p: float, answers_per_prompt: int |
| 16 | +) -> Task: |
| 17 | + """ |
| 18 | + This computes which task to serve to the user. |
| 19 | + In general, this method aims to get rankable tasks out of the active pool ASAP. |
| 20 | + Before checking anything else, we first have a p% probability of running a ranking task. |
| 21 | + After that, we can dynamically determine which task to serve by balancing the number of active tasks. |
| 22 | +
|
| 23 | + Parameters: |
| 24 | + num_ranking_tasks (int): number of prompts that are ready to do ranking (i.e. have "answers_per_prompt" many answers) |
| 25 | + current_prompts (int): how many prompts are currently in the active pool |
| 26 | + target_num_prompts (int): how many prompts _should_ be in the active pool |
| 27 | + p (float): probability to serve a ranking task, if one is available |
| 28 | + answers_per_prompt (int): number of answers we want to have per prompt |
| 29 | + Returns: |
| 30 | + task (Task): the task Enum that corresponds to one of the four tasks |
| 31 | + """ |
| 32 | + if num_ranking_tasks > 0 and np.random.rand() < p: |
| 33 | + return Task.RANKING |
| 34 | + rate = 50 / (current_prompts * 2) |
| 35 | + prob_prompt_task = 0.5 + (target_num_prompts - current_prompts) * rate |
| 36 | + # Yes, I'm too lazy to solve this analytically... |
| 37 | + prob_unfinished_prompt = optimize.linprog( |
| 38 | + np.array([1, 1]), A_eq=np.array([[1, 1], [1, -answers_per_prompt]]), b_eq=np.array([1, 0]), bounds=(0, None) |
| 39 | + ).x[0] |
| 40 | + if np.random.rand() < prob_prompt_task: |
| 41 | + if np.random.rand() < prob_unfinished_prompt: |
| 42 | + return Task.ANSWER |
| 43 | + else: |
| 44 | + return Task.PROMPT |
| 45 | + else: |
| 46 | + return Task.VOTE |
| 47 | + |
| 48 | + |
| 49 | +def next_answer_task(possible_prompts, answers_per_prompt): |
| 50 | + """ |
| 51 | + If the `task_selection`method returns "answer", you can use this method to decide which |
| 52 | + prompt should get an answer next. |
| 53 | + The goal of this is to finish off the prompts that have almost enough answers collected already: |
| 54 | + I.e. if we want 5 answers, this is going to give preferential sampling to those prompts that already |
| 55 | + have 4/5 answers. |
| 56 | + This helps to not have too much close-to-finished prompts in the active set. |
| 57 | +
|
| 58 | + Parameters: |
| 59 | + possible_prompts (dict[prompt_id, num_answers]): a dictonary containing all open prompts and the number of answers these prompts currently have. |
| 60 | + answers_per_prompt (int): number of answers we per prompt to target |
| 61 | + Returns: |
| 62 | + prompt_id (int): the prompt_id corresponding to the next prompt that should get a new answer |
| 63 | + """ |
| 64 | + nums = list(set(possible_prompts.values())) |
| 65 | + p = np.array([max(x / answers_per_prompt, 1 / answers_per_prompt) for x in nums]) |
| 66 | + idx = np.random.choice(nums, p=p / p.sum()) |
| 67 | + sample = np.random.choice([k for k, v in possible_prompts.items() if v == idx]) |
| 68 | + return sample |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + x = task_selection(1, 500, 1000, 0.1, 5) |
| 73 | + print(x) |
| 74 | + y = next_answer_task({"this": 2, "is": 4, "a": 1, "test": 4}, 5) |
| 75 | + print(y) |
0 commit comments