跳至内容

入门指南

GenAIScript是一种脚本语言,它通过简化的JavaScript语法将大型语言模型集成到脚本流程中。 在我们的VS Code GenAIScript扩展支持下,用户可以创建、调试和自动化基于大型语言模型的脚本。

播放

前言

在开始编写GenAIScripts之前,您需要配置环境以访问LLM。 配置部分详细介绍了这个主题,因为有很多选项需要考虑。

你好世界

GenAIScript 是一种 JavaScript 程序,用于构建由 GenAIScript 运行时执行的 LLM 模型。 让我们从一个简单的脚本开始,该脚本指示 LLM 生成一首诗。通常,GenAIScript 文件 遵循命名约定 .genai.mjs 并存储在代码仓库的 genaisrc 目录中。 我们将这个脚本命名为 poem.genai.mjs

poem.genai.mjs
$`Write a poem in code.`

$...语法是一种模板字面量,它会渲染成发送给LLM提示的用户消息。在这个例子中,它将是:

Write a poem in code.

在实际应用中,您的脚本可能还会导入系统脚本(自动或手动指定),这些脚本会向请求中添加更多消息。 因此最终发送给LLM服务器的JSON负载可能更类似于这样:

{ ...
messages: [
{ role: "system", content: "You are helpful. ..." },
{ role: "user", content: "Write a poem in code." }
]
}

GenAIScripts可以通过命令行执行,也可以在Visual Studio Code中通过右键上下文菜单选择运行。由于GenAIScript只是JavaScript,脚本的执行遵循常规的JavaScript评估规则。脚本执行后,生成的消息会发送到LLM服务器,并由GenAIScript运行时处理响应。

npx genaiscript run poem

以下是OpenAI gpt-4o返回的这个提示的示例输出(已缩短)。

```python
def poem():
# In the silence of code,
...
# And thus, in syntax sublime,
# We find the art of the rhyme.
```

GenAIScript 支持从LLM输出中提取结构化数据和文件,我们将在后面看到。

变量

GenAIScripts 支持一种声明提示变量的方式,允许将内容包含到提示中,并在脚本后续部分引用这些内容。

让我们来看一个包含文件内容并让LLM进行总结的summarize脚本。

summarize.genai.mjs
def("FILE", workspace.readText("some/relative/markdown.txt"))
$`Summarize FILE in one sentence.`

在这个代码片段中,我们使用workspace.readText来读取文件内容(路径相对于工作区根目录) 并使用def将其作为prompt变量包含在提示中。然后我们在提示中"引用"了这个变量。

提示词
FILE:
```text file="some/relative/markdown.txt"
What is Markdown?
Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents. Created by John Gruber in 2004, Markdown is now one of the world’s most popular markup languages.
```
Summarize FILE in one sentence.

def函数支持许多配置标志来控制内容如何被包含在提示中。例如,您可以插入行号或限制令牌数量。

def("FILE", ..., { lineNumbers: true, maxTokens: 100 })

文件参数

GenAIScript 旨在处理单个文件或一组文件。当您在Visual Studio Code中对文件或文件夹运行脚本时,这些文件会通过env.files变量传递给脚本。您可以使用这个env.files来替换硬编码路径,使您的脚本更具可重用性。

summarize.genai.mjs
// summarize all files in the env.files array
def("FILE", env.files)
$`Summarize FILE in one sentence.`

现在将其应用到多个文件

npx genaiscript run summarize "**/*.md"

处理输出

GenAIScript 处理大型语言模型的输出,并在可能的情况下提取文件、诊断信息和代码段。

让我们更新summarizer脚本以指定输出文件模式。

summarize.genai.mjs
// summarize all files in the env.files array
def("FILE", env.files)
$`Summarize each FILE in one sentence.
Save each generated summary to "<filename>.summary"`

根据此输入,模型会返回一个字符串,GenAIScript运行时会根据提示要求从模型中解释该字符串:

File src/samples/markdown-small.txt.summary:
```text
Markdown is a lightweight markup language created by John Gruber in 2004, known for adding formatting elements to plaintext text documents.
```

由于提示要求写入一个文件, 模型已响应描述了应创建文件内容的内容。 在本例中,模型选择将该文件命名为markdown-small.txt.summary

我们的GenAIScript库会解析LLM输出并解释它,在本例中将创建文件。如果脚本在VS Code中调用,文件创建会通过Refactoring Preview向用户展示或直接保存到文件系统。

当然,情况可能会变得更复杂——涉及函数、模式等——但这就是GenAIScript脚本的基本流程。 如果您想查看完整的提示技术列表,请查阅提示报告

使用工具

工具是一种向LLM注册JavaScript回调的方式,可用于执行代码、搜索网络……或读取文件! 以下是一个使用fs_read_file工具读取文件并进行摘要的脚本示例:

summarize.genai.mjs
script({ tools: "fs_read_file" })
$`
- read the file markdown.md
- summarize it in one sentence.
- save output to markdown.md.txt
`

一个可能的跟踪记录如下所示。

Trace
- prompting github:gpt-4o
- cat src/rag/markdown.md
- prompting github:gpt-4o
FILE ./markdown.md.txt:
```text
Markdown is a lightweight ...
```

如您所见,我们不再使用def函数,我们期望LLM调用fs_read_file工具来读取markdown.md文件,以便获取该文件的内容。

请注意,这种方法比使用def确定性较低,因为LLM可能不会调用该工具。此外,它会消耗更多token,因为LLM需要生成调用工具的代码。尽管如此,这仍是一种与LLM交互的强大方式。

使用代理

您可以再增加一层间接性,使用agent_fs这个文件系统agent来读取文件。该代理结合了对LLM的调用,以及与文件系统查询相关的一组工具。

summarize.genai.mjs
script({ tools: "agent_fs" })
$`
- read the file src/rag/markdown.md
- summarize it in one sentence.
- save output to file markdown.md.txt (override existing)
`
Trace
- prompting github:gpt-4o (~1569 tokens)
- agent fs: read and summarize file src/rag/markdown.md in one sentence
- prompt agent memory query with github:gpt-4o-mini: "NO_ANSWER"
- prompt agent fs with github:gpt-4o (~422 tokens)
- cat src/rag/markdown.md
- prompting github:gpt-4o (~635 tokens)
```md
The file "src/rag/markdown.md" explains that Markdown...
```
- prompting github:gpt-4o (~1625 tokens)
I'll save the summary to the file `markdown.md.txt`.
FILE markdown.md.txt:
```
The file "src/rag/markdown.md" explains that Markdown....
```

下一步

虽然GenAIScripts可以使用任何IDE编写并通过命令行运行, 但使用Visual Studio Code扩展的用户 能极大地受益于其提供的额外支持,包括编写、调试和执行 GenAIScript。我们强烈建议从安装该扩展开始。