Skip to main content

llm-grep

LLM plugin for matching text using semantic regular expressions. The high-level matching technique is loosely based on this paper. Matching is done in two passes: one using traditional regular expressions to narrow down candidate matches, and a second pass using an LLM to filter those candidates based on any semantic tags.

The pattern syntax is similar to traditional regular expressions (enclosed in {{ and }}), but adds semantic tags (enclosed in < and >) to indicate the type of concept being matched.

Installation

Install this plugin in the same environment as LLM.

llm install llm-grep

Usage

The plugin adds a new command, llm grep. This command has an interface similar to the GNU grep command, but extends it with semantic matching capabilities, using an LLM as a matching oracle.

Input can be a standard file or stdin. Simple examples you can try:

# Match lines from a file
llm grep -e '^{{.*}}<about birds>$' notes.txt

# Read from standard input
cat recipes.txt | llm grep -e '^{{.*}}<baking related>$'

# Enable color, only output matched content, and use a custom model and prompt:
llm grep --color always -o -e '{{[A-Za-z0-9]+}}<outdoor activities>' --model gpt-4\
    --prompt 'Answer yes or no. Query: {query} Text: {span}.' headlines.txt

# Slightly more specific capture (will not match names like 'sparrow')
llm grep -e '\\b{{[A-Z][a-z]+(?:\\s+[A-Z][a-z]+)?}}<bird species>\\b' bird_log.txt

The default prompt used is:

Does the following text satisfy the semantic concept described by the query?

Query: {query}

Text: {span}

Your answer should include "yes" or "no".

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd llm-grep
python3 -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

python -m pytest

Future plans

  • Currently, some of grep's functionality is not implemented (e.g. -r for recursive searching). Re-implementing these features is a losing battle, and will be deprioritized in favor of somehow hooking into (or wrapping around) existing grep implementations.

Metadata

Release files for llm-grep 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-grep 0.1
File Size Uploaded
llm_grep-0.1.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-grep 0.1
File Interpreter ABI Platform
llm_grep-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / llm_grep-0.1.tar.gz

Download URL llm_grep-0.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3dd7d4ab1edb282cc0fc7af38cbeb33c42d9be0bcef72aeed401bbaf85b06091
BLAKE2b-256 checksum
How to use checksums
c142435f44768e9c1fe6c788a4f9427323b2482009dea2224adf5085c83f79aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / llm_grep-0.1-py3-none-any.whl

Download URL llm_grep-0.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b97f338b875afaf0bac82acee3e08a71d6dccc1e873c54f9296a5e25abf0d5e2
BLAKE2b-256 checksum
How to use checksums
9f71e150af99bac74fddb6ad6b77dafbaabfd901632852dabb882076a6e19b16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page