Parlant 是一个用于LLM agent的开源代理行为建模引擎,旨在帮助开发人员创建具有控制力、清晰度和信心的客户参与、业务一致的对话代理。

官网:github.com

镜像:parlant: Parlant 是一个用于 LLM agent 的开源代理行为建模引擎,旨在帮助开发人员快速创建具有控制力、清晰度和信心的客户参与、业务一致的对话代理

中文手册:https://www.zdoc.app/zh/emcie-co/parlant

怎么找到的Parlant

我在搜索“哪里有好的多agent协同工作的框架?”时,并没有看到parlant,但是AI列出的框架里面有Python-MAS,于是用bing搜索“Python-MAS” ,发现第三个推荐页面,就是parlant,而且第一个和第二个跟多agent框架无关:

然后通过这篇介绍文章,找到了parlant 。后面看parlant的说明书,发现它确实有些比较特别的东西,非常值得学习和使用!

文档说:60 秒内启动您的智能体

这真的很吸引人!

每个智能体均可通过独特配置实现风格、仪态和交互模式的定制化,精准契合目标用户需求。更重要的是,不同业务单元可以独立拥有并维护专属智能体。例如:

  • IT 部门管理 Hexon
  • 客户成功团队负责 Sprocket
  • 销售/市场部门掌控 Piston

这种基于智能体的设计为 Parlant 内部关注点分离创造了天然边界

开发者为何选择 Parlant

🏗️ 传统 AI 框架

⚡ Parlant

  • 编写复杂的系统提示
  • 期望大语言模型遵循提示
  • 调试不可预测的行为
  • 通过提示工程进行扩展
  • 祈祷系统可靠性
  • 用自然语言定义规则
  • 确保规则合规性
  • 可预测、一致的行为
  • 通过添加指南进行扩展
  • 从第一天起即可投入生产

解决方案:停止与提示词博弈,转而传授原则

Parlant彻底颠覆了AI智能体的开发模式。与其寄望于大语言模型会遵循指令,Parlant能确保它必定遵循

# Traditional approach: Cross your fingers 🤞
system_prompt = "You are a helpful assistant. Please follow these 47 rules..."

# Parlant approach: Ensured compliance ✅
await agent.create_guideline(
    condition="Customer asks about refunds",
    action="Check order status first to see if eligible",
    tools=[check_order_status],
)

🌟 开发者评价

"这是我迄今为止遇到过的最优雅的对话式 AI 框架!使用 Parlant 进行开发是一种纯粹的享受。" — Vishal Ahuja,摩根大通客户对话 AI 高级主管

实践

安装Parlant

pip install parlant

创建第一个智能体

 环境变量加上:

export OPENAI_API_KEY=hello
export OPENAI_BASE_URL=http://192.168.1.5:1337/
export MODEL_NAME = "default"

执行下面python代码:

import asyncio
import parlant.sdk as p

async def main():
  async with p.Server() as server:
    agent = await server.create_agent(
        name="Otto Carmen",
        description="You work at a car dealership",
    )

asyncio.run(main())

运行后服务启动:

2025-09-25T14:35:29.933741Z [info     ] [<main>] Parlant server version 3.0.2
2025-09-25T14:35:29.934211Z [info     ] [<main>] Using home directory '/home/skywalk/github/parlant-data'
2025-09-25T14:35:29.937969Z [info     ] [<main>] No external modules selected
2025-09-25T14:35:30.578504Z [info     ] [<main>] Initialized OpenAIService


                             ..
                          :=++++=-
                        :+***+++**+.
                      .=*****++++*+=:.
                     .=+++*******-
             ..:::::...  .::::=++
         .-+***#####**+=-..=+=:.
       :+######***********. =***=.
      =####**###**********+ .*****-
     =#******###** v3.0 **+ .******-
    :#*******#######****=. =********:
    .*#******#*:---=-::..-*********+
     -##*##***. -----=++*******++**:
      :*###**: =****###**********+:
        -+*#- -****************+-
          .: .*******++++++==-.
            .****+=:.
            =+=:.
           ..


Caching entity embeddings (1) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0:00:00
2025-09-25T14:37:18.053789Z [info     ] [<main>] .-----------------------------------------.
2025-09-25T14:37:18.054136Z [info     ] [<main>] | Server is ready for some serious action |
2025-09-25T14:37:18.054627Z [info     ] [<main>] '-----------------------------------------'
2025-09-25T14:37:18.054756Z [info     ] [<main>] Server authorization policy: development
2025-09-25T14:37:18.054960Z [info     ] [<main>] Try the Sandbox UI at http://localhost:8800

用浏览器登录:

http://localhost:8800

如果不是本机,需要将localhost换成相应的ip地址。

暂时对话还没调通,待续。

经过高强度的调试,终于让它能说话了,但是感觉并没有很聪明,没有达到我的需求,可能是配置的大模型不太聪明的原因,因为官方推荐使用的模型是gpt-4o,而我没有。

调试过程见:

https://blog.csdn.net/skywalk8163/article/details/152177264

https://skywalk.blog.csdn.net/article/details/152253434

最终输出是这样的:

总结

最近几个月parlant很流行,但是需要gpt-4o等模型配合,对于不想翻墙不想用美金的人来说,可能基本无法享受到它的便利。

国内可以使用deepseek、qwen等模型,也可以使用ollama模型,或者像我一样自建openai兼容llm服务器来使用。

最终测试文件是这样写的:

# 导入必要的库
import tiktoken
import time
import asyncio
import os


os.environ["G4F_API_KEY"] = "your_custom_api_key"  # 自定义API密钥(可为任意值,仅作占位)
os.environ["G4F_BASE_URL"] = "http://192.168.0.98:1337/v1"  # 自定义大模型API地址
os.environ["G4F_MODEL"] = "default"  # 自定义大模型API地址


import asyncio
import parlant.sdk as p
from parlant.sdk import NLPServices

# DEEPSEEK_API_KEY
async def main():
  async with p.Server(nlp_service=NLPServices.g4f) as server:
    agent = await server.create_agent(
        name="Otto Carmen",
        description="You work at a car dealership",
        # model="default"
    )
 
asyncio.run(main())

在github\parlant\src目录,执行测试:

python test_server.py

启动后打开8800端口的网页即可。

调试

运行第一个智能体报错Please set OPENAI_API_KEY in your environment before running Parlant.

  File "/home/skywalk/py312/lib/python3.12/site-packages/parlant/sdk.py", line 249, in openai
    raise SDKError(error)
parlant.sdk.SDKError: You're using the OpenAI NLP service, but OPENAI_API_KEY is not set.
Please set OPENAI_API_KEY in your environment before running Parlant.

You're using the OpenAI NLP service, but OPENAI_API_KEY is not set.
Please set OPENAI_API_KEY in your environment before running Parlant.

环境变量加上:

export OPENAI_API_KEY=hello
export OPENAI_BASE_URL=http://192.168.1.5:1337/

对话卡住

对啊,我忘记给它设定模型了啊

找了下,发现parlant没有找到设置模型的地方啊!(最后发现是在parlant调用的时候,加上了模型的设定,如:async with p.Server(nlp_service=NLPServices.g4f) as server:)

环境变量加上

export OPENAI_API_KEY=hello
export OPENAI_BASE_URL=http://192.168.1.5:1337/
export MODEL_NAME = "default"

我这里是自己架设的服务器,所以模型名字用了default,如果用openai官方服务器,那么服务器地址可以省略,key需要填上,模型名字也要修改成类似export MODEL_NAME = "gpt-4o"等

再次修改

export OPENAI_API_KEY=hello
export OPENAI_BASE_URL=http://192.168.1.5:1337/v1
export OPENAI_MODEL_NAME="default"
export OPENAI_MODEL="default"

再换一个服务器

export OPENAI_API_KEY=hello
export OPENAI_BASE_URL=http://192.168.0.98:8000/
export OPENAI_MODEL_NAME="default"
export OPENAI_MODEL="default"

在代码里配置

import os
os.environ["OPENAI_API_KEY"] = "your_custom_api_key"  # 自定义API密钥(可为任意值,仅作占位)
os.environ["OPENAI_BASE_URL"] = "http://192.168.0.98:8000/"  # 自定义大模型API地址
os.environ["OPENAI_MODEL"] = "default"  # 自定义模型名称(如gpt-5-mini)

调用大模型api报错"'ModelNotFoundError: AnyProvider: Model gpt-4o-2024-08-06 not found in any "
 "provider.'

 '"/home/skywalk/py312/lib/python3.12/site-packages/openai/_base_client.py", '
 'line 1562, in _request\n'
 '    raise self._make_status_error_from_response(err.response) from None\n',
 "openai.NotFoundError: Error code: 404 - {'error': {'message': "
 "'ModelNotFoundError: AnyProvider: Model gpt-4o-2024-08-06 not found in any "
 "provider.'}, 'model': 'gpt-4o-2024-08-06'}\n"]

这说明parlant是直接调用了gpt-4o的模型.....问题是没有找到设定模型的地方啊!

import os
os.environ["OPENAI_API_KEY"] = "your_custom_api_key"  # 自定义API密钥(可为任意值,仅作占位)
os.environ["OPENAI_BASE_URL"] = "http://192.168.0.98:8000/"  # 自定义大模型API地址
os.environ["OPENAI_MODEL"] = "default"  # 自定义模型名称(如gpt-5-mini)
# 试试这个
os.environ["OPENAI_BASE_URL"] = "http://192.168.1.5:8000/"

设定os.environ["OPENAI_BASE_URL"] = "http://192.168.1.5:8000/v1/"

发现在这个项目里,设定这个url,大模型是能正确收到传过来的信息的。

import os
os.environ["OPENAI_API_KEY"] = "your_custom_api_key"  # 自定义API密钥(可为任意值,仅作占位)
os.environ["OPENAI_BASE_URL"] = "http://192.168.1.5:8000/v1/"  # 自定义大模型API地址
os.environ["OPENAI_MODEL"] = "default"  # 自定义模型名称(如gpt-5-mini)

发现parlant就是要用这个模型:gpt-4o-2024-08-06 ,而自己的大模型里没有,只有gpt-4o 

到自己的大模型里,把源代码改一下

        # 如果指定了模型,则使用指定模型,否则使用默认模型
        model = data.get("model")
        if model == "gpt-4o-2024-08-06":
            model = "gpt-4o"
        if model and model != "default":
            g4f_params["model"] = model
            # g4f_params["model"] = "default"

这样还是不行,后来明白了,openai在使用gpt-4o的时候,会自动使用相应的模型做tokenizer的操作,而我自定义的g4f没有....后面针对这个是颇费了一些周折。

直接代码cp会报错

这段代码

import asyncio
import parlant.sdk as p

async def main():
  async with p.Server() as server:
    agent = await server.create_agent(
        name="Otto Carmen",
        description="You work at a car dealership",
    )

asyncio.run(main())

这是官方的最简单的例子,它默认可能是调用的openai的api,我最终是修改成了g4f

async def main():
  async with p.Server(nlp_service=NLPServices.g4f) as server:
    agent = await server.create_agent(
        name="Otto Carmen",
        description="You work at a car dealership",
        # model="default"
    )

直接点右上角的“复制”,在python里黏贴进去,执行会报错:

>>> async def main():
...   async with p.Server() as server:
...     agent = await server.create_agent(
...         name="Otto Carmen",
...         description="You work at a car dealership",
...     )
...
>>> ————————————————
  File "<stdin>", line 1
    ————————————————
    ^
SyntaxError: invalid character '—' (U+2014)

证明里面有个不对的字符!

import asyncio
import parlant.sdk as p

async def main():
  async with p.Server() as server:
    agent = await server.create_agent(
        name="Otto Carmen",
        description="You work at a car dealership",
    )

asyncio.run(main())

发现在csdn里,有些时候copy代码会有点小问题,解决的方法是手工看看是否空格啥的有问题,改成半角空格。

这个没再去跟踪处理。

Logo

更多推荐