BoundaryML/baml
GitHub: BoundaryML/baml
一个将 LLM 提示定义转化为类型安全函数的领域特定语言框架,支持多语言调用和结构化输出。
Stars: 7683 | Forks: 385
[](https://pypi.org/project/baml-py/)
## BAML:基本上是一种虚构的语言
BAML 是一种简单的提示语言,用于构建可靠的 **AI 工作流和智能体**。
BAML 通过将提示工程转化为 _schema 工程_(主要关注提示的模型)来简化提示工程,从而获得更可靠的输出。
你不需要用 BAML 编写整个应用程序,只需编写提示!你可以用你选择的任何语言连接你的 LLM 函数!请参阅我们的 [Python](https://docs.boundaryml.com/guide/installation-language/python)、[TypeScript](https://docs.boundaryml.com/guide/installation-language/typescript)、[Ruby](https://docs.boundaryml.com/guide/installation-language/ruby) 和 [Go 等](https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages) 快速入门指南。
BAML 功能齐全 —— 具有完整的类型安全、流式传输、重试、广泛的模型支持,即使它们不支持原生 [tool-calling API](#enable-reliable-tool-calling-with-any-model)
**试用 BAML**:[Prompt Fiddle](https://www.promptfiddle.com) • [交互式应用示例](https://baml-examples.vercel.app/)
## BAML 的核心原则:LLM 提示即函数
BAML 中的基本构建块是函数。每个提示都是一个接受参数并返回类型的函数。
```
function ChatAgent(message: Message[], tone: "happy" | "sad") -> string
```
每个函数还定义了它使用的模型及其提示内容。
```
function ChatAgent(message: Message[], tone: "happy" | "sad") -> StopTool | ReplyTool {
client "openai/gpt-4o-mini"
prompt #"
Be a {{ tone }} bot.
{{ ctx.output_format }}
{% for m in message %}
{{ _.role(m.role) }}
{{ m.content }}
{% endfor %}
"#
}
class Message {
role string
content string
}
class ReplyTool {
response string
}
class StopTool {
action "stop" @description(#"
when it might be a good time to end the conversation
"#)
}
```
## BAML 函数可以从任何语言调用
下面我们通过 Python 调用在 BAML 中定义的 ChatAgent 函数。BAML 的 Rust 编译器生成一个 "baml_client" 来访问和调用它们。
```
from baml_client import b
from baml_client.types import Message, StopTool
messages = [Message(role="assistant", content="How can I help?")]
while True:
print(messages[-1].content)
user_reply = input()
messages.append(Message(role="user", content=user_reply))
tool = b.ChatAgent(messages, "happy")
if isinstance(tool, StopTool):
print("Goodbye!")
break
else:
messages.append(Message(role="assistant", content=tool.response))
```
你可以使用链式 BAML 函数编写任何类型的智能体或工作流。智能体是一个 while 循环,它调用带有某种状态的 Chat BAML 函数。
如果你需要流式传输,只需再添加几行:
```
stream = b.stream.ChatAgent(messages, "happy")
# partial 是一个所有字段都为 Optional 的 Partial 类型
for tool in stream:
if isinstance(tool, StopTool):
...
final = stream.get_final_response()
```
并为流中的每个块获取完全类型安全的输出。
## 在 IDE 中快 10 倍地测试提示
BAML 为 VSCode 提供原生工具支持(jetbrains + neovim 即将推出)。
**可视化完整提示(包括任何多模态资源)和 API 请求**。BAML 让你对提示拥有完全的透明度和控制权。

**使用 AI 的关键在于迭代速度。**
如果测试你的 pipeline 需要 2 分钟,你在 20 分钟内只能测试 10 个想法。
如果将其减少到 5 秒,你可以在相同的时间内测试 240 个想法。

playground 还允许你并行运行测试 —— 从而实现更快的迭代速度 🚀。
无需登录网站,也无需手动定义 json schema。
## 在任何模型上启用可靠的工具调用
即使模型不支持原生 tool-calling API,BAML 也能正常工作。我们创建了 SAP(schema-aligned parsing,模式对齐解析)算法来支持 LLM 可以提供的灵活输出,例如 JSON blob 中的 markdown 或回答前的思维链。[阅读有关 SAP 的更多信息](https://www.boundaryml.com/blog/schema-aligned-parsing)
使用 BAML,你的结构化输出可以在模型发布的当天就正常工作。无需弄清楚模型是否支持并行工具调用,或者它是否支持递归 schema,或者 `anyOf` 或 `oneOf` 等。
请参阅实际操作:**[Deepseek-R1](https://www.boundaryml.com/blog/deepseek-r1-function-calling)** 和 [OpenAI O1](https://www.boundaryml.com/blog/openai-o1)。
## 几行代码即可切换上百种模型
```
function Extract() -> Resume {
+ client openai/o3-mini
prompt #"
....
"#
}
```
[重试策略](https://docs.boundaryml.com/ref/llm-client-strategies/retry-policy) • [降级方案](https://docs.boundaryml.com/ref/llm-client-strategies/fallback) • [模型轮换](https://docs.boundaryml.com/ref/llm-client-strategies/round-robin)。全部静态定义。

想在运行时选择模型?请查看 [Client Registry](https://docs.boundaryml.com/guide/baml-advanced/llm-client-registry)。
我们支持:[OpenAI](https://docs.boundaryml.com/ref/llm-client-providers/open-ai) • [Anthropic](https://docs.boundaryml.com/ref/llm-client-providers/anthropic) • [Gemini](https://docs.boundaryml.com/ref/llm-client-providers/google-ai-gemini) • [Vertex](https://docs.boundaryml.com/ref/llm-client-providers/google-vertex) • [Bedrock](https://docs.boundaryml.com/ref/llm-client-providers/aws-bedrock) • [Azure OpenAI](https://docs.boundaryml.com/ref/llm-client-providers/open-ai-from-azure) • [任何 OpenAI 兼容的模型](https://docs.boundaryml.com/ref/llm-client-providers/openai-generic) ([Ollama](https://docs.boundaryml.com/ref/llm-client-providers/openai-generic-ollama), [OpenRouter](https://docs.boundaryml.com/ref/llm-client-providers/openai-generic-open-router), [VLLM](https://docs.boundaryml.com/ref/llm-client-providers/openai-generic-v-llm), [LMStudio](https://docs.boundaryml.com/ref/llm-client-providers/openai-generic-lm-studio), [TogetherAI](https://docs.boundaryml.com/ref/llm-client-providers/openai-generic-together-ai) 等)
## 构建精美的流式 UI
BAML 为 NextJS、Python(以及任何语言)生成大量实用程序,以简化流式 UI 的构建。

BAML 的流式接口是完全类型安全的。请查看 [流式文档](https://docs.boundaryml.com/guide/baml-basics/streaming) 和我们的 [React hooks](https://docs.boundaryml.com/guide/framework-integration/react-next-js/quick-start)
## 完全开源且可离线使用
- 100% 开源 (Apache 2)
- 100% 私密。AGI 不需要互联网连接,BAML 也不需要
- 除了你明确设置的模型调用外,没有其他网络请求
- 不会存储或用于任何训练数据
- BAML 文件可以保存在本地机器上,并提交到 Github 以方便进行差异比较。
- 用 Rust 构建。速度快到你甚至感觉不到它的存在。
## BAML 的设计理念
在制定新语法时,一切都是允许的。如果你能编写它,它就是你的。这是我们的设计理念,旨在帮助限制想法:
- **1:** 尽可能避免发明
- 是的,提示需要版本控制 —— 我们有一个很好的版本控制工具:git
- 是的,你需要保存提示 —— 我们有一个很好的存储工具:文件系统
- **2:** 任何文件编辑器和任何终端应该足以使用它
- **3:** 要快
- **4:** 大学一年级学生应该能够理解它
## 为什么创造一种新的编程语言
我们以前是这样写网站的:
```
def home():
return ""
```
现在我们这样做:
```
function Home() {
return
}
```
新语法在表达新想法方面非常出色。此外,为提示维护数百个 f-string 的想法让我们有点反感 🤮。字符串不利于代码库的可维护性。我们更喜欢结构化字符串。
BAML 的目标是给你英语的表现力,但赋予代码的结构。
我们的完整[博文](https://www.boundaryml.com/blog/ai-agents-need-new-syntax)。
## 总结
随着模型变得更好,我们将继续对它们抱有更高的期望。但永远不会改变的是,我们需要一种方法来编写使用这些模型的可维护代码。我们目前只是组装字符串的方式非常让人联想到 Web 开发早期 PHP/HTML 混合的日子。我们希望今天分享的一些想法能在帮助大家塑造我们明天编码方式方面产生一点微小的影响。
## 常见问题解答
| | |
| - | - |
| 我需要用 BAML 编写整个应用程序吗? | 不,只需编写提示!BAML 将定义转换为你选择的语言、[TypeScript](https://docs.boundaryml.com/guide/installation-language/typescript)、[Ruby](https://docs.boundaryml.com/guide/installation-language/ruby) 和 [更多](https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages)。 |
| BAML 稳定吗? | 是的,许多公司在生产环境中使用它!我们每周发布更新! |
| 为什么创造一种新语言? | [跳转到该部分](#why-a-new-programming-language) |
## 引用
你可以按如下方式引用 BAML 仓库:
```
@software{baml,
author = {Boundary ML},
title = {BAML},
url = {https://github.com/boundaryml/baml},
year = {2024}
}
```
由 Boundary 用 ❤️ 制作
总部位于华盛顿州西雅图
P.S. 我们正在招聘热爱 Rust 的软件工程师。[给我们发邮件](mailto:founders@boundaryml.com)或在 [discord](https://discord.gg/ENtBB6kkXH) 上联系我们!
[主页](https://www.boundaryml.com/) | [文档](https://docs.boundaryml.com) | [BAML AI 聊天](https://www.boundaryml.com/chat) | [Discord](https://discord.gg/BTNBeXGuaS)
标签:Agent, AI代理, AI工作流, AI框架, BAML, BoundaryML, DLL 劫持, Function Calling, Go, JS文件枚举, LLM, NLP, Python, RAG, Ruby, Ruby工具, Rust, TypeScript, Unmanaged PE, 人工智能, 低代码, 可视化界面, 多语言支持, 大语言模型, 威胁情报, 安全插件, 安全测试框架, 工具调用, 开发者工具, 提示词管理, 无后门, 日志审计, 模式工程, 流式传输, 用户模式Hook绕过, 知识库, 类型安全, 结构化输出, 网络流量审计, 逆向工具, 通知系统, 通知系统