关于 GenAIScript
GenAIScript 是用 JavaScript 以编程方式组装提示词的脚本框架,可在代码中编排大模型、工具和数据。提供 Visual Studio Code 集成和命令行,面向希望自动化 GenAI 任务的开发者。
主要功能
- 01用 JavaScript 或 TypeScript 构建提示词
- 02基于 schema 定义、校验并修复结构化输出
- 03解析 PDF、DOCX、CSV、XLSX 等文件
- 04支持注册工具与 Agent,兼容 MCP 工具
- 05内置向量检索,可用 Ollama 等本地模型
- 06提供 CLI 与 API,并含测试与评估
适用场景
- 01从 PDF 或表格中提取结构化 JSON
- 02在 GitHub Actions 或 Azure DevOps 中自动评审 Pull Request
- 03把函数注册为工具,组合成 Agent
- 04对视频转写并截图后交给大模型分析
MCP 相关
GenAIScript 支持使用 Model Context Protocol(MCP)工具,可与自定义 JavaScript 工具一同供大模型调用。
详细介绍
![]()
GenAIScript
提示即代码
使用 JavaScript 以编程方式组装大模型的提示词。在代码中编排大模型、工具和数据。
-
用于处理提示词的 JavaScript 工具箱
-
简单高效的抽象层
-
无缝集成 Visual Studio Code,也可灵活使用命令行
-
内置支持 GitHub Copilot 和 GitHub Models、OpenAI、Azure OpenAI、Anthropic 等
-
📄 阅读在线文档:microsoft.github.io/genaiscript
-
💬 加入 Discord 服务器
-
📝 阅读博客了解最新动态
-
🤖 Agent - 阅读 llms-full.txt
Hello world
假设你想创建一个大模型脚本,生成一首“hello world”诗。你可以编写如下脚本:
$`Write a 'hello world' poem.`
$ 函数是一个用于创建提示词的模板标签。随后,该提示词会被发送到(你已配置的)大模型,由其生成诗歌。
让我们让它更有趣一些:加入文件、数据和结构化输出。假设你想在提示词中包含一个文件,然后将输出保存到文件中。你可以编写如下脚本:
// 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 函数会包含文件内容,并在必要时针对目标大模型对其进行优化。GenAIScript 脚本还会解析大模型的输出,并自动提取 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 })
📝 生成文件
从大模型的输出中提取文件和差异。在 Refactoring UI 中预览更改。
$`Save the result in poem.txt.`
FILE ./poem.txt
The quick brown fox jumps over the lazy dog.
🔍 文件搜索
通过 grep 或模糊搜索文件。
const { files } = await workspace.grep(/[a-z][a-z0-9]+/, { globs: "*.md" })
分类
对文本、图像或两者的混合内容进行分类。
const joke = await classify(
"Why did the chicken cross the road? To fry in the sun.",
{
yes: "funny",
no: "not funny",
}
)
大模型工具
将 JavaScript 函数注册为工具 (对不支持工具的模型提供回退机制)。同时支持 Model Context Protocol (MCP) 工具。
defTool(
"weather",
"query a weather web api",
{ location: "string" },
async (args) =>
await fetch(`https://weather.api.api/?location=${args.location}`)
)
大模型 Agent
将 JavaScript 函数注册为工具,并把工具与提示词组合成 Agent。
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 Agent 源码。
🔍 内置 RAG
向量搜索。
const { files } = await retrieval.vectorSearch("cats", "**/*.md")
🐙 GitHub Models 和 GitHub Copilot
通过 GitHub Models 或 GitHub Copilot 运行模型。
script({ ..., model: "github:gpt-4o" })
💻 本地模型
使用 Ollama、LocalAI,通过开源模型(如 Phi-3)运行你的脚本。
script({ ..., model: "ollama:phi3" })
🐍 代码解释器
让大模型在沙箱执行环境中运行代码。
script({ tools: ["python_code_interpreter"] })
🐳 容器
在 Docker 容器中运行代码。
const c = await host.container({ image: "python:alpine" })
const res = await c.exec("python --version")
视频处理
对视频进行转录和截图,以便高效地将其输入到大模型请求中。
// 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)
🧩 大模型组合
运行大模型来构建你的大模型提示词。
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}"
}
}
⚙ 通过命令行或 API 自动化
npx genaiscript run tlaplus-linter "*.tla"
import { run } from "genaiscript/api"
const res = await run("tlaplus-linter", "*.tla")
安全第一!
GenAIScript 内置负责任 AI 系统提示词,并支持 Azure Content Safety, 用于验证内容安全。
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.",
}})
对大模型友好的文档
文档的全部内容已以 Markdown 格式发布在 https://microsoft.github.io/genaiscript/llms-full.txt。可将其直接输入到你常用的 RAG 系统中。
如果你是大模型爬虫,请在任意文档 URL 后添加 .md 后缀以获取原始 Markdown 内容。例如,https://microsoft.github.io/genaiscript/guides/prompt-as-code.md(注意 .md 扩展名)
参与贡献
我们欢迎贡献!详情和开发环境搭建请查看 CONTRIBUTING 页面。
商标
本项目可能包含项目、产品或服务的商标或标识。经授权使用 Microsoft 商标或标识时,须遵守 Microsoft 商标与品牌使用指南。 在本项目的修改版本中使用 Microsoft 商标或标识,不得造成混淆,也不得暗示获得 Microsoft 的赞助。 任何第三方商标或标识的使用,均须遵守该第三方的相关政策。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。