客户支持聊天机器人

一款专为电脑产品公司设计的客户支持聊天机器人,具备产品搜索、订单历史查询和自然语言对话功能。

  • by AlphaNen
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 DevAsadYasin/mcp-customer-support-chatbot

使用说明

客户支持聊天机器人

一家计算机产品公司的客户支持聊天机器人原型,采用MCP(模型上下文协议)集成和轻量级LLM构建。

特性

  • 使用电子邮件和PIN进行客户身份验证
  • 产品搜索和浏览
  • 订单历史查看
  • 产品详细信息查询
  • 使用OpenRouter API的自然语言对话

建筑学

  • MCP 服务器:通过Streamable HTTP处理所有产品和订单操作
  • LLM:使用OpenRouter API进行智能响应
  • 用户界面:Gradio界面便于交互
  • 部署:拥抱面部空间

项目结构

chatbot/
├── app.py                 # Main Gradio application entry point
├── config.py             # Configuration and environment variables
├── mcp_client.py          # MCP server client implementation
├── llm_client.py         # OpenRouter LLM API client
├── utils.py              # Utility functions (extractors, helpers)
├── intent_detection.py   # Intent detection logic
├── response_generator.py # LLM response generation
├── auth.py               # User authentication functions
├── message_processor.py  # Message processing and tool orchestration
├── requirements.txt      # Python dependencies
├── README.md            # This file
└── .env                 # Environment variables (not in repo)

模块说明

  • app.py:设置Gradio界面和处理UI交互的主应用程序文件
  • config.py:加载环境变量、配置日志记录和初始化LLM客户端
  • mcp_client.py: MCPServer 用于与MCP服务器交互的类(产品、订单、身份验证)
  • llm_client.py:用于调用OpenRouter API的函数
  • utils.py:用于提取SKU、搜索查询和客户ID的帮助函数
  • intent_detection.py:确定用户意图并选择适当的工具
  • response_generator.py:使用LLM生成会话响应
  • auth.py:通过电子邮件和PIN处理客户身份验证
  • message_processor.py:协调消息处理、工具执行和响应生成

设置

本地开发

  1. 创建并激活虚拟环境:
python3 -m venv venv
source venv/bin/activate
  1. 安装依赖项:
pip install --upgrade pip
pip install -r requirements.txt
  1. 运行应用程序:
python app.py
  1. 打开浏览器 http://localhost:7860

测试证书

  • donaldgarcia@example.net /PIN: 7912
  • michellejames@example.com /PIN: 1520
  • laurahenderson@example.org /PIN: 1488
  • spenceamanda@example.org /PIN: 2535
  • glee@example.net /PIN: 4582

MCP服务器工具

  • verify_customer_pin -对客户进行身份验证
  • list_products -浏览产品目录
  • search_products -按关键字搜索产品
  • get_product -获取详细的产品信息
  • list_orders -查看客户订单历史记录
  • get_order -获取订单详细信息
  • create_order -下新订单

部署

拥抱面部空间

  1. 在HuggingFace上创建新空间
  2. 将SDK设置为“Gradio”
  3. 上传所有Python文件(app.py, config.py, mcp_client.py, llm_client.py, utils.py, intent_detection.py, response_generator.py, auth.py, message_processor.py), requirements.txt,以及 README.md
  4. 在“空间”设置中设置环境变量
  5. 空间将自动构建和部署

环境变量

必需的

  • OPENROUTER_API_KEY:OpenRouter API密钥(必需)
  • OPENROUTER_MODEL:要使用的OpenRouter模型(默认值: google/gemini-2.5-flash)

可选的

  • MCP_SERVER_URL:MCP服务器终结点(默认值:提供的URL)
  • OPENROUTER_BASE_URL:OpenRouter API基本URL(默认值: https://openrouter.ai/api/v1)

示例设置

  1. 复制示例环境文件:
cp .env.example .env
  1. 编辑 .env 并添加您的OpenRouter API密钥:
OPENROUTER_API_KEY=your-actual-api-key-here
  1. 运行应用程序:
python app.py

或者,您可以直接设置环境变量:

export OPENROUTER_API_KEY="your-api-key-here"
export OPENROUTER_MODEL="google/gemini-2.5-flash"  # Optional, this is the default
python app.py

需求

  • Python 3.8+
  • 等级 4.44.0+
  • 请求2.31.0+
  • python dotenv 1.0.0+

LLM配置

  • 提供者:OpenRouter API
  • 默认模型: google/gemini-2.5-flash (轻便、快速)
  • 无本地模型:使用基于API的LLM(不需要下载模型)