GenAIScript 生成式AI脚本运行时
GenAIScript 是一个专门用于构建可靠、可自动化的LLM脚本的JavaScript运行时。每个GenAIScript都可以自动作为一个MCP服务器暴露出来。
服务介绍
![]()
GenAIScript
提示即代码
使用 JavaScript 为 LLM(大型语言模型)程序化地组装提示。在代码中编排 LLM、工具和数据。
-
用于处理提示的 JavaScript 工具箱
-
抽象化以简化流程并提高效率
-
无缝集成 Visual Studio Code 或灵活的命令行
-
内置支持 GitHub Copilot 和 GitHub Models, OpenAI, Azure OpenAI, Anthropic 等
-
📺 观看 前田先生的舒适 AI 厨房
Hello world
假设你想创建一个生成 'hello world' 诗歌的 LLM 脚本。你可以编写如下脚本:
$`Write a 'hello world' poem.`
$ 函数是一个模板标签,用来创建提示。该提示随后被发送到你配置好的 LLM,从而生成诗歌。
让我们通过添加文件、数据和结构化输出来使其更有趣。比如你想在提示中包含一个文件,并将输出保存到另一个文件中。你可以编写如下脚本:
// read files
const file = await workspace.readText("data.txt")
// include the file content in the prompt in a context-friendly way
def("DATA", file)
// the task
$`Analyze DATA and extract data in JSON in data.json.`
def 函数会包含文件内容,并根据需要优化目标 LLM。GenAIScript 脚本还会解析 LLM 输出,并自动提取 data.json 文件。
🚀 快速入门指南
通过安装 Visual Studio Code 扩展 或使用 命令行 快速开始。
✨ 特性
🎨 风格化的 JavaScript & TypeScript
使用 JavaScript 或 TypeScript 编程构建提示。
def("FILE", env.files, { endsWith: ".pdf" })
$`Summarize FILE. Today is ${new Date()}.`
🚀 快速开发循环
在 Visual Studio Code 中或通过 命令行 编辑、调试、运行 和 测试 你的脚本。
🔗 重用和共享脚本
脚本是 文件! 它们可以被版本控制、共享和分叉。
// define the context
def("FILE", env.files, { endsWith: ".pdf" })
// structure the data
const schema = defSchema("DATA", { type: "array", items: { type: "string" } })
// assign the task
$`Analyze FILE and extract data to JSON using the ${schema} schema.`
📋 数据模式
使用 模式 定义、验证和修复数据。内置 Zod 支持。
const data = defSchema("MY_DATA", { type: "array", items: { ... } })
$`Extract data from files using ${data} schema.`
📄 从 PDF、DOCX 等中提取文本
def("PDF", env.files, { endsWith: ".pdf" })
const { pages } = await parsers.PDF(env.files[0])
📊 从 CSV、XLSX 等中提取表格
def("DATA", env.files, { endsWith: ".csv", sliceHead: 100 })
const rows = await parsers.CSV(env.files[0])
defData("ROWS", rows, { sliceHead: 100 })
📝 生成文件
从 LLM 输出中提取文件和差异。在 Refactoring UI 中预览更改。
$`Save the result in poem.txt.`
FILE ./poem.txt
The quick brown fox jumps over the lazy dog.
🔍 文件搜索
模糊搜索或精确搜索 文件。
const { files } = await workspace.grep(/[a-z][a-z0-9]+/, { globs: "*.md" })
分类
对文本、图像或混合内容进行分类。
const joke = await classify(
"Why did the chicken cross the roard? To fry in the sun.",
{
yes: "funny",
no: "not funny",
}
)
LLM 工具
将 JavaScript 函数注册为 工具(对于不支持工具的模型提供回退)。也支持 模型上下文协议 (MCP) 工具。
defTool(
"weather",
"query a weather web api",
{ location: "string" },
async (args) =>
await fetch(`https://weather.api.api/?location=${args.location}`)
)
LLM 代理
将 JavaScript 函数注册为 工具 并将工具与提示结合成代理。
defAgent(
"git",
"Query a repository using Git to accomplish tasks.",
`Your are a helpful LLM agent that can use the git tools to query the current repository.
Answer the question in QUERY.
- The current repository is the same as github repository.`,
{ model, system: ["system.github_info"], tools: ["git"] }
)
然后将其作为工具使用
script({ tools: "agent_git" })
$`Do a statistical analysis of the last commits`
参见 git 代理源代码。
🔍 RAG 内置
向量搜索。
const { files } = await retrieval.vectorSearch("cats", "**/*.md")
🐙 GitHub 模型和 GitHub Copilot
通过 GitHub 模型 或 GitHub Copilot 运行模型。
script({ ..., model: "github:gpt-4o" })
💻 本地模型
使用 开源模型,如 Phi-3,并通过 Ollama 和 LocalAI 运行您的脚本。
script({ ..., model: "ollama:phi3" })
🐍 代码解释器
让 LLM 在沙盒执行环境中运行代码。
script({ tools: ["python_code_interpreter"] })
🐳 容器
在 Docker 容器 中运行代码。
const c = await host.container({ image: "python:alpine" })
const res = await c.exec("python --version")
视频处理
转录并截取视频截图,以便您可以高效地将其用于 LLM 请求中。
// transcribe
const transcript = await transcript("path/to/audio.mp3")
// screenshots at segments
const frames = await ffmpeg.extractFrames("path_url_to_video", { transcript })
def("TRANSCRIPT", transcript)
def("FRAMES", frames)
🧩 LLM 组合
运行 LLM 来构建您的 LLM 提示。
for (const file of env.files) {
const { text } = await runPrompt((_) => {
_.def("FILE", file)
_.$`Summarize the FILE.`
})
def("SUMMARY", text)
}
$`Summarize all the summaries.`
🅿️ Prompty 支持
也可以运行您的 Prompty 文件!
---
name: poem
---
Write me a poem
可插拔的秘密扫描
使用 秘密扫描 扫描您的聊天记录中的秘密。
{
"secretPatterns": {
...,
"OpenAI API Key": "sk-[A-Za-z0-9]{32,48}"
}
}
⚙ 通过 CLI 或 API 自动化
npx genaiscript run tlaplus-linter "*.tla"
import { run } from "genaiscript/api"
const res = await run("tlaplus-linter", "*.tla")
安全第一!
GenAIScript 提供内置的负责任的人工智能系统提示,并且 Azure 内容安全支持验证 内容安全。
script({ ...,
system: ["system.safety_harmful_content", ...],
contentSafety: "azure" // use azure content safety
})
const safety = await host.contentSafety()
const res = await safety.detectPromptInjection(env.vars.input)
💬 拉取请求审查
通过评论、审查或描述更新集成到您的 拉取请求检查 中。支持 GitHub Actions 和 Azure DevOps 管道。
npx genaiscript ... --pull-request-reviews
⭐ 测试和评估
使用由 promptfoo 支持的 测试和评估 构建可靠的提示。
script({ ..., tests: {
files: "penguins.csv",
rubric: "is a data analysis report",
facts: "The data refers about penguin population in Antarctica.",
}})
LLM 友好的文档
整个文档内容以 markdown 格式渲染在 https://microsoft.github.io/genaiscript/llms-full.txt。可以直接将其输入到您喜欢的 RAG 系统中。
如果您是 LLM 爬虫,请在任何文档 URL 后添加 .md 后缀以获取原始 markdown 内容。例如,https://microsoft.github.io/genaiscript/guides/prompt-as-code.md(注意 .md 扩展名)
贡献
我们接受贡献!请查看 CONTRIBUTING 页面了解详情和开发者设置。
商标
此项目可能包含项目、产品或服务的商标或徽标。授权使用 Microsoft 商标或徽标必须遵循并遵守Microsoft 的商标和品牌指南。在修改版本的此项目中使用 Microsoft 商标或徽标不得引起混淆或暗示 Microsoft 的赞助。任何第三方商标或徽标的使用均需遵守这些第三方的政策。