Skip to content

Commit

Permalink
llm logs --id-gt and --id-gte options, closes #801
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 28, 2025
1 parent 48f67f4 commit 3a60290
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ Options:
--xl, --extract-last Extract last fenced code block
-c, --current Show logs from the current conversation
--cid, --conversation TEXT Show logs for this conversation ID
--id-gt TEXT Return responses with ID > this
--id-gte TEXT Return responses with ID >= this
--json Output logs as JSON
--help Show this message and exit.
```
Expand Down
15 changes: 14 additions & 1 deletion docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Or `-n 0` to see everything that has ever been logged:
```bash
llm logs -n 0
```
You can truncate the display of the prompts and responses using the `-t/--truncate` option. This can help make the JSON output more readable:
You can truncate the display of the prompts and responses using the `-t/--truncate` option. This can help make the JSON output more readable - though the `--short` option is usually better.
```bash
llm logs -n 1 -t --json
```
Expand Down Expand Up @@ -179,6 +179,19 @@ llm logs -q 'cheesecake'
```
The most relevant terms will be shown at the bottom of the output.

(logging-filter-id)=

### Filtering past a specific ID

If you want to retrieve all of the logs that were recorded since a specific response ID you can do so using these options:

- `--id-gt $ID` - every record with an ID greater than $ID
- `--id-gte $ID` - every record with an ID greater than or equal to $ID

IDs are always issued in ascending order by time, so this provides a useful way to see everything that has happened since a particular record.

This can be particularly useful when {ref}`working with schema data <schemas-logs>`, where you might want to access every record that you have created using a specific `--schema` but exclude records you have previously processed.

(logging-filter-model)=

### Filtering by model
Expand Down
14 changes: 13 additions & 1 deletion llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,8 @@ def logs_turn_off():
"--conversation",
help="Show logs for this conversation ID",
)
@click.option("--id-gt", help="Return responses with ID > this")
@click.option("--id-gte", help="Return responses with ID >= this")
@click.option(
"json_output",
"--json",
Expand All @@ -996,6 +998,8 @@ def logs_list(
extract_last,
current_conversation,
conversation_id,
id_gt,
id_gte,
json_output,
):
"Show recent logged prompts and their responses"
Expand Down Expand Up @@ -1069,6 +1073,10 @@ def logs_list(
where_bits.append("responses.model = :model")
if conversation_id:
where_bits.append("responses.conversation_id = :conversation_id")
if id_gt:
where_bits.append("responses.id > :id_gt")
if id_gte:
where_bits.append("responses.id >= :id_gte")
schema_id = None
if schema:
schema_id = make_schema_id(schema)[0]
Expand All @@ -1087,6 +1095,8 @@ def logs_list(
"query": query,
"conversation_id": conversation_id,
"schema_id": schema_id,
"id_gt": id_gt,
"id_gte": id_gte,
},
)
)
Expand Down Expand Up @@ -1209,7 +1219,9 @@ def logs_list(
"# {}{}\n{}".format(
row["datetime_utc"].split(".")[0],
(
" conversation: {}".format(row["conversation_id"])
" conversation: {} id: {}".format(
row["conversation_id"], row["id"]
)
if should_show_conversation
else ""
),
Expand Down
9 changes: 6 additions & 3 deletions tests/test_llm_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def schema_log_path(user_path):


datetime_re = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}")
id_re = re.compile(r"id: \w+")


@pytest.mark.parametrize("usage", (False, True))
Expand All @@ -94,9 +95,11 @@ def test_logs_text(log_path, usage):
output = result.output
# Replace 2023-08-17T20:53:58 with YYYY-MM-DDTHH:MM:SS
output = datetime_re.sub("YYYY-MM-DDTHH:MM:SS", output)
# Replace id: whatever with id: xxx
output = id_re.sub("id: xxx", output)
expected = (
(
"# YYYY-MM-DDTHH:MM:SS conversation: abc123\n\n"
"# YYYY-MM-DDTHH:MM:SS conversation: abc123 id: xxx\n\n"
"Model: **davinci**\n\n"
"## Prompt\n\n"
"prompt\n\n"
Expand All @@ -107,7 +110,7 @@ def test_logs_text(log_path, usage):
)
+ ("## Token usage:\n\n2 input, 5 output\n\n" if usage else "")
+ (
"# YYYY-MM-DDTHH:MM:SS conversation: abc123\n\n"
"# YYYY-MM-DDTHH:MM:SS conversation: abc123 id: xxx\n\n"
"Model: **davinci**\n\n"
"## Prompt\n\n"
"prompt\n\n"
Expand All @@ -116,7 +119,7 @@ def test_logs_text(log_path, usage):
)
+ ("## Token usage:\n\n2 input, 5 output\n\n" if usage else "")
+ (
"# YYYY-MM-DDTHH:MM:SS conversation: abc123\n\n"
"# YYYY-MM-DDTHH:MM:SS conversation: abc123 id: xxx\n\n"
"Model: **davinci**\n\n"
"## Prompt\n\n"
"prompt\n\n"
Expand Down

0 comments on commit 3a60290

Please sign in to comment.