generated from opentensor/bittensor-subnet-template
-
Notifications
You must be signed in to change notification settings - Fork 64
Features/generic instruction task #202
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aa833b3
Add generic instruction task
bkb2135 9bd93ff
Add generic instruction dataset
bkb2135 8241395
Add Generic Instruction
bkb2135 e04af62
Fix Unit Tests
bkb2135 a7d2906
Update config
steffencruz 4a3aa9c
Add back reference prompt
steffencruz f1fb5e1
Make info['source'] a string
steffencruz 05351fb
Rename generic instruction to generic
steffencruz d5ba6f3
Update Config
steffencruz 50d5e94
Update validator.py
bkb2135 1b75f13
Remove mock task from config
bkb2135 350716b
Update config.py
bkb2135 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| from .tasks import Task, MockTask, SummarizationTask, QuestionAnsweringTask, DebuggingTask, MathTask, DateQuestionAnsweringTask | ||
| from .tools import MockDataset, WikiDataset, HFCodingDataset, StackOverflowDataset, MathDataset, WikiDateDataset | ||
| from .tasks import Task, MockTask, SummarizationTask, QuestionAnsweringTask, DebuggingTask, MathTask, DateQuestionAnsweringTask, GenericInstructionTask | ||
| from .tools import MockDataset, WikiDataset, HFCodingDataset, StackOverflowDataset, MathDataset, WikiDateDataset, GenericInstructionDataset | ||
|
|
||
| # TODO: Expand this to include extra information beyond just the task and dataset names | ||
| mock_task, mock_dataset = MockTask.name, [MockDataset.name] | ||
| summarization_task, summarization_dataset = SummarizationTask.name, [WikiDataset.name] | ||
| qa_task, qa_dataset = QuestionAnsweringTask.name, [WikiDataset.name] | ||
| debugging_task, debugging_dataset = DebuggingTask.name, [HFCodingDataset.name] | ||
| #debugging_task, debugging_dataset = DebuggingTask.name, [HFCodingDataset.name] | ||
| math_task, math_dataset = MathTask.name, [MathDataset.name] | ||
| date_qa_task, date_qa_dataset = DateQuestionAnsweringTask.name, [WikiDateDataset.name] | ||
| generic_instruction_task, generic_instruction_dataset = GenericInstructionTask.name, [GenericInstructionDataset.name] | ||
|
|
||
| TASK_REGISTRY = { | ||
| mock_task: mock_dataset, | ||
| summarization_task: summarization_dataset, | ||
| qa_task: qa_dataset, | ||
| debugging_task: debugging_dataset, | ||
| #debugging_task: debugging_dataset, | ||
| math_task: math_dataset, | ||
| date_qa_task: date_qa_dataset, | ||
| generic_instruction_task: generic_instruction_dataset | ||
| } |
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 |
|---|---|---|
| @@ -1,141 +1,46 @@ | ||
| import re | ||
| import bittensor as bt | ||
| from dataclasses import dataclass | ||
| from tenacity import retry, stop_after_attempt | ||
| from prompting.tasks import Task | ||
| from typing import Tuple | ||
|
|
||
| CRITERIA_GENERATION_PROMPT = """\ | ||
| We are brainstorming criteria with which to grade a language model on its responses in | ||
| diverse situations. | ||
| A ‘criteria‘ is some useful, real-world objective, and associated rubric for scores 1-5, that | ||
| tests a capability. | ||
|
|
||
| Please brainstorm a new criteria and scoring rubrics. | ||
| Be creative and create new but useful criteria that people in different settings or industries | ||
| might find practical. | ||
| Please format the output as same as the above examples with no extra or surrounding text. | ||
| Write [END] after you are done. | ||
| New Criteria: | ||
| QUERY_PROMPT_TEMPLATE = """\ | ||
| You are a question-generating expert, focusing on delivering comprehensive and accurate questions with depth and clarity. Your response contains only the question, nothing more, nothing less. You will adhere to a word limit of 100 words. | ||
| {context} | ||
| """ | ||
|
|
||
| REFERENCE_PROMPT_TEMPLATE = """\ | ||
| Answer the following question. | ||
|
|
||
| INSTRUCTION_GENERATION_PROMPT = """\ | ||
| Your job is to generate a new novel problem and a response that is related to the given score | ||
| rubric. | ||
| The score rubric: | ||
| {CRITERIA} | ||
| * Problem | ||
| - The problem should inherently be related to the score criteria and score rubric given above. | ||
| Specifically, the score criteria should be the core attributes required to solve the problem. | ||
| - The problem itself should not be too generic or easy to solve. | ||
| - If the score rubric is related to logical abilities, generate problems that require math or | ||
| coding abilities. | ||
| - Try to make the person who might solve the problem not notice the existence of the score | ||
| rubric by not explicitly mentioning it, and also provide additional inputs and options if | ||
| needed. | ||
| - Assume a situation where a user is interacting with an AI model. The user would try to | ||
| ask in a first-person point of view, but not using terms like ”I”, ”A User” or ”You” in the | ||
| first sentence. | ||
| - Do not give a role to the AI, assume that the user is asking a question from his point of | ||
| view. | ||
| - Do not include any phrase related to AI model in the problem. | ||
| * Response | ||
| - The response should be a response that would get a score of 5 from the score rubric. | ||
| - The response should be as detailed as possible unless the score rubric is related to | ||
| conciseness or brevity. It should consist of multiple paragraphs, a list of items, or a | ||
| step-by-step reasoning process. | ||
| - The response should look like how a well-prompted GPT-4 would normally answer your | ||
| problem. | ||
| * Format | ||
| - DO NOT WRITE ANY GREETING MESSAGES, just write the problem and response | ||
| only. | ||
| - In front of the problem, append the phrase ”Problem:” and in front of the response, append | ||
| the phrase ”Response:”. | ||
| - Write in the order of ”Problem” - ”Response”, where the two items are separated by the | ||
| phrase ”[NEXT]”. | ||
| - Write [END] after you are done. | ||
| Data Generation: | ||
| """ | ||
| # Question: | ||
| {query}""" | ||
|
|
||
|
|
||
| @dataclass | ||
| class GenericInstructionTask(Task): | ||
| challenge_type = 'query' | ||
| name = "generic" | ||
| desc = "get help on answering a general instruction" | ||
| goal = "to get the answer to the following instruction" | ||
|
|
||
| reward_definition = [ | ||
| dict(name="rouge", ngram="rouge-1", metric="f", weight=1.0), | ||
| dict(name="relevance", threshold=None, weight=1.0), | ||
| ] | ||
| penalty_definition = [ | ||
| dict(name="rouge", ngram="rouge-1", metric="f", weight=0.5), | ||
| ] | ||
|
|
||
| def __init__(self, llm_pipeline): | ||
| super().__init__( | ||
| name="generic_instruction", | ||
| goal="to get the answer to a instruction", | ||
| delimiter="```", | ||
| reward_types=[ | ||
| "CRITERIA_REWARD", | ||
| ], | ||
| reward_threshold=0.5, | ||
| use_challenge_as_prompt=True, | ||
| desc="", | ||
| topics={}, | ||
| topic="", | ||
| subtopic="", | ||
| challenge="", | ||
| reference="", | ||
| criteria="", | ||
| ) | ||
|
|
||
| self.criteria = self.create_criteria(llm_pipeline) | ||
| instruction, reference = self.create_instruction_and_reference(llm_pipeline) | ||
| self.challenge = instruction | ||
| self.reference = reference | ||
|
|
||
| def extract_instruction_and_reference_from_text(self, text: str) -> Tuple[str, str]: | ||
| # Split the text into problem and response using regular expression | ||
| split_text = re.split(r"\nResponse:\n", text) | ||
|
|
||
| # Extract problem and response | ||
| problem = split_text[0].strip() | ||
| response = split_text[1].strip() | ||
|
|
||
| return problem, response | ||
|
|
||
| def create_criteria(self, llm) -> str: | ||
| bt.logging.debug("🎲 Creating a generic criteria-scoring rubric ...") | ||
|
|
||
| # Generate a score rubric with defined criterias | ||
| criteria_generation_response = llm(CRITERIA_GENERATION_PROMPT) | ||
| return criteria_generation_response | ||
|
|
||
| @retry(stop=stop_after_attempt(5)) | ||
| def create_instruction_and_reference(self, llm) -> Tuple[str, str]: | ||
| try: | ||
| bt.logging.debug("📋 🎯 Creating instruction and referece text...") | ||
| cleaning_pipeline = [ | ||
| dict(name="remove_quotes"), | ||
| dict(name="prune_ending"), | ||
| dict(name="remove_roles"), | ||
| ] | ||
|
|
||
| if not self.criteria: | ||
| raise ValueError( | ||
| "Criteria must be defined before creating a generic instruction." | ||
| ) | ||
| def __init__(self, llm_pipeline, context, create_reference=True): | ||
| self.context = context | ||
|
|
||
| # Create generic instruction with the score rubric | ||
| instruction_generation_prompt_with_criteria = ( | ||
| INSTRUCTION_GENERATION_PROMPT.format(CRITERIA=self.criteria) | ||
| ) | ||
| instruction_generation_response = llm( | ||
| instruction_generation_prompt_with_criteria | ||
| ) | ||
| self.query_prompt = QUERY_PROMPT_TEMPLATE.format(context=context.content) | ||
| self.query = self.generate_query(llm_pipeline) | ||
|
|
||
| # Extract generic instruction and reference response from the generated text | ||
| ( | ||
| instruction, | ||
| reference, | ||
| ) = self.extract_instruction_and_reference_from_text( | ||
| instruction_generation_response | ||
| ) | ||
| self.reference_prompt = REFERENCE_PROMPT_TEMPLATE.format(query = self.query) | ||
| if create_reference: | ||
| self.reference = self.generate_reference(llm_pipeline) | ||
|
|
||
| return instruction, reference | ||
| except Exception as e: | ||
| bt.logging.error( | ||
| f"Failed to create instruction and reference text: {e}, retrying..." | ||
| ) | ||
| raise e | ||
| self.topic = context.title | ||
| self.subtopic = context.topic | ||
| self.tags = context.tags |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.