-
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.
- Loading branch information
Showing
4 changed files
with
124 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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
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,14 @@ | ||
from pathlib import Path | ||
|
||
from llmfoundry.utils import prompt_files as utils | ||
|
||
def test_load_prompt_strings(tmp_path: Path): | ||
assert utils.load_prompts(['hello', 'goodbye']) == ['hello', 'goodbye'] | ||
|
||
with open(tmp_path / 'prompts.txt', 'w') as f: | ||
f.write('hello goodbye') | ||
|
||
temp = utils.PROMPTFILE_PREFIX + str(tmp_path / 'prompts.txt') | ||
assert utils.load_prompts( | ||
[temp, temp, 'why'], | ||
' ') == ['hello', 'goodbye', 'hello', 'goodbye', 'why'] |