Skip to content

Commit

Permalink
feat(guide): add Invocation of External Scripts section
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Sep 2, 2024
1 parent 20e969a commit feedc42
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/guide/lang-reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,79 @@ system: |-
{{description}}
```

## Invocation of External Scripts

### Chains Invocation of Agent Scripts Or Instructions

Within messages, results can be forwarded to other agents.

If no parameters are specified, the AI outcome will be passed as the `content` parameter to the agent. For instance,

`list-expression.ai.yaml`:

```yaml
system: Only list the calculation expression, do not calculate the result
---
user: "Three candies plus five candies."
assistant: "[[CalcExpression]]"
# The actual input to the agent in this case is: {content: "[AI-generated calculation expression]"}
-> calculator
$echo: "#A total of {{LatestResult}} pieces of candy"
```

`calculator.ai.yaml`:


```yaml
---
# Below is the front-matter configuration
parameters:
response_format:
type: "json"
output:
type: "number"
---
# Below is the script
system: Please as a calculator to calculate the result of the following expression. Only output the result.
--- # mark the beginning of new dialogue
user: "{{content}}"
```

When parameters are included, the AI `content` is combined with these parameters and forwarded together to the agent. For example,

```yaml
user: "Tell me a joke!"
assistant: "[[JOKE]]"
# The actual input to the agent here is: {content: "[This is a joke generated by AI]", target_lang: "Portuguese"}
-> translator(target_lang="Portuguese") -> $print
```

**Note**:

* Call internal instruction with `$` prefix
* If the script returns a value of type `string`/`boolean`/`number`, that return value will be placed to the `content` field. If the return value is an `object`, its contents will be directly passed to the agent.

### External Script Invocation Formatting

In messages, we support content substitution by invoking scripts or instructions. The script or instructions must return a string value. For example:

```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')"
```
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.
* 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)
```

0 comments on commit feedc42

Please sign in to comment.