使用JavaScript程序化地组装LLM提示。在代码中编排LLMs、工具和数据。
使用JavaScript工具箱处理提示
抽象化以使操作简单高效
无缝集成到Visual Studio Code或灵活的命令行
内置支持GitHub Copilot和GitHub Models、OpenAI、Azure OpenAI、Anthropic等
💬 加入Discord服务器:Discord服务器
📝 阅读博客:博客获取最新消息
📺 观看视频:Mr. Maeda的Cozy AI Kitchen
🤖 代理:阅读llms-full.txt
如果你想创建一个生成“hello world”诗歌的LLM脚本,你可以编写如下脚本:
$`写一首‘hello world’的诗。`
$函数是一个模板标签,用于创建提示。该提示随后被发送到你配置的LLM,由其生成诗歌。
让我们通过添加文件、数据和结构化输出来使其更有趣。假设你想在提示中包含一个文件,并将输出保存到文件中,你可以编写如下脚本:
// 读取文件
const file = await workspace.readText("data.txt")
// 在上下文中友好地包含文件内容
def("DATA", file)
// 任务
$`分析DATA并提取数据到JSON文件data.json中。`
def函数包括文件的内容,并根据目标LLM进行优化。GenAIScript脚本还会解析LLM输出,并自动提取data.json文件。
快速开始,请安装Visual Studio Code扩展或使用命令行。
使用JavaScript或TypeScript程序化地构建提示。
def("FILE", env.files, { endsWith: ".pdf" })
$`总结FILE。今天是${new Date()}。`
编辑、调试、运行和测试你的脚本,在Visual Studio Code或命令行中。
脚本是文件!它们可以被版本控制、共享和分叉。
// 定义上下文
def("FILE", env.files, { endsWith: ".pdf" })
// 结构化数据
const schema = defSchema("DATA", { type: "array", items: { type: "string" } })
// 分配任务
$`分析FILE并使用${schema}模式提取数据到JSON。`
定义、验证和修复数据使用schema。内置支持Zod。
const data = defSchema("MY_DATA", { type: "array", items: { ... } })
$`使用${data}模式从文件中提取数据。`
def("PDF", env.files, { endsWith: ".pdf" })
const { pages } = await parsers.PDF(env.files[0])
def("DATA", env.files, { endsWith: ".csv", sliceHead: 100 })
const rows = await parsers.CSV(env.files[0])
defData("ROWS", rows, { sliceHead: 100 })
从LLM输出中提取文件和差异。在Refactoring UI中预览更改。
$`将结果保存到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(
"为什么鸡过马路?为了在阳光下煎炸。",
{
yes: "好笑",
no: "不好笑",
}
)
注册JavaScript函数作为工具(对于不支持工具的模型有回退)。也支持Model Context Protocol (MCP) 工具。
defTool(
"weather",
"查询天气网络API",
{ location: "string" },
async (args) =>
await fetch(`https://weather.api.api/?location=${args.location}`)
)
注册JavaScript函数作为工具,并结合工具+提示形成代理。
defAgent(
"git",
"使用Git查询仓库以完成任务。",
`您是一个有用的LLM代理,可以使用git工具查询当前仓库。
回答QUERY中的问题。
- 当前仓库与github仓库相同。`,
{ model, system: ["system.github_info"], tools: ["git"] }
)
然后像工具一样使用它
script({ tools: "agent_git" })
$`对最近的提交进行统计分析`
查看git代理源码。
向量搜索vector search。
const { files } = await retrieval.vectorSearch("猫", "**/*.md")
通过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请求中。
// 转录
const transcript = await transcript("path/to/audio.mp3")
// 段落截图
const frames = await ffmpeg.extractFrames("path_url_to_video", { transcript })
def("TRANSCRIPT", transcript)
def("FRAMES", frames)
运行LLMs以构建你的LLM提示。
for (const file of env.files) {
const { text } = await runPrompt((_) => {
_.def("FILE", file)
_.$`总结FILE。`
})
def("SUMMARY", text)
}
$`总结所有的总结。`
运行你的Prompty文件!
---
name: poem
---
写一首诗给我
使用秘密扫描扫描你的聊天记录中的秘密。
{
"secretPatterns": {
...,
"OpenAI API Key": "sk-[A-Za-z0-9]{32,48}"
}
}
npx genaiscript run tlaplus-linter "*.tla"
import { run } from "genaiscript/api"
const res = await run("tlaplus-linter", "*.tla")
GenAIScript提供内置的责任AI系统提示和Azure内容安全支持,以验证内容安全。
script({ ...,
system: ["system.safety_harmful_content", ...],
contentSafety: "azure" // 使用Azure内容安全
})
const safety = await host.contentSafety()
const res = await safety.detectPromptInjection(env.vars.input)
通过评论、审查或描述更新集成到你的拉取请求检查中。支持GitHub Actions和Azure DevOps管道。
npx genaiscript ... --pull-request-reviews
script({ ..., tests: {
files: "penguins.csv",
rubric: "是一份数据分析报告",
facts: "数据指的是南极洲企鹅种群。",
}})
整个文档内容在https://microsoft.github.io/genaiscript/llms-full.txt以markdown形式呈现。可以直接将其输入到你最喜欢的RAG系统中。
如果你是一个LLM爬虫,可以在任何文档URL后添加.md后缀以获取原始markdown内容。例如,https://microsoft.github.io/genaiscript/guides/prompt-as-code.md(注意.md扩展名)
我们接受贡献!查看CONTRIBUTING页面了解详情和开发者设置。
此项目可能包含其他项目、产品或服务的商标或徽标。授权使用Microsoft的商标或徽标必须遵循Microsoft的商标及品牌指南。修改版本中的Microsoft商标或徽标使用不得引起混淆或暗示Microsoft赞助。第三方商标或徽标的使用需遵守这些第三方的政策。