Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 17, 2024
1 parent d73de53 commit cd8ef84
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
25 changes: 25 additions & 0 deletions gptme/logmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Literal, TypeAlias
from collections.abc import Callable

from rich import print

Expand Down Expand Up @@ -368,6 +369,30 @@ def get_user_conversations() -> Generator[ConversationMeta, None, None]:
yield conv


def format_conversation(conv: ConversationMeta) -> str:
"""Format a conversation for display."""
return f"{conv.name}: {conv.messages} messages, last modified {conv.modified}"


def list_conversations(
limit: int = 20, formatter: Callable[[ConversationMeta], str] | None = None
) -> tuple[list[ConversationMeta], bool]:
"""List conversations with a limit, returns (conversations, found_any)."""
if formatter is None:
formatter = format_conversation

found = False
conversations = []
for conv in get_user_conversations():
if limit <= 0:
break
conversations.append(conv)
limit -= 1
found = True

return conversations, found


def _gen_read_jsonl(path: PathLike) -> Generator[Message, None, None]:
with open(path) as file:
for line in file.readlines():
Expand Down
14 changes: 6 additions & 8 deletions gptme/util/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
import click

from ..logmanager import get_user_conversations
from ..message import Message


Expand All @@ -25,16 +24,15 @@ def chats():
@click.option("-n", "--limit", default=20, help="Maximum number of chats to show.")
def chats_list(limit: int):
"""List conversation logs."""
found = False
for conv in get_user_conversations():
if limit <= 0:
break
print(f"{conv.name}: {conv.messages} messages, last modified {conv.modified}")
limit -= 1
found = True
from ..logmanager import list_conversations, format_conversation

conversations, found = list_conversations(limit)
if not found:
print("No conversations found.")
return

for conv in conversations:
print(format_conversation(conv))


@chats.command("read")
Expand Down
89 changes: 86 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ myst-parser = "*"
# types
types-tabulate = "*"
types-lxml = "*"
pylint = "^3.3.1"

[tool.poetry.extras]
server = ["flask", "flask-cors"]
Expand Down

0 comments on commit cd8ef84

Please sign in to comment.