From 0863ed460e983fdbb846eaac02c6454fa0774be3 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 17 Jun 2025 23:23:45 -0700 Subject: [PATCH] llm logs -l/--latest -q option, closes #1177 --- docs/help.md | 1 + docs/logging.md | 7 ++++++- llm/cli.py | 12 ++++++++++-- tests/test_llm_logs.py | 4 ++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/help.md b/docs/help.md index e4c4374..de0720a 100644 --- a/docs/help.md +++ b/docs/help.md @@ -349,6 +349,7 @@ Options: --tools Filter for prompts with results from any tools --schema TEXT JSON schema, filepath or ID --schema-multi TEXT JSON schema used for multiple results + -l, --latest Return latest results matching search query --data Output newline-delimited JSON data for schema --data-array Output JSON array of data for schema --data-key TEXT Return JSON objects from array in this key diff --git a/docs/logging.md b/docs/logging.md index 9620234..d1a46dc 100644 --- a/docs/logging.md +++ b/docs/logging.md @@ -177,7 +177,12 @@ You can search the logs for a search term in the `prompt` or the `response` colu ```bash llm logs -q 'cheesecake' ``` -The most relevant terms will be shown at the bottom of the output. +The most relevant results will be shown first. + +To switch to sorting with most recent first, add `-l/--latest`. This can be combined with `-n` to limit the number of results shown: +```bash +llm logs -q 'cheesecake' -l -n 3 +``` (logging-filter-id)= diff --git a/llm/cli.py b/llm/cli.py index 38e1e33..4c5151d 100644 --- a/llm/cli.py +++ b/llm/cli.py @@ -1426,7 +1426,7 @@ from responses left join schemas on responses.schema_id = schemas.id left join conversations on responses.conversation_id = conversations.id{extra_where} -order by responses.id desc{limit} +order by {order_by}{limit} """ LOGS_SQL_SEARCH = """ select @@ -1437,7 +1437,7 @@ left join schemas on responses.schema_id = schemas.id left join conversations on responses.conversation_id = conversations.id join responses_fts on responses_fts.rowid = responses.rowid where responses_fts match :query{extra_where} -order by responses_fts.rank desc{limit} +order by {order_by}{limit} """ ATTACHMENTS_SQL = """ @@ -1504,6 +1504,9 @@ order by prompt_attachments."order" "--schema-multi", help="JSON schema used for multiple results", ) +@click.option( + "-l", "--latest", is_flag=True, help="Return latest results matching search query" +) @click.option( "--data", is_flag=True, help="Output newline-delimited JSON data for schema" ) @@ -1565,6 +1568,7 @@ def logs_list( any_tools, schema_input, schema_multi, + latest, data, data_array, data_key, @@ -1638,8 +1642,11 @@ def logs_list( model_id = model sql = LOGS_SQL + order_by = "responses.id desc" if query: sql = LOGS_SQL_SEARCH + if not latest: + order_by = "responses_fts.rank desc" limit = "" if count is not None and count > 0: @@ -1649,6 +1656,7 @@ def logs_list( "limit": limit, "columns": LOGS_COLUMNS, "extra_where": "", + "order_by": order_by, } where_bits = [] sql_params = { diff --git a/tests/test_llm_logs.py b/tests/test_llm_logs.py index 2dac375..ef582fe 100644 --- a/tests/test_llm_logs.py +++ b/tests/test_llm_logs.py @@ -300,6 +300,10 @@ def test_logs_filtered(user_path, model, path_option): # Model filter should work too ("llama", ["-m", "davinci"], ["doc1", "doc3"]), ("llama", ["-m", "davinci2"], []), + # Adding -l/--latest should return latest first (order by id desc) + ("llama", [], ["doc1", "doc3"]), + ("llama", ["-l"], ["doc3", "doc1"]), + ("llama", ["--latest"], ["doc3", "doc1"]), ), ) def test_logs_search(user_path, query, extra_args, expected):