Skip to content

Commit ce234ae

Browse files
committed
add cli prompt search
1 parent 7be48c4 commit ce234ae

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

docs/how-to/prompts/promptfoo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ specified in the [promptfoo documentation](https://www.promptfoo.dev/docs/instal
55
configuration files for all the prompts discovered by our autodiscover mechanism by running the following command:
66

77
```bash
8-
ragbits prompts generate-promptfoo-configs
8+
ragbits prompts promptfoo
99
```
1010

1111
This command will generate a YAML files in the directory specified by `--target-path` (`promptfooconfigs` by

packages/ragbits-core/src/ragbits/core/prompt/_cli.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import inspect
23
import json
34
from importlib import import_module
45
from pathlib import Path
@@ -9,6 +10,7 @@
910
from ragbits.cli import print_output
1011
from ragbits.core.config import core_config
1112
from ragbits.core.llms.base import LLM, LLMType
13+
from ragbits.core.prompt.discovery import PromptDiscovery
1214
from ragbits.core.prompt.prompt import ChatFormat, Prompt
1315

1416
prompts_app = typer.Typer(no_args_is_help=True)
@@ -21,6 +23,16 @@ class LLMResponseCliOutput(BaseModel):
2123
answer: str | BaseModel | None = None
2224

2325

26+
class PromptInfo(BaseModel):
27+
"""Information about a Prompt class for CLI display"""
28+
29+
name: str
30+
import_path: str
31+
description: str
32+
input_type: str
33+
output_type: str
34+
35+
2436
def _render(prompt_path: str, payload: str | None) -> Prompt:
2537
module_stringified, object_stringified = prompt_path.split(":")
2638
prompt_cls = getattr(import_module(module_stringified), object_stringified)
@@ -34,7 +46,7 @@ def _render(prompt_path: str, payload: str | None) -> Prompt:
3446

3547

3648
@prompts_app.command()
37-
def generate_promptfoo_configs(
49+
def promptfoo(
3850
file_pattern: str = core_config.prompt_path_pattern,
3951
root_path: Path = Path.cwd(), # noqa: B008
4052
target_path: Path = Path("promptfooconfigs"),
@@ -49,6 +61,25 @@ def generate_promptfoo_configs(
4961
generate_configs(file_pattern=file_pattern, root_path=root_path, target_path=target_path)
5062

5163

64+
@prompts_app.command()
65+
def search(file_pattern: str = core_config.prompt_path_pattern, root_path: Path = Path.cwd()) -> None: # noqa: B008
66+
"""
67+
Lists all available prompts that can be used with the 'render' and 'exec' commands.
68+
"""
69+
prompt_classes = PromptDiscovery(file_pattern=file_pattern, root_path=root_path).discover()
70+
prompt_infos = [
71+
PromptInfo(
72+
name=prompt_cls.__name__,
73+
import_path=f"{prompt_cls.__module__}:{prompt_cls.__name__}",
74+
description=inspect.getdoc(prompt_cls) or "",
75+
input_type=getattr(prompt_cls.input_type, "__name__", str(prompt_cls.input_type)),
76+
output_type=getattr(prompt_cls.output_type, "__name__", str(prompt_cls.output_type)),
77+
)
78+
for prompt_cls in prompt_classes
79+
]
80+
print_output(prompt_infos)
81+
82+
5283
@prompts_app.command()
5384
def render(prompt_path: str, payload: str | None = None) -> None:
5485
"""

packages/ragbits-core/src/ragbits/core/prompt/discovery/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/ragbits-core/tests/unit/prompts/discovery/test_prompt_discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22

3-
from ragbits.core.prompt.discovery.prompt_discovery import PromptDiscovery
3+
from ragbits.core.prompt.discovery import PromptDiscovery
44

55
current_dir = Path(__file__).parent
66

0 commit comments

Comments
 (0)