跳至内容

你的第一个GenAI脚本

GenAIScript 使用风格化的 JavaScript 并保持最简语法。 它们以文件形式存储在您的项目中(genaisrc/*.genai.mjsgenaisrc/*.genai.mts)。 执行 genaiscript 会创建将发送给大语言模型的提示词。

  1. 使用命令面板中的> GenAIScript: Create new script...命令 (Windows/Linux上按Ctrl+Shift+P,Mac上按⇧⌘P) 来创建新脚本。

    A search bar with the text "createn" entered, displaying a suggestion for "GenAIScript: Create new script..." in a dropdown menu.

  2. 生成的文件将被放置在您项目的genaisrc文件夹中。

    • Directorygenaisrc scripts are created here by default
      • genaiscript.d.ts (TypeScript类型定义)
      • jsconfig.json (TypeScript编译器配置)
      • proofreader.genai.mjs

提示词

GenAIScript的执行会生成一个提示(以及其他内容)发送给LLM模型。

$``...`` 模板字符串函数会格式化并将字符串写入提示中,然后发送给LLM。

poem.genai.mjs
$`Write a one sentence poem.`
👤 user
Write a one sentence poem.
🤖 assistant
Roses bloom, hearts swoon, under the silver moon.

上下文

GenAIScript通过env变量暴露上下文。上下文由您开始执行脚本的位置隐式定义。

  • 你可以右键点击一个文件夹,env.files将包含该文件夹下所有嵌套的文件。
  • 你可以右键点击文件或文件内部,此时env.files将仅包含该文件。
  • 你可以通过命令行界面运行脚本,并在CLI参数中指定env.files的内容。
proofreader.genai.mjs
def("FILES", env.files)
👤 user
FILES:
```md file="src/samples/markdown.md"
---
title: What is Markdown? - Understanding Markdown Syntax
description: Learn about Markdown, a lightweight markup language for formatting plain text, its syntax, and how it differs from WYSIWYG editors.
keywords: Markdown, markup language, formatting, plain text, syntax
sidebar: mydoc_sidebar
---
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.
Using Markdown is different than using a WYSIWYG editor. In an application like Microsoft Word, you click buttons to format words and phrases, and the changes are visible immediately. Markdown isn’t like that. When you create a Markdown-formatted file, you add Markdown syntax to the text to indicate which words and phrases should look different.
For example, to denote a heading, you add a number sign before it (e.g., # Heading One). Or to make a phrase bold, you add two asterisks before and after it (e.g., **this text is bold**). It may take a while to get used to seeing Markdown syntax in your text, especially if you’re accustomed to WYSIWYG applications. The screenshot below shows a Markdown file displayed in the Visual Studio Code text editor....
```

任务

$函数用于构建提示文本,它会渲染并将文本写入提示中($是一个模板字面量)。

proofreader.genai.mjs
def("FILES", env.files)
$`You are an expert technical writer and proofreader.
Review the documents in FILE and report the 2 most important issues.`
👤 user
FILES:
```md file="src/samples/markdown.md"
---
title: What is Markdown? - Understanding Markdown Syntax
description: Learn about Markdown, a lightweight markup language for formatting plain text, its syntax, and how it differs from WYSIWYG editors.
keywords: Markdown, markup language, formatting, plain text, syntax
sidebar: mydoc_sidebar
---
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.
Using Markdown is different than using a WYSIWYG editor. In an application like Microsoft Word, you click buttons to format words and phrases, and the changes are visible immediately. Markdown isn’t like that. When you create a Markdown-formatted file, you add Markdown syntax to the text to indicate which words and phrases should look different.
For example, to denote a heading, you add a number sign before it (e.g., # Heading One). Or to make a phrase bold, you add two asterisks before and after it (e.g., **this text is bold**). It may take a while to get used to seeing Markdown syntax in your text, especially if you’re accustomed to WYSIWYG applications. The screenshot below shows a Markdown file displayed in the Visual Studio Code text editor....
```
You are an expert technical writer and proofreader.
Review the documents in FILE and report the 2 most important issues.
🤖 assistant
I reviewed the document in "src/samples/markdown.md" and found the following two important issues:
1. **Missing Consistency in Heading Styles**: The document lacks consistency in heading styles, which can make the structure of the content unclear. For instance, it should use either the "atx-style" (with # symbols for headings) or the "setext-style" (with underlining for headings) consistently throughout the document to maintain a clear visual hierarchy.
2. **Lack of Examples**: The document describes Markdown syntax but lacks concrete examples to illustrate how to use Markdown for formatting. Including examples would enhance the reader's understanding of the syntax and its practical application.
These are the two most important issues that need to be addressed in the document.

元数据

您可以添加对script函数的调用来提供关于脚本和模型的元数据。这些元数据用于在用户界面中显示脚本并配置LLM模型。

proofreader.genai.mjs
// the metadata
script({
// user interface
title: "Technical proofreading",
description: "Reviews the text as a tech writer.",
group: "documentation",
// model configuration
model: "large",
temperature: 0,
})
def("FILES", env.files)
$`You are an expert technical writer and proofreader.
Review the documents in FILE and report the 2 most important issues.`
👤 user
FILES:
```md file="src/samples/markdown.md"
---
title: What is Markdown? - Understanding Markdown Syntax
description: Learn about Markdown, a lightweight markup language for formatting plain text, its syntax, and how it differs from WYSIWYG editors.
keywords: Markdown, markup language, formatting, plain text, syntax
sidebar: mydoc_sidebar
---
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.
Using Markdown is different than using a WYSIWYG editor. In an application like Microsoft Word, you click buttons to format words and phrases, and the changes are visible immediately. Markdown isn’t like that. When you create a Markdown-formatted file, you add Markdown syntax to the text to indicate which words and phrases should look different.
For example, to denote a heading, you add a number sign before it (e.g., # Heading One). Or to make a phrase bold, you add two asterisks before and after it (e.g., **this text is bold**). It may take a while to get used to seeing Markdown syntax in your text, especially if you’re accustomed to WYSIWYG applications. The screenshot below shows a Markdown file displayed in the Visual Studio Code text editor....
```
You are an expert technical writer and proofreader.
Review the documents in FILE and report the 2 most important issues.
🤖 assistant
File src/samples/markdown.md:
1. **Missing Consistency in Heading Styles**: The document lacks consistency in heading styles. For instance, it uses both "What is Markdown?" and "What is Markdown" as headings. Consistency in heading styles is crucial for a professional and polished document.
2. **Lack of Visual Examples**: While the document explains Markdown syntax, it would benefit from visual examples to illustrate the formatting. Visual examples can enhance understanding, especially for readers who are new to Markdown.
These are the two most important issues in the document.

titledescriptiongroup属性用于在用户界面中显示脚本,当用户搜索脚本时这些属性会很有帮助。

A screenshot of a text editor showing a task labeled "Technical proofreading" with the description "Reviews the text as a tech writer." A hyperlink labeled "documentation" is on the right.

下一步