文末提供程序和源码下载
genaiscript开源程序可自动化的 GenAI 脚本,以编程方式组合使用 JavaScript 的LLMs提示。在代码中编排 LLMs、 工具和数据。
用于处理提示的 JavaScript 工具箱
抽象化,使其变得简单和高效
无缝的 Visual Studio Code 集成或灵活的命令行
对 GitHub Copilot 和 GitHub 模型、OpenAI、Azure OpenAI、Anthropic 等的内置支持
阅读 microsoft.github.io/genaiscript 上的联机文档
Say to you want to create a LLM script to generate a 'hello world' 诗歌。您可以编写以下脚本:
$`Write a 'hello world' poem.`
该 $
函数是创建提示的模板标签。然后,该提示将发送到 LLM (you configured),这将生成诗歌。
让我们通过添加文件、数据和结构化输出来使其更有趣。假设您希望在提示符中包含一个文件,然后将输出保存在文件中。您可以编写以下脚本:
// 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 以编程方式构建提示。
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.`
使用 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输出中提取文件和 diff。在 Refactoring UI 中预览更改。
$`Save the result in poem.txt.`
FILE ./poem.txt
The quick brown fox jumps over the lazy dog.
Grep 或 fuzz 搜索文件。
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",
}
)
将 JavaScript 函数注册为工具(对于不支持工具的模型,具有回退功能)。还支持模型上下文协议 (MCP) 工具。
defTool(
"weather",
"query a weather web api",
{ location: "string" },
async (args) =>
await fetch(`https://weather.api.api/?location=${args.location}`)
)
将 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`
向量搜索。
const { files } = await retrieval.vectorSearch("cats", "**/*.md")
通过 GitHub 模型或 GitHub Copilot 运行模型。
script({ ..., model: "github:gpt-4o" })
使用 Ollama 和 LocalAI 通过 Phi-3 等开源模型运行脚本。
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")
转录和截取您的视频,以便您可以在您的LLMs请求中有效地提供它们。
// 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)
Run LLMs 构建您的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 文件!
---
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 提供内置的负责任 AI 系统提示和 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)
通过评论、评论或描述更新集成到您的 Pull Requests 检查中。支持 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.",
}})
夸克网盘分享
本文信息来源于GitHub作者地址:GitHub - microsoft/genaiscript: Automatable GenAI Scripting