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

Introducing directory parameter #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions chatblade/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fetch_and_cache(messages, params):
else:
response_msg = chat.query_chat_gpt(messages, params)
messages.append(response_msg)
storage.to_cache(messages)
storage.to_cache(messages, params)
return messages


Expand All @@ -49,7 +49,7 @@ def handle_input(query, params):
utils.debug(title="cli input", query=query, params=params)

if params.last:
messages = storage.messages_from_cache()
messages = storage.messages_from_cache(params)
if query: # continue conversation
messages.append(chat.Message("user", query))
elif params.prompt_file:
Expand All @@ -63,6 +63,9 @@ def handle_input(query, params):
printer.warn("no query or option given. nothing to do...")
exit()

if type(params.directory) == type(None):
del params["directory"]

if params.tokens:
token_prices = chat.get_tokens_and_costs(messages)
printer.print_tokens(messages, token_prices, params)
Expand Down
6 changes: 6 additions & 0 deletions chatblade/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def parse(args):
help="Stream the incoming text to the terminal",
action="store_true",
)
parser.add_argument(
"--directory",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better named as --cache-dir. I think having the word "cache" in the argument is important, as chatblade uses a couple directories, not just one.

metavar="d",
type=str,
help="Set the chatblade cache location (default ~/.cache/chatblade, ~/Library/Caches/chatblade on osx)",
)

# ------ Display Options
parser.add_argument(
Expand Down
18 changes: 12 additions & 6 deletions chatblade/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
APP_NAME = "chatblade"


def get_cache_file_path():
def get_cache_file_path(args):
"""
if ~/.cache is availabe always use ~/.cache/chatblade as the cachefile
otherwise fallback to the platform recommended location and create the directory
e.g. ~/Library/Caches/chatblade on osx
"""
cache_path = os.path.expanduser("~/.cache")
if "directory" not in args:
cache_path = os.path.expanduser("~/.cache")
else:
cache_path = os.path.expanduser(args.directory)
if not os.path.exists(cache_path):
os.makedirs(cache_path)

if not os.path.exists(cache_path):
cache_path = platformdirs.user_cache_dir(APP_NAME)
if not os.path.exists(cache_path):
Expand All @@ -28,15 +34,15 @@ def get_cache_file_path():
return os.path.join(cache_path, APP_NAME)


def to_cache(messages):
def to_cache(messages, args):
"""cache the current messages state"""
with open(get_cache_file_path(), "wb") as f:
with open(get_cache_file_path(args), "wb") as f:
pickle.dump(messages, f)


def messages_from_cache():
def messages_from_cache(args):
"""load messages from last state or ChatbladeError if not exists"""
file_path = get_cache_file_path()
file_path = get_cache_file_path(args)
if not os.path.exists(file_path):
raise errors.ChatbladeError("No last state cached from which to begin")
else:
Expand Down