diff --git a/pet_supplies_agent.py b/pet_supplies_agent.py new file mode 100644 index 0000000000000000000000000000000000000000..8a80c2218548ed800a2c6883ed533c597981f455 --- /dev/null +++ b/pet_supplies_agent.py @@ -0,0 +1,726 @@ +import os +import json +from datetime import datetime +from lazyllm import OnlineChatModule, WebModule, pipeline, TrainableModule +import re + +# 宠物用品选品智能助手Agent +class PetSuppliesAgent: + def __init__(self): + # 初始化LazyLLM聊天模块 + try: + # 首先尝试使用OnlineChatModule + self.chat_module = self._init_local_model() + print(f"成功加载模型") + except Exception as e: + print(f"无法初始化模型: {e}") + try: + # 如果模型加载失败,尝试在线模型 + self.chat_module = OnlineChatModule() + print("成功加载在线模型") + except Exception as e2: + print(f"无法初始化在线模型: {e2}") + print("请设置API密钥,例如:export OPENAI_API_KEY=your_key") + # 使用模拟的聊天模块作为后备 + self.chat_module = self._create_mock_chat_module() + print("使用模拟聊天模块作为后备方案") + + # 宠物用品类别数据 + self.product_categories = { + "宠物食品": { + "subcategories": ["干粮", "湿粮", "零食", "营养品", "处方粮", "冻干食品", "生骨肉"], + "features": ["营养均衡", "适口性", "安全性", "功能性"] + }, + "宠物用品": { + "subcategories": ["食盆水盆", "宠物窝", "玩具", "牵引绳", "猫砂盆", "宠物笼", "美容工具"], + "features": ["耐用性", "安全性", "舒适度", "易清洁"] + }, + "宠物护理": { + "subcategories": ["洗护用品", "驱虫药", "美容用品", "口腔护理", "耳部清洁", "眼部护理"], + "features": ["温和性", "有效性", "安全性", "便捷性"] + }, + "宠物健康": { + "subcategories": ["保健品", "医疗用品", "监测设备", "急救用品", "康复用品", "疫苗"], + "features": ["专业性", "安全性", "有效性", "易用性"] + }, + "宠物出行": { + "subcategories": ["宠物包", "车载用品", "旅行用品", "户外装备", "航空箱", "便携用品"], + "features": ["便携性", "安全性", "舒适度", "多功能性"] + } + } + + # 目标市场数据 + self.target_markets = { + "北美": { + "countries": ["美国", "加拿大"], + "preferences": ["高品质", "有机认证", "人性化设计", "环保材料"], + "price_sensitivity": "中低", + "logistics_cost_factor": 1.2 + }, + "欧洲": { + "countries": ["德国", "英国", "法国", "意大利", "西班牙"], + "preferences": ["动物福利认证", "环保包装", "设计感", "天然成分"], + "price_sensitivity": "中", + "logistics_cost_factor": 1.3 + }, + "东南亚": { + "countries": ["新加坡", "马来西亚", "泰国", "越南", "菲律宾"], + "preferences": ["性价比", "耐用性", "多功能", "本地化口味"], + "price_sensitivity": "高", + "logistics_cost_factor": 0.8 + } + } + + # 构建选品流程 + self.selection_pipeline = self._build_selection_pipeline() + + # 构建对话流程 + self.chat_pipeline = self._build_chat_pipeline() + + def _init_local_model(self): + """初始化本地模型""" + try: + # 尝试从环境变量获取API密钥 + api_key = os.environ.get("DOUBAO_API_KEY") or os.environ.get("LAZYLLM_DOUBAO_API_KEY") or os.environ.get("OPENAI_API_KEY") + if api_key: + print("检测到环境变量中的API密钥,使用在线模型") + try: + # 优先尝试豆包模型 + if os.environ.get("DOUBAO_API_KEY") or os.environ.get("LAZYLLM_DOUBAO_API_KEY"): + model = OnlineChatModule( + source="doubao", + api_key=api_key + ) + else: + # 如果没有豆包密钥但有OpenAI密钥,尝试OpenAI + model = OnlineChatModule( + source="openai", + api_key=api_key + ) + + # 测试API密钥是否有效 + try: + test_response = model("测试") + print("API密钥验证成功,使用在线模型") + return model + except Exception as test_error: + print(f"API密钥验证失败: {str(test_error)}") + print("将尝试其他模型选项") + # 继续尝试其他选项 + except Exception as api_error: + print(f"使用API密钥初始化在线模型失败: {str(api_error)}") + print("将尝试其他模型选项") + # 继续尝试其他选项 + + # 尝试加载本地模型 + local_model_path = os.environ.get("LOCAL_MODEL_PATH") + if local_model_path: + if os.path.exists(local_model_path): + print(f"尝试加载本地模型: {local_model_path}") + try: + model = TrainableModule(local_model_path) + # 尝试启动模型 + try: + model.start() + print("本地模型启动成功") + except Exception as start_error: + print(f"本地模型启动失败: {str(start_error)}") + print("将尝试使用在线模型作为后备") + raise start_error + return model + except Exception as e: + print(f"加载本地模型失败: {str(e)}") + else: + print(f"本地模型路径不存在: {local_model_path}") + + # 尝试使用常见的本地模型路径 + common_model_paths = [ + os.path.expanduser("~/.cache/huggingface/hub"), + "/models", + "./models" + ] + + for path in common_model_paths: + if os.path.exists(path): + print(f"尝试从常见路径加载模型: {path}") + try: + # 尝试列出目录中的模型 + model_dirs = [d for d in os.listdir(path) + if os.path.isdir(os.path.join(path, d))] + if model_dirs: + # 尝试第一个模型目录 + model_path = os.path.join(path, model_dirs[0]) + print(f"找到模型目录: {model_path}") + model = TrainableModule(model_path) + # 尝试启动模型 + try: + model.start() + print("本地模型启动成功") + return model + except Exception as start_error: + print(f"本地模型启动失败: {str(start_error)}") + print("将尝试下一个模型或在线模型") + continue + except Exception as e: + print(f"从常见路径加载模型失败: {str(e)}") + continue + + # 尝试使用在线模型 + print("尝试使用在线模型...") + try: + model = OnlineChatModule(source="doubao") + # 测试模型是否可用 + try: + test_response = model("测试") + print("在线模型验证成功") + return model + except Exception as test_error: + print(f"在线模型验证失败: {str(test_error)}") + # 使用模拟聊天模块作为后备 + print("使用模拟聊天模块作为后备") + model = self._create_mock_chat_module() + return model + except Exception as online_error: + print(f"在线模型初始化失败: {str(online_error)}") + # 使用模拟聊天模块作为后备 + print("使用模拟聊天模块作为后备") + model = self._create_mock_chat_module() + return model + except Exception as e: + print(f"初始化模型失败: {str(e)}") + # 使用模拟聊天模块作为后备 + print("使用模拟聊天模块作为后备") + model = self._create_mock_chat_module() + return model + + def _create_mock_chat_module(self): + """创建模拟聊天模块作为后备方案""" + class MockChatModule: + def __call__(self, prompt): + # 简单的模拟响应 + if "产品类别" in prompt or "选择最合适的一个" in prompt: + return "宠物食品" + elif "热门的" in prompt and "产品" in prompt: + return """```json + { + "products": [ + { + "name": "天然有机狗粮", + "features": ["无谷物配方", "高蛋白含量", "天然成分"], + "target_audience": "注重健康的宠物主人", + "price_range": {"min": 30, "max": 80} + }, + { + "name": "智能宠物喂食器", + "features": ["定时喂食", "远程控制", "大容量储粮"], + "target_audience": "上班族宠物主人", + "price_range": {"min": 50, "max": 150} + } + ] + } + ```""" + elif "供应商" in prompt or "成本" in prompt: + return """```json + { + "production_cost": {"min": 10, "max": 25}, + "logistics_cost": {"min": 8, "max": 18}, + "tariffs": {"percentage": 8}, + "risks": ["原材料供应不稳定", "动物疫情风险"], + "supplier_type": "宠物食品加工厂" + } + ```""" + elif "竞争对手" in prompt or "竞争分析" in prompt: + return """```json + { + "competitors": [ + { + "name": "皇家宠物食品", + "price_range": {"min": 35, "max": 90}, + "market_share": "20%", + "strengths": ["品牌知名度", "专业配方"], + "weaknesses": ["价格较高", "产品线单一"] + } + ] + } + ```""" + else: + return "这是一个模拟响应,请配置API密钥以获取真实的大模型响应。" + + return MockChatModule() + + def _build_selection_pipeline(self): + """构建宠物用品选品流程""" + + def market_analysis(user_input): + """市场分析:根据用户需求分析目标市场和产品类别""" + # 提取目标市场 + target_market = None + for market in self.target_markets: + if market in user_input: + target_market = market + break + + # 如果没有指定市场,默认为北美 + if not target_market: + target_market = "北美" + + # 提取产品类别 + product_category = None + for category in self.product_categories: + if category in user_input: + product_category = category + break + + # 如果没有指定类别,使用大模型分析 + if not product_category: + prompt = f""" + 根据用户需求"{user_input}",从以下宠物用品类别中选择最合适的一个: + {list(self.product_categories.keys())} + + 只回答类别名称,不需要解释。 + """ + product_category = self.chat_module(prompt).strip() + if product_category not in self.product_categories: + product_category = "宠物食品" # 默认类别 + + return { + "target_market": target_market, + "product_category": product_category, + "market_info": self.target_markets[target_market], + "category_info": self.product_categories[product_category] + } + + def product_research(analysis_result): + """产品研究:基于市场和类别研究热门产品""" + target_market = analysis_result["target_market"] + product_category = analysis_result["product_category"] + + # 构建产品研究提示 + prompt = f""" + 作为跨境电商选品专家,请为{target_market}市场研究5款热门的{product_category}产品。 + + 要求: + 1. 每款产品包含:产品名称、主要特点、目标客户群体、价格区间(美元) + 2. 考虑{target_market}市场的消费者偏好:{', '.join(analysis_result['market_info']['preferences'])} + 3. 考虑价格敏感度:{analysis_result['market_info']['price_sensitivity']} + 4. 产品应具有创新性和差异化优势 + 5. 以JSON格式返回,包含products数组,每个产品包含name, features, target_audience, price_range字段 + + 示例格式: + {{ + "products": [ + {{ + "name": "产品名称", + "features": ["特点1", "特点2"], + "target_audience": "目标客户群体", + "price_range": {{ + "min": 10, + "max": 50 + }} + }} + ] + }} + """ + + try: + response = self.chat_module(prompt) + # 尝试解析JSON + if "```json" in response: + json_str = response.split("```json")[1].split("```")[0].strip() + else: + # 尝试提取JSON部分 + json_match = re.search(r'\{.*\}', response, re.DOTALL) + json_str = json_match.group(0) if json_match else response + + product_data = json.loads(json_str) + return { + **analysis_result, + "researched_products": product_data.get("products", []) + } + except Exception as e: + print(f"解析产品研究数据失败: {e}") + # 返回默认产品数据 + default_products = [ + { + "name": f"优质{product_category}A", + "features": ["天然成分", "营养均衡", "易消化"], + "target_audience": "注重健康的宠物主人", + "price_range": {"min": 25, "max": 70} + }, + { + "name": f"智能{product_category}B", + "features": ["自动化", "远程控制", "大容量"], + "target_audience": "上班族宠物主人", + "price_range": {"min": 40, "max": 120} + } + ] + + return { + **analysis_result, + "researched_products": default_products + } + + def supplier_analysis(research_result): + """供应商分析:分析潜在供应商和成本结构""" + products = research_result["researched_products"] + target_market = research_result["target_market"] + logistics_factor = research_result["market_info"]["logistics_cost_factor"] + + analyzed_products = [] + + for product in products: + # 构建供应商分析提示 + prompt = f""" + 作为供应链专家,请分析"{product['name']}"的供应商情况: + + 1. 估算生产成本(基于产品特点和价格区间) + 2. 估算物流成本(考虑{target_market}市场,物流系数{logistics_factor}) + 3. 估算关税和税费 + 4. 识别潜在供应链风险 + 5. 推荐供应商类型(工厂、贸易公司等) + + 以JSON格式返回,包含production_cost, logistics_cost, tariffs, risks, supplier_type字段 + """ + + try: + response = self.chat_module(prompt) + # 尝试解析JSON + if "```json" in response: + json_str = response.split("```json")[1].split("```")[0].strip() + else: + # 尝试提取JSON部分 + json_match = re.search(r'\{.*\}', response, re.DOTALL) + json_str = json_match.group(0) if json_match else response + + analysis_data = json.loads(json_str) + except Exception as e: + print(f"解析供应商分析数据失败: {e}") + # 返回默认分析数据 + price_mid = (product["price_range"]["min"] + product["price_range"]["max"]) / 2 + analysis_data = { + "production_cost": price_mid * 0.3, + "logistics_cost": price_mid * 0.1 * logistics_factor, + "tariffs": price_mid * 0.05, + "risks": ["供应链不稳定", "质量波动"], + "supplier_type": "工厂直销" + } + + # 计算利润率 + # 确保cost_analysis中的值是数字类型 + try: + production_cost = float(analysis_data.get("production_cost", 0)) + # 如果production_cost是字典,尝试获取其中的值 + if isinstance(production_cost, dict): + production_cost = float(production_cost.get("min", production_cost.get("max", 0))) + except (ValueError, TypeError): + production_cost = 0 + + try: + logistics_cost = float(analysis_data.get("logistics_cost", 0)) + # 如果logistics_cost是字典,尝试获取其中的值 + if isinstance(logistics_cost, dict): + logistics_cost = float(logistics_cost.get("min", logistics_cost.get("max", 0))) + except (ValueError, TypeError): + logistics_cost = 0 + + try: + tariffs = float(analysis_data.get("tariffs", 0)) + # 如果tariffs是字典,尝试获取其中的值 + if isinstance(tariffs, dict): + tariffs = float(tariffs.get("percentage", tariffs.get("min", tariffs.get("max", 0)))) + except (ValueError, TypeError): + tariffs = 0 + + total_cost = production_cost + logistics_cost + tariffs + avg_price = (product["price_range"]["min"] + product["price_range"]["max"]) / 2 + profit_margin = (avg_price - total_cost) / avg_price * 100 + + analyzed_products.append({ + **product, + "cost_analysis": analysis_data, + "total_cost": total_cost, + "average_price": avg_price, + "profit_margin": profit_margin + }) + + return { + **research_result, + "analyzed_products": analyzed_products + } + + def competition_analysis(analysis_result): + """竞争分析:分析市场竞争格局和差异化机会""" + products = analysis_result["analyzed_products"] + target_market = analysis_result["target_market"] + product_category = analysis_result["product_category"] + + competition_results = [] + + for product in products: + # 构建竞争分析提示 + prompt = f""" + 作为市场分析师,请分析"{product['name']}"在{target_market}{product_category}市场的竞争情况: + + 1. 识别主要竞争对手(2-3个) + 2. 分析竞争对手定价策略 + 3. 识别市场空白和差异化机会 + 4. 评估市场进入难度 + 5. 提供差异化建议 + + 以JSON格式返回,包含competitors, pricing_strategy, market_gaps, entry_difficulty, differentiation字段 + """ + + try: + response = self.chat_module(prompt) + # 尝试解析JSON + if "```json" in response: + json_str = response.split("```json")[1].split("```")[0].strip() + else: + # 尝试提取JSON部分 + json_match = re.search(r'\{.*\}', response, re.DOTALL) + json_str = json_match.group(0) if json_match else response + + competition_data = json.loads(json_str) + except Exception as e: + print(f"解析竞争分析数据失败: {e}") + # 返回默认分析数据 + competition_data = { + "competitors": ["品牌A", "品牌B"], + "pricing_strategy": "中高端定价", + "market_gaps": ["环保材料", "个性化定制"], + "entry_difficulty": "中等", + "differentiation": ["创新设计", "优质材料"] + } + + competition_results.append({ + **product, + "competition_analysis": competition_data + }) + + return { + **analysis_result, + "competition_results": competition_results + } + + def generate_recommendation(competition_result): + """生成最终推荐:基于所有分析生成选品推荐""" + products = competition_result["competition_results"] + target_market = competition_result["target_market"] + product_category = competition_result["product_category"] + + # 按利润率排序 + sorted_products = sorted(products, key=lambda x: x["profit_margin"], reverse=True) + + # 构建推荐提示 + top_products = sorted_products[:3] # 取前3个产品 + products_summary = "\n".join([ + f"{i+1}. {p['name']}: 利润率{p['profit_margin']:.1f}%, 市场差异化: {', '.join(p['competition_analysis'].get('differentiation', ['无差异化信息']))}" + for i, p in enumerate(top_products) + ]) + + prompt = f""" + 基于以下分析结果,为{target_market}{product_category}市场提供选品建议: + + 产品分析结果: + {products_summary} + + 请提供: + 1. 最推荐的产品及理由 + 2. 市场进入策略 + 3. 风险控制建议 + 4. 长期发展规划 + + 以专业、简洁的顾问口吻回答,突出关键建议。 + """ + + recommendation = self.chat_module(prompt) + + return { + "market": target_market, + "category": product_category, + "top_recommendations": sorted_products[:3], + "detailed_recommendation": recommendation, + "generation_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S") + } + + # 构建流程 + return pipeline([ + market_analysis, + product_research, + supplier_analysis, + competition_analysis, + generate_recommendation + ]) + + def _build_chat_pipeline(self): + """构建对话流程""" + + def intent_recognition(user_input): + """意图识别:识别用户意图是选品咨询还是其他问题""" + prompt = f""" + 请识别用户意图:"用户说:{user_input}" + + 意图类型: + 1. 选品咨询 - 用户想要获取产品选品建议 + 2. 市场咨询 - 用户想了解市场情况 + 3. 产品咨询 - 用户想了解特定产品信息 + 4. 其他 - 其他类型问题 + + 只回答意图类型编号(1-4),不需要解释。 + """ + + try: + intent = self.chat_module(prompt).strip() + intent_map = { + "1": "选品咨询", + "2": "市场咨询", + "3": "产品咨询", + "4": "其他" + } + return intent_map.get(intent, "其他") + except: + return "其他" + + def response_generation(user_input, intent): + """根据意图生成回复""" + if intent == "选品咨询": + # 调用选品流程 + try: + result = self.selection_pipeline(user_input) + return self._format_selection_result(result) + except Exception as e: + return f"选品分析过程中出现错误:{str(e)}。请尝试重新描述您的需求。" + + elif intent == "市场咨询": + prompt = f""" + 作为跨境电商市场专家,请回答以下市场咨询问题: + 用户问题:{user_input} + + 请提供专业、简洁的回答,重点关注北美、欧洲和东南亚市场。 + """ + return self.chat_module(prompt) + + elif intent == "产品咨询": + prompt = f""" + 作为宠物用品产品专家,请回答以下产品咨询问题: + 用户问题:{user_input} + + 请提供专业、准确的产品信息和建议。 + """ + return self.chat_module(prompt) + + else: + prompt = f""" + 作为跨境宠物用品选品智能助手,请回答以下问题: + 用户问题:{user_input} + + 请提供专业、有帮助的回答。如果问题超出范围,请礼貌说明。 + """ + return self.chat_module(prompt) + + # 构建对话流程 + def chat_process(user_input): + intent = intent_recognition(user_input) + response = response_generation(user_input, intent) + return { + "user_input": user_input, + "intent": intent, + "response": response, + "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S") + } + + return chat_process + + def _format_selection_result(self, result): + """格式化选品结果为易读的文本""" + # 检查result是否为字典类型 + if not isinstance(result, dict): + return f"选品结果格式错误:期望字典类型,收到{type(result).__name__}类型\n\n原始内容:\n{str(result)}" + + # 检查必要的键是否存在 + required_keys = ['market', 'category', 'generation_time', 'top_recommendations', 'detailed_recommendation'] + for key in required_keys: + if key not in result: + return f"选品结果格式错误:缺少必要字段 {key}" + + response = f""" +## 🌍 {result['market']}市场{result['category']}选品分析报告 + +**生成时间:** {result['generation_time']} + +### 📊 推荐产品 + +""" + + # 检查top_recommendations是否为列表 + if not isinstance(result['top_recommendations'], list): + response += "推荐产品数据格式错误:期望列表类型\n\n" + else: + for i, product in enumerate(result['top_recommendations'], 1): + # 检查product是否为字典且包含必要字段 + if not isinstance(product, dict): + response += f"产品 {i} 数据格式错误:期望字典类型,收到{type(product).__name__}类型\n\n" + continue + + # 检查必要字段是否存在 + product_required_keys = ['name', 'target_audience', 'price_range', 'profit_margin', 'features', 'competition_analysis'] + missing_keys = [key for key in product_required_keys if key not in product] + if missing_keys: + response += f"产品 {i} 缺少必要字段: {', '.join(missing_keys)}\n\n" + continue + + # 检查price_range是否为字典且包含min和max + if not isinstance(product['price_range'], dict) or 'min' not in product['price_range'] or 'max' not in product['price_range']: + response += f"产品 {i} 价格范围格式错误\n\n" + continue + + # 检查competition_analysis是否为字典且包含differentiation和entry_difficulty + if not isinstance(product['competition_analysis'], dict) or 'differentiation' not in product['competition_analysis'] or 'entry_difficulty' not in product['competition_analysis']: + response += f"产品 {i} 竞争分析格式错误\n\n" + continue + + response += f""" +**{i}. {product['name']}** +- 目标客户:{product['target_audience']} +- 价格区间:${product['price_range']['min']}-${product['price_range']['max']} +- 预估利润率:{product['profit_margin']:.1f}% +- 产品特点:{', '.join(product['features'])} +- 差异化优势:{', '.join(product['competition_analysis']['differentiation'])} +- 市场进入难度:{product['competition_analysis']['entry_difficulty']} + +""" + + response += f""" +### 💡 专业建议 + +{result['detailed_recommendation']} + +--- +*本分析由跨境宠物用品选品智能助手基于LazyLLM框架生成* + """ + + return response + + def chat(self, user_input): + """处理用户输入并返回回复""" + result = self.chat_pipeline(user_input) + # 如果结果是字典,返回response字段;否则直接返回结果 + if isinstance(result, dict): + return result.get("response", str(result)) + return result + + def start_web_chat(self): + """启动Web聊天界面""" + # 创建Web模块 + web_module = WebModule(self.chat_pipeline) + # 启动Web服务 + web_module.start().wait() + +# 主函数 +if __name__ == "__main__": + # 创建宠物用品选品智能助手 + agent = CrossBorderPetSuppliesAgent() + + # 启动Web聊天界面 + print("正在启动跨境宠物用品选品智能助手Web界面...") + agent.start_web_chat() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..d119c62c7522e0e936a557648ba201ed390b67cc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,18 @@ +lazyllm>=0.1.0 +transformers>=4.30.0 +pydantic>=2.0.0 +requests>=2.31.0 +pandas>=2.0.0 +flask>=2.0.0 +numpy>=1.24.0 +matplotlib>=3.7.0 +openai>=1.0.0 # 用于真实API调用 +tiktoken>=0.5.0 # 用于token计算 +aiohttp>=3.8.0 # 用于异步HTTP请求 +asyncio # 用于异步处理 +torch>=2.0.0 # 本地模型所需 +accelerate>=0.20.0 # 本地模型加速 +sentencepiece>=0.1.99 # 本地模型分词 +protobuf>=3.20.0 # 本地模型依赖 +scipy>=1.10.0 # 本地模型依赖 +modelscope>=1.30.0 # 本地模型下载和管理 \ No newline at end of file diff --git a/web_app.py b/web_app.py new file mode 100644 index 0000000000000000000000000000000000000000..4065b6efd39c639e491bf300dc568ffdf170db50 --- /dev/null +++ b/web_app.py @@ -0,0 +1,568 @@ +from flask import Flask, render_template_string, request, jsonify +from pet_supplies_agent import PetSuppliesAgent + +app = Flask(__name__) + +# 创建宠物用品选品智能助手实例 +agent = PetSuppliesAgent() + +# HTML模板 +html_template = ''' + + + + + + 宠物用品选品智能助手 + + + +
+
+

🐾 跨境宠物用品选品智能助手

+

基于LazyLLM框架的智能选品顾问,助您开拓宠物用品全球市场

+
+ +
+

功能特点

+
+
+

🎯 智能选品分析

+

基于市场数据和AI分析,为您提供精准的宠物用品选品建议

+
+
+

🌐 全球市场洞察

+

覆盖北美、欧洲、东南亚等主要市场,了解各地宠物主人偏好

+
+
+

💰 成本利润分析

+

全面分析产品成本、物流费用和预期利润率,优化定价策略

+
+
+

🔍 竞争格局研究

+

深入分析竞争对手和市场机会,找到差异化优势

+
+
+
+ +
+
+
🐾
+
+

宠物用品选品助手

+

基于LazyLLM的智能顾问

+
+
+ +
+
北美宠物食品
+
欧洲智能用品
+
东南亚护理用品
+
北美健康用品
+
出行用品趋势
+
+ +
+
+
+ 您好!我是跨境宠物用品选品智能助手,基于LazyLLM框架为您提供专业的选品建议和市场分析。请问您想了解哪个市场或品类的选品信息? +
+
宠物用品选品助手 · 刚刚
+
+
+ +
+
+ + + +
+ 正在分析您的需求... +
+ +
+ + +
+
+
+ + + + +''' + +@app.route('/') +def index(): + return render_template_string(html_template) + +@app.route('/chat', methods=['POST']) +def chat(): + data = request.json + message = data.get('message', '') + + try: + # 调用智能助手处理消息 + response = agent.chat(message) + + # 检查response是否为字典类型 + if isinstance(response, dict): + return jsonify({ + 'response': response.get('response', str(response)), + 'intent': response.get('intent', '未知') + }) + else: + # 如果response不是字典,创建一个包含response的字典 + return jsonify({ + 'response': str(response), + 'intent': '未知' + }) + except Exception as e: + return jsonify({ + 'response': f'处理请求时出现错误: {str(e)}', + 'intent': '错误' + }), 500 + +if __name__ == "__main__": + app.run(host='0.0.0.0', port=3001, debug=True) \ No newline at end of file diff --git "a/\346\212\200\346\234\257\346\226\207\346\241\243.md" "b/\346\212\200\346\234\257\346\226\207\346\241\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..36f6c03e70af34b38db4ebd0c67e349c611dd0f7 --- /dev/null +++ "b/\346\212\200\346\234\257\346\226\207\346\241\243.md" @@ -0,0 +1,369 @@ +# 跨境宠物用品选品云助手技术文档 + +## 1. 项目介绍 + +跨境宠物用品选品云助手,专为跨境电商卖家设计,旨在解决跨境宠物用品选品的复杂性和市场分析难题。基于LazyLLM框架开发,集成了AI大语言模型能力,提供精准的选品建议、成本利润分析和竞争格局研究。支持北美、欧洲、东南亚等多个海外市场,覆盖宠物食品、宠物用品、宠物护理、宠物健康、宠物出行等多个宠物用品类别,帮助卖家优化产品选择和定价策略,提升跨境电商业务的成功率和效率。 + +## 2. 成员详情 + +- 主要负责人:吴龙龙 +- 技术栈:LazyLLM框架、Flask Web框架、Python编程语言 +- 核心功能:AI模型集成、选品流程构建、市场数据分析、Web应用开发 + +## 3. 技术栈 + + +| 技术/框架 | 版本要求 | 用途 | +| ------------- | -------- | -------------------------- | +| LazyLLM | >=0.1.0 | 构建智能对话和选品分析模块 | +| Flask | >=2.0.0 | 构建Web应用界面 | +| Python | 3.8+ | 编程语言 | +| Transformers | >=4.30.0 | 自然语言处理模型 | +| Pydantic | >=2.0.0 | 数据验证 | +| Requests | >=2.31.0 | HTTP请求 | +| Pandas | >=2.0.0 | 数据分析 | +| NumPy | >=1.24.0 | 数值计算 | +| Matplotlib | >=3.7.0 | 数据可视化 | +| OpenAI | >=1.0.0 | 真实API调用 | +| Tiktoken | >=0.5.0 | token计算 | +| Aiohttp | >=3.8.0 | 异步HTTP请求 | +| Asyncio | - | 异步处理 | +| Torch | >=2.0.0 | 本地模型支持 | +| Accelerate | >=0.20.0 | 本地模型加速 | +| Sentencepiece | >=0.1.99 | 本地模型分词 | +| Protobuf | >=3.20.0 | 本地模型依赖 | +| Scipy | >=1.10.0 | 本地模型依赖 | +| Modelscope | >=1.30.0 | 本地模型下载和管理 | + +## 4. 接口设计 + +### 4.1 核心类接口 + +#### PetSuppliesAgent类 + +```python +class PetSuppliesAgent: + def __init__(self): + """初始化PetSuppliesAgent,加载模型和数据""" + + def _init_local_model(self): + """初始化本地模型""" + + def _create_mock_chat_module(self): + """创建模拟聊天模块作为后备""" + + def _build_selection_pipeline(self): + """构建选品流程""" + + def _build_chat_pipeline(self): + """构建对话流程""" + + def analyze_market(self, product_category, target_market): + """分析特定市场的产品需求和趋势""" + + def suggest_products(self, product_category, target_market, budget=None): + """根据产品类别和目标市场提供选品建议""" + + def evaluate_competition(self, product_name, target_market): + """评估产品在目标市场的竞争情况""" + + def calculate_profit_margin(self, product_cost, shipping_cost, target_price): + """计算产品的利润率""" + + def get_product_categories(self): + """获取所有产品类别""" + + def get_target_markets(self): + """获取所有目标市场""" +``` + +### 4.2 Web接口 + + +| 接口路径 | 请求方法 | 参数 | 返回值 | 用途 | +| ------------------------- | -------- | ----------------------------------------- | -------------- | ---------------------------------- | +| /api/analyze-market | POST | product_category, target_market | 市场分析报告 | 分析特定市场的产品需求和趋势 | +| /api/suggest-products | POST | product_category, target_market, budget | 选品建议列表 | 根据产品类别和目标市场提供选品建议 | +| /api/evaluate-competition | POST | product_name, target_market | 竞争分析报告 | 评估产品在目标市场的竞争情况 | +| /api/calculate-profit | POST | product_cost, shipping_cost, target_price | 利润率计算结果 | 计算产品的利润率 | +| /api/product-categories | GET | 无 | 产品类别列表 | 获取所有产品类别 | +| /api/target-markets | GET | 无 | 目标市场列表 | 获取所有目标市场 | + +## 5. 项目结构 + +``` +[项目名称]/ +├── pet_supplies_agent.py # 核心宠物用品选品Agent实现 +├── web_app.py # Flask Web应用实现 +├── requirements.txt # 项目依赖包 +├── README.md # 项目说明文档 +└── 技术文档.md # 技术文档 +``` + +## 6. 核心功能 + +### 6.1 选品分析功能 + +- **产品类别分析**: 支持宠物食品、宠物用品、宠物护理、宠物健康、宠物出行等多个类别的产品分析 +- **目标市场分析**: 支持北美、欧洲、东南亚等多个目标市场的分析 +- **竞品分析**: 提供竞品对比和分析功能 +- **趋势分析**: 分析市场趋势和消费者偏好 +- **利润率计算**: 提供产品利润率计算功能 + +### 6.2 对话交互功能 + +- **智能问答**: 提供关于跨境宠物用品选品的智能问答服务 +- **个性化推荐**: 根据用户需求提供个性化的产品推荐 +- **实时交互**: 支持与用户的实时对话交互 + +### 6.3 Web界面功能 + +- **可视化选品**: 提供直观的选品界面 +- **数据分析**: 展示选品分析结果和数据 +- **用户友好**: 响应式设计,支持多种设备访问 + +## 7. 部署说明 + +### 7.1 环境要求 + +- Python 3.8+ +- pip 20.0+ + +### 7.2 安装步骤 + +1. 克隆项目代码到本地 +2. 安装依赖包:`pip install -r requirements.txt` +3. 设置API密钥(可选):`export OPENAI_API_KEY=your_key` +4. 运行应用:`python web_app.py` + +## 8. 使用说明 + +### 8.1 Web应用界面 + +1. 运行应用后,访问`http://localhost:5000` +2. 在Web界面上选择产品类别和目标市场 +3. 点击"获取选品建议"按钮 +4. 查看系统返回的选品建议和市场分析报告 +5. 使用聊天功能与AI助手进行交互,获取更多选品相关信息 + +## 9. 技术亮点 + +1. **多模型支持**:自动检测并支持本地模型、豆包模型、OpenAI模型等多种AI模型 +2. **智能选品流程**:基于LazyLLM框架构建的选品流程,能够根据产品类别和目标市场提供精准的选品建议 +3. **多市场覆盖**:支持北美、欧洲、东南亚等多个海外市场 +4. **完整的数据分析**:提供市场分析、竞争分析、利润率计算等完整的数据分析功能 +5. **灵活的部署方式**:基于Flask框架的Web应用,支持灵活部署和扩展 + +## 10. 未来规划 + +1. **增加更多产品类别**:扩展到更多宠物用品类别,如宠物服装、宠物玩具等 +2. **增加更多目标市场**:扩展到更多海外市场,如南美、非洲等 +3. **优化选品算法**:基于更多的市场数据和用户反馈,优化选品算法,提高选品建议的精准度 +4. **增加用户管理功能**:增加用户注册、登录、个人中心等功能,实现个性化的选品建议 +5. **增加数据可视化功能**:增加更多的数据可视化图表,如市场趋势图、产品对比图等,帮助用户更直观地了解市场情况 + +- `accelerate`:模型加速库 +- `sentencepiece`:分词工具 +- `protobuf`:Protocol Buffers +- `scipy`:科学计算库 +- `modelscope`:模型下载和管理 + +## 4. 接口设计 + +### 4.1 核心类接口 + +#### CrossBorderPetSuppliesAgent类 + +- **初始化方法**:`__init__()` + + - 功能:初始化Agent,构建选品流程和地区信息 + - 参数:无 + - 返回值:无 +- **模型初始化**:`_init_local_model()` + + - 功能:初始化本地模型或在线模型 + - 参数:无 + - 返回值:聊天模块对象 +- **模拟聊天模块**:`_create_mock_chat_module()` + + - 功能:创建模拟聊天模块作为后备方案 + - 参数:无 + - 返回值:模拟聊天模块对象 +- **构建选品流程**:`_build_selection_pipeline()` + + - 功能:构建3C配件选品的处理管道 + - 参数:无 + - 返回值:LazyLLM Pipeline对象 +- **构建对话流程**:`_build_chat_pipeline()` + + - 功能:构建对话处理流程 + - 参数:无 + - 返回值:LazyLLM Pipeline对象 +- **对话入口**:`chat(user_input)` + + - 功能:执行用户对话的主入口 + - 参数: + - `user_input`:用户输入(字符串) + - 返回值:助手回复字符串或字典 + +### 4.2 Web API接口 + +- **首页**:`/` (GET) + + - 功能:提供选品界面 + - 请求方法:GET + - 返回值:HTML页面 +- **聊天接口**:`/chat` (POST) + + - 功能:接收聊天请求并执行分析 + - 请求方法:POST + - 请求体:JSON格式,包含message参数 + - 返回值:JSON格式的回复 + +## 5. 系统架构 + +### 5.1 整体架构 + +系统采用模块化设计,主要包含以下核心模块: + +- **Agent核心模块**:实现3C配件选品的主要逻辑 +- **Pipeline处理模块**:构建和管理选品流程 +- **模型管理模块**:处理本地模型和在线模型的加载与切换 +- **数据分析模块**:包含市场分析、产品研究、供应商分析和竞争分析等子模块 +- **用户界面模块**:提供Web和命令行两种交互方式 + +### 5.2 模块关系 + +``` +用户 -> 用户界面模块(Web/CLI) -> Agent核心模块 -> Pipeline处理模块 -> 数据分析模块 -> 模型管理模块 -> 结果返回 +``` + +### 5.3 数据流向 + +1. 用户通过界面输入选品需求 +2. Agent接收请求并启动选品流程 +3. 通过Pipeline依次执行市场分析、产品研究、供应商分析和竞争分析 +4. 生成选品报告并返回给用户 + +## 6. 功能模块详解 + +### 6.1 市场分析模块 + +- **功能**:根据用户输入分析目标市场和产品类别 +- **实现**: + - 从用户输入中提取目标市场(北美、欧洲、东南亚) + - 从用户输入中提取产品类别(宠物食品、智能用品、护理用品、健康用品、出行用品等) + - 获取市场信息和产品类别信息 +- **输出**:包含目标市场、产品类别、市场信息和类别信息的字典 + +### 6.2 产品研究模块 + +- **功能**:基于市场和类别研究热门产品 +- **实现**: + - 使用大模型分析目标市场的热门产品 + - 考虑市场消费者偏好和价格敏感度 + - 生成产品名称、特点、目标客户和价格区间 +- **输出**:包含产品列表的字典 + +### 6.3 供应商分析模块 + +- **功能**:分析潜在供应商和成本结构 +- **实现**: + - 估算生产成本、物流成本和关税 + - 识别潜在供应链风险 + - 推荐供应商类型 + - 计算利润率 +- **输出**:包含成本分析和利润率的产品信息 + +### 6.4 竞争分析模块 + +- **功能**:分析竞争对手和市场机会 +- **实现**: + - 识别主要竞争对手 + - 分析竞争对手的价格和市场份额 + - 识别竞争对手的优势和劣势 + - 提供差异化建议 +- **输出**:包含竞争分析的产品信息 + +### 6.5 报告生成模块 + +- **功能**:整合分析结果,生成结构化选品报告 +- **实现**: + - 汇总所有分析数据 + - 生成最终的选品建议 + - 提供市场机会分析 +- **输出**:完整的选品报告 + +## 7. 数据模型 + +### 7.1 产品类别数据结构 + +```python +self.product_categories = { + "类别名称": { + "subcategories": ["子类别1", "子类别2"], + "features": ["特性1", "特性2"] + } +} +``` + +### 7.2 目标市场数据结构 + +```python +self.target_markets = { + "市场名称": { + "countries": ["国家1", "国家2"], + "preferences": ["偏好1", "偏好2"], + "price_sensitivity": "价格敏感度", + "logistics_cost_factor": 物流成本系数 + } +} +``` + +### 7.3 产品数据结构 + +```python +{ + "name": "产品名称", + "features": ["特点1", "特点2"], + "target_audience": "目标客户群体", + "price_range": { + "min": 最低价格, + "max": 最高价格 + }, + "production_cost": 生产成本, + "logistics_cost": 物流成本, + "tariffs": 关税, + "risks": ["风险1", "风险2"], + "supplier_type": "供应商类型", + "profit_margin": 利润率, + "competitors": [ + { + "name": "竞争对手名称", + "price_range": {"min": 最低价格, "max": 最高价格}, + "market_share": "市场份额", + "strengths": ["优势1", "优势2"], + "weaknesses": ["劣势1", "劣势2"] + } + ] +} +``` + +## 8. 部署说明 + +### 8.1 环境要求 + +- Python 3.8+ +- 所需依赖包(见requirements.txt) + +### 8.2 安装步骤 + +1. 安装依赖:`pip install -r requirements.txt` +2. 配置API密钥(豆包或OpenAI) +3. 启动Web应用:`python web_app.py` +4. 访问Web界面:http://localhost:3333 + +### 8.3 运行模式 + +- **Web模式**:默认启动,提供图形化界面 +- **命令行模式**:可通过直接运行`python pet_supplies_agent.py`切换