Genaiscript
提示即编码
使用JavaScript以编程方式组装LLM的提示。在代码中编排LLM、工具和数据。
- 用于处理提示的JavaScript工具箱
- 抽象化,使其简单高效
- 无缝集成Visual Studio代码或灵活的命令行
- 内置对GitHub Copilot和GitHub模型、OpenAI、Azure OpenAI、Anthropic等的支持
- 📄 阅读在线文档,网址为 Microsoft.ithub.i/genaiscript
- 💬 加入 Discord服务器
- 📝 阅读 博客 获取最新消息
- 📺 手表 前田先生的舒适AI厨房
- 🤖 代理人-阅读 llms-full.txt
______________________________________________________________________
你好,世界
告诉你想创建一个LLM脚本,生成一首“hello world”诗。您可以编写以下脚本:
$`Write a 'hello world' poem.`这 $ 函数是一个创建提示的模板标记。然后,提示会被发送到LLM(您配置的),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 文件自动。
______________________________________________________________________
🚀 快速入门指南
______________________________________________________________________
✨ 特性
🎨 风格化的JavaScript和TypeScript
def("FILE", env.files, { endsWith: ".pdf" })
$`Summarize FILE. Today is ${new Date()}.`______________________________________________________________________
🚀 快速开发循环
______________________________________________________________________
🔗 重用和共享脚本
脚本是 文件!它们可以被版本化、共享和分叉。
// 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输出中提取文件和差异。预览重构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",
}
)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副本
运行模型 或 .
script({ ..., model: "github:gpt-4o" })______________________________________________________________________
💻 局部模型
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.`______________________________________________________________________
🅿️ 快速支持
运行你的 迅速 文件也是!
---
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提供内置的Responsible 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)______________________________________________________________________
💬 拉取请求评论
融入您的 拉取请求检查 通过评论、评论或描述更新。支持GitHub操作和Azure DevOps管道。
npx genaiscript ... --pull-request-reviews______________________________________________________________________
⭐ 测试和评估
script({ ..., tests: {
files: "penguins.csv",
rubric: "is a data analysis report",
facts: "The data refers about penguin population in Antarctica.",
}})______________________________________________________________________
LLM友好文档
文档的全部内容呈现在https://microsoft.github.io/genaiscript/llms-full.txt在markdown。将其直接输入您最喜欢的RAG系统。
如果您是LLM爬虫,请添加 .md 任何文档URL的后缀,以获取原始的markdown内容。例如,https://microsoft.github.io/genaiscript/guides/prompt-as-code.md(注意.md扩展名)
贡献
我们接受捐款!查看 贡献 详细信息和开发人员设置页面。
______________________________________________________________________
商标
此项目可能包含项目、产品或服务的商标或徽标。授权使用Microsoft 商标或徽标必须遵守 微软商标和品牌指南. 在本项目的修改版本中使用Microsoft商标或徽标不得造成混淆或暗示Microsoft赞助。 任何使用第三方商标或徽标的行为均受这些第三方政策的约束。
