AI提示编程工具

GenAIScript是一个使用JavaScript编程方式构建LLM提示的工具箱,提供丰富的功能集成和开发工具支持,适用于自动化文本处理、数据分析和代码生成等场景。

  • by microsoft
  • TypeScript
  • 本地部署
  • 2874 星标
  • 更新于
在 GitHub 查看 microsoft/genaiscript

使用说明

Genaiscript

提示即编码

使用JavaScript以编程方式组装LLM的提示。在代码中编排LLM、工具和数据。


你好,世界

告诉你想创建一个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 文件自动。


🚀 快速入门指南

通过安装快速入门 Visual Studio代码扩展 或使用 命令行.


✨ 特性

🎨 风格化的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、。..

操纵 PDF文件, DOCX 文件, ...

def("PDF", env.files, { endsWith: ".pdf" })
const { pages } = await parsers.PDF(env.files[0])

📊 从CSV、XLSX、。..

从以下位置操作表格数据 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副本

运行模型 GitHub模型 或 GitHub Copilot.

script({ ..., model: "github:gpt-4o" })

💻 局部模型

使用以下命令运行脚本 开源模型,比如 Phi-3,使用 奥拉玛, 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.`

🅿️ 快速支持

运行你的 迅速 文件也是!

---
name: poem
---

Write me a poem

可插入的秘密扫描

使用扫描聊天记录中的秘密 秘密扫描.

{
    "secretPatterns": {
        ...,
        "OpenAI API Key": "sk-[A-Za-z0-9]{32,48}"
    }
}

⚙ 使用CLI或API实现自动化

使用自动化 命令行界面 或 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

⭐ 测试和评估

使用构建可靠的提示 测试和评估 由...驱动 promptfoo.

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赞助。 任何使用第三方商标或徽标的行为均受这些第三方政策的约束。