Skip to content
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

feat: Add default prompt via env variable #74

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ This won't perform any action over the wire, and just calculates the tokens loca

The system message is used to instruct the model how to behave, see [OpenAI - Instructing Chat Models](https://platform.openai.com/docs/guides/chat/instructing-chat-models).

These can be loaded with `-p`. For convenience any file we place under ~/.config/chatblade/ will be picked up by this command.
These can be loaded with `-p`. For convenience any file we place under `~/.config/chatblade/` will be picked up by this command.

So for example, given the following file `~/.config/chatblade/etymology`, which contains:

Expand Down Expand Up @@ -203,7 +203,13 @@ We can now run a command and refer to this prompt with `-p etymology`:
chatblade -p etymology gregarious
```

You can also point -p to a file path directly to load a system message from any arbitrary location
If you set the environment variable `CHATBLADE_DEFAULT_PROMPT` to the name of a prompt, chatblade will use its value if no other prompt is specified on the command line.
```bash
% export CHATBLADE_DEFAULT_PROMPT=default
% chatblade "Hi there" # Acts like -p default
```

You can also point `-p` to a file path directly to load a system message from any arbitrary location

<img src="assets/example5.png">

Expand Down
13 changes: 11 additions & 2 deletions chatblade/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def handle_input(query, params):
messages = None
if params.session:
messages = storage.messages_from_cache(params.session)
if messages: # a session specified and it alredy exists
if params.prompt_file:
if messages: # a session specified and it already exists
if params.prompt_file and not params.prompt_set_by_env:
printer.warn("refusing to prepend prompt to existing session")
exit(1)
if query: # continue conversation
Expand Down Expand Up @@ -143,6 +143,15 @@ def cli():
migrate_old_cache_file_if_exists()

query, params = parser.parse(sys.argv[1:])

# If we don't have a prompt on the command line, but the user has
# defined a CHATBLADE_DEFAULT_PROMPT environment variable, use the env
# variable as the prompt.
params.prompt_set_by_env = False
if not params.prompt_file and "CHATBLADE_DEFAULT_PROMPT" in os.environ:
params.prompt_file = os.environ["CHATBLADE_DEFAULT_PROMPT"]
params.prompt_set_by_env = True

if params.session_op:
ret = do_session_op(params.session, params.session_op, params.rename_to)
exit(ret)
Expand Down