Skip to content

Commit

Permalink
Revive click for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kagami-l committed May 10, 2024
1 parent 5ccb007 commit b2c7e62
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 197 deletions.
4 changes: 0 additions & 4 deletions devchat/_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .run import run
from .topic import topic
from .route import route
from .command import commands, command, Command

script_dir = os.path.dirname(os.path.realpath(__file__))
os.environ['TIKTOKEN_CACHE_DIR'] = os.path.join(script_dir, '..', 'tiktoken_cache')
Expand All @@ -15,7 +14,4 @@
'run',
'topic',
'route',
'commands',
'command',
'Command'
]
11 changes: 0 additions & 11 deletions devchat/_cli/click_main.py

This file was deleted.

66 changes: 0 additions & 66 deletions devchat/_cli/command.py

This file was deleted.

30 changes: 8 additions & 22 deletions devchat/_cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Optional, List, Dict
from dataclasses import dataclass, field

from .command import command, Command
import click

@dataclass
class PromptData:
Expand All @@ -19,27 +19,13 @@ class PromptData:
response_tokens: int = 0


@command('log', help='Process logs')
@Command.option('--skip',
type=int,
default=0,
help='Skip number prompts before showing the prompt history.')
@Command.option('-n',
'--max-count',
type=int,
default=1,
help='Limit the number of commits to output.')
@Command.option('-t',
'--topic',
dest='topic_root',
default=None,
help='Hash of the root prompt of the topic to select prompts from.')
@Command.option('--insert',
default=None,
help='JSON string of the prompt to insert into the log.')
@Command.option('--delete',
default=None,
help='Hash of the leaf prompt to delete from the log.')
@click.command(help='Process logs')
@click.option('--skip', default=0, help='Skip number prompts before showing the prompt history.')
@click.option('-n', '--max-count', default=1, help='Limit the number of commits to output.')
@click.option('-t', '--topic', 'topic_root', default=None,
help='Hash of the root prompt of the topic to select prompts from.')
@click.option('--insert', default=None, help='JSON string of the prompt to insert into the log.')
@click.option('--delete', default=None, help='Hash of the leaf prompt to delete from the log.')
def log(skip, max_count, topic_root, insert, delete):
"""
Manage the prompt history.
Expand Down
29 changes: 10 additions & 19 deletions devchat/_cli/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
"""
This module contains the main function for the DevChat CLI.
"""
import argparse
import sys
import click

from devchat.utils import get_logger
# pylint: disable=unused-import
from devchat._cli import log
from devchat._cli import prompt
from devchat._cli import run
from devchat._cli import topic
from devchat._cli import route
from devchat._cli import commands

logger = get_logger(__name__)


def main(argv=None):
if argv is None:
argv = sys.argv[1:]

parser = argparse.ArgumentParser(description="CLI tool")
subparsers = parser.add_subparsers(help='sub-command help')
for _1, cmd in commands.items():
cmd.register(subparsers)
@click.group()
def main():
"""DevChat CLI: A command-line interface for DevChat."""

args = parser.parse_args(argv)
if hasattr(args, 'func'):
func_args = vars(args).copy()
del func_args['func']

args.func(**func_args)
else:
parser.print_help()
main.add_command(prompt)
main.add_command(log)
main.add_command(run)
main.add_command(topic)
main.add_command(route)
24 changes: 12 additions & 12 deletions devchat/_cli/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
import sys
from typing import List, Optional

from .command import command, Command
import click


@command('prompt', help='Interact with the large language model (LLM).')
@Command.argument('content')
@Command.option('-p', '--parent', help='Input the parent prompt hash to continue the conversation.')
@Command.option('-r', '--reference', multiple=True,
@click.command(help='Interact with the large language model (LLM).')
@click.argument('content', required=False)
@click.option('-p', '--parent', help='Input the parent prompt hash to continue the conversation.')
@click.option('-r', '--reference', multiple=True,
help='Input one or more specific previous prompts to include in the current prompt.')
@Command.option('-i', '--instruct', multiple=True,
@click.option('-i', '--instruct', multiple=True,
help='Add one or more files to the prompt as instructions.')
@Command.option('-c', '--context', multiple=True,
@click.option('-c', '--context', multiple=True,
help='Add one or more files to the prompt as a context.')
@Command.option('-m', '--model', help='Specify the model to use for the prompt.')
@Command.option('--config', dest="config_str", required=False,
@click.option('-m', '--model', help='Specify the model to use for the prompt.')
@click.option('--config', 'config_str',
help='Specify a JSON string to overwrite the default configuration for this prompt.')
@Command.option('-f', '--functions',
@click.option('-f', '--functions', type=click.Path(exists=True),
help='Path to a JSON file with functions for the prompt.')
@Command.option('-n', '--function-name',
@click.option('-n', '--function-name',
help='Specify the function name when the content is the output of a function.')
@Command.option('-ns', '--not-store', is_flag=True, default=False, required=False,
@click.option('-ns', '--not-store', is_flag=True, default=False, required=False,
help='Do not save the conversation to the store.')
def prompt(content: Optional[str], parent: Optional[str], reference: Optional[List[str]],
instruct: Optional[List[str]], context: Optional[List[str]],
Expand Down
20 changes: 10 additions & 10 deletions devchat/_cli/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
import sys
from typing import List, Optional

from .command import command, Command
import click


@command('route', help='Route a prompt to the specified LLM')
@Command.argument('content')
@Command.option('-p', '--parent', help='Input the parent prompt hash to continue the conversation.')
@Command.option('-r', '--reference', multiple=True,
@click.command(help='Route a prompt to the specified LLM')
@click.argument('content', required=False)
@click.option('-p', '--parent', help='Input the parent prompt hash to continue the conversation.')
@click.option('-r', '--reference', multiple=True,
help='Input one or more specific previous prompts to include in the current prompt.')
@Command.option('-i', '--instruct', multiple=True,
@click.option('-i', '--instruct', multiple=True,
help='Add one or more files to the prompt as instructions.')
@Command.option('-c', '--context', multiple=True,
@click.option('-c', '--context', multiple=True,
help='Add one or more files to the prompt as a context.')
@Command.option('-m', '--model', help='Specify the model to use for the prompt.')
@Command.option('--config', dest='config_str',
@click.option('-m', '--model', help='Specify the model to use for the prompt.')
@click.option('--config', 'config_str',
help='Specify a JSON string to overwrite the default configuration for this prompt.')
@Command.option('-a', '--auto', is_flag=True, default=False, required=False,
@click.option('-a', '--auto', is_flag=True, default=False, required=False,
help='Answer question by function-calling.')
def route(content: Optional[str], parent: Optional[str], reference: Optional[List[str]],
instruct: Optional[List[str]], context: Optional[List[str]],
Expand Down
24 changes: 12 additions & 12 deletions devchat/_cli/run.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# pylint: disable=import-outside-toplevel
from typing import List, Optional, Tuple
from .command import command, Command

import click

@command('run',
@click.command(
help="The 'command' argument is the name of the command to run or get information about.")
@Command.argument('command', nargs='?', default='')
@Command.option('--list', dest='list_flag', is_flag=True, default=False,
@click.argument('command', required=False, default='')
@click.option('--list', 'list_flag', is_flag=True, default=False,
help='List all specified commands in JSON format.')
@Command.option('--recursive', '-r', dest='recursive_flag', is_flag=True, default=True,
@click.option('--recursive', '-r', 'recursive_flag', is_flag=True, default=True,
help='List commands recursively.')
@Command.option('--update-sys', dest='update_sys_flag', is_flag=True, default=False,
@click.option('--update-sys', 'update_sys_flag', is_flag=True, default=False,
help='Pull the `sys` command directory from the DevChat repository.')
@Command.option('-p', '--parent', help='Input the parent prompt hash to continue the conversation.')
@Command.option('--reference', multiple=True,
@click.option('-p', '--parent', help='Input the parent prompt hash to continue the conversation.')
@click.option('-r', '--reference', multiple=True,
help='Input one or more specific previous prompts to include in the current prompt.')
@Command.option('-i', '--instruct', multiple=True,
@click.option('-i', '--instruct', multiple=True,
help='Add one or more files to the prompt as instructions.')
@Command.option('-c', '--context', multiple=True,
@click.option('-c', '--context', multiple=True,
help='Add one or more files to the prompt as a context.')
@Command.option('-m', '--model', help='Specify the model to use for the prompt.')
@Command.option('--config', dest='config_str',
@click.option('-m', '--model', help='Specify the model to use for the prompt.')
@click.option('--config', 'config_str',
help='Specify a JSON string to overwrite the default configuration for this prompt.')
# pylint: disable=redefined-outer-name
def run(command: str, list_flag: bool, recursive_flag: bool, update_sys_flag: bool,
Expand Down
11 changes: 5 additions & 6 deletions devchat/_cli/topic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# pylint: disable=import-outside-toplevel
from .command import command, Command
import click


@command('topic', help='Manage topics')
@Command.option('--list', '-l', dest='list_topics', is_flag=True,
@click.command(help='Manage topics')
@click.option('--list', '-l', 'list_topics', is_flag=True,
help='List topics in reverse chronological order.')
@Command.option('--skip', default=0, help='Skip number of topics before showing the list.')
@Command.option('-n', '--max-count', default=100, help='Limit the number of topics to output.')
@click.option('--skip', default=0, help='Skip number of topics before showing the list.')
@click.option('-n', '--max-count', default=100, help='Limit the number of topics to output.')
def topic(list_topics: bool, skip: int, max_count: int):
"""
Manage topics.
Expand Down

0 comments on commit b2c7e62

Please sign in to comment.