跳转至

customer_service · 客服 Hello World

本页收录可运行示例,配套本文档「快速开始」(快速开始)。 代码为随框架交付的示例,已一并收录到本站供直接对照使用。

说明与运行

示例: 客服工作流(阶段 0 Hello World). 运行: cd agentframework PYTHONPATH=src python examples/customer_service.py

运行前需在已安装 agentframeworkPython ≥ 3.11 环境;示例多为自带本地模型,无需真实 API Key;需要外部依赖(如 chromadb / 数据库驱动)会在说明中注明。

完整源码

"""示例: 客服工作流(阶段 0 Hello World).

运行:
    cd agentframework
    PYTHONPATH=src python examples/customer_service.py
"""

from agentframework import Workflow, serve
from agentframework.nodes import Answer, ChatLLM


class _EchoModel:
    """本地演示模型(无真实 LLM): 回显用户输入."""

    def invoke(self, messages):
        text = str(messages[-1].content) if messages else "你好"
        return f"客服回复: {text}"

    def stream(self, messages):
        from langchain_core.messages import AIMessage

        reply = self.invoke(messages)
        for ch in reply:
            yield AIMessage(content=ch)


def build_customer_service() -> Workflow:
    """构建客服工作流: ChatLLM → Answer(透传回复)."""
    wf = Workflow(name="customer_service")
    wf.add_node(
        ChatLLM(
            name="llm",
            chat_model=_EchoModel(),
            system="你是一个专业的客服助手",
            output=False,  # 严格收口: LLM 流不进 answer, 由 Answer 收口(骨架设计 5.2)
        )
    )
    wf.add_node(Answer(name="ans", text=wf.ref("llm.reply")))
    wf.add_edge("llm", "ans")
    return wf


if __name__ == "__main__":
    wf = build_customer_service()
    app = wf.compile()
    print("已编译工作流:", wf.name)
    print("注册应用 appId=app-001, 启动网关 http://0.0.0.0:8080")
    serve({"app-001": app})

回到示例索引