-
Notifications
You must be signed in to change notification settings - Fork 3
Fix/opro #38
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
Fix/opro #38
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c12ab62
v1.3.1 (#37)
mo374z 271119f
opro reimplementation according to the paper
mo374z 3dc9b87
fine opro implementation
mo374z 89705c7
opro test scripts alignment
mo374z dc43e37
implement opro review
mo374z 8914b87
small fix in score handling
mo374z 6784ab9
Merge branch 'dev' into fix/opro
finitearth 6317df9
adjust hyperparameters
mo374z 362d649
Merge branch 'fix/opro' of https://github.com/finitearth/promptolutio…
mo374z e9625ce
add early stopping at convergence to opro
mo374z c72feba
Update promptolution/optimizers/opro.py
finitearth 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Test run for the Opro optimizer.""" | ||
|
|
||
| import argparse | ||
| import random | ||
| from logging import Logger | ||
|
|
||
| from promptolution.callbacks import LoggerCallback, CSVCallback, TokenCountCallback | ||
| from promptolution.templates import OPRO_TEMPLATE_TD | ||
| from promptolution.helpers import get_llm | ||
| from promptolution.tasks import ClassificationTask | ||
| from promptolution.predictors import MarkerBasedClassificator | ||
| from promptolution.optimizers import Opro | ||
| from datasets import load_dataset | ||
|
|
||
| logger = Logger(__name__) | ||
|
|
||
| """Run a test run for any of the implemented optimizers.""" | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--model") | ||
| parser.add_argument("--model-storage-path", default="../models/") | ||
| parser.add_argument("--output-dir", default="results/opro_test/") | ||
| parser.add_argument("--max-model-len", type=int, default=2048) | ||
| parser.add_argument("--n-steps", type=int, default=999) | ||
| parser.add_argument("--token", default=None) | ||
| parser.add_argument("--seed", type=int, default=187) | ||
| args = parser.parse_args() | ||
|
|
||
| callbacks = [ | ||
| LoggerCallback(logger), | ||
| CSVCallback(args.output_dir), | ||
| TokenCountCallback(5000000, "input_tokens"), | ||
| ] | ||
|
|
||
| df = load_dataset("SetFit/ag_news", split="train", revision="main").to_pandas().sample(300, random_state=args.seed) | ||
|
|
||
| df["input"] = df["text"] | ||
| df["target"] = df["label_text"] | ||
|
|
||
| task = ClassificationTask( | ||
| df, | ||
| description="The dataset contains news articles categorized into four classes: World, Sports, Business, and Tech. The task is to classify each news article into one of the four categories.", | ||
| x_column="input", | ||
| y_column="target", | ||
| ) | ||
|
|
||
| initial_prompts = [ | ||
| "Classify this news article as World, Sports, Business, or Tech. Provide your answer between <final_answer> and </final_answer> tags.", | ||
| "Read the following news article and determine which category it belongs to: World, Sports, Business, or Tech. Your classification must be placed between <final_answer> </final_answer> markers.", | ||
| "What is the primary category of this news piece? Choose from World, Sports, Business, or Tech. Place your selected category between <final_answer> </final_answer>.", | ||
| "Analyze this news article and categorize it as either World, Sports, Business, or Tech. Format your answer within <final_answer> </final_answer> tags.", | ||
| "Your task is to identify whether this news article belongs to World, Sports, Business, or Tech news. Provide your classification between the markers <final_answer> </final_answer>.", | ||
| "Please review the following news content and classify it into one of these categories: World, Sports, Business, or Tech. Your answer must be formatted with <final_answer> </final_answer> tags.", | ||
| "Based on the content, determine if this news article falls under World, Sports, Business, or Tech category. Return only your classification within <final_answer> </final_answer>.", | ||
| "Examine this news article and identify its primary category (World, Sports, Business, or Tech). Your final classification should be enclosed between <final_answer> </final_answer> markers.", | ||
| "In this task, you must categorize a news article into one of four classes: World, Sports, Business, or Tech. Remember to place your answer between <final_answer> </final_answer> tags for proper evaluation.", | ||
| "Read the provided news excerpt carefully and assign it to either World, Sports, Business, or Tech category. Ensure your answer appears between <final_answer> </final_answer> tags.", | ||
| "Considering the main subject matter, classify this news article as World, Sports, Business, or Tech. Format your response with <final_answer> </final_answer>.", | ||
| "Determine the appropriate category for this news article from the following options: World, Sports, Business, or Tech. Your selected category must be placed within <final_answer> </final_answer> markers.", | ||
| "After analyzing the given news article, assign it to the most suitable category: World, Sports, Business, or Tech. Your classification should be enclosed in <final_answer> </final_answer> tags.", | ||
| "Your objective is to classify the news article into one of the following categories: World, Sports, Business, or Tech based on its primary focus. Submit your answer between <final_answer> </final_answer> tags.", | ||
| "Which category best describes this news article: World, Sports, Business, or Tech? Provide your answer inside <final_answer> </final_answer> markers.", | ||
| "As a content classifier, determine if the following news article belongs to World, Sports, Business, or Tech news. Place your answer within <final_answer> </final_answer> tags.", | ||
| "Evaluate the following news article and indicate whether it primarily concerns World, Sports, Business, or Tech topics. Your classification must appear between <final_answer> </final_answer>.", | ||
| "Given a news article, your task is to determine its primary category from World, Sports, Business, or Tech. The final classification must be provided between <final_answer> </final_answer> tags.", | ||
| "Conduct a thorough analysis of the provided news article and classify it as belonging to one of these four categories: World, Sports, Business, or Tech. Your answer should be presented within <final_answer> </final_answer> markers.", | ||
| "Simply indicate whether this news article is about World, Sports, Business, or Tech. Include your answer between <final_answer> </final_answer> tags.", | ||
| ] | ||
|
|
||
| initial_prompts = random.sample(initial_prompts, 10) | ||
|
|
||
| if "vllm" in args.model: | ||
| llm = get_llm( | ||
| args.model, | ||
| batch_size=None, | ||
| max_model_len=args.max_model_len, | ||
| model_storage_path=args.model_storage_path, | ||
| revision="main", | ||
| ) | ||
| else: | ||
| llm = get_llm(args.model, args.token) | ||
|
|
||
| downstream_llm = llm | ||
| meta_llm = llm | ||
|
|
||
| predictor = MarkerBasedClassificator(downstream_llm, classes=task.classes) | ||
|
|
||
| optimizer = Opro( | ||
| task=task, | ||
| prompt_template=OPRO_TEMPLATE_TD.replace("<task_desc", task.description), | ||
| predictor=predictor, | ||
| meta_llm=meta_llm, | ||
| initial_prompts=initial_prompts, | ||
| callbacks=callbacks, | ||
| max_num_instructions=20, | ||
| num_instructions_per_step=8, | ||
| num_few_shots=3, | ||
| verbosity=2 | ||
| ) | ||
|
|
||
| best_prompts = optimizer.optimize(n_steps=args.n_steps) | ||
|
|
||
| logger.info(f"Optimized prompts: {best_prompts}") |
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.
Add release notes and version increase