-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cli for generating promptfoo configs
- Loading branch information
1 parent
eadfdcc
commit 57413e9
Showing
4 changed files
with
61 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
# Ragbits Development Kit | ||
|
||
## Promptfoo Integration | ||
|
||
Ragbits' `Prompt` abstraction can be seamlessly integrated with the `promptfoo` tool. After installing `promptfoo` as | ||
specified in the [promptfoo documentation](https://www.promptfoo.dev/docs/installation/), you can generate promptfoo | ||
configuration files for all the prompts discovered by our autodiscover mechanism by running the following command: | ||
|
||
```bash | ||
rbts prompts generate-promptfoo-configs | ||
``` | ||
|
||
This command will generate a YAML files in the directory specified by `--target-path` (`promptfooconfigs` by | ||
default). The generated file should look like this: | ||
|
||
```yaml | ||
prompts: | ||
- file:///path/to/your/prompt:PromptClass.to_promptfoo | ||
``` | ||
You can then edit the generated file to add your custom `promptfoo` configurations. Once your `promptfoo` configuration | ||
file is ready, you can run `promptfoo` with the following command: | ||
|
||
```bash | ||
promptfoo -c /path/to/generated/promptfoo-config.yaml eval | ||
``` |
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,33 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
import yaml | ||
from rich.console import Console | ||
|
||
from ragbits.dev_kit.prompt_lab.discovery import PromptDiscovery | ||
from ragbits.dev_kit.prompt_lab.discovery.prompt_discovery import DEFAULT_FILE_PATTERN | ||
|
||
|
||
def generate_configs( | ||
file_pattern: str = DEFAULT_FILE_PATTERN, root_path: Path = Path.cwd(), target_path: Path = Path("promptfooconfigs") | ||
) -> None: | ||
""" | ||
Generates promptfoo configuration files for all discovered prompts. | ||
Args: | ||
file_pattern: The file pattern to search for Prompt objects. Defaults to "**/prompt_*.py" | ||
root_path: The root path to search for Prompt objects. Defaults to the directory where the script is run. | ||
target_path: The path to save the promptfoo configuration files. Defaults to "promptfooconfigs". | ||
""" | ||
prompts = PromptDiscovery(file_pattern=file_pattern, root_path=root_path).discover() | ||
Console().print( | ||
f"Discovered {len(prompts)} prompts." | ||
f" Saving promptfoo configuration files to [bold green]{target_path}[/] folder ..." | ||
) | ||
|
||
if not target_path.exists(): | ||
target_path.mkdir() | ||
for prompt in prompts: | ||
with open(target_path / f"{prompt.__qualname__}.yaml", "w", encoding="utf-8") as f: | ||
prompt_path = f'file://{prompt.__module__.replace(".", os.sep)}.py:{prompt.__qualname__}.to_promptfoo' | ||
yaml.dump({"prompts": [prompt_path]}, f) |