使用说明
客户支持聊天机器人
一家计算机产品公司的客户支持聊天机器人原型,采用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:协调消息处理、工具执行和响应生成
设置
本地开发
- 创建并激活虚拟环境:
python3 -m venv venv
source venv/bin/activate
- 安装依赖项:
pip install --upgrade pip
pip install -r requirements.txt
- 运行应用程序:
python app.py
- 打开浏览器
http://localhost:7860
测试证书
donaldgarcia@example.net/PIN:7912michellejames@example.com/PIN:1520laurahenderson@example.org/PIN:1488spenceamanda@example.org/PIN:2535glee@example.net/PIN:4582
MCP服务器工具
verify_customer_pin-对客户进行身份验证list_products-浏览产品目录search_products-按关键字搜索产品get_product-获取详细的产品信息list_orders-查看客户订单历史记录get_order-获取订单详细信息create_order-下新订单
部署
拥抱面部空间
- 在HuggingFace上创建新空间
- 将SDK设置为“Gradio”
- 上传所有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 - 在“空间”设置中设置环境变量
- 空间将自动构建和部署
环境变量
必需的
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)
示例设置
- 复制示例环境文件:
cp .env.example .env
- 编辑
.env并添加您的OpenRouter API密钥:
OPENROUTER_API_KEY=your-actual-api-key-here
- 运行应用程序:
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(不需要下载模型)
