Skip to content

Commit

Permalink
Improve topic management and add topic command
Browse files Browse the repository at this point in the history
- Add topic command with list, skip, and max-count options
- Refactor _initialize_topics_table in store.py to handle roots and ancestors
- Display topic logs in reverse chronological order
  • Loading branch information
basicthinker committed Jun 6, 2023
1 parent 5df0553 commit 041b972
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
39 changes: 35 additions & 4 deletions devchat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@click.group()
def main():
"""DevChat CLI: A command-line interface for the DevChat chatbot."""
"""DevChat CLI: A command-line interface for DevChat."""


@contextmanager
Expand Down Expand Up @@ -172,11 +172,12 @@ def prompt(content: Optional[str], parent: Optional[str], reference: Optional[Li
sys.exit(os.EX_DATAERR)


@main.command()
@click.option('--skip', default=0, help='Skip number prompts before showing the prompt history.')
@click.option('-n', '--max-count', default=100, help='Limit the number of commits to output.')
@click.option('-t', '--topic', default=None,
@click.option('-t', '--topic', 'topic_root', default=None,
help='Hash of the root prompt of the topic to select prompts from.')
def log(skip, max_count, topic):
def log(skip, max_count, topic_root):
"""
Show the prompt history.
"""
Expand All @@ -190,11 +191,41 @@ def log(skip, max_count, topic):
click.echo(f"Error: Invalid LLM in configuration '{provider}'", err=True)
sys.exit(os.EX_DATAERR)

recent_prompts = store.select_prompts(skip, skip + max_count, topic)
recent_prompts = store.select_prompts(skip, skip + max_count, topic_root)
logs = []
for record in recent_prompts:
try:
logs.append(record.shortlog())
except Exception:
continue
click.echo(json.dumps(logs, indent=2))


@main.command()
@click.option('--list', '-l', 'list_topics', is_flag=True,
help='List topics in reverse chronological order.')
@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.
"""
config, chat_dir = init_dir()
provider = config.get('provider')
if provider == 'OpenAI':
openai_config = OpenAIChatConfig(model=config['model'], **config['OpenAI'])
chat = OpenAIChat(openai_config)
store = Store(chat_dir, chat)
else:
click.echo(f"Error: Invalid LLM in configuration '{provider}'", err=True)
sys.exit(os.EX_DATAERR)

if list_topics:
topics = store.select_topics(skip, skip + max_count)
topic_logs = []
for topic_data in topics:
try:
topic_logs.append(topic_data['root_prompt'].shortlog())
except Exception:
continue
click.echo(json.dumps(topic_logs, indent=2))
7 changes: 5 additions & 2 deletions devchat/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ def __init__(self, store_dir: str, chat: Chat):
def _initialize_topics_table(self):
roots = [node for node in self._graph.nodes() if self._graph.out_degree(node) == 0]
for root in roots:
latest_time = max(self._graph.nodes[node]['timestamp'] for
node in nx.ancestors(self._graph, root))
ancestors = nx.ancestors(self._graph, root)
if not ancestors:
latest_time = self._graph.nodes[root]['timestamp']
else:
latest_time = max(self._graph.nodes[node]['timestamp'] for node in ancestors)
self._topics_table.insert({
'root': root,
'latest_time': latest_time,
Expand Down

0 comments on commit 041b972

Please sign in to comment.