Skip to main content

JavaScript execution as a tool for LLM

Project description

llm-tools-quickjs

PyPI Changelog Tests License

JavaScript execution as a tool for LLM

Installation

Install this plugin in the same environment as LLM.

llm install llm-tools-quickjs

Usage

To use this with the LLM command-line tool:

llm --tool QuickJS "Calculate 123 * 98742" --tools-debug

With the LLM Python API:

import llm
from llm_tools_quickjs import QuickJS

model = llm.get_model("gpt-4.1-mini")

result = model.chain(
    "Calculate 123 * 98742",
    tools=[QuickJS()]
).text()
print(result)

The QuickJS() instance maintains interpreter state between calls, so this kind of thing works:

quickjs = QuickJS()
conversation = model.conversation(tools=[quickjs])
print(conversation.chain("set a to 'rabbit'").text())
print(conversation.chain("calculate length of a times 50").text())
print(quickjs._get_context().eval("a"))
# Outputs 'rabbit'

Something a bit more fun:

llm -T QuickJS 'Draw a 40 character wide mandelbrot with JavaScript' --td

I tried this and got:

Tool call: QuickJS_execute_javascript({'javascript': "function mandelbrot(width, height, max_iter) {\n let result = '';\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n let cx = (x / width) * 3.5 - 2.5;\n let cy = (y / height) * 2 - 1;\n let zx = 0, zy = 0, iter = 0;\n while (zx * zx + zy * zy < 4 && iter < max_iter) {\n let xtemp = zx * zx - zy * zy + cx;\n zy = 2 * zx * zy + cy;\n zx = xtemp;\n iter++;\n }\n if (iter === max_iter) {\n result += '#';\n } else {\n result += ' ';\n }\n }\n result += '\\n';\n }\n return result;\n}\n\nmandelbrot(40, 20, 100);"})

Here is a 40 character wide Mandelbrot set visualization in text form using JavaScript:

```
                                        
                                        
                           ##           
                          ###           
                       # ######         
                       ##########       
                      ###########       
                     ############       
                #### #############      
               ##### ############       
      ##########################        
               ##### ############       
                #### #############      
                     ############       
                      ###########       
                       ##########       
                       # ######         
                          ###           
                           ##           
                                        
```

If you'd like, I can provide the JavaScript code used to generate this.

Function alternative

The QuickJS tool is a Toolbox - it persists state in between calls.

This plugin also provides a function variant with no persisted state. That can be used like this:

llm -T quickjs 'Calculate 123 * 98742' --td

Or in Python like this:

import llm
from llm_tools_quickjs import quickjs

model = llm.get_model("gpt-4.1-mini")

chain = model.chain(
    "Draw a 40 character wide mandelbrot with JavaScript",
    tools=[quickjs]
)
print(chain.text())

Some models that have trouble with class-based tools may work better with the function variant.

Development

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

cd llm-tools-quickjs
python -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

llm install -e '.[test]'

To run the tests:

python -m pytest

Project details


Download files

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

Source Distribution

llm_tools_quickjs-0.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

llm_tools_quickjs-0.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_tools_quickjs-0.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llm_tools_quickjs-0.1.tar.gz
Algorithm Hash digest
SHA256 d38bb1958b6b9d228452442ccaa1b27e3e3121ffd563e72cf9e28d30f5e940bd
MD5 9f6def409ac2cca6d6cb4377711fea8b
BLAKE2b-256 6c56826495beb94348420cdc10b4a2e4fbe5a0189020b58693e7a78d0dae93aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_tools_quickjs-0.1.tar.gz:

Publisher: publish.yml on simonw/llm-tools-quickjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for llm_tools_quickjs-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ca2f088f147fa8db9899ae3f4cc6a619e1a2ca3c6bb92c330e41e9f4261d8a1
MD5 84b30a8d8f8166391dab9e5757af2ec9
BLAKE2b-256 d01c255d2eb0843204a33fbdd0ecb02685260e8feab1d0ede036717f6e8d4f3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_tools_quickjs-0.1-py3-none-any.whl:

Publisher: publish.yml on simonw/llm-tools-quickjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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