🌐 网站 • ⚡ 快速开始 • 💬 Discord • 📖 示例
Deutsch | Español | français | 日本語 | 한국어 | Português | Русский | 中文
你构建了一个 AI 代理。测试中它表现完美。然后真实用户开始和它对话,结果……
听起来很耳熟? 不只你一个人这样。这是开发生产级 AI 代理的开发者的第一大痛点。
Parlant 颠覆了 AI 代理的开发方式。与其祈祷你的 LLM 遵循指令,Parlant 确保它遵从指令。
# 传统方式:全靠运气 🤞
system_prompt = "You are a helpful assistant. Please follow these 47 rules..."
# Parlant 方式:确保合规 ✅
await agent.create_guideline(
condition="Customer asks about refunds",
action="Check order status first to see if eligible",
tools=[check_order_status],
)
当你的代理收到消息后,Parlant 引擎会在生成回复前准备一个完全对齐的回复:
%%{init: {'theme': 'base', 'themeVariables': {'primaryColor': '#e8f5e9', 'primaryTextColor': '#1b5e20', 'primaryBorderColor': '#81c784', 'lineColor': '#66bb6a', 'secondaryColor': '#fff9e1', 'tertiaryColor': '#F3F5F6'}}}%%
flowchart LR
A(User):::outputNode
subgraph Engine["Parlant Engine"]
direction LR
B["匹配准则并解析旅程状态"]:::matchNode
C["调用上下文相关的工具"]:::toolNode
D["生成消息"]:::composeNode
E["预设消息"]:::cannedNode
end
A a@-->|💬 用户输入| B
B b@--> C
C c@-->|流体输出模式?| D
C d@-->|严格输出模式?| E
D e@-->|💬 流体输出| A
E f@-->|💬 预设输出| A
a@{animate: true}
b@{animate: true}
c@{animate: true}
d@{animate: true}
e@{animate: true}
f@{animate: true}
linkStyle 2 stroke-width:2px
linkStyle 4 stroke-width:2px
linkStyle 3 stroke-width:2px,stroke:#3949AB
linkStyle 5 stroke-width:2px,stroke:#3949AB
classDef composeNode fill:#F9E9CB,stroke:#AB8139,stroke-width:2px,color:#7E5E1A,stroke-width:0
classDef cannedNode fill:#DFE3F9,stroke:#3949AB,stroke-width:2px,color:#1a237e,stroke-width:0
与当前对话状态相关的准则和工具会被仔细匹配并强制执行,使你的代理即使在复杂的行为配置下也能保持专注和一致。
pip install parlant
import parlant.sdk as p
@p.tool
async def get_weather(context: p.ToolContext, city: str) -> p.ToolResult:
# 你的天气 API 逻辑
return p.ToolResult(f"Sunny, 72°F in {city}")
@p.tool
async def get_datetime(context: p.ToolContext) -> p.ToolResult:
from datetime import datetime
return p.ToolResult(datetime.now())
async def main():
async with p.Server() as server:
agent = await server.create_agent(
name="WeatherBot",
description="Helpful weather assistant"
)
# 使用上下文变量,让代理的上下文在每次响应时更新(更新间隔可自定义)
await agent.create_variable(name="current-datetime", tool=get_datetime)
# 使用自然语言控制和引导代理行为
await agent.create_guideline(
condition="User asks about weather",
action="Get current weather and provide tips and suggestions",
tools=[get_weather]
)
# 添加其他(可靠执行的)行为建模元素
# ...
# 🎉 测试游乐场已在 http://localhost:8800 准备就绪
# 将官方的 React 小组件集成到你的应用中,
# 或按照教程构建你自己的前端!
if __name__ == "__main__":
import asyncio
asyncio.run(main())
就这样! 你的代理已经启动并运行,能够确保遵循规则的行为。

使用集成的测试与评估框架来验证代理行为。
from parlant.testing import Suite, InteractionBuilder
from parlant.testing.steps import AgentMessage, CustomerMessage
suite = Suite(server_url="http://localhost:8800", agent_id="your_agent")
@suite.scenario
async def test_booking_flow():
async with suite.session() as session:
# 构建对话历史
history = (
InteractionBuilder()
.step(CustomerMessage("Man it's cold today"))
.step(AgentMessage("Tell me about it, I'm freezing my nuts and bolts off."))
.step(CustomerMessage("Where are you from? I'm from Boston"))
.step(AgentMessage("What a dream! I'm stuck in a data center in San Fran..."))
.build()
)
# 用事件历史预加载会话
await session.add_events(history)
# 发送客户消息
response = await session.send("What's the temperature there today?")
# 使用 LLM-as-a-Judge 对代理回复进行断言
await response.should("provide weather details for San Francisco")
运行命令:parlant-test your_tests.py

| ### 🏗️ **传统 AI 框架** | ### ⚡ **Parlant** |
| - 编写复杂的系统提示 - 希望 LLM 遵循它们 - 调试不可预测的行为 - 通过提示工程扩展 - 祈祷系统的可靠性 | - 用自然语言定义规则 - **确保**规则被遵循 - 可预测、一致的行为 - 通过添加准则扩展 - 从第一天起就达到生产就绪 |
"这是迄今为止我遇到过的最优雅的对话式 AI 框架!使用 Parlant 开发是纯粹的乐趣。" — Vishal Ahuja, JPMorgan Chase 客户面对型对话式 AI 高级负责人
| 🎯 我想亲自测试 | → 5 分钟快速入门 |
| 🛠️ 我想看个例子 | → 医疗保健代理示例 |
| 🚀 我想参与其中 | → 加入我们的 Discord 社区 |
Apache 2.0 - 可在任何地方使用,包括商业项目。