-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into shashank/seq_id_flash_attn
- Loading branch information
Showing
17 changed files
with
815 additions
and
191 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,58 @@ | ||
# Copyright 2022 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
from typing import List, Optional | ||
|
||
PROMPTFILE_PREFIX = 'file::' | ||
|
||
|
||
def load_prompts(prompts: List[str], | ||
prompt_delimiter: Optional[str] = None) -> List[str]: | ||
"""Loads a set of prompts, both free text and from file. | ||
Args: | ||
prompts (List[str]): List of free text prompts and prompt files | ||
prompt_delimiter (Optional str): Delimiter for text file | ||
If not provided, assumes the prompt file is a single prompt (non-delimited) | ||
Returns: | ||
List of prompt string(s) | ||
""" | ||
prompt_strings = [] | ||
for prompt in prompts: | ||
if prompt.startswith(PROMPTFILE_PREFIX): | ||
prompts = load_prompts_from_file(prompt, prompt_delimiter) | ||
prompt_strings.extend(prompts) | ||
else: | ||
prompt_strings.append(prompt) | ||
return prompt_strings | ||
|
||
|
||
def load_prompts_from_file(prompt_path: str, | ||
prompt_delimiter: Optional[str] = None) -> List[str]: | ||
"""Load a set of prompts from a text fie. | ||
Args: | ||
prompt_path (str): Path for text file | ||
prompt_delimiter (Optional str): Delimiter for text file | ||
If not provided, assumes the prompt file is a single prompt (non-delimited) | ||
Returns: | ||
List of prompt string(s) | ||
""" | ||
if not prompt_path.startswith(PROMPTFILE_PREFIX): | ||
raise ValueError(f'prompt_path_str must start with {PROMPTFILE_PREFIX}') | ||
|
||
_, prompt_file_path = prompt_path.split(PROMPTFILE_PREFIX, maxsplit=1) | ||
prompt_file_path = os.path.expanduser(prompt_file_path) | ||
if not os.path.isfile(prompt_file_path): | ||
raise FileNotFoundError( | ||
f'{prompt_file_path=} does not match any existing files.') | ||
|
||
with open(prompt_file_path, 'r') as f: | ||
prompt_string = f.read() | ||
|
||
if prompt_delimiter is None: | ||
return [prompt_string] | ||
return [i for i in prompt_string.split(prompt_delimiter) if i] |
This file contains 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.