教程笔记本
本笔记本是一个GenAIScript教程。它是一个Markdown文档,其中每个JavaScript代码部分都是可运行的GenAIScript。您可以单独执行每个代码块,并在代码块下方的输出部分查看结果。要在Visual Studio Code中打开此笔记本,请按F1并运行GenAIScript: Create GenAIScript Markdown Notebook。
按照配置中的步骤设置您的环境和LLM访问权限。
提示即代码
GenAIScript 允许您将提示词编写为JavaScript程序。GenAIScript 会运行您的程序;生成聊天消息;然后处理与LLM API的剩余交互。
写入提示符 $
让我们从一个简单的"Hello World"程序开始。
$`Say "hello!" in emojis`
👤 user
Say "hello!" in emojis
🤖 assistant
👋😃!
$
函数用于格式化字符串并将其写入用户消息。该用户消息会被添加到聊天消息中并发送至LLM API。在代码片段下方,您可以查看用户消息(由我们程序生成)和助手(LLM)的响应。
您可以通过点击代码块左上角的执行单元格按钮来运行代码块。默认情况下它会尝试使用来自不同提供商的LLMs。如果需要使用不同的模型,请更新文档开头front matter中的model
字段。configuration中记录了许多选项。
执行完成后,您还会看到一个额外的trace条目,允许您深入了解GenAIScript执行的内部细节。这对于诊断提示词(prompt)相关问题非常有帮助。由于追踪信息可能非常庞大,因此不会在markdown文件中序列化这些数据。
您可以使用JavaScript的for
循环并通过多次$
调用来向用户消息追加文本。您还可以使用内部表达式来生成动态内容。
// let's give 3 tasks to the LLM// to get 3 different outputsfor (let i = 1; i <= 3; i++) $`- Say "hello!" in ${i} emojis.`$`Respond with a markdown list`
👤 user
- Say "hello!" in 1 emojis.- Say "hello!" in 2 emojis.- Say "hello!" in 3 emojis. Respond with a markdown list
🤖 assistant
- 👋- 👋😊- 👋✨😃
总结一下,GenAIScript运行后会生成用户消息;这些消息会被发送给LLM。您可以在追踪记录中查看该用户消息(以及其他消息)。
def
和 env.files
def
function 允许您声明和分配LLM变量。变量的概念最适用于导入上下文数据(特别是文件),并在提示的其余部分引用它们。
def("FILE", env.files)$`Summarize FILE in one short sentence. Respond as plain text.`
👤 user
FILE:
```md file="src/samples/markdown.md"---title: What is Markdown? - Understanding Markdown Syntaxdescription: 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, syntaxsidebar: 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....```
Summarize FILE in one short sentence. Respond as plain text.
🤖 assistant
Markdown is a lightweight markup language for formatting plain text, using syntax to indicate formatting elements.
在GenAIScript中,env.files
变量包含上下文中的文件列表,这可以通过用户在界面中的选择、命令行参数或像本脚本中预先配置的方式来确定。您可以通过编辑文档开头前置元数据中的files
字段来更改env.files
中的文件。
过滤 env.files
当从用户界面使用GenAIScript时,通常会将脚本应用于整个文件夹。这意味着您会在env.files
中获得一堆文件,包括一些不需要的文件。def
函数提供了各种选项来过滤文件,例如endsWith
选项。
def
还提供了 maxTokens
参数,可以将内容大小修剪为指定数量的token。请注意LLM的上下文长度是有限的!
script({ files: "src/**" }) // glob all files under src/samplesdef("FILE", env.files, { endsWith: ".md", maxTokens: 1000 }) // only consider markdown files$`Summarize FILE in one short sentence. Respond as plain text.`
👤 user
FILE:
```md file="src/samples/markdown.md"---title: What is Markdown? - Understanding Markdown Syntaxdescription: 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, syntaxsidebar: 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....```
Summarize FILE in one short sentence. Respond as plain text.
🤖 assistant
Markdown is a lightweight markup language for formatting plaintext documents, different from WYSIWYG editors.
工具
你可以将JavaScript函数注册为工具,LLM会根据需要调用它们。
// requires openai, azure openai or github modelsdefTool( "fetch", "Download text from a URL", { url: "https://..." }, ({ url }) => host.fetchText(url))
$`Summarize https://raw.githubusercontent.com/microsoft/genaiscript/main/README.md in 1 sentence.`
子提示
您可以运行嵌套的LLM模型在其他较小的模型上执行任务。
// summarize each files individuallyfor (const file of env.files) { const { text } = await runPrompt((_) => { _.def("FILE", file) _.$`Summarize the FILE.` }) def("FILE", { ...file, content: text })}// summarize all summaries$`Summarize FILE.`