8000 GitHub - simonw/llm-tools-quickjs: JavaScript execution as a tool for LLM
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

simonw/llm-tools-quickjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

JavaScript execution as a tool for LLM

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages

0