llm logs -l/--latest -q option, closes #1177

This commit is contained in:
Simon Willison 2025-06-17 23:23:45 -07:00
parent 544ce17c1d
commit 0863ed460e
4 changed files with 21 additions and 3 deletions

View file

@ -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

View file

@ -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)=

View file

@ -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 = {

View file

@ -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):