Skip to content

Commit

Permalink
refactor!: Change the Advanced Script Invocation Formatting Spec from…
Browse files Browse the repository at this point in the history
… `@script` to `[[@script]]`
  • Loading branch information
snowyu committed Dec 17, 2024
1 parent bd042fe commit 9082a8d
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ EXAMPLES
│[info]:Start Script: ...
```

_See code: [src/commands/run/index.ts](https://github.com/offline-ai/cli-plugin-core.js/blob/v0.8.16/src/commands/run/index.ts)_
_See code: [src/commands/run/index.ts](https://github.com/offline-ai/cli-plugin-core.js/blob/v0.8.17/src/commands/run/index.ts)_
<!-- commandsstop -->

# Credit
Expand Down
16 changes: 8 additions & 8 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This file defines a simple text file/url loader library for the Programmable Pro
* **Loads text files:** Reads the contents of text files specified by a file path or url.
* **Environment Variable Support:** Allows use of environment variables within file paths (e.g., "$HOME/documents/document.md").
* **Prompt Integration:** Designed to be integrated into PPE prompts, allowing users to reference file content directly within prompts (e.g., `user: summary the following file content: @file(document.md)`).
* **Prompt Integration:** Designed to be integrated into PPE prompts, allowing users to reference file content directly within prompts (e.g., `user: summary the following file content: [[@file(document.md)]]`).

**Input Configuration:**

Expand All @@ -63,14 +63,14 @@ This file defines a simple text file/url loader library for the Programmable Pro
**Usage Example:**

```yaml
user: summary the following file content: @file(document.md)
user: summary the following file content: [[@file(document.md)]]
```

This prompt instructs the PPE to load the content of "document.md" and then summarize the loaded text.

**Workflow:**

1. The PPE encounters the `@file(document.md)` directive in the prompt.
1. The PPE encounters the `[[@file(document.md)]]` directive in the prompt.
2. The PPE invokes the `file.ai.yaml` library.
3. The library uses the `loadFile()` function to read the file content from "document.md" (resolving any environment variables in the path).
4. The loaded content is returned as a string, formatted with the filename and content.
Expand Down Expand Up @@ -184,7 +184,7 @@ The library provides two usage examples:

**Usage Example:**

* **Inline Prompt:** `Title: @titleify(file=document.md)`
* **Inline Prompt:** `Title: [[@titleify(file=document.md)]]`
* **Command-Line Execution:**

```bash
Expand All @@ -193,7 +193,7 @@ The library provides two usage examples:

**Workflow:**

1. **Input Processing:** The script first checks if a "file" input is provided. If so, it loads the content from the specified file path using the `@file()` function.
1. **Input Processing:** The script first checks if a "file" input is provided. If so, it loads the content from the specified file path using the `[[@file()]]` function.
2. **Summarization:** The loaded content is then passed to a summarization engine (represented by `[[titles:max_tokens=len]]`). This engine likely utilizes a large language model to generate several potential titles based on the input text and the desired length.
3. **Title Selection:** The script interacts with the user (in an interactive mode) to select the best title from the generated options.
4. **Output:** Finally, the chosen title is returned as the output of the "titleify" function.
Expand Down Expand Up @@ -238,7 +238,7 @@ $ai run --no-chats -f translator "{content:'我爱我的祖国和故乡.', targe
- **Integration within a prompt:**

```yaml
assistant: "Translate: @translator(file='document.md', target='English')"
assistant: "Translate: [[@translator(file='document.md', target='English')]]"
```
**Workflow:**
Expand All @@ -257,7 +257,7 @@ This file defines a simple fetch url library for the Programmable Prompt Engine
**Key Functionality:**
* **Loads web content from url:** Reads the content specified by a url path.
* **Prompt Integration:** Designed to be integrated into PPE prompts, allowing users to reference url content directly within prompts (e.g., `user: summary the following web page: @url("https://example.com/page.html")`).
* **Prompt Integration:** Designed to be integrated into PPE prompts, allowing users to reference url content directly within prompts (e.g., `user: summary the following web page: [[@url("https://example.com/page.html")]]`).

**Input Configuration:**

Expand All @@ -270,7 +270,7 @@ This file defines a simple fetch url library for the Programmable Prompt Engine
**Usage Example:**

```yaml
user: summary the following web content: @url("https://example.com/page.html")
user: summary the following web content: [[@url("https://example.com/page.html")]]
```

This prompt instructs the PPE to load the content from "https://example.com/page.html" and then summarize the loaded text.
2 changes: 1 addition & 1 deletion lib/file.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type: lib
description: |-
the simple text file loader. You can use environment variables in file path, eg, "$HOME/documents/document.md".
It can be used in prompt. eg, `user: "think about the following file content: @file(document.md)"`
It can be used in prompt. eg, `user: "think about the following file content: [[@file(document.md)]]"`
tag:
- file
- url
Expand Down
6 changes: 3 additions & 3 deletions lib/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ The role messages can be formatted using Jinja2 templates and advanced replaceme

* **Jinja2 Templates:** Reference variables from input configuration or prompt settings using double curly braces (e.g., `{{name}}`).
* **Advanced AI Replacement:** As described above, triggers AI execution and stores the response.
* **External Script Replacement:** Invoke external scripts using the `@` symbol (e.g., `@say_hi_script(param1=value1)`).
* **Internal Instruction Replacement:** Call internal instructions similarly (e.g., `@$instruction(param1=value1)`).
* **External Script Replacement:** Invoke external scripts using the `@` symbol (e.g., `[[@say_hi_script(param1=value1)]]`).
* **Internal Instruction Replacement:** Call internal instructions similarly (e.g., `[[@$instruction(param1=value1)]]`).
* **Regular Expression Replacement:** Use `/RegExp/[RegOpts]:Answer[:index_or_group_name]` for pattern-based replacement on the `Answer` variable.

### [Programmable Prompt Engine Language - Script Capabilities](./lang-script.md)

* **Chaining Outputs:** The `->` operator connect script outputs to subsequent instructions or scripts, creating complex workflows.
* **Instruction Invocation:** The `$` prefix calls script instructions (e.g., `$fn: {param1:value1}`).
* **Control Flow:** Directives like `$if`, `$pipe`, `$set`, `$get`, `$print`, `$echo` provide control flow mechanisms.
* **Control Flow:** Directives like `$if`, `$while`, `$for`, `$match` provide control flow mechanisms.
* **Event-Driven Architecture:** Functions like `$on`, `$once`, `$emit` and `$off` enable event-based programming for flexible script behavior.
* **JavaScript Extension:** The `!fn` directive allows declaring JavaScript functions to extend script functionality.

Expand Down
6 changes: 3 additions & 3 deletions lib/guide/guide.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ IsServerRunning: Please make sure the brain(LLM) server has been started. Are yo
}
return result
}
- $if: "@input(inputType='confirm', content={{Welcome+'\n'+IsServerRunning}}, value=true, format=(answer) => answer ? 'Yes' : 'No')"
- $if: "[[@input(inputType='confirm', content={{Welcome+'\n'+IsServerRunning}}, value=true, format=(answer) => answer ? 'Yes' : 'No')]]"
then:
# call support_langs to get the supported languages from LLM and assign it to the languages variable.
- $set:
languages: "@support_langs"
languages: "[[@support_langs]]"
- $set:
# call input to get the preferLang from user and assign it to the preferLang variable.
preferLang: "@input(inputType='AutoComplete', content='Which language do you prefer?', choices=languages, limit=10)"
preferLang: "[[@input(inputType='AutoComplete', content='Which language do you prefer?', choices=languages, limit=10)]]"
# `#` prefix means format the template string immediately and assign it to the variable.
GiveMeGoodBrain: "\n[Start Guide]An expert should have a good brain(LLM)."
- $if: "preferLang !== 'en'"
Expand Down
6 changes: 3 additions & 3 deletions lib/guide/guide_lib_explain_file.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ file: "README.md"
- -> trans(lang) -> $ret
- user: |-
{%if file != 'README.md'%}
@file({{__dirname+file}})
[[@file({{__dirname+file}})]]
---
{%endif%}
Summarize the content in detail, capturing the key points and essence of it.
Expand All @@ -29,12 +29,12 @@ file: "README.md"
# - -> titleify(content=summary) -> trans(lang) -> $set('title')
- -> extract_title(content=summary) -> trans(lang) -> $set('title')
- -> trans(content="Do you have any questions about this file?", lang) -> $set('anyQuestion')
- $while: "@input(inputType='confirm', content=anyQuestion + ' ' + title, memoized=false, format=(answer) => answer ? 'Yes' : 'No')"
- $while: "[[@input(inputType='confirm', content=anyQuestion + ' ' + title, memoized=false, format=(answer) => answer ? 'Yes' : 'No')]]"
do:
- ---
- -> trans(content="What do you want to know?", lang) -> input(memoized=false) -> $set('question') -> $print()
- user: "{{question}}"
- assistant: "Let me explain it more clearly: [[answer:temperature=0.01]]"
- -> trans(lang) -> $print(content)
# call guide.ai.yaml lib to return main entry point
# - $echo: "@guide"
# - $echo: "[[@guide]]"
2 changes: 1 addition & 1 deletion lib/guide/guide_lib_list.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ file: "README.md"
# - Output JSON format, following the JSON schema:
# {{output}}
# ---
# @file({{__dirname + file}})
# [[@file({{__dirname + file}})]]
# - user: Output all urls with title in markdown format from the README.md
# - assistant: "[[urls:temperature=0.01]]"
# - -> summary(file={{__dirname + file}}, content)
Expand Down
2 changes: 1 addition & 1 deletion lib/guide/instructor.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ description: |-
- system: |-
You are an expert Programmable Prompt Engine(PPE) Language Script instructor, good at writing and communication.
---
@file({{__dirname+"README.md"}}, onlyContent=true)
[[@file({{__dirname+"README.md"}}, onlyContent=true)]]
10 changes: 5 additions & 5 deletions lib/guide/lang-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,34 @@ If you want to select one randomly from the list using the computer's local rand
In messages, we support content substitution by invoking scripts or instructions. The script or instructions must return a string value. For example:

```yaml
user: "#five plus two equals @calculator(5+2)"
user: "#five plus two equals [[@calculator(5+2)]]"
```
Notes:
* The prefix `#` indicates immediate formatting of the string.
* The prefix `@` indicates calling an external script with the ID `calculator`. if there are no parameters, you must omit the parentheses.
* The Invocation formatting should be placed within two square brackets. The prefix `@` indicates calling an external script with the ID `calculator`. if there are no parameters, you must omit the parentheses.
* If placed within text, ensure there is at least one space before and after. Extra spaces will be removed after substitution.

Here’s an example of how to load a file and generate a summary using this method:

```yaml
user: |-
Generate a summary for the following file:
@file(file.txt)
[[@file(file.txt)]]
```

## Internal Instruction Invocation Formatting

Internal Instruction Invocation Formatting similarly External Script Invocation Formatting. just add `$` prefix. To call an internal instruction, use the prefix `$`, such as `@$echo`; eg: `@$echo("hi world")`
Internal Instruction Invocation Formatting similarly External Script Invocation Formatting. just add `$` prefix. To call an internal instruction, use the prefix `$`, such as `@$echo`; eg: `[[@$echo("hi world")]]`

```yaml
# define a internal instruction by javascript function
!fn |-
function inc(n) {
return n + 1
}
user: "3 increment 1 is: @$inc(3)"
user: "3 increment 1 is: [[@$inc(3)]]"
```

## Regular Expression (RegExp) Formatting
Expand Down
6 changes: 3 additions & 3 deletions lib/guide/lang-reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ In messages, we support content substitution by invoking scripts or instructions
```yaml
# the `#` prefix means immediate formatting
# call the `calculator.ai.yaml` script with the parameter '5+2'
user: "#five plus two equals @calculator('5+2')"
user: "#five plus two equals [[@calculator('5+2')]]"
```
Notes:
* The prefix `#` indicates immediate formatting of the string.
* The prefix `@` indicates calling an external script with the ID `calculator`. if there are no parameters, you must omit the parentheses.
* The Invocation formatting should be placed within two square brackets. The prefix `@` indicates calling an external script with the ID `calculator`. if there are no parameters, you must omit the parentheses.
* If placed within text, ensure there is at least one space before and after. Extra spaces will be removed after substitution.

Here’s an example of how to load a file and generate a summary using this method:

```yaml
user: |-
Generate a summary for the following file:
@file(file.txt)
[[@file(file.txt)]]
```
4 changes: 2 additions & 2 deletions lib/guide/lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ The role messages can be formatted using Jinja2 templates and advanced replaceme
* **AI Parameter Passing:** AI parameters can be passed within double brackets (e.g., `[[Answer:temperature=0.7]]`).
* **Constrained AI Responses:** Double brackets can enforce that AI responses are limited to specific options (e.g., `[[FRUITS:|Apple|Banana]]`).
* **Script and Instruction Replacement:** call the script or instruction in the message text. The script or instruction's output is then replaced into the message text.
* **External Script Replacement:** External scripts can be invoked using the `@` symbol (e.g., `@say_hi_script(param1=value1, p2=v2)`, it will be replaced by `hi`).
* **Internal Instruction Replacement:** Internal instructions can be called and replaced similarly (e.g., `@$instruction(param1=value1)`).
* **External Script Replacement:** External scripts can be invoked using the `@` symbol (e.g., `[[@say_hi_script(param1=value1, p2=v2)]]`, it will be replaced by `hi`).
* **Internal Instruction Replacement:** Internal instructions can be called and replaced similarly (e.g., `[[@$instruction(param1=value1)]]`).
* **Regular Expression Replacement:** `/RegExp/[RegOpts]:Answer[:index_or_group_name]` allows for pattern-based replacement on the `Answer` variable.

### Chaining and Script Function Calls
Expand Down
2 changes: 1 addition & 1 deletion lib/guide/trans.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ content: "This is a test."
then:
- $set:
lang: "$getLanguageFromIso6391(lang)"
- $echo: "@translator(content=content, terms='- PPE: Programmable Prompt Engine', lang='English', target=lang)"
- $echo: "[[@translator(content=content, terms='- PPE: Programmable Prompt Engine', lang='English', target=lang)]]"
else:
- $echo: ?=content
4 changes: 2 additions & 2 deletions lib/summary.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: |-
Summarize the content or a text file in detail, capturing the key points and essence of the document.
It takes a filepath as input and outputs a concise summary that reflects the main theme or content.
It can be used in prompt. eg, `Summary: @summary(file=document.md)`
It can be used in prompt. eg, `Summary: [[@summary(file=document.md)]]`
Or, run it directly:
Expand All @@ -32,7 +32,7 @@ output:
- $if: "this.file"
then:
$set:
fileContent: "@file({{file}})"
fileContent: "[[@file({{file}})]]"
- system: |-
Summarize the following content or file provided by the user in detail, capturing the key points and essence of it.
{% if len and len > 0 %}
Expand Down
4 changes: 2 additions & 2 deletions lib/titleify.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: |-
Summarize the content or a text file into a single best title that captures the essence of the document.
It takes a filepath as input and outputs a concise title that reflects the main theme or content.
It can be used in prompt. eg, `Title: @titleify(file=document.md)`
It can be used in prompt. eg, `Title: [[@titleify(file=document.md)]]`
Or, run it directly:
Expand All @@ -32,7 +32,7 @@ temperature: 0.01 # the default temperature of the LLM.
- $if: "this.file"
then:
$set:
fileContent: "@file({{file}})"
fileContent: "[[@file({{file}})]]"
- system: |-
Summarize the content provided by the user into some best titles that captures the essence of the document.
{%- if len and len > 0 -%}
Expand Down
4 changes: 2 additions & 2 deletions lib/translator.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 0.1.1
name: "Translator"
description: |-
Translate the content or a file to the target language.
It can be used in prompt. eg, `assistant: "Translation: @translator(file='document.md', target='English')"`
It can be used in prompt. eg, `assistant: "Translation: [[@translator(file='document.md', target='English')]]"`
Or, run it directly:
Expand Down Expand Up @@ -56,7 +56,7 @@ parameters:
- $if: "this.file"
then:
$set:
content: "@file({{file}})"
content: "[[@file({{file}})]]"
- system: |-
You are the best master of translation in the world.
- $if: "this.content"
Expand Down
2 changes: 1 addition & 1 deletion lib/url.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type: lib
description: |-
the simple fetch web page content from url.
It can be used in prompt. eg, `user: "think about the following web content: @url(https://www.example.com)"`
It can be used in prompt. eg, `user: "think about the following web content: [[@url(https://www.example.com)]]"`
tag:
- url
- loader
Expand Down

0 comments on commit 9082a8d

Please sign in to comment.