Skip to content

Commit

Permalink
llm fragments -q search option
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 18, 2025
1 parent 02ccf91 commit 9695183
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
5 changes: 3 additions & 2 deletions docs/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,9 @@ Usage: llm fragments list [OPTIONS]
List current fragments
Options:
--json Output as JSON
--help Show this message and exit.
-q, --query TEXT Search for fragments matching these strings
--json Output as JSON
--help Show this message and exit.
```

(help-fragments-set)=
Expand Down
5 changes: 5 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ Use `llm fragments` to list all fragments that have been stored:
```bash
llm fragments
```
You can search by passing one or more `-q X` search strings. This will return results matching all of those strings, across the source, hash, aliases and content:
```bash
llm fragments -q pytest -q asyncio
```

The `llm fragments remove` command removes an alias. It does not delete the fragment record itself as those are linked to previous prompts and responses and cannot be deleted independently of them.
```bash
llm fragments remove cli
Expand Down
33 changes: 30 additions & 3 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,11 +1490,35 @@ def fragments():


@fragments.command(name="list")
@click.option(
"queries",
"-q",
"--query",
multiple=True,
help="Search for fragments matching these strings",
)
@click.option("json_", "--json", is_flag=True, help="Output as JSON")
def fragments_list(json_):
def fragments_list(queries, json_):
"List current fragments"
db = sqlite_utils.Database(logs_db_path())
migrate(db)
params = {}
param_count = 0
where_bits = []
for q in queries:
param_count += 1
p = f"p{param_count}"
params[p] = q
where_bits.append(
f"""
(fragments.hash = :{p} or fragment_aliases.alias = :{p}
or fragments.source like '%' || :{p} || '%'
or fragments.content like '%' || :{p} || '%')
"""
)
where = "\n and\n ".join(where_bits)
if where:
where = " where " + where
sql = """
select
fragments.hash,
Expand All @@ -1509,10 +1533,13 @@ def fragments_list(json_):
fragments
left join
fragment_aliases on fragment_aliases.fragment_id = fragments.id
{where}
group by
fragments.id, fragments.hash, fragments.content, fragments.datetime_utc, fragments.source;
"""
results = list(db.query(sql))
""".format(
where=where
)
results = list(db.query(sql, params))
for result in results:
result["aliases"] = json.loads(result["aliases"])
if json_:
Expand Down

0 comments on commit 9695183

Please sign in to comment.