Skip to main content

LLM plugin for filtering text using semantic regular expressions

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_grep-0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_grep-0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file llm_grep-0.1.tar.gz.

File metadata

  • Download URL: llm_grep-0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for llm_grep-0.1.tar.gz
Algorithm Hash digest
SHA256 3dd7d4ab1edb282cc0fc7af38cbeb33c42d9be0bcef72aeed401bbaf85b06091
MD5 2052db45f2e208fba701bef567e18021
BLAKE2b-256 c142435f44768e9c1fe6c788a4f9427323b2482009dea2224adf5085c83f79aa

See more details on using hashes here.

File details

Details for the file llm_grep-0.1-py3-none-any.whl.

File metadata

  • Download URL: llm_grep-0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for llm_grep-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b97f338b875afaf0bac82acee3e08a71d6dccc1e873c54f9296a5e25abf0d5e2
MD5 7b7c88dc5e2f1120e4d5918f5f280e45
BLAKE2b-256 9f71e150af99bac74fddb6ad6b77dafbaabfd901632852dabb882076a6e19b16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page