Skip to content

Commit

Permalink
fix: save timestamps in logs, show correct 'created' timestamp in web…
Browse files Browse the repository at this point in the history
… UI, improvements to web UI
  • Loading branch information
ErikBjare committed Oct 16, 2023
1 parent a01c442 commit 8dd938c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions gptme/logmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ def _conversations() -> list[Path]:
def get_conversations() -> Generator[dict, None, None]:
for c in _conversations():
msgs = [Message(**json.loads(line)) for line in open(c)]
first_timestamp = msgs[0].timestamp.timestamp() if msgs else c.stat().st_mtime
yield {
"name": f"{c.parent.name}",
"path": str(c),
"ctime": c.stat().st_ctime,
"mtime": c.stat().st_mtime,
"created": first_timestamp,
"modified": c.stat().st_mtime,
"messages": len(msgs),
}
15 changes: 11 additions & 4 deletions gptme/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ def __init__(
pinned: bool = False,
hide: bool = False,
quiet: bool = False,
timestamp: datetime | None = None,
timestamp: datetime | str | None = None,
):
assert role in ["system", "user", "assistant"]
self.role = role
self.content = content.strip()
self.timestamp = timestamp or datetime.now()
if isinstance(timestamp, str):
self.timestamp = datetime.fromisoformat(timestamp)
else:
self.timestamp = timestamp or datetime.now()
if user:
self.user = user
else:
Expand All @@ -43,12 +46,16 @@ def __init__(
# Wether this message should be printed on execution (will still print on resume, unlike hide)
self.quiet = quiet

def to_dict(self):
def to_dict(self, keys=None):
"""Return a dict representation of the message, serializable to JSON."""
return {
d = {
"role": self.role,
"content": self.content,
"timestamp": self.timestamp.isoformat(),
}
if keys:
return {k: d[k] for k in keys}
return d

def format(self, oneline: bool = False, highlight: bool = False) -> str:
return format_msgs([self], oneline=oneline, highlight=highlight)[0]
Expand Down
8 changes: 4 additions & 4 deletions gptme/util.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import random
import logging
import random
from datetime import datetime, timedelta

import tiktoken
from rich import print
from rich.console import Console
from rich.syntax import Syntax

from .message import Message

import tiktoken

EMOJI_WARN = "⚠️"

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -50,7 +49,8 @@ def msgs2text(msgs: list[Message]) -> str:


def msgs2dicts(msgs: list[Message]) -> list[dict]:
return [msg.to_dict() for msg in msgs]
"""Convert a list of Message objects to a list of dicts ready to pass to an LLM."""
return [msg.to_dict(keys=["role", "content"]) for msg in msgs]


def generate_unique_name():
Expand Down
14 changes: 7 additions & 7 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ <h2 class="text-lg font-bold">Conversations</h1>
<tr>
<th class="text-left">Name</th>
<th class="text-right" @click="changeSort('messages')"># msgs</th>
<th class="text-right hidden md:table-cell" @click="changeSort('mtime')">Edited</th>
<th class="text-right hidden md:table-cell" @click="changeSort('ctime')">Created</th>
<th class="text-right hidden md:table-cell" @click="changeSort('modified')">Edited</th>
<th class="text-right hidden md:table-cell" @click="changeSort('created')">Created</th>
</tr>
</thead>
<tbody>
<tr v-for="conversation in sortedConversations">
<td><a @click="selectConversation(conversation.name)">{{ conversation.name }}</a></td>
<td class="text-right">{{ conversation.messages }}</td>
<td class="text-right hidden md:table-cell">
<time :datetime="new Date(1000 * conversation.mtime).toISOString()">
{{ fromNow(1000 * conversation.mtime) }}
<time :datetime="new Date(1000 * conversation.modified).toISOString()">
{{ fromNow(1000 * conversation.modified) }}
</time>
</td>
<td class="text-right hidden md:table-cell">
<time :datetime="new Date(1000 * conversation.ctime).toISOString()">
{{ fromNow(1000 * conversation.ctime) }}
<time :datetime="new Date(1000 * conversation.created).toISOString()">
{{ fromNow(1000 * conversation.created) }}
</time>
</td>
</tr>
Expand Down Expand Up @@ -111,7 +111,7 @@ <h1 class="text-lg font-bold">{{ selectedConversation }}</h1>
chatLog: [],

// Options
sortBy: "mtime",
sortBy: "modified",
hideSystemMessages: true, // hide initial system messages

// Inputs
Expand Down

0 comments on commit 8dd938c

Please sign in to comment.