-
Notifications
You must be signed in to change notification settings - Fork 7
Openai miner dataset collector #33
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
4 commits
Select commit
Hold shift + click to select a range
1adfadc
adds openai dataset functionality
p-ferreira 0b0aed8
Merge branch 'main' into features/openai-miner-dataset-collector
p-ferreira 0466b31
updates readme
p-ferreira 8ee58de
Merge branch 'main' into features/openai-miner-dataset-collector
p-ferreira 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import pandas as pd | ||
| import tqdm | ||
| import json | ||
| from typing import List | ||
| from dataclasses import dataclass | ||
|
|
||
| @dataclass | ||
| class OpenAISample: | ||
| prompt: str | ||
| completion: str | ||
|
|
||
| def create_json_dataset( | ||
| df: pd.DataFrame, | ||
| include_scoring: bool, | ||
| blacklist: List[str] | ||
| ) -> str: | ||
| dict_dataset = {} | ||
|
|
||
| for _, row in tqdm.tqdm(df.iterrows(), desc='Creating mining dataset', total=len(df), unit='run'): | ||
| base_prompt = row['base_prompt'] | ||
| best_followup = row['best_followup'] | ||
|
|
||
| answer_prompt = row['answer_prompt'] | ||
| best_answer = row['best_answer'] | ||
|
|
||
| if best_answer not in blacklist: | ||
| if include_scoring: | ||
| scores = 0 | ||
| if isinstance(row["answer_rewards"], list): | ||
| scores = max(row["answer_rewards"]) | ||
| elif isinstance(row["answer_rewards"], float): | ||
p-ferreira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| scores = row["answer_rewards"] | ||
|
|
||
| dict_dataset[answer_prompt] = {best_answer: scores} | ||
| else: | ||
| dict_dataset[answer_prompt] = best_answer | ||
|
|
||
| if best_followup not in blacklist: | ||
| if include_scoring: | ||
| scores = 0 | ||
| if isinstance(row["answer_rewards"], list): | ||
| scores = max(row["answer_rewards"]) | ||
| elif isinstance(row["answer_rewards"], float): | ||
| scores = row["answer_rewards"] | ||
| dict_dataset[base_prompt] = {best_followup: scores} | ||
| else: | ||
| dict_dataset[base_prompt] = best_followup | ||
|
|
||
| return dict_dataset | ||
|
|
||
|
|
||
| def create_csv_dataset( | ||
| df: pd.DataFrame, | ||
| include_scoring: bool, | ||
| blacklist: List[str] | ||
| ) -> pd.DataFrame: | ||
| if include_scoring: | ||
| mining_df = df[['base_prompt', 'best_followup', 'followup_rewards', 'answer_prompt', 'best_answer', 'answer_rewards']] | ||
| # Excludes blacklisted phrases from the dataset | ||
| filtered_df = mining_df[~df['best_followup'].isin(blacklist)] | ||
| filtered_df = filtered_df[~df['best_answer'].isin(blacklist)] | ||
|
|
||
| # Gets the max score for each answer and followup | ||
| filtered_df['followup_rewards'] = filtered_df['followup_rewards'].apply(lambda rewards: max(rewards)) | ||
| filtered_df['answer_rewards'] = filtered_df['answer_rewards'].apply(lambda rewards: max(rewards)) | ||
|
|
||
| return filtered_df | ||
| else: | ||
| mining_df = df[['base_prompt', 'best_followup', 'answer_prompt', 'best_answer']] | ||
| # Excludes blacklisted phrases from the dataset | ||
| filtered_df = mining_df[~df['best_followup'].isin(blacklist)] | ||
| filtered_df = filtered_df[~df['best_answer'].isin(blacklist)] | ||
|
|
||
| return filtered_df | ||
|
|
||
|
|
||
| def create_openai_dataset( | ||
| df: pd.DataFrame, | ||
| blacklist: List[str] | ||
| ) -> str: | ||
| samples = [] | ||
|
|
||
| for _, row in tqdm.tqdm(df.iterrows(), desc='Creating openai mining dataset', total=len(df), unit='run'): | ||
| base_prompt = row['base_prompt'] | ||
| best_followup = row['best_followup'] | ||
|
|
||
| answer_prompt = row['answer_prompt'] | ||
| best_answer = row['best_answer'] | ||
|
|
||
| if best_followup not in blacklist: | ||
| samples += [OpenAISample(base_prompt, best_followup)] | ||
|
|
||
| if best_answer not in blacklist: | ||
| samples += [OpenAISample(answer_prompt, best_answer)] | ||
|
|
||
| # Convert dataclass objects to dictionaries | ||
| jsonl_data = "\n".join( | ||
| json.dumps({"prompt": sample.prompt, "completion": sample.completion}) | ||
| for sample in samples | ||
| ) | ||
|
|
||
| return jsonl_data | ||
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.