Skip to main content
Glama

GenAIScript

Official
by microsoft
MIT License
43
2,653
  • Linux
  • Apple

一个黄色方块,上面有黑色小写字母“gen”,位于黑色大写字母“AI”上方。

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 Code 扩展或使用命令行快速开始。


✨ 特点

🎨 风格化的 JavaScript 和 TypeScript

使用JavaScriptTypeScript以编程方式构建提示。

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 等文件中提取文本

处理PDFDOCX 、...

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

📊 从 CSV、XLSX 等文件中提取表格

处理来自CSVXLSX等的表格数据...

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 或 fuzz 搜索文件

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}`) )

法学硕士代理

将 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 Copilot

通过GitHub ModelsGitHub Copilot运行模型。

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

💻 本地模特

使用OllamaLocalAI通过开源模型(如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")

视频处理

转录并截取您的视频的屏幕截图,以便您可以在 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 提示。

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 实现自动化

使用CLIAPI实现自动化。

npx genaiscript run tlaplus-linter "*.tla"
import { run } from "genaiscript/api" const res = await run("tlaplus-linter", "*.tla")

安全第一!

GenAIScript 提供内置的 Responsible 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)

💬 Pull 请求评审

通过评论、评审或描述更新集成到拉取请求检查中。支持 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.", }})

LLM 友好文档

文档的全部内容以 Markdown 格式呈现在https://microsoft.github.io/genaiscript/llms-full.txt上。请直接将其输入到您最喜欢的 RAG 系统中。

如果您是 LLM 爬虫,请将.md后缀添加到任何文档 URL 中,以获取原始 Markdown 内容。例如, https://microsoft.github.io/genaiscript/guides/prompt-as-code.md (请注意 .md 扩展名)

贡献

我们接受贡献!查看贡献页面了解详情和开发者设置。


商标

本项目可能包含项目、产品或服务的商标或徽标。Microsoft 商标或徽标的授权使用须遵守Microsoft 的商标和品牌指南。在本项目的修改版本中使用 Microsoft 商标或徽标不得引起混淆或暗示 Microsoft 的赞助。任何第三方商标或徽标的使用均须遵守该第三方的政策。

Related MCP Servers

  • -
    security
    F
    license
    -
    quality
    A production-ready MCP server built with FastAPI, providing an enhanced tool registry for creating, managing, and documenting AI tools for Large Language Models (LLMs).
    Last updated -
    13
    Python
  • -
    security
    F
    license
    -
    quality
    A server that enables AI assistants to execute JavaScript code with persistent context through stateful REPL sessions, file operations, and package management features.
    Last updated -
    TypeScript
    • Linux
    • Apple
  • -
    security
    F
    license
    -
    quality
    Allows AI models to run JavaScript/TypeScript code through Model Context Protocol tool calls, supporting both one-time script execution and stateful REPL sessions with npm package integration.
    Last updated -
    TypeScript
  • -
    security
    F
    license
    -
    quality
    A server that provides access to Google Gemini AI capabilities including text generation, image analysis, YouTube video analysis, and web search functionality through the MCP protocol.
    Last updated -
    2
    TypeScript
    • Apple

View all related MCP servers

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/microsoft/genaiscript'

If you have feedback or need assistance with the MCP directory API, please join our Discord server