This Notebook is a GenAISCript tutorial. It is a Markdown document where each JavaScript code section is a runnable GenAIScript. You can execute each code block individually and see the results in the output section below the code block. To open this notebook in Visual Studio Code, press F1 and run GenAIScript: Create GenAIScript Markdown Notebook.
Follow the steps in configuration to set up your environment and LLM access.
Prompt as code
GenAIScript lets you write prompts as a JavaScript program. GenAIScript runs your program; generate chat messages; then handles the remaining interaction with the LLM API.
Write to prompt $
Let’s start with a simple hello world program.
👤 user🤖 assistant
The $ function formats the strings and write them to the user message. This user message is added to the chat messages and sent to the LLM API. Under the snippet, you can review both the user message (that our program generated) and the assistant (LLM) response.
You can run the code block by clicking the Execute Cell button on the top left corner of the code block. It will be default try to use the LLMs from various providers. If you need to use a different model, update the model field in the front matter at the start of the document. There are many options documented in configuration.
Once the execution is done, you will also an additional trace entry that allows you to dive in the internal details of the GenAIScript execution. This is very helpful to diagnose issues with your prompts. The trace can be quite large so it is not serialized in the markdown file.
You can use the JavaScript for loop and sequence multiple $ calls to append text to the user message. You can also inner expression to generate dynamic content.
👤 user🤖 assistant
To recap, the GenAIScript runs and generates a user messages; that gets sent to the LLM. You can review the user message (and others) in the trace.
def and env.files
The def function lets you declare and assign LLM variables. The concept of variable is most useful to import context data, in particular files, and refer to them in the rest of the prompt.
👤 user🤖 assistant
In GenAIScript, the env.files variable contains the list of files in context, which can be determined by a user selection in the UI, CLI arguments, or pre-configured like in this script. You can change the files in env.files by editing the files field in the front matter at the start of the document.
Filtering env.files
When using GenAIScript from the user interface, it is common to apply a script to an entire folder. This means that you’ll get a bunch of files in env.files including some unneeded ones. The def function provides various options to filter the files, like the endsWith option.
def also provides maxTokens which will trim the content size to a number of tokens. LLM context is finite!
👤 user🤖 assistant
Tools
You can register JavaScript functions as tools that the LLM will call as needed.
Sub-prompt
You can run nested LLMs to execute tasks on other, smaller models.