Skip to content
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
31 changes: 31 additions & 0 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,14 @@ async def inner():
default=5,
help="How many chained tool responses to allow, default 5, set 0 for unlimited",
)
@click.option(
"-n",
"--count",
type=int,
is_flag=False,
flag_value=3,
help="Number of previous messages to show. Requires -c or --cid. Default 3, set 0 for unlimited",
)
def chat(
system,
model_id,
Expand All @@ -1032,6 +1040,7 @@ def chat(
tools_debug,
tools_approve,
chain_limit,
count,
):
"""
Hold an ongoing chat with a model.
Expand Down Expand Up @@ -1146,6 +1155,28 @@ def chat(
raise click.ClickException(str(ex))

click.echo("Chatting with {}".format(model.model_id))

if conversation and conversation.responses and count is not None:
if count <= 0:
history_label = "All Messages"
start_index = 0
else:
history_label = "Last {} Messages".format(count)
# Calculate the start index to get the last 'count' turns
start_index = max(0, len(conversation.responses) - count)

click.echo("\n--- Conversation History ({}) ---".format(history_label))


# Iterate over the relevant slice of conversation responses
for i, response_obj in enumerate(conversation.responses[start_index:]):
# Each response_obj is an llm.Response object, representing a turn
click.echo(f"\n# {response_obj.datetime_utc()} id: {response_obj.id}")
click.echo(f"\n\n## Prompt\n{response_obj.prompt.prompt}")
click.echo(f"\n\n## Response\n{response_obj}")

click.echo("\n--- End History ---")

click.echo("Type 'exit' or 'quit' to exit")
click.echo("Type '!multi' to enter multiple lines, then '!end' to finish")
click.echo("Type '!edit' to open your default editor and modify the prompt")
Expand Down
5 changes: 4 additions & 1 deletion llm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ def from_row(cls, db, row, _async=False):
)
prompt_json = json.loads(row["prompt_json"] or "null")
response.id = row["id"]
response._start_utcnow = datetime.datetime.fromisoformat(row["datetime_utc"])
response._prompt_json = prompt_json
response.response_json = json.loads(row["response_json"] or "null")
response._done = True
Expand Down Expand Up @@ -1141,7 +1142,9 @@ def duration_ms(self) -> int:

def datetime_utc(self) -> str:
self._force()
return self._start_utcnow.isoformat() if self._start_utcnow else ""
if self._start_utcnow:
return self._start_utcnow.strftime("%Y-%m-%dT%H:%M:%S")
return ""

def usage(self) -> Usage:
self._force()
Expand Down
Loading