KirkHan commited on
Commit
a990ce3
·
verified ·
1 Parent(s): 5c52ac5

Upload 8 files

Browse files
Files changed (8) hide show
  1. DEPLOY_INSTRUCTIONS.md +71 -0
  2. Dockerfile +30 -0
  3. README.md +35 -0
  4. app.py +203 -0
  5. database_setup_lite.py +325 -0
  6. mock_data.json +1108 -0
  7. rag_engine.py +446 -0
  8. requirements.txt +8 -0
DEPLOY_INSTRUCTIONS.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Spaces 部署说明
2
+
3
+ ## 📁 需要上传的文件
4
+
5
+ 这个文件夹包含所有需要推送到 Hugging Face Spaces 的文件:
6
+
7
+ ```
8
+ hf_deploy/
9
+ ├── app.py # FastAPI 应用(已更新 max_tokens: 4000)
10
+ ├── Dockerfile # Docker 配置
11
+ ├── requirements.txt # Python 依赖
12
+ ├── README.md # HF Spaces 配置(包含 sdk_version)
13
+ ├── database_setup_lite.py # 数据库设置
14
+ ├── rag_engine.py # RAG 引擎(已更新 max_tokens: 4000)
15
+ └── mock_data.json # 示例数据
16
+ ```
17
+
18
+ ## 🚀 部署步骤
19
+
20
+ ### 方法 1: 使用 Git(推荐)
21
+
22
+ 1. **克隆 HF Space 仓库**(如果还没有):
23
+ ```bash
24
+ git clone https://huggingface.co/spaces/curio-lab/GraphRAG_Backend
25
+ cd GraphRAG_Backend
26
+ ```
27
+
28
+ 2. **复制所有文件到仓库根目录**:
29
+ ```bash
30
+ # 从 hf_deploy 目录复制所有文件
31
+ cp ../hf_deploy/* .
32
+ ```
33
+
34
+ 3. **提交并推送**:
35
+ ```bash
36
+ git add .
37
+ git commit -m "Update: Increase max_tokens to 4000"
38
+ git push origin main
39
+ ```
40
+
41
+ ### 方法 2: 使用 Web UI
42
+
43
+ 1. 访问 https://huggingface.co/spaces/curio-lab/GraphRAG_Backend
44
+ 2. 点击 **"Files and versions"** 标签
45
+ 3. 点击 **"Add file"** → **"Upload file"**
46
+ 4. 依次上传 `hf_deploy/` 目录下的所有文件:
47
+ - `app.py`
48
+ - `Dockerfile`
49
+ - `requirements.txt`
50
+ - `README.md`
51
+ - `database_setup_lite.py`
52
+ - `rag_engine.py`
53
+ - `mock_data.json`
54
+
55
+ ## ⚠️ 重要提示
56
+
57
+ 1. **文件必须在根目录**:所有文件必须上传到 HF Space 仓库的根目录,不要放在子文件夹中
58
+
59
+ 2. **环境变量**:确保在 HF Spaces Settings → Secrets 中设置了:
60
+ - `LLM_API_BASE` = `https://api.ai-gaochao.cn/v1`
61
+ - `LLM_API_KEY` = `sk-你的真实密钥`
62
+ - `LLM_MODEL` = `gemini-2.5-flash`
63
+ - `EMBEDDING_MODEL` = `text-embedding-3-small`
64
+
65
+ 3. **等待构建**:推送后,HF Spaces 会自动构建,通常需要 5-10 分钟
66
+
67
+ ## 📝 本次更新内容
68
+
69
+ - ✅ `rag_engine.py`: 将 `max_tokens` 从 2000 增加到 4000,避免内容被截断
70
+ - ✅ 所有文件已更新到最新版本
71
+
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # 安装系统依赖
6
+ RUN apt-get update && apt-get install -y \
7
+ git \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ # 复制 requirements.txt
11
+ COPY requirements.txt .
12
+
13
+ # 安装 Python 依赖
14
+ RUN pip install --no-cache-dir -r requirements.txt
15
+
16
+ # 复制应用代码和依赖文件
17
+ COPY app.py .
18
+ COPY database_setup_lite.py .
19
+ COPY rag_engine.py .
20
+ COPY mock_data.json .
21
+
22
+ # 暴露端口(HF Spaces 使用 7860)
23
+ EXPOSE 7860
24
+
25
+ # 设置环境变量
26
+ ENV PORT=7860
27
+ ENV TOKENIZERS_PARALLELISM=false
28
+
29
+ # 启动应用(使用 uvicorn)
30
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: GraphRAG Backend
3
+ emoji: 🕸️
4
+ colorFrom: purple
5
+ colorTo: pink
6
+ sdk: docker
7
+ sdk_version: "4.0.0"
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # GraphRAG Backend API
13
+
14
+ GraphRAG 后端服务,提供图增强检索和文案生成功能。
15
+
16
+ ## API 端点
17
+
18
+ - `GET /` - API 信息
19
+ - `GET /api/products` - 获取产品列表
20
+ - `GET /api/styles` - 获取风格列表
21
+ - `GET /api/graph` - 获取图数据
22
+ - `GET /api/vector-db` - 获取向量数据库
23
+ - `POST /api/search` - 对比检索
24
+ - `POST /api/generate` - 生成文案
25
+ - `POST /api/features/search` - 搜索特征
26
+
27
+ ## 环境变量
28
+
29
+ 在 Hugging Face Spaces 的 Settings → Secrets 中设置:
30
+
31
+ - `LLM_API_BASE` - AI API 基础地址
32
+ - `LLM_API_KEY` - AI API 密钥(敏感)
33
+ - `LLM_MODEL` - AI 模型名称(可选)
34
+ - `EMBEDDING_MODEL` - Embedding 模型名称(可选)
35
+
app.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ FastAPI 后端服务 - 用于 Hugging Face Spaces
3
+ """
4
+ import os
5
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
6
+
7
+ from fastapi import FastAPI, HTTPException
8
+ from fastapi.middleware.cors import CORSMiddleware
9
+ from pydantic import BaseModel
10
+ from typing import Optional, List
11
+ import json
12
+
13
+ # 导入数据库和 RAG 引擎
14
+ # 注意:在 HF Spaces 中,这些文件应该在同一个目录下
15
+ from database_setup_lite import setup_databases
16
+ from rag_engine import RAGEngine
17
+
18
+ # 初始化 FastAPI 应用
19
+ app = FastAPI(title="GraphRAG Backend API")
20
+
21
+ # 配置 CORS - 允许所有来源(生产环境可以限制为特定域名)
22
+ app.add_middleware(
23
+ CORSMiddleware,
24
+ allow_origins=["*"], # 生产环境可以设置为 ["https://your-frontend.vercel.app"]
25
+ allow_credentials=True,
26
+ allow_methods=["*"],
27
+ allow_headers=["*"],
28
+ )
29
+
30
+ # 初始化数据库和引擎(全局变量,避免重复初始化)
31
+ print("正在初始化数据库...")
32
+ graph_db, vector_db = setup_databases()
33
+ rag_engine = RAGEngine(graph_db, vector_db)
34
+
35
+ # 加载数据用于前端展示
36
+ with open("mock_data.json", "r", encoding="utf-8") as f:
37
+ mock_data = json.load(f)
38
+
39
+ # Pydantic 模型
40
+ class SearchRequest(BaseModel):
41
+ query: str
42
+ product_name: Optional[str] = ""
43
+ style_name: Optional[str] = ""
44
+
45
+ class GenerateRequest(BaseModel):
46
+ query: str
47
+ product_name: str
48
+ style_name: str
49
+ use_graph: bool = True
50
+
51
+ class FeatureSearchRequest(BaseModel):
52
+ query: str
53
+
54
+ @app.get("/")
55
+ def root():
56
+ """根路径"""
57
+ return {
58
+ "message": "GraphRAG Backend API",
59
+ "version": "1.0.0",
60
+ "endpoints": [
61
+ "GET /api/products",
62
+ "GET /api/styles",
63
+ "GET /api/graph",
64
+ "GET /api/vector-db",
65
+ "POST /api/search",
66
+ "POST /api/generate",
67
+ "POST /api/features/search"
68
+ ]
69
+ }
70
+
71
+ @app.get("/api/products")
72
+ def get_products():
73
+ """获取产品列表"""
74
+ demo_product = {
75
+ "id": "P_DEMO",
76
+ "name": "真丝睡眠眼罩"
77
+ }
78
+ return [demo_product]
79
+
80
+ @app.get("/api/styles")
81
+ def get_styles():
82
+ """获取风格列表"""
83
+ styles = [{"id": s["id"], "name": s["name"]} for s in mock_data["styles"]]
84
+ return styles
85
+
86
+ @app.get("/api/graph")
87
+ def get_graph():
88
+ """获取图结构数据"""
89
+ nodes = []
90
+ edges = []
91
+
92
+ # 添加节点
93
+ for node_id, node_data in graph_db.nodes.items():
94
+ nodes.append({
95
+ "id": node_id,
96
+ "type": node_data["type"],
97
+ "label": node_data["properties"].get("name") or node_data["properties"].get("content", "")[:20] or node_id,
98
+ "properties": node_data["properties"]
99
+ })
100
+
101
+ # 添加边
102
+ for edge in graph_db.edges:
103
+ edges.append({
104
+ "source": edge["source"],
105
+ "target": edge["target"],
106
+ "relationship": edge["relationship"]
107
+ })
108
+
109
+ return {
110
+ "nodes": nodes,
111
+ "edges": edges
112
+ }
113
+
114
+ @app.post("/api/search")
115
+ def search(request: SearchRequest):
116
+ """搜索接口"""
117
+ if not request.query:
118
+ raise HTTPException(status_code=400, detail="查询不能为空")
119
+
120
+ comparison = rag_engine.compare_retrieval(
121
+ request.query,
122
+ request.product_name or "",
123
+ request.style_name or ""
124
+ )
125
+
126
+ return comparison
127
+
128
+ @app.post("/api/generate")
129
+ def generate(request: GenerateRequest):
130
+ """生成文案接口"""
131
+ if not all([request.query, request.product_name, request.style_name]):
132
+ raise HTTPException(status_code=400, detail="缺少必要参数")
133
+
134
+ result = rag_engine.generate_copywriting(
135
+ request.query,
136
+ request.product_name,
137
+ request.style_name,
138
+ request.use_graph
139
+ )
140
+
141
+ return result
142
+
143
+ @app.get("/api/vector-db")
144
+ def get_vector_db():
145
+ """获取传统RAG的向量数据库内容"""
146
+ try:
147
+ collection = vector_db.collection
148
+ all_docs = collection.get()
149
+
150
+ documents = []
151
+ for i, doc_id in enumerate(all_docs["ids"]):
152
+ documents.append({
153
+ "id": doc_id,
154
+ "content": all_docs["documents"][i] if "documents" in all_docs and i < len(all_docs["documents"]) else "",
155
+ "metadata": all_docs["metadatas"][i] if "metadatas" in all_docs and i < len(all_docs["metadatas"]) else {}
156
+ })
157
+
158
+ return {
159
+ "total": len(documents),
160
+ "documents": documents
161
+ }
162
+ except Exception as e:
163
+ raise HTTPException(status_code=500, detail=str(e))
164
+
165
+ @app.post("/api/features/search")
166
+ def search_features(request: FeatureSearchRequest):
167
+ """根据查询搜索相关特征"""
168
+ query = request.query.lower()
169
+
170
+ if not query:
171
+ return {"features": []}
172
+
173
+ # 获取所有特征节点
174
+ feature_nodes = graph_db.find_nodes_by_type("Feature")
175
+ matched_features = []
176
+
177
+ for node in feature_nodes:
178
+ feature_name = node["properties"].get("name", node["id"]).lower()
179
+ # 简单的关键词匹配
180
+ if query in feature_name or any(keyword in feature_name for keyword in query.split()):
181
+ matched_features.append({
182
+ "id": node["id"],
183
+ "name": node["properties"].get("name", node["id"]),
184
+ "related_products": []
185
+ })
186
+
187
+ # 查找使用该特征的产品
188
+ for edge in graph_db.edges:
189
+ if edge["target"] == node["id"] and edge["relationship"] == "HAS_FEATURE":
190
+ product_node = graph_db.nodes.get(edge["source"], {})
191
+ if product_node.get("type") == "Product":
192
+ matched_features[-1]["related_products"].append(
193
+ product_node["properties"].get("name", edge["source"])
194
+ )
195
+
196
+ return {"features": matched_features[:10]} # 最多返回10个
197
+
198
+ # HF Spaces 会自动使用 Dockerfile 中的 CMD 启动
199
+ # 如果需要本地测试,可以取消下面的注释
200
+ # if __name__ == "__main__":
201
+ # import uvicorn
202
+ # port = int(os.getenv("PORT", 7860))
203
+ # uvicorn.run(app, host="0.0.0.0", port=port)
database_setup_lite.py ADDED
@@ -0,0 +1,325 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 轻量级图向量数据库 - 不依赖 ChromaDB,避免超过 Vercel 250MB 限制
3
+ 使用纯 Python 实现简单的向量搜索
4
+ """
5
+ import os
6
+ import json
7
+ import math
8
+ from typing import List, Dict, Optional
9
+ import requests
10
+
11
+ # 延迟导入 sentence_transformers,避免依赖冲突
12
+ HAS_SENTENCE_TRANSFORMERS = False
13
+ SentenceTransformer = None
14
+
15
+ def _try_import_sentence_transformers():
16
+ """尝试导入 sentence_transformers"""
17
+ global HAS_SENTENCE_TRANSFORMERS, SentenceTransformer
18
+ if HAS_SENTENCE_TRANSFORMERS:
19
+ return True
20
+ try:
21
+ from sentence_transformers import SentenceTransformer as ST
22
+ SentenceTransformer = ST
23
+ HAS_SENTENCE_TRANSFORMERS = True
24
+ return True
25
+ except (ImportError, RuntimeError, AttributeError) as e:
26
+ HAS_SENTENCE_TRANSFORMERS = False
27
+ return False
28
+
29
+ # 简单的内存图数据库
30
+ class SimpleGraphDB:
31
+ """简单的内存图数据库模拟"""
32
+ def __init__(self):
33
+ self.nodes = {} # {node_id: {type, properties}}
34
+ self.edges = [] # [{source, target, relationship}]
35
+
36
+ def add_node(self, node_id: str, node_type: str, properties: Dict):
37
+ """添加节点"""
38
+ self.nodes[node_id] = {
39
+ "type": node_type,
40
+ "properties": properties
41
+ }
42
+
43
+ def add_edge(self, source: str, target: str, relationship: str):
44
+ """添加边"""
45
+ self.edges.append({
46
+ "source": source,
47
+ "target": target,
48
+ "relationship": relationship
49
+ })
50
+
51
+ def get_neighbors(self, node_id: str, relationship: Optional[str] = None) -> List[Dict]:
52
+ """获取邻居节点"""
53
+ neighbors = []
54
+ for edge in self.edges:
55
+ if edge["source"] == node_id:
56
+ if relationship is None or edge["relationship"] == relationship:
57
+ target_node = self.nodes.get(edge["target"], {})
58
+ neighbors.append({
59
+ "node_id": edge["target"],
60
+ "relationship": edge["relationship"],
61
+ "properties": target_node.get("properties", {})
62
+ })
63
+ return neighbors
64
+
65
+ def find_nodes_by_type(self, node_type: str) -> List[Dict]:
66
+ """根据类型查找节点"""
67
+ return [
68
+ {"id": node_id, **node_data}
69
+ for node_id, node_data in self.nodes.items()
70
+ if node_data["type"] == node_type
71
+ ]
72
+
73
+ def find_node_by_property(self, node_type: str, property_name: str, property_value: str) -> Optional[Dict]:
74
+ """根据属性查找节点"""
75
+ for node_id, node_data in self.nodes.items():
76
+ if node_data["type"] == node_type:
77
+ props = node_data.get("properties", {})
78
+ if props.get(property_name) == property_value:
79
+ return {"id": node_id, **node_data}
80
+ return None
81
+
82
+ def cosine_similarity(vec1: List[float], vec2: List[float]) -> float:
83
+ """计算余弦相似度"""
84
+ dot_product = sum(a * b for a, b in zip(vec1, vec2))
85
+ magnitude1 = math.sqrt(sum(a * a for a in vec1))
86
+ magnitude2 = math.sqrt(sum(a * a for a in vec2))
87
+ if magnitude1 == 0 or magnitude2 == 0:
88
+ return 0.0
89
+ return dot_product / (magnitude1 * magnitude2)
90
+
91
+ class VectorDB:
92
+ """轻量级向量数据库 - 使用内存存储,不依赖 ChromaDB"""
93
+ def __init__(self):
94
+ # 文档存储:{id: {content, metadata, embedding}}
95
+ self.documents: Dict[str, Dict] = {}
96
+
97
+ # Embedding 配置
98
+ self.embedding_api_base = os.getenv("LLM_API_BASE", "https://api.ai-gaochao.cn/v1")
99
+ self.embedding_api_key = os.getenv("LLM_API_KEY", "")
100
+ self.embedding_model = os.getenv("EMBEDDING_MODEL", "text-embedding-3-small")
101
+ self.use_openai_embedding = bool(self.embedding_api_key)
102
+
103
+ if self.use_openai_embedding:
104
+ print(f"✅ 使用 OpenAI Embeddings API: {self.embedding_model}")
105
+ else:
106
+ print("ℹ️ 使用简单文本匹配(关键词搜索)")
107
+
108
+ def _get_openai_embeddings(self, texts: List[str]) -> List[List[float]]:
109
+ """调用 OpenAI Embeddings API 获取向量"""
110
+ url = f"{self.embedding_api_base}/embeddings"
111
+ headers = {
112
+ "Content-Type": "application/json",
113
+ "Authorization": f"Bearer {self.embedding_api_key}"
114
+ }
115
+ data = {
116
+ "input": texts,
117
+ "model": self.embedding_model
118
+ }
119
+
120
+ response = requests.post(url, headers=headers, json=data, timeout=30)
121
+ response.raise_for_status()
122
+ result = response.json()
123
+
124
+ return [item["embedding"] for item in result["data"]]
125
+
126
+ def _simple_text_match(self, query: str, document: str) -> float:
127
+ """简单的文本匹配评分(关键词匹配)"""
128
+ query_words = set(query.lower().split())
129
+ doc_words = set(document.lower().split())
130
+
131
+ if not query_words:
132
+ return 0.0
133
+
134
+ # 计算匹配的关键词比例
135
+ matches = len(query_words & doc_words)
136
+ return matches / len(query_words)
137
+
138
+ def add_documents(self, documents: List[str], ids: List[str], metadatas: List[Dict]):
139
+ """添加文档到向量数据库"""
140
+ if self.use_openai_embedding:
141
+ # 使用 OpenAI Embeddings API
142
+ try:
143
+ embeddings = self._get_openai_embeddings(documents)
144
+ for doc, doc_id, meta, emb in zip(documents, ids, metadatas, embeddings):
145
+ self.documents[doc_id] = {
146
+ "content": doc,
147
+ "metadata": meta,
148
+ "embedding": emb
149
+ }
150
+ except Exception as e:
151
+ print(f"⚠️ OpenAI Embeddings API 调用失败: {e}")
152
+ # 回退到简单存储(无 embedding)
153
+ for doc, doc_id, meta in zip(documents, ids, metadatas):
154
+ self.documents[doc_id] = {
155
+ "content": doc,
156
+ "metadata": meta,
157
+ "embedding": None
158
+ }
159
+ else:
160
+ # 不使用 embedding,只存储文档
161
+ for doc, doc_id, meta in zip(documents, ids, metadatas):
162
+ self.documents[doc_id] = {
163
+ "content": doc,
164
+ "metadata": meta,
165
+ "embedding": None
166
+ }
167
+
168
+ def search(self, query: str, n_results: int = 5) -> List[Dict]:
169
+ """语义搜索"""
170
+ if self.use_openai_embedding:
171
+ # 使用向量相似度搜索
172
+ try:
173
+ query_embedding = self._get_openai_embeddings([query])[0]
174
+
175
+ # 计算所有文档的相似度
176
+ results = []
177
+ for doc_id, doc_data in self.documents.items():
178
+ if doc_data["embedding"]:
179
+ similarity = cosine_similarity(query_embedding, doc_data["embedding"])
180
+ results.append({
181
+ "content": doc_data["content"],
182
+ "metadata": doc_data["metadata"],
183
+ "distance": 1 - similarity, # 转换为距离(越小越相似)
184
+ "id": doc_id
185
+ })
186
+
187
+ # 按相似度排序
188
+ results.sort(key=lambda x: x["distance"])
189
+ return results[:n_results]
190
+ except Exception as e:
191
+ print(f"⚠️ 向量搜索失败,回退到文本匹配: {e}")
192
+ # 回退到文本匹配
193
+ return self._text_search(query, n_results)
194
+ else:
195
+ # 使用简单文本匹配
196
+ return self._text_search(query, n_results)
197
+
198
+ def _text_search(self, query: str, n_results: int) -> List[Dict]:
199
+ """简单的文本匹配搜索"""
200
+ results = []
201
+ for doc_id, doc_data in self.documents.items():
202
+ score = self._simple_text_match(query, doc_data["content"])
203
+ if score > 0:
204
+ results.append({
205
+ "content": doc_data["content"],
206
+ "metadata": doc_data["metadata"],
207
+ "distance": 1 - score, # 转换为距离
208
+ "id": doc_id
209
+ })
210
+
211
+ # 按相似度排序
212
+ results.sort(key=lambda x: x["distance"])
213
+ return results[:n_results]
214
+
215
+ @property
216
+ def collection(self):
217
+ """兼容性属性,模拟 ChromaDB 的 collection 接口"""
218
+ class MockCollection:
219
+ def __init__(self, vector_db):
220
+ self.vector_db = vector_db
221
+
222
+ def get(self):
223
+ """获取所有文档"""
224
+ ids = list(self.vector_db.documents.keys())
225
+ documents = [self.vector_db.documents[id]["content"] for id in ids]
226
+ metadatas = [self.vector_db.documents[id]["metadata"] for id in ids]
227
+ return {
228
+ "ids": ids,
229
+ "documents": documents,
230
+ "metadatas": metadatas
231
+ }
232
+
233
+ return MockCollection(self)
234
+
235
+ def setup_databases():
236
+ """初始化数据库"""
237
+ # 加载数据
238
+ with open("mock_data.json", "r", encoding="utf-8") as f:
239
+ data = json.load(f)
240
+
241
+ # 初始化图数据库
242
+ graph_db = SimpleGraphDB()
243
+
244
+ # 添加产品节点
245
+ for product in data["products"]:
246
+ graph_db.add_node(
247
+ product["id"],
248
+ "Product",
249
+ {
250
+ "name": product["name"],
251
+ "type": product["type"],
252
+ "keywords": product["keywords"],
253
+ "features": product["features"]
254
+ }
255
+ )
256
+
257
+ # 添加风格节点
258
+ for style in data["styles"]:
259
+ graph_db.add_node(
260
+ style["id"],
261
+ "Style",
262
+ {
263
+ "name": style["name"],
264
+ "description": style["description"],
265
+ "characteristics": style["characteristics"]
266
+ }
267
+ )
268
+
269
+ # 添加文案节点
270
+ for copy in data["copywritings"]:
271
+ graph_db.add_node(
272
+ copy["id"],
273
+ "Copywriting",
274
+ {
275
+ "content": copy["content"],
276
+ "tag": copy["tag"],
277
+ "target_audience": copy["target_audience"]
278
+ }
279
+ )
280
+
281
+ # 添加特征节点
282
+ all_features = set()
283
+ for product in data["products"]:
284
+ for feature in product.get("features", []):
285
+ all_features.add(feature)
286
+
287
+ for feature in all_features:
288
+ graph_db.add_node(feature, "Feature", {"name": feature})
289
+
290
+ # 添加关系
291
+ for rel in data["relationships"]:
292
+ graph_db.add_edge(
293
+ rel["source"],
294
+ rel["target"],
295
+ rel["relationship"]
296
+ )
297
+
298
+ # 初始化轻量级向量数据库
299
+ vector_db = VectorDB()
300
+
301
+ # 添加文案到向量数据库
302
+ documents = []
303
+ ids = []
304
+ metadatas = []
305
+
306
+ for copy in data["copywritings"]:
307
+ documents.append(copy["content"])
308
+ ids.append(copy["id"])
309
+ metadatas.append({
310
+ "product_id": copy["product_id"],
311
+ "style_id": copy["style_id"],
312
+ "tag": copy["tag"],
313
+ "target_audience": copy["target_audience"]
314
+ })
315
+
316
+ vector_db.add_documents(documents, ids, metadatas)
317
+ print(f"✅ 向量数据库已更新,包含 {len(documents)} 个文案")
318
+
319
+ print("数据库初始化完成!")
320
+ print(f"- 图数据库节点数: {len(graph_db.nodes)}")
321
+ print(f"- 图数据库边数: {len(graph_db.edges)}")
322
+ print(f"- 向量数据库文档数: {len(documents)}")
323
+
324
+ return graph_db, vector_db
325
+
mock_data.json ADDED
@@ -0,0 +1,1108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "products": [
3
+ {
4
+ "id": "P1",
5
+ "name": "索尼降噪耳机 WH-1000XM5",
6
+ "type": "数码",
7
+ "keywords": [
8
+ "降噪",
9
+ "隔绝世界",
10
+ "静谧",
11
+ "安静",
12
+ "独处"
13
+ ],
14
+ "features": [
15
+ "主动降噪",
16
+ "长续航",
17
+ "舒适佩戴",
18
+ "便携",
19
+ "静音设计"
20
+ ]
21
+ },
22
+ {
23
+ "id": "P2",
24
+ "name": "祖玛珑香薰蜡烛 英国梨与小苍兰",
25
+ "type": "家居",
26
+ "keywords": [
27
+ "独处",
28
+ "氛围感",
29
+ "治愈",
30
+ "安静",
31
+ "避难所"
32
+ ],
33
+ "features": [
34
+ "持久留香",
35
+ "天然蜡质",
36
+ "精致包装",
37
+ "环保材质"
38
+ ]
39
+ },
40
+ {
41
+ "id": "P3",
42
+ "name": "真丝睡眠眼罩",
43
+ "type": "家居",
44
+ "keywords": [
45
+ "遮光",
46
+ "安静",
47
+ "独处",
48
+ "避难所",
49
+ "断联"
50
+ ],
51
+ "features": [
52
+ "100%真丝",
53
+ "完全遮光",
54
+ "舒适贴合",
55
+ "便携"
56
+ ]
57
+ },
58
+ {
59
+ "id": "P4",
60
+ "name": "复古CCD相机",
61
+ "type": "数码",
62
+ "keywords": [
63
+ "颗粒感",
64
+ "低像素",
65
+ "复古",
66
+ "怀旧",
67
+ "氛围感"
68
+ ],
69
+ "features": [
70
+ "复古外观",
71
+ "颗粒感照片",
72
+ "轻便便携",
73
+ "便携"
74
+ ]
75
+ },
76
+ {
77
+ "id": "P5",
78
+ "name": "手冲咖啡套装",
79
+ "type": "生活",
80
+ "keywords": [
81
+ "仪式感",
82
+ "慢生活",
83
+ "独处",
84
+ "治愈"
85
+ ],
86
+ "features": [
87
+ "精品咖啡豆",
88
+ "专业器具",
89
+ "教程指导",
90
+ "精致包装"
91
+ ]
92
+ },
93
+ {
94
+ "id": "P6",
95
+ "name": "无印良品香薰机",
96
+ "type": "家居",
97
+ "keywords": [
98
+ "氛围感",
99
+ "治愈",
100
+ "安静",
101
+ "独处",
102
+ "放松"
103
+ ],
104
+ "features": [
105
+ "静音设计",
106
+ "LED夜灯",
107
+ "定时功能"
108
+ ]
109
+ },
110
+ {
111
+ "id": "P7",
112
+ "name": "Kindle电子书阅读器",
113
+ "type": "数码",
114
+ "keywords": [
115
+ "安静",
116
+ "独处",
117
+ "思考",
118
+ "避世",
119
+ "自我"
120
+ ],
121
+ "features": [
122
+ "护眼屏幕",
123
+ "长续航",
124
+ "海量图书",
125
+ "便携"
126
+ ]
127
+ },
128
+ {
129
+ "id": "P8",
130
+ "name": "瑜伽垫",
131
+ "type": "运动",
132
+ "keywords": [
133
+ "放松",
134
+ "治愈",
135
+ "独处",
136
+ "自我",
137
+ "安静"
138
+ ],
139
+ "features": [
140
+ "防滑设计",
141
+ "环保材质",
142
+ "便携",
143
+ "舒适贴合"
144
+ ]
145
+ },
146
+ {
147
+ "id": "P9",
148
+ "name": "蓝牙音箱",
149
+ "type": "数码",
150
+ "keywords": [
151
+ "氛围感",
152
+ "治愈",
153
+ "放松",
154
+ "享受"
155
+ ],
156
+ "features": [
157
+ "360度环绕",
158
+ "长续航",
159
+ "防水设计"
160
+ ]
161
+ },
162
+ {
163
+ "id": "P10",
164
+ "name": "茶具套装",
165
+ "type": "生活",
166
+ "keywords": [
167
+ "仪式感",
168
+ "慢生活",
169
+ "独处",
170
+ "思考"
171
+ ],
172
+ "features": [
173
+ "精美设计",
174
+ "陶瓷材质",
175
+ "礼盒包装",
176
+ "精致包装",
177
+ "环保材质"
178
+ ]
179
+ }
180
+ ],
181
+ "styles": [
182
+ {
183
+ "id": "S1",
184
+ "name": "清冷避世风",
185
+ "description": "强调孤独、逃离人群、自我对话",
186
+ "characteristics": [
187
+ "孤独",
188
+ "避世",
189
+ "自我",
190
+ "安静",
191
+ "独处",
192
+ "避难所"
193
+ ]
194
+ },
195
+ {
196
+ "id": "S2",
197
+ "name": "疯狂种草风",
198
+ "description": "强调断货、颜值、必须买",
199
+ "characteristics": [
200
+ "绝绝子",
201
+ "必买",
202
+ "断货",
203
+ "颜值",
204
+ "种草"
205
+ ]
206
+ },
207
+ {
208
+ "id": "S3",
209
+ "name": "深夜emo风",
210
+ "description": "深夜情绪、感伤、思考",
211
+ "characteristics": [
212
+ "深夜",
213
+ "emo",
214
+ "感伤",
215
+ "思考",
216
+ "情绪"
217
+ ]
218
+ },
219
+ {
220
+ "id": "S4",
221
+ "name": "治愈系风格",
222
+ "description": "温暖、治愈、放松",
223
+ "characteristics": [
224
+ "治愈",
225
+ "温暖",
226
+ "放松",
227
+ "舒适",
228
+ "安心"
229
+ ]
230
+ },
231
+ {
232
+ "id": "S5",
233
+ "name": "极简生活风",
234
+ "description": "简约、品质、生活态度",
235
+ "characteristics": [
236
+ "简约",
237
+ "品质",
238
+ "生活",
239
+ "态度",
240
+ "精致"
241
+ ]
242
+ }
243
+ ],
244
+ "copywritings": [
245
+ {
246
+ "id": "C1",
247
+ "product_id": "P2",
248
+ "style_id": "S1",
249
+ "content": "夜幕低垂,城市的喧嚣仿佛被一层无形的结界隔绝,此刻,我只想退守一隅,与自己对话。点燃祖玛珑英国梨与小苍兰香薰蜡���,那一缕清冽而又微甜的香气,便如薄雾般温柔弥漫开来,瞬间构建起我的专属“避难所”。\n\n它不张扬,不聒噪,是恰到好处的疏离感。前调的英国梨带来一抹清新的果香,不甜腻,反而透着一丝清冷,恰似秋日清晨的露珠。随之而来的小苍兰,则赋予空间一抹温柔的芬芳,不浓烈,却足够治愈。这香气是持久留香的,无需刻意捕捉,它便如影随形,伴我度过每一个沉静的夜晚。\n\n我喜欢这种与世无争的氛围,它让我在独处中找到真正的自我。窝在沙发一角,翻开一本旧书,或只是静静地冥想,任由思绪在香气的引导下自由飘荡。纯粹的天然蜡质,燃烧时没有一丝杂质,只有干净的火焰和纯粹的香气,这与我对生活极简主义的追求不谋而合。精致的磨砂玻璃瓶身,无需点燃时也自成一道风景,低调而有格调,彰显着一种无声的品味。\n\n它不仅仅是一支蜡烛,更是我远离尘嚣的仪式感,是喧嚣世界里一片私密的安静角落。在这片小小的避难所里,我得以疗愈身心,与内心深处的自我坦诚相见。",
250
+ "tag": "HOT",
251
+ "target_audience": "都市独居青年"
252
+ },
253
+ {
254
+ "id": "C2",
255
+ "product_id": "P2",
256
+ "style_id": "S2",
257
+ "content": "绝绝子姐妹们!我宣布!祖玛珑英国梨与小苍兰香薰蜡烛,绝对是你们独处时光的灵魂伴侣!😭 香氛控们听好了,这支根本就是艺术品,颜值高到爆炸,摆在哪都高级感满满,简直是家居氛围感的杀手锏!\n\n想象一下,忙碌一天后,卸下疲惫,回到只属于你的小天地。点燃它,瞬间,整个房间都被那温柔又高级的香气包裹。前调是清新的英国梨,甜而不腻,带着露水的湿润感,随后小苍兰的优雅与含蓄缓缓绽放,最后以广藿香的沉稳收尾。这种多层次的治愈香气,真的能让人瞬间放松下来,仿佛置身于宁静的英式花园,忘却所有烦恼,找到属于自己的那份安静避难所。\n\n它不仅留香超级持久,天然蜡质燃烧起来也非常干净,环保材质也让人用得安心。每一次点燃,那温暖的光晕和弥漫的香气,都让我的独处时间变得格外有仪式感。它不是简单的香薰,更是一种生活情调,一种对自己温柔的投资!\n\n实话告诉你们,这支香薰蜡烛真的是“断货王”中的“断货王”!每次补货都秒空,根本抢不到!所以姐妹们,如果你看到它,请务必、立刻、马上抱走!这是我年度必买清单No.1,不买真的会后悔一整年!快冲啊!💖",
258
+ "tag": "HOT",
259
+ "target_audience": "年轻女性"
260
+ },
261
+ {
262
+ "id": "C3",
263
+ "product_id": "P2",
264
+ "style_id": "S3",
265
+ "content": "夜深了,窗外细雨沥沥,世界仿佛沉入一片寂静。是不是每个人,都有那么几个瞬间,渴望把自己包裹起来,寻一个无人打扰的“避难所”?当那些无处安放的思绪涌上心头,感伤和思考交织,我总会习惯性地走向它——祖玛珑英国梨与小苍兰香薰蜡烛。\n\n轻柔地点燃,微弱的火光在昏暗中跳动,瞬间,整个空间都被那股独特的香气温柔地笼罩。它不是那种咄咄逼人的浓郁,而更像是一场细腻的耳语:英国梨的清甜带着一丝恰到好处的凉意,小苍兰的优雅则如同月光般倾泻而下,缓缓抚平了白天所有的喧嚣和疲惫。这份香气持久留香,一点点渗入空气,也一点点渗入我的心底。\n\n我喜欢在这样的深夜,放下手机,任由天然蜡质燃烧时那份纯净的气息,与我此刻的情绪融为一体。烛光把我的影子拉得很长很长,看着它精致的包装,触碰着环保材质的烛杯,所有的细节都在无声地诉说着一份对生活、对自我的温柔呵护。这份独处的“氛围感”被极致放大,让感伤的情绪不再那么尖锐,反而多了一份沉淀后的安宁。\n\n它不仅仅是一支香薰蜡烛,更像是我深夜里那位无声的知己。它不评判,不打扰,只是默默地用它温柔的光和香,为我搭建起一个安静的港湾,一个可以自由emo、思考人生的专属空间。在这里,心底那些纷繁复杂的情绪,似乎都被这治愈的香气温柔地安抚。那一刻,我与香气共鸣,与孤独和解,在光影交错中,找回了久违的平静与力量。",
266
+ "tag": "HOT",
267
+ "target_audience": "深夜思考者"
268
+ },
269
+ {
270
+ "id": "C4",
271
+ "product_id": "P1",
272
+ "style_id": "S1",
273
+ "content": "在都市的喧嚣中,你是否也渴望一处纯粹的空谷,只闻心跳,不染尘嚣?🎧 索尼WH-1000XM5,于我而言,早已超越了耳机的范畴,它是我的便携式“避难所”,是我与这个世界之间,那道若有似无的结界。\n\n戴上它的一瞬,世界仿佛按下静音键,所有冗余的嘈杂被温柔且坚定地隔���在外。咖啡馆的窃窃私语、地铁的轰鸣、甚至窗外的风声,都化作了背景里模糊的低语,不再有侵扰。这份极致的主动降噪,像为我的灵魂筑起了一方无声的穹顶,只留给我最纯粹的静谧。\n\n在这片由XM5构筑的独处空间里,我可以毫无保留地与自我对话。不必去迎合外界的期待,不用去理会人群的纷扰。我听得见自己的呼吸,感受得到思绪的流淌,每一次心跳都变得清晰而有力。它不只是隔绝噪音,更是提供了一个让内心得以沉淀、自我得以生长的避世之所。\n\n长达数十小时的续航,让这份清冷与自我,得以从晨曦延续至深夜,无需担忧被打断的烦恼。而轻若无物的舒适佩戴感,即便在漫长的冥想或深度阅读中,也感受不到丝毫压迫,仿佛它本就是你的一部分。它的静音设计,简约而深沉,与我的避世美学不谋而合。这不叫孤独,这叫在浮躁世界里,为自己觅得一隅安心的净土。它是我的“暂停键”,更是每一次心神",
274
+ "tag": "COLD",
275
+ "target_audience": "通勤族"
276
+ },
277
+ {
278
+ "id": "C5",
279
+ "product_id": "P1",
280
+ "style_id": "S2",
281
+ "content": "姐妹们!我真的挖到宝藏了!这款索尼WH-1000XM5降噪耳机,简直是我的救命稻草,直接刷新了我对“安静”的认知!这颜值,绝绝子!高级感直接拉满,戴上你就是氛围感女王本人!\n\n以前通勤地铁上人声鼎沸,办公室键盘声此起彼伏,想安静地看本书、听首歌都难。自从有了它,一键开启,整个世界瞬间“静音”!那种被温柔包裹的感觉,太治愈了!所有的喧嚣、嘈杂,统统被它隔绝在外,瞬间拥有一个完全属于自己的静谧小宇宙。它不是简单的降噪,而是给你一片极致的安静,让你能真正沉浸在自己的独处时光里。\n\n而且,它的佩戴舒适度简直无敌,轻若无物,戴一天耳朵都不会累,仿佛没戴一样。长续航更是出差旅行必备,告别电量焦虑,无论多长的旅途,它都能稳稳陪伴。便携性也超赞,轻巧收纳,不占空间,随时随地都能享受这份专属的静谧。\n\n真的,姐妹们!如果你也渴望一份属于自己的安静,想要把这个浮躁的世界“静音”,这款索尼XM5你真的不能错过!它已经不止是耳机了,它是你心灵的避风港,是你的私人静谧舱!这波不冲你一定会后悔!最近已经有断货趋势了,赶紧行动起来,不然真的抢不到啦!快去拥有它,给你的耳朵和心灵一个最温柔的拥抱吧!",
282
+ "tag": "HOT",
283
+ "target_audience": "数码爱好者"
284
+ },
285
+ {
286
+ "id": "C6",
287
+ "product_id": "P4",
288
+ "style_id": "S2",
289
+ "content": "姐妹们!救命🆘!我挖到宝藏了!这台复古CCD相机简直是我的梦中情机,颜值逆天,直接把我带回千禧年! ✨\n\n自从入手了它,我的手机拍照功能都失宠了!它的外观是那种超有质感的复古设计,拿在手里就是时尚单品,随便一拍都是大片既视感,秒杀一切滤镜。但真正让我沦陷的,是它拍出来的照片!那独特的低像素颗粒感,简直绝绝子!不是那种模糊感,而是自带电影胶片滤镜的怀旧风情,每一张都充满故事和浓浓的氛围感,瞬间把日常场景变成艺术品。\n\n无论是和姐妹下午茶,还是周末出去Citywalk,或者记录旅行沿途的风景,它轻巧到可以随便塞进小包,完全没有负担。拍出来的照片不用P,自带的复古调调就高级到不行,那种时间沉淀下来的温暖感,让你的回忆都变得特别有温度。再也不用花大把时间调色找滤镜了,随手一按,就是氛围感天花板!\n\n我跟你们说,这玩意儿真的太火了,好多地方都断货了!如果你也爱复古风,追求那种独特的视觉体验,那么这台CCD相机你一定要拥有!它会让你重新爱上记录生活,感受每一个瞬间的美好,绝对是今年必入的单品,不买真的会后悔一辈子啊!",
290
+ "tag": "HOT",
291
+ "target_audience": "摄影爱好者"
292
+ },
293
+ {
294
+ "id": "C7",
295
+ "product_id": "P5",
296
+ "style_id": "S1",
297
+ "content": "当城市被喧嚣吞噬,夜色温柔地落下,我选择将自己抽离,遁入一个只属于我的安静角落。窗外霓虹闪烁,而我的世界,只剩下咖啡研磨的细碎声和水流轻柔地落在咖啡粉上的沙沙响。☕️ 这套手冲咖啡套装,不只是一组工具,它是我的避难所,是我与自我对话的个人仪式。\n\n打开精致的包装,里面是精心挑选的精品咖啡豆,带着泥土和阳光的气息,等待被唤醒。专业的器具冰冷而有温度,它们等待着被赋予生命。我依照教程指引,每一步都充满专注,细致地研磨,再用温热的水,以缓慢而均匀的速度,画着圈圈,让咖啡的灵魂一点点被唤醒���\n\n氤氲的香气,像一张无形的网,将我与尘嚣彻底隔开。此刻,没有社交的压力,没有琐碎的烦扰,只有我和我的咖啡,以及内心深处最真实的对话。这是一种孤独,却也无比治愈的独处。它让我慢下来,让思绪沉淀,每一口都带着深邃的苦涩和回甘,像极了人生,需要细细品味。\n\n它不是为了取悦任何人,而是为了滋养自我。在咖啡的暖意中,我找到了片刻的安宁,一个专属的安静角落。在这里,我可以卸下所有的伪装,与自我坦诚相待。这份清冷避世的慢生活,是我送给自己最好的礼物。✨",
298
+ "tag": "COLD",
299
+ "target_audience": "咖啡爱好者"
300
+ },
301
+ {
302
+ "id": "C8",
303
+ "product_id": "P6",
304
+ "style_id": "S4",
305
+ "content": "忙碌了一天,最期待的就是回到家,卸下所有疲惫,沉浸在只属于自己的小世界里。这时候,我的无印良品香薰机就是我的治愈法宝。它轻柔地吐出细密的雾气,带着我最爱的精油香气,瞬间让整个房间被温暖又安静的氛围感包裹。那种由内而外的放松和治愈,真的能让一天的烦恼都烟消云散。\n\n最爱它的静音设计,几乎感受不到它的存在,只有淡淡的香气和柔和的LED夜灯在默默陪伴。无论是深夜窝在沙发里读一本书,还是只是静静发呆,享受独处的安静时光,这份不被打扰的舒适感都弥足珍贵。开启定时功能,让香气温柔地伴随我缓缓进入甜美的梦乡,再也不用担心忘记关掉,安心感十足。\n\n看着它散发出的袅袅白雾,仿佛时间都慢了下来。它不仅仅是一台香薰机,更是我打造一方舒适安心小天地的秘密武器。在浮躁的世界里,给自己留一点与自我对话的时间,被温柔包围,感受生活中每一个值得珍藏的小确幸。这份温暖与舒适,值得每一个你拥有。",
306
+ "tag": "HOT",
307
+ "target_audience": "都市白领"
308
+ },
309
+ {
310
+ "id": "C9",
311
+ "product_id": "P6",
312
+ "style_id": "S1",
313
+ "content": "当世界按下静音键,只有我和无印良品香薰机。\n\n都市的喧嚣与疲惫,在踏入家门的那一刻,仿佛被一道无形的墙壁隔绝。我喜欢这种抽离感,将所有嘈杂抛诸脑后,只留下最纯粹的自我。这时,无印良品香薰机,成了我构筑这份避世清冷的唯一伴侣。\n\n它静默地立于一隅,没有一丝多余的色彩,如同我心境的投射。轻柔的水雾缓缓升腾,携带着我钟爱的木质香气,在空气中无声弥散。这不仅仅是“氛围感”,更是一种治愈系的结界,将我圈入一个只属于自己的小宇宙。静音设计是它的温柔,没有任何电机噪音能打破这份珍贵的安静,让我的思绪得以完全沉淀,与自我进行深层对话。\n\n夜幕降临,开启LED夜灯,那束微弱而温暖的光,如同夜空中最遥远的星辰,照亮我的阅读页,或者仅仅是陪伴我放空。不需要强烈的刺激,只需要这样淡淡的存在,就能带来极致的放松。定时功能更是贴心,它懂得我需要一个不被打扰的独处时光,也懂得何时让这一切温柔地结束。在这里,孤独并非寂寞,而是一种选择,一种力量。我的房间,我的避难所,因它而变得更加完整。在这里,我与世无争,只与自己同在。",
314
+ "tag": "HOT",
315
+ "target_audience": "都市独居青年"
316
+ },
317
+ {
318
+ "id": "C10",
319
+ "product_id": "P7",
320
+ "style_id": "S4",
321
+ "content": "最近是不是也觉得,生活节奏快到让人有点喘不过气?🌿 我找到了一个可以随时‘避世’的治愈小空间,那就是我的Kindle电子书阅读器。\n\n下班后,窝进柔软的沙发里,或是周末午后在阳光洒进的飘窗边,拿起它,世界的喧嚣仿佛瞬间被隔绝在外。那块特有的护眼屏幕,真的做到了像纸质书一样的温柔,长时间阅读眼睛也一点不觉得累,简直是为我们这些书虫量身定制的舒适感。📖\n\n最爱它超长续航的安心感,常常一两周都不用充电,让我可以完全沉浸在故事里,不必担心电量不足的焦虑,这种无打扰的体验太珍贵了。📚 海量的图书资源,无论是想探索浩瀚的宇宙,还是沉浸在浪漫的文学世界,都能轻松找到,每一次翻阅都是一次全新的自我发现之旅。\n\n小小一只,轻巧便携,无论是通勤路上,还是旅行途中,它都是那个默默陪伴你的知己。它不仅是冰冷的数码产品,更像是一个温暖的港湾,为你提供独处的机会,思考人生的真谛,找回内心深处的平静。这种只属于你和文字的亲密时刻,是现代生活中不可多得的治愈力量。✨ 拥有它,就拥有了一片属于自己的安静天地,让每一次阅读都成为一场灵魂的洗礼,温柔且坚定地与自我对话。",
322
+ "tag": "HOT",
323
+ "target_audience": "阅读爱好者"
324
+ },
325
+ {
326
+ "id": "C11",
327
+ "product_id": "P7",
328
+ "style_id": "S1",
329
+ "content": "当外界喧嚣渐远,当人群的浮躁令人疲惫,我需要一个只属于自己的角落。Kindle,便是我清冷的避难所,是我与世界保持距离的温柔屏障。它不是冰冷的数码设备,而是通往无数精神世界的密钥,是我与自我深层对话的唯一听众。\n\n指尖轻触,护眼屏幕上墨色文字温柔浮现,没有刺眼的光源,只有知识的温润与智慧的沉淀。我常在深夜,城市灯火阑珊时;在雨后的窗边,听雨打芭蕉声;或是在人迹罕至的山顶,呼吸清冽的空气,打开它。长续航让我的独处时光无需被外界琐事打扰,我可以放心地将整个下午或夜晚,沉浸在哲学巨著的浩瀚思考中,或在异世奇幻里避世长游,让思绪自由驰骋。\n\n海量图书是取之不尽的精神宝藏,从卡尔维诺的奇幻旅程到叔本华的孤独哲学,从村上春树的都市迷茫到梭罗的湖畔沉思,都能在此寻得共鸣。每一本书都是一个独立宇宙,而Kindle,就是通往这些宇宙的便携式飞船。它轻巧得仿佛不存在,却能装下整个宇宙的智慧与情感,让我的精神世界无远弗届。\n\n手握Kindle,我不是在简单的阅读,而是在进行一场私密的精神漫游。那些在书中相遇的灵魂,与我一同避开尘世的喧嚣,在文字构筑的宁静空间里,重新审视自我,沉淀思绪。孤独是我的勋章,独处是我的享受。Kindle,予我一片宁静的栖息地,让我安心地与自己的",
330
+ "tag": "HOT",
331
+ "target_audience": "通勤族"
332
+ },
333
+ {
334
+ "id": "C12",
335
+ "product_id": "P8",
336
+ "style_id": "S4",
337
+ "content": "姐妹们,有没有觉得最近生活节奏太快,身心俱疲?我最近爱上了在家里的独处瑜伽时光,真的是太治愈了!今天想跟大家分享我的秘密武器——这款让我爱不释手的瑜伽垫。\n\n每次下班回家,褪去一天的疲惫,铺开这张瑜伽垫,就像是开启了一个只属于我的安静结界。它的环保材质摸起来特别亲肤,没有任何异味,让我能安心地进行深呼吸,感觉就像是与大自然亲密接触。踩上去的那一刻,感觉整个人都被温柔地包裹起来,软硬适中,不会陷进去,也不会硬邦邦,完美地舒适贴合着身体曲线。\n\n最让我惊喜的是它的防滑设计!无论是做猫牛式舒展脊柱,还是挑战一些平衡体式,它都能稳稳地抓住地面,给我满满的安全感,再也不用担心滑倒啦。而且,它的舒适贴合度真的超赞,每一次伸展,每一次扭转,都能感觉到身体与垫子完美融合,动作也变得更加流畅自如。\n\n周末的清晨,泡一杯热茶,放上轻柔的冥想音乐,在这张瑜伽垫上练习,感受身体的苏醒与放松,仿佛所有的压力和烦恼都随着汗水一点点释放,找回内心的平衡。它不仅仅是一块运动垫,更是我寻找内心平静,实现自我治愈的小天地。因为它便携的设计,我还经常带它去公园,享受阳光下的瑜伽,把那份治愈带到户外。\n\n如果你也渴望拥有这样一份独处的治愈时光,真的推荐你试试它。让身体和心灵都能在这份柔软与安静中,找到久违的放松与平衡,体验真正的自我治愈。",
338
+ "tag": "HOT",
339
+ "target_audience": "瑜伽爱好者"
340
+ },
341
+ {
342
+ "id": "C13",
343
+ "product_id": "P8",
344
+ "style_id": "S1",
345
+ "content": "城市万象喧嚣,人群川流不息,总有那么一刻,只想逃离,寻一隅清净,与自我深谈。🧘‍♀️ 我的“避难所”,就是这张看似普通的瑜伽垫。当它轻柔地铺展开来,便瞬间隔绝了世界的芜杂,为我圈出一片独享的宁静天地。\n\n指尖触及的,是来自环保材质的纯粹与温柔,冰凉中带着一丝安抚。脚下的防滑设计,稳稳地托住我的每寸肌肤,让我不再担心外界的跌宕,只专注于内心的平衡与呼吸的节奏。✨ 在这里,没有旁人的目光,没有催促的声音,只有我自己,和这片专属的方寸之地。每一次缓慢的舒展,每一次沉静的冥想,都是与“自我”最深度、最真实的对话,仿佛在安静的宇宙里,独自漂浮,没有束缚。\n\n这种清冷而纯粹的独处,像一剂良药,治愈着日常积累的疲惫与浮躁。它不只是一件运动器械,更是我心灵的避难所,一个只属于我的精神港湾。当外界嘈杂入耳,我便会自然而然地走向它,寻求那份不言而喻的安静。而且它的轻巧便携,让我可以将这份宁静随身携带,无论是窗边、阳台,还是旅行中的一角,只要有它,我便能随时进入我的“避世”模式,找到那份舒适贴合的安稳。\n\n寻一个专属的角落,让身心在这片宁静中得到最深沉的放松与疗愈。这不是逃避,���是为了更好地面对,在孤独中找到力量与安宁。🌿",
346
+ "tag": "HOT",
347
+ "target_audience": "瑜伽爱好者"
348
+ },
349
+ {
350
+ "id": "C14",
351
+ "product_id": "P9",
352
+ "style_id": "S2",
353
+ "content": "OMG!姐妹们!最近挖到个蓝牙音箱绝绝子!氛围感秒拉满,不买会后悔一辈子!\n\n真的要疯狂安利这个宝藏音箱!颜值高到犯规,高级感满满,摆在哪儿都像一件艺术品,瞬间提升家居品味!它的360度环绕音效简直不要",
354
+ "tag": "HOT",
355
+ "target_audience": "音乐爱好者"
356
+ },
357
+ {
358
+ "id": "C15",
359
+ "product_id": "P9",
360
+ "style_id": "S3",
361
+ "content": "夜深了,城市万籁俱寂,只有指尖轻触屏幕的微光,和心底那一丝挥之不去的emo。耳机总觉得隔绝了世界,手机外放又少了点仪式感。直到我遇见了这个小小的蓝牙音箱,它成了我深夜情绪的专属治愈师。\n\n当熟悉的旋律从音箱中缓缓流淌出来,360度环绕音效瞬间将我包裹,不再是单薄的音符,而是像一层温柔的薄雾,将我笼罩在一个只属于我的声音结界里。闭上眼,仿佛置身于某个遥远而宁静的海岸,海浪声、风声、歌声,所有的一切都变得立体而真实。那些白日里堆积的疲惫、委屈和迷茫,在这一刻,都被音乐轻轻抚平,心里涌动的感伤,也仿佛找到了宣泄的出口,得到了温柔的安抚。\n\n它的长续航能力也让人格外安心,深夜的思绪总是漫长而无止境,无需担心电量耗尽会打断这份珍贵的沉浸。我可以任由自己沉溺在音乐的海洋中,从星辰满天到晨曦微露。更惊喜的是,它还拥有防水设计。有时,我会带着它走进浴室,让温热的水汽与舒缓的音乐一同蒸腾,洗去一天的尘埃和烦恼。在水声和歌声的交织中,我允许自己放下所有防备,享受这份极致的放松。\n\n它不只是一款音箱,更是深夜里一份懂得你心事的温柔陪伴。它带来的不止是声音,更是一种无法言喻的「氛围感」,一种深沉的「治愈」力量,以及最纯粹的「放松」与「享受」。深夜emo,有它在,好像也没那么孤独了。",
362
+ "tag": "HOT",
363
+ "target_audience": "深夜思考者"
364
+ },
365
+ {
366
+ "id": "C16",
367
+ "product_id": "P10",
368
+ "style_id": "S5",
369
+ "content": "都市喧嚣里,你是否也渴望一方宁静,一份属于自己的慢时光?今天想和大家分享一套让我爱不释手的【极简陶瓷茶具套装】,它不仅仅是一套茶具,更是我开启品质慢生活的仪式感入口。\n\n精美的设计,线条流畅,没有任何多余的装饰,完美诠释了“less is more”的极简美学。陶瓷材质温润如玉,握在手中,指尖能感受到细腻的触感,每一杯茶都仿佛被赋予了温度。更值得一提的是,它选用环保材质,在享受生活的同时,也对地球多了一份温柔。\n\n清晨,阳光透过窗帘洒进屋子,泡一壶清茶,任由茶香氤氲。一个人的午后,它陪伴我阅读、思考,或是放空,让思绪在茶烟袅袅中自由飞舞。这份独处的宁静,是都市人最奢侈的享受,它教会我如何在忙碌中找到平衡,重拾生活的节奏感。\n\n每次用它泡茶,都像在进行一场小小的仪式",
370
+ "tag": "HOT",
371
+ "target_audience": "茶文化爱好者"
372
+ },
373
+ {
374
+ "id": "C17",
375
+ "product_id": "P10",
376
+ "style_id": "S1",
377
+ "content": "城市喧嚣渐次远去,我寻一方避世之所,只为与自己独处。当外界纷扰渐远,这款陶瓷茶具,便成了我独享时光的静谧港湾,一个只属于我的精神避难所。\n\n它的精美设计,不是为了张扬,而是内敛地安抚着每一寸视线。温润的陶瓷手感,仿佛沉淀了千年岁月,每一次轻柔的触碰,都带着古朴的宁静与温度。泡一壶清茶,看茶叶在水中缓慢舒展,热气氤氲,模糊了窗外的一切尘嚣。这不是简单的饮茶,而是一场专属我的仪式感,是对慢生活最深沉的诠释。打开🎁精致的礼盒包装时,那份拆解惊喜便已将我带入另一个世界,连环保材质的选择,都透着一份对自然的敬意与体悟。\n\n这套茶具,不为迎合任何人的目光,只为安放我那颗渴望清净的灵魂。在茶香缭绕中,我思考,我沉淀,我与那个最真实的自己坦诚对话。它陪伴我度过无数个安静的午后,让孤独不再是空虚的代名词,而是丰盈自我的深刻体验。它是我在人群中抽离的理由,是我为自己打造的精神港湾。这份由茶具所赋予的独处,成为了我对抗世界喧嚣的武器,也成为了我内心最深处的慰藉与力量。\n\n如果你也渴望一份不被打扰的清寂,渴望在日常中寻觅一个避世之所,那么它,或许正是你一直在寻找的答案。让茶韵伴你,",
378
+ "tag": "HOT",
379
+ "target_audience": "茶文化爱好者"
380
+ },
381
+ {
382
+ "id": "C18",
383
+ "product_id": "P1",
384
+ "style_id": "S4",
385
+ "content": "城市里的喧嚣是不是常常让你感到心力交瘁?你有没有想过,能随时随地拥有一片只属于自己的宁静小天地?我的答案,是索尼WH-1000XM5降噪耳机。\n\n每天通勤的地铁上,人潮拥挤,嘈杂声此起彼伏,而我,却能安然享受着自己的一方天地。戴上它的那一刻,仿佛有一道无形的屏障轻轻落下,瞬间隔绝了所有噪音。那些烦躁的引擎声、人声鼎沸的交谈,都像被施了魔法般消失不见。只剩下纯粹的音乐,或是安静的留白,让思绪自由飘散,找到久违的平静。\n\n它的主动降噪功能真的太强大了,连微小的环境音都能被温柔地过滤掉。而且佩戴感简直是极致的舒适,轻盈得好像戴了片云朵,耳朵完全没有压迫感,即使是长途飞行或午后小憩,也能安心佩戴。超长续航也让人毫无后顾之忧,从早到晚都能有这份静谧相伴。\n\n无论是想沉浸在喜欢的音乐世界里,还是需要一个绝对安静的环境来专注工作、阅读,甚至是冥想,它都能完美满足。它不仅仅是一副耳机,更是我忙碌生活中的一份慰藉,",
386
+ "tag": "HOT",
387
+ "target_audience": "音乐爱好者"
388
+ },
389
+ {
390
+ "id": "C19",
391
+ "product_id": "P4",
392
+ "style_id": "S3",
393
+ "content": "深夜,万籁俱寂,只有屏幕微光映照着疲惫的脸。刷着那些高清到失真的完美瞬间,心头却涌上一股难以言喻的空虚。总觉得,现代世界的极致清晰,反而模糊了真实的情绪,是不是,我们都太习惯于追求无暇,却忘了残缺的美好?就在这样一个人静下来的夜晚,我遇见了它——这台复古CCD相机。\n\n它小巧轻便得能随意塞进包里,复古的外壳,握在手里仿佛就能触碰到旧时光的纹理。它不是为了挑战像素极限,而是想温柔地帮你留住那些转瞬即逝的“不完美”。按下快门,它捕捉到的不是冰冷的数字,而是带着温度的颗粒感,那种独特的低像素美学,让人瞬间掉进回忆的漩涡。\n\n深夜的咖啡馆,氤氲的水汽,窗外细密的雨丝,都被它赋予了一种独特的怀旧滤镜。低像素的照片,没有锐利的棱角,只有朦胧的氛围感,就像记忆深处那些被温柔模糊的旧梦。那些不经意间记录下的,或是深夜独酌的咖啡杯,或是路灯下被拉长的孤单身影,都因为这份独特的颗粒感,变得格外有故事,带着一丝感伤却又温柔的底色。\n\n它让我重新爱上那些模糊的、有点失焦的瞬间,因为它们更真实,更贴近心底的脆弱与感伤。每一张照片都像一封写给过去的信,又像一次与内心深处的对话,它不刻意,不喧哗,只是静静地,替你收藏起那些只有你自己懂得的,深夜情绪。带着它穿梭于城市边缘的寂静街道,那些被遗忘的角落,被它镀上了一层温柔的旧电影光晕。或许,我们需要的从来不是极致的清晰,而是能触动灵魂深处,那份带着温度的,独一无二的怀旧感。",
394
+ "tag": "HOT",
395
+ "target_audience": "摄影爱好者"
396
+ },
397
+ {
398
+ "id": "C20",
399
+ "product_id": "P5",
400
+ "style_id": "S4",
401
+ "content": "都市快节奏生活,你是不是也渴望片刻的宁静和自我对话?最近,我的治愈秘密武器就是这款手冲咖啡套装啦!它不仅是一套工具,更是一张通往慢生活的船票,带你体验独属的治愈时光。\n\n从研磨咖啡豆的沙沙声开始,到热水缓缓注入咖啡粉,看咖啡液如琥珀般一滴一滴坠落,整个过程都充满了庄重而美好的仪式感。精选的精品咖啡豆,打开瞬间就能闻到馥郁的香气,那是大自然最温柔的馈赠。一口下去,醇厚回甘,仿佛把一整个午后温暖的阳光都喝进了肚子里,瞬间洗去所有疲惫。\n\n即使你是咖啡小白也不用担心,专业的器具设计精巧,搭配详尽贴心的教程指导,让你轻松上手,享受成为自己专属咖啡师的乐趣。这份精致的包装,无论是送给自己一份心意,还是作为礼物馈赠好友,都显得格外有品味。独处的午后,一杯手冲,一本书,一段舒缓的音乐,所有的烦恼都被这温暖的香气温柔包裹,瞬间被治愈。它不只是一杯咖啡,更是一种慢下来、爱自己的生活态度。\n\n让手冲咖啡成为你生活中的小确幸,给自己一个安心舒适的治愈时刻吧。",
402
+ "tag": "HOT",
403
+ "target_audience": "咖啡爱好者"
404
+ }
405
+ ],
406
+ "relationships": [
407
+ {
408
+ "source": "P1",
409
+ "source_type": "Product",
410
+ "relationship": "HAS_FEATURE",
411
+ "target": "主动降噪",
412
+ "target_type": "Feature"
413
+ },
414
+ {
415
+ "source": "P1",
416
+ "source_type": "Product",
417
+ "relationship": "HAS_FEATURE",
418
+ "target": "长续航",
419
+ "target_type": "Feature"
420
+ },
421
+ {
422
+ "source": "P1",
423
+ "source_type": "Product",
424
+ "relationship": "HAS_FEATURE",
425
+ "target": "舒适佩戴",
426
+ "target_type": "Feature"
427
+ },
428
+ {
429
+ "source": "P1",
430
+ "source_type": "Product",
431
+ "relationship": "HAS_FEATURE",
432
+ "target": "便携",
433
+ "target_type": "Feature"
434
+ },
435
+ {
436
+ "source": "P1",
437
+ "source_type": "Product",
438
+ "relationship": "HAS_FEATURE",
439
+ "target": "静音设计",
440
+ "target_type": "Feature"
441
+ },
442
+ {
443
+ "source": "P2",
444
+ "source_type": "Product",
445
+ "relationship": "HAS_FEATURE",
446
+ "target": "持久留香",
447
+ "target_type": "Feature"
448
+ },
449
+ {
450
+ "source": "P2",
451
+ "source_type": "Product",
452
+ "relationship": "HAS_FEATURE",
453
+ "target": "天然蜡质",
454
+ "target_type": "Feature"
455
+ },
456
+ {
457
+ "source": "P2",
458
+ "source_type": "Product",
459
+ "relationship": "HAS_FEATURE",
460
+ "target": "精致包装",
461
+ "target_type": "Feature"
462
+ },
463
+ {
464
+ "source": "P2",
465
+ "source_type": "Product",
466
+ "relationship": "HAS_FEATURE",
467
+ "target": "环保材质",
468
+ "target_type": "Feature"
469
+ },
470
+ {
471
+ "source": "P3",
472
+ "source_type": "Product",
473
+ "relationship": "HAS_FEATURE",
474
+ "target": "100%真丝",
475
+ "target_type": "Feature"
476
+ },
477
+ {
478
+ "source": "P3",
479
+ "source_type": "Product",
480
+ "relationship": "HAS_FEATURE",
481
+ "target": "完全遮光",
482
+ "target_type": "Feature"
483
+ },
484
+ {
485
+ "source": "P3",
486
+ "source_type": "Product",
487
+ "relationship": "HAS_FEATURE",
488
+ "target": "舒适贴合",
489
+ "target_type": "Feature"
490
+ },
491
+ {
492
+ "source": "P3",
493
+ "source_type": "Product",
494
+ "relationship": "HAS_FEATURE",
495
+ "target": "便携",
496
+ "target_type": "Feature"
497
+ },
498
+ {
499
+ "source": "P4",
500
+ "source_type": "Product",
501
+ "relationship": "HAS_FEATURE",
502
+ "target": "复古外观",
503
+ "target_type": "Feature"
504
+ },
505
+ {
506
+ "source": "P4",
507
+ "source_type": "Product",
508
+ "relationship": "HAS_FEATURE",
509
+ "target": "颗粒感照片",
510
+ "target_type": "Feature"
511
+ },
512
+ {
513
+ "source": "P4",
514
+ "source_type": "Product",
515
+ "relationship": "HAS_FEATURE",
516
+ "target": "轻便便携",
517
+ "target_type": "Feature"
518
+ },
519
+ {
520
+ "source": "P4",
521
+ "source_type": "Product",
522
+ "relationship": "HAS_FEATURE",
523
+ "target": "便携",
524
+ "target_type": "Feature"
525
+ },
526
+ {
527
+ "source": "P5",
528
+ "source_type": "Product",
529
+ "relationship": "HAS_FEATURE",
530
+ "target": "精品咖啡豆",
531
+ "target_type": "Feature"
532
+ },
533
+ {
534
+ "source": "P5",
535
+ "source_type": "Product",
536
+ "relationship": "HAS_FEATURE",
537
+ "target": "专业器具",
538
+ "target_type": "Feature"
539
+ },
540
+ {
541
+ "source": "P5",
542
+ "source_type": "Product",
543
+ "relationship": "HAS_FEATURE",
544
+ "target": "教程指导",
545
+ "target_type": "Feature"
546
+ },
547
+ {
548
+ "source": "P5",
549
+ "source_type": "Product",
550
+ "relationship": "HAS_FEATURE",
551
+ "target": "精致包装",
552
+ "target_type": "Feature"
553
+ },
554
+ {
555
+ "source": "P6",
556
+ "source_type": "Product",
557
+ "relationship": "HAS_FEATURE",
558
+ "target": "静音设计",
559
+ "target_type": "Feature"
560
+ },
561
+ {
562
+ "source": "P6",
563
+ "source_type": "Product",
564
+ "relationship": "HAS_FEATURE",
565
+ "target": "LED夜灯",
566
+ "target_type": "Feature"
567
+ },
568
+ {
569
+ "source": "P6",
570
+ "source_type": "Product",
571
+ "relationship": "HAS_FEATURE",
572
+ "target": "定时功能",
573
+ "target_type": "Feature"
574
+ },
575
+ {
576
+ "source": "P7",
577
+ "source_type": "Product",
578
+ "relationship": "HAS_FEATURE",
579
+ "target": "护眼屏幕",
580
+ "target_type": "Feature"
581
+ },
582
+ {
583
+ "source": "P7",
584
+ "source_type": "Product",
585
+ "relationship": "HAS_FEATURE",
586
+ "target": "长续航",
587
+ "target_type": "Feature"
588
+ },
589
+ {
590
+ "source": "P7",
591
+ "source_type": "Product",
592
+ "relationship": "HAS_FEATURE",
593
+ "target": "海量图书",
594
+ "target_type": "Feature"
595
+ },
596
+ {
597
+ "source": "P7",
598
+ "source_type": "Product",
599
+ "relationship": "HAS_FEATURE",
600
+ "target": "便携",
601
+ "target_type": "Feature"
602
+ },
603
+ {
604
+ "source": "P8",
605
+ "source_type": "Product",
606
+ "relationship": "HAS_FEATURE",
607
+ "target": "防滑设计",
608
+ "target_type": "Feature"
609
+ },
610
+ {
611
+ "source": "P8",
612
+ "source_type": "Product",
613
+ "relationship": "HAS_FEATURE",
614
+ "target": "环保材质",
615
+ "target_type": "Feature"
616
+ },
617
+ {
618
+ "source": "P8",
619
+ "source_type": "Product",
620
+ "relationship": "HAS_FEATURE",
621
+ "target": "便携",
622
+ "target_type": "Feature"
623
+ },
624
+ {
625
+ "source": "P8",
626
+ "source_type": "Product",
627
+ "relationship": "HAS_FEATURE",
628
+ "target": "舒适贴合",
629
+ "target_type": "Feature"
630
+ },
631
+ {
632
+ "source": "P9",
633
+ "source_type": "Product",
634
+ "relationship": "HAS_FEATURE",
635
+ "target": "360度环绕",
636
+ "target_type": "Feature"
637
+ },
638
+ {
639
+ "source": "P9",
640
+ "source_type": "Product",
641
+ "relationship": "HAS_FEATURE",
642
+ "target": "长续航",
643
+ "target_type": "Feature"
644
+ },
645
+ {
646
+ "source": "P9",
647
+ "source_type": "Product",
648
+ "relationship": "HAS_FEATURE",
649
+ "target": "防水设计",
650
+ "target_type": "Feature"
651
+ },
652
+ {
653
+ "source": "P10",
654
+ "source_type": "Product",
655
+ "relationship": "HAS_FEATURE",
656
+ "target": "精美设计",
657
+ "target_type": "Feature"
658
+ },
659
+ {
660
+ "source": "P10",
661
+ "source_type": "Product",
662
+ "relationship": "HAS_FEATURE",
663
+ "target": "陶瓷材质",
664
+ "target_type": "Feature"
665
+ },
666
+ {
667
+ "source": "P10",
668
+ "source_type": "Product",
669
+ "relationship": "HAS_FEATURE",
670
+ "target": "礼盒包装",
671
+ "target_type": "Feature"
672
+ },
673
+ {
674
+ "source": "P10",
675
+ "source_type": "Product",
676
+ "relationship": "HAS_FEATURE",
677
+ "target": "精致包装",
678
+ "target_type": "Feature"
679
+ },
680
+ {
681
+ "source": "P10",
682
+ "source_type": "Product",
683
+ "relationship": "HAS_FEATURE",
684
+ "target": "环保材质",
685
+ "target_type": "Feature"
686
+ },
687
+ {
688
+ "source": "P2",
689
+ "source_type": "Product",
690
+ "relationship": "HAS_COPY",
691
+ "target": "C1",
692
+ "target_type": "Copywriting"
693
+ },
694
+ {
695
+ "source": "P2",
696
+ "source_type": "Product",
697
+ "relationship": "HAS_COPY",
698
+ "target": "C2",
699
+ "target_type": "Copywriting"
700
+ },
701
+ {
702
+ "source": "P2",
703
+ "source_type": "Product",
704
+ "relationship": "HAS_COPY",
705
+ "target": "C3",
706
+ "target_type": "Copywriting"
707
+ },
708
+ {
709
+ "source": "P1",
710
+ "source_type": "Product",
711
+ "relationship": "HAS_COPY",
712
+ "target": "C4",
713
+ "target_type": "Copywriting"
714
+ },
715
+ {
716
+ "source": "P1",
717
+ "source_type": "Product",
718
+ "relationship": "HAS_COPY",
719
+ "target": "C5",
720
+ "target_type": "Copywriting"
721
+ },
722
+ {
723
+ "source": "P4",
724
+ "source_type": "Product",
725
+ "relationship": "HAS_COPY",
726
+ "target": "C6",
727
+ "target_type": "Copywriting"
728
+ },
729
+ {
730
+ "source": "P5",
731
+ "source_type": "Product",
732
+ "relationship": "HAS_COPY",
733
+ "target": "C7",
734
+ "target_type": "Copywriting"
735
+ },
736
+ {
737
+ "source": "P6",
738
+ "source_type": "Product",
739
+ "relationship": "HAS_COPY",
740
+ "target": "C8",
741
+ "target_type": "Copywriting"
742
+ },
743
+ {
744
+ "source": "P6",
745
+ "source_type": "Product",
746
+ "relationship": "HAS_COPY",
747
+ "target": "C9",
748
+ "target_type": "Copywriting"
749
+ },
750
+ {
751
+ "source": "P7",
752
+ "source_type": "Product",
753
+ "relationship": "HAS_COPY",
754
+ "target": "C10",
755
+ "target_type": "Copywriting"
756
+ },
757
+ {
758
+ "source": "P7",
759
+ "source_type": "Product",
760
+ "relationship": "HAS_COPY",
761
+ "target": "C11",
762
+ "target_type": "Copywriting"
763
+ },
764
+ {
765
+ "source": "P8",
766
+ "source_type": "Product",
767
+ "relationship": "HAS_COPY",
768
+ "target": "C12",
769
+ "target_type": "Copywriting"
770
+ },
771
+ {
772
+ "source": "P8",
773
+ "source_type": "Product",
774
+ "relationship": "HAS_COPY",
775
+ "target": "C13",
776
+ "target_type": "Copywriting"
777
+ },
778
+ {
779
+ "source": "P9",
780
+ "source_type": "Product",
781
+ "relationship": "HAS_COPY",
782
+ "target": "C14",
783
+ "target_type": "Copywriting"
784
+ },
785
+ {
786
+ "source": "P9",
787
+ "source_type": "Product",
788
+ "relationship": "HAS_COPY",
789
+ "target": "C15",
790
+ "target_type": "Copywriting"
791
+ },
792
+ {
793
+ "source": "P10",
794
+ "source_type": "Product",
795
+ "relationship": "HAS_COPY",
796
+ "target": "C16",
797
+ "target_type": "Copywriting"
798
+ },
799
+ {
800
+ "source": "P10",
801
+ "source_type": "Product",
802
+ "relationship": "HAS_COPY",
803
+ "target": "C17",
804
+ "target_type": "Copywriting"
805
+ },
806
+ {
807
+ "source": "P1",
808
+ "source_type": "Product",
809
+ "relationship": "HAS_COPY",
810
+ "target": "C18",
811
+ "target_type": "Copywriting"
812
+ },
813
+ {
814
+ "source": "P4",
815
+ "source_type": "Product",
816
+ "relationship": "HAS_COPY",
817
+ "target": "C19",
818
+ "target_type": "Copywriting"
819
+ },
820
+ {
821
+ "source": "P5",
822
+ "source_type": "Product",
823
+ "relationship": "HAS_COPY",
824
+ "target": "C20",
825
+ "target_type": "Copywriting"
826
+ },
827
+ {
828
+ "source": "C1",
829
+ "source_type": "Copywriting",
830
+ "relationship": "HAS_STYLE",
831
+ "target": "S1",
832
+ "target_type": "Style"
833
+ },
834
+ {
835
+ "source": "C2",
836
+ "source_type": "Copywriting",
837
+ "relationship": "HAS_STYLE",
838
+ "target": "S2",
839
+ "target_type": "Style"
840
+ },
841
+ {
842
+ "source": "C3",
843
+ "source_type": "Copywriting",
844
+ "relationship": "HAS_STYLE",
845
+ "target": "S3",
846
+ "target_type": "Style"
847
+ },
848
+ {
849
+ "source": "C4",
850
+ "source_type": "Copywriting",
851
+ "relationship": "HAS_STYLE",
852
+ "target": "S1",
853
+ "target_type": "Style"
854
+ },
855
+ {
856
+ "source": "C5",
857
+ "source_type": "Copywriting",
858
+ "relationship": "HAS_STYLE",
859
+ "target": "S2",
860
+ "target_type": "Style"
861
+ },
862
+ {
863
+ "source": "C6",
864
+ "source_type": "Copywriting",
865
+ "relationship": "HAS_STYLE",
866
+ "target": "S2",
867
+ "target_type": "Style"
868
+ },
869
+ {
870
+ "source": "C7",
871
+ "source_type": "Copywriting",
872
+ "relationship": "HAS_STYLE",
873
+ "target": "S1",
874
+ "target_type": "Style"
875
+ },
876
+ {
877
+ "source": "C8",
878
+ "source_type": "Copywriting",
879
+ "relationship": "HAS_STYLE",
880
+ "target": "S4",
881
+ "target_type": "Style"
882
+ },
883
+ {
884
+ "source": "C9",
885
+ "source_type": "Copywriting",
886
+ "relationship": "HAS_STYLE",
887
+ "target": "S1",
888
+ "target_type": "Style"
889
+ },
890
+ {
891
+ "source": "C10",
892
+ "source_type": "Copywriting",
893
+ "relationship": "HAS_STYLE",
894
+ "target": "S4",
895
+ "target_type": "Style"
896
+ },
897
+ {
898
+ "source": "C11",
899
+ "source_type": "Copywriting",
900
+ "relationship": "HAS_STYLE",
901
+ "target": "S1",
902
+ "target_type": "Style"
903
+ },
904
+ {
905
+ "source": "C12",
906
+ "source_type": "Copywriting",
907
+ "relationship": "HAS_STYLE",
908
+ "target": "S4",
909
+ "target_type": "Style"
910
+ },
911
+ {
912
+ "source": "C13",
913
+ "source_type": "Copywriting",
914
+ "relationship": "HAS_STYLE",
915
+ "target": "S1",
916
+ "target_type": "Style"
917
+ },
918
+ {
919
+ "source": "C14",
920
+ "source_type": "Copywriting",
921
+ "relationship": "HAS_STYLE",
922
+ "target": "S2",
923
+ "target_type": "Style"
924
+ },
925
+ {
926
+ "source": "C15",
927
+ "source_type": "Copywriting",
928
+ "relationship": "HAS_STYLE",
929
+ "target": "S3",
930
+ "target_type": "Style"
931
+ },
932
+ {
933
+ "source": "C16",
934
+ "source_type": "Copywriting",
935
+ "relationship": "HAS_STYLE",
936
+ "target": "S5",
937
+ "target_type": "Style"
938
+ },
939
+ {
940
+ "source": "C17",
941
+ "source_type": "Copywriting",
942
+ "relationship": "HAS_STYLE",
943
+ "target": "S1",
944
+ "target_type": "Style"
945
+ },
946
+ {
947
+ "source": "C18",
948
+ "source_type": "Copywriting",
949
+ "relationship": "HAS_STYLE",
950
+ "target": "S4",
951
+ "target_type": "Style"
952
+ },
953
+ {
954
+ "source": "C19",
955
+ "source_type": "Copywriting",
956
+ "relationship": "HAS_STYLE",
957
+ "target": "S3",
958
+ "target_type": "Style"
959
+ },
960
+ {
961
+ "source": "C20",
962
+ "source_type": "Copywriting",
963
+ "relationship": "HAS_STYLE",
964
+ "target": "S4",
965
+ "target_type": "Style"
966
+ },
967
+ {
968
+ "source": "C1",
969
+ "source_type": "Copywriting",
970
+ "relationship": "TARGET_AUDIENCE",
971
+ "target": "都市独居青年",
972
+ "target_type": "Persona"
973
+ },
974
+ {
975
+ "source": "C2",
976
+ "source_type": "Copywriting",
977
+ "relationship": "TARGET_AUDIENCE",
978
+ "target": "年轻女性",
979
+ "target_type": "Persona"
980
+ },
981
+ {
982
+ "source": "C3",
983
+ "source_type": "Copywriting",
984
+ "relationship": "TARGET_AUDIENCE",
985
+ "target": "深夜思考者",
986
+ "target_type": "Persona"
987
+ },
988
+ {
989
+ "source": "C4",
990
+ "source_type": "Copywriting",
991
+ "relationship": "TARGET_AUDIENCE",
992
+ "target": "通勤族",
993
+ "target_type": "Persona"
994
+ },
995
+ {
996
+ "source": "C5",
997
+ "source_type": "Copywriting",
998
+ "relationship": "TARGET_AUDIENCE",
999
+ "target": "数码爱好者",
1000
+ "target_type": "Persona"
1001
+ },
1002
+ {
1003
+ "source": "C6",
1004
+ "source_type": "Copywriting",
1005
+ "relationship": "TARGET_AUDIENCE",
1006
+ "target": "摄影爱好者",
1007
+ "target_type": "Persona"
1008
+ },
1009
+ {
1010
+ "source": "C7",
1011
+ "source_type": "Copywriting",
1012
+ "relationship": "TARGET_AUDIENCE",
1013
+ "target": "咖啡爱好者",
1014
+ "target_type": "Persona"
1015
+ },
1016
+ {
1017
+ "source": "C8",
1018
+ "source_type": "Copywriting",
1019
+ "relationship": "TARGET_AUDIENCE",
1020
+ "target": "都市白领",
1021
+ "target_type": "Persona"
1022
+ },
1023
+ {
1024
+ "source": "C9",
1025
+ "source_type": "Copywriting",
1026
+ "relationship": "TARGET_AUDIENCE",
1027
+ "target": "都市独居青年",
1028
+ "target_type": "Persona"
1029
+ },
1030
+ {
1031
+ "source": "C10",
1032
+ "source_type": "Copywriting",
1033
+ "relationship": "TARGET_AUDIENCE",
1034
+ "target": "阅读爱好者",
1035
+ "target_type": "Persona"
1036
+ },
1037
+ {
1038
+ "source": "C11",
1039
+ "source_type": "Copywriting",
1040
+ "relationship": "TARGET_AUDIENCE",
1041
+ "target": "通勤族",
1042
+ "target_type": "Persona"
1043
+ },
1044
+ {
1045
+ "source": "C12",
1046
+ "source_type": "Copywriting",
1047
+ "relationship": "TARGET_AUDIENCE",
1048
+ "target": "瑜伽爱好者",
1049
+ "target_type": "Persona"
1050
+ },
1051
+ {
1052
+ "source": "C13",
1053
+ "source_type": "Copywriting",
1054
+ "relationship": "TARGET_AUDIENCE",
1055
+ "target": "瑜伽爱好者",
1056
+ "target_type": "Persona"
1057
+ },
1058
+ {
1059
+ "source": "C14",
1060
+ "source_type": "Copywriting",
1061
+ "relationship": "TARGET_AUDIENCE",
1062
+ "target": "音乐爱好者",
1063
+ "target_type": "Persona"
1064
+ },
1065
+ {
1066
+ "source": "C15",
1067
+ "source_type": "Copywriting",
1068
+ "relationship": "TARGET_AUDIENCE",
1069
+ "target": "深夜思考者",
1070
+ "target_type": "Persona"
1071
+ },
1072
+ {
1073
+ "source": "C16",
1074
+ "source_type": "Copywriting",
1075
+ "relationship": "TARGET_AUDIENCE",
1076
+ "target": "茶文化爱好者",
1077
+ "target_type": "Persona"
1078
+ },
1079
+ {
1080
+ "source": "C17",
1081
+ "source_type": "Copywriting",
1082
+ "relationship": "TARGET_AUDIENCE",
1083
+ "target": "茶文化爱好者",
1084
+ "target_type": "Persona"
1085
+ },
1086
+ {
1087
+ "source": "C18",
1088
+ "source_type": "Copywriting",
1089
+ "relationship": "TARGET_AUDIENCE",
1090
+ "target": "音乐爱好者",
1091
+ "target_type": "Persona"
1092
+ },
1093
+ {
1094
+ "source": "C19",
1095
+ "source_type": "Copywriting",
1096
+ "relationship": "TARGET_AUDIENCE",
1097
+ "target": "摄影爱好者",
1098
+ "target_type": "Persona"
1099
+ },
1100
+ {
1101
+ "source": "C20",
1102
+ "source_type": "Copywriting",
1103
+ "relationship": "TARGET_AUDIENCE",
1104
+ "target": "咖啡爱好者",
1105
+ "target_type": "Persona"
1106
+ }
1107
+ ]
1108
+ }
rag_engine.py ADDED
@@ -0,0 +1,446 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ RAG引擎:实现传统RAG和GraphRAG的检索逻辑
3
+ """
4
+ from typing import List, Dict, Tuple
5
+ # 优先使用轻量级版本(避免超过 Vercel 250MB 限制)
6
+ try:
7
+ from database_setup_lite import SimpleGraphDB, VectorDB
8
+ except ImportError:
9
+ from database_setup import SimpleGraphDB, VectorDB
10
+ import json
11
+ import requests
12
+
13
+ # LLM配置(从环境变量读取,确保安全)
14
+ import os
15
+ LLM_API_BASE = os.getenv("LLM_API_BASE", "https://api.ai-gaochao.cn/v1")
16
+ LLM_API_KEY = os.getenv("LLM_API_KEY", "")
17
+ LLM_MODEL = os.getenv("LLM_MODEL", "gemini-2.5-flash")
18
+
19
+ if not LLM_API_KEY:
20
+ raise ValueError("LLM_API_KEY 环境变量未设置!请在 .env 文件中设置 LLM_API_KEY")
21
+
22
+ class TraditionalRAG:
23
+ """传统语义RAG"""
24
+ def __init__(self, vector_db: VectorDB, graph_db: SimpleGraphDB = None):
25
+ self.vector_db = vector_db
26
+ self.graph_db = graph_db # 用于限制搜索范围
27
+
28
+ def retrieve(self, query: str, product_name: str = None, style_name: str = None, n_results: int = 5) -> Dict:
29
+ """语义检索(传统RAG:直接向量搜索,不利用图结构,返回片段句子)"""
30
+ # 传统RAG的特点:直接进行语义相似度搜索,不利用图结构
31
+ # 使用相同的文案数据库,但只返回相似的片段句子(而不是完整文案)
32
+
33
+ # 直接进行向量搜索(传统RAG的特点)
34
+ # 传统RAG限制结果数量,只返回最相关的2-3个结果
35
+ limited_results = min(3, n_results) # 最多返回3个结果
36
+ all_results = self.vector_db.search(query, n_results=limited_results * 2) # 多搜索一些,用于提取片段
37
+
38
+ # 从完整文案中提取与查询最相关的片段句子
39
+ processed_results = []
40
+ query_keywords = set(query.lower().split())
41
+
42
+ for result in all_results[:limited_results * 2]:
43
+ full_content = result.get("content", "")
44
+ if not full_content:
45
+ continue
46
+
47
+ # 将文案按句子分割(中文句号、英文句号、感叹号、问号)
48
+ import re
49
+ sentences = re.split(r'[。!?.!?]', full_content)
50
+ sentences = [s.strip() for s in sentences if s.strip()]
51
+
52
+ # 找到与查询最相关的句子片段
53
+ best_sentences = []
54
+ for sentence in sentences:
55
+ # 计算句子与查询的相关度(简单关键词匹配)
56
+ sentence_lower = sentence.lower()
57
+ keyword_matches = sum(1 for keyword in query_keywords if keyword in sentence_lower)
58
+ if keyword_matches > 0:
59
+ best_sentences.append((sentence, keyword_matches))
60
+
61
+ # 按相关度排序,取前2-3个最相关的句子
62
+ best_sentences.sort(key=lambda x: x[1], reverse=True)
63
+ selected_sentences = [s[0] for s in best_sentences[:3]]
64
+
65
+ # 如果没有找到相关句子,取前3个句子作为片段
66
+ if not selected_sentences and sentences:
67
+ selected_sentences = sentences[:3]
68
+
69
+ # 组合成片段(最多150字,确保有足够内容)
70
+ snippet = "。".join(selected_sentences)
71
+ if not snippet and sentences:
72
+ # 如果还是空的,至少取前3个句子
73
+ snippet = "。".join(sentences[:3])
74
+ if len(snippet) > 150:
75
+ snippet = snippet[:150] + "..."
76
+ elif len(snippet) < 30 and len(sentences) > 0:
77
+ # 如果片段太短,至少取前2-3个句子
78
+ snippet = "。".join(sentences[:min(3, len(sentences))])
79
+ if len(snippet) > 150:
80
+ snippet = snippet[:150] + "..."
81
+
82
+ if snippet:
83
+ processed_results.append({
84
+ "content": snippet, # 返回片段而不是完整文案
85
+ "full_content": full_content, # 保留完整内容用于显示
86
+ "metadata": result.get("metadata", {}),
87
+ "distance": result.get("distance", 0),
88
+ "is_snippet": True # 标记这是片段
89
+ })
90
+
91
+ if len(processed_results) >= limited_results:
92
+ break
93
+
94
+ # 如果结果太少,至少返回1-2个语义相似的结果
95
+ if len(processed_results) < 1:
96
+ # 如果提取片段失败,至少返回一些结果
97
+ for result in all_results[:max(1, limited_results)]:
98
+ content = result.get("content", "")
99
+ if content:
100
+ # 简单截取前150字作为片段
101
+ snippet = content[:150] + "..." if len(content) > 150 else content
102
+ processed_results.append({
103
+ "content": snippet,
104
+ "full_content": content,
105
+ "metadata": result.get("metadata", {}),
106
+ "distance": result.get("distance", 0),
107
+ "is_snippet": True
108
+ })
109
+ if len(processed_results) >= limited_results:
110
+ break
111
+
112
+ return {
113
+ "method": "语义检索",
114
+ "query": query,
115
+ "product": product_name,
116
+ "style": style_name,
117
+ "results": processed_results[:limited_results],
118
+ "retrieval_path": [
119
+ "向量相似度搜索(传统RAG:不利用图结构)",
120
+ f"找到 {len(processed_results)} 个语义相似的片段",
121
+ "⚠️ 局限性:只返回片段句子,没有图结构,无法找到跨品类的风格相关文案"
122
+ ],
123
+ "explanation": "传统RAG直接通过语义相似度搜索相关文案,使用相同的文案数据库,但只返回与查询最相关的片段句子(而不是完整文案)。没有图结构,无法找到跨品类的风格相关文案。"
124
+ }
125
+
126
+ class GraphRAG:
127
+ """图增强RAG"""
128
+ def __init__(self, graph_db: SimpleGraphDB, vector_db: VectorDB):
129
+ self.graph_db = graph_db
130
+ self.vector_db = vector_db
131
+
132
+ def retrieve(self, query: str, product_name: str = None, style_name: str = None, n_results: int = 5) -> Dict:
133
+ """图增强检索"""
134
+ retrieval_path = []
135
+ retrieved_docs = []
136
+
137
+ # 步骤1: 尝试找到风格节点
138
+ style_node = None
139
+ if style_name:
140
+ style_node = self.graph_db.find_node_by_property("Style", "name", style_name)
141
+ if style_node:
142
+ retrieval_path.append(f"定位风格节点: {style_node['properties']['name']}")
143
+
144
+ # 步骤2: 通过风格节点找到相关文案(跨品类)
145
+ if style_node:
146
+ # 反向查找:找到连接到风格的文案节点
147
+ for edge in self.graph_db.edges:
148
+ if edge["target"] == style_node["id"] and edge["relationship"] == "HAS_STYLE":
149
+ copy_node = self.graph_db.nodes.get(edge["source"])
150
+ if copy_node and copy_node["type"] == "Copywriting":
151
+ content = copy_node["properties"]["content"]
152
+ # 获取该文案关联的产品(HAS_COPY关系:Product -> Copywriting)
153
+ product_id = None
154
+ for e in self.graph_db.edges:
155
+ if e["target"] == edge["source"] and e["relationship"] == "HAS_COPY":
156
+ product_id = e["source"]
157
+ break
158
+
159
+ product_info = self.graph_db.nodes.get(product_id, {}).get("properties", {})
160
+ retrieved_docs.append({
161
+ "content": content,
162
+ "source": "图遍历",
163
+ "product": product_info.get("name", "未知"),
164
+ "style": style_name,
165
+ "tag": copy_node["properties"].get("tag", ""),
166
+ "retrieval_reason": f"通过风格节点'{style_name}'找到的跨品类文案(来自产品:{product_info.get('name', '未知')})"
167
+ })
168
+
169
+ if retrieved_docs:
170
+ retrieval_path.append(f"通过风格节点遍历找到 {len(retrieved_docs)} 个相关文案")
171
+ else:
172
+ retrieval_path.append("未找到该风格的相关文案")
173
+
174
+ # 步骤3: 如果指定了产品,查找产品特征
175
+ product_features = []
176
+ if product_name:
177
+ product_node = self.graph_db.find_node_by_property("Product", "name", product_name)
178
+ if product_node:
179
+ retrieval_path.append(f"定位产品节点: {product_name}")
180
+ features = product_node["properties"].get("features", [])
181
+ keywords = product_node["properties"].get("keywords", [])
182
+ product_features = features + keywords
183
+ retrieval_path.append(f"提取产品特征: {', '.join(product_features[:5])}")
184
+
185
+ # 步骤4: 如果图检索结果不足,用向量检索补充
186
+ if len(retrieved_docs) < n_results:
187
+ vector_results = self.vector_db.search(query, n_results=n_results - len(retrieved_docs))
188
+ for result in vector_results:
189
+ # 避免重复
190
+ if not any(doc["content"] == result["content"] for doc in retrieved_docs):
191
+ retrieved_docs.append({
192
+ "content": result["content"],
193
+ "source": "向量检索补充",
194
+ "product": result["metadata"].get("product_id", "未知"),
195
+ "style": result["metadata"].get("style_id", "未知"),
196
+ "tag": result["metadata"].get("tag", ""),
197
+ "retrieval_reason": "语义���似度补充检索"
198
+ })
199
+ if vector_results:
200
+ retrieval_path.append(f"向量检索补充 {len(vector_results)} 个结果")
201
+
202
+ return {
203
+ "method": "图增强检索",
204
+ "query": query,
205
+ "product": product_name,
206
+ "style": style_name,
207
+ "product_features": product_features,
208
+ "results": retrieved_docs[:n_results],
209
+ "retrieval_path": retrieval_path,
210
+ "explanation": "通过图结构找到跨品类的风格相关文案,即使产品不同,但风格相通,可以借鉴文案模板。"
211
+ }
212
+
213
+ class RAGEngine:
214
+ """RAG引擎主类"""
215
+ def __init__(self, graph_db: SimpleGraphDB, vector_db: VectorDB):
216
+ self.graph_db = graph_db
217
+ self.traditional_rag = TraditionalRAG(vector_db, graph_db)
218
+ self.graph_rag = GraphRAG(graph_db, vector_db)
219
+
220
+ def compare_retrieval(self, query: str, product_name: str = None, style_name: str = None) -> Dict:
221
+ """对比传统RAG和GraphRAG的检索结果"""
222
+ traditional_result = self.traditional_rag.retrieve(query, product_name, style_name)
223
+ graph_result = self.graph_rag.retrieve(query, product_name, style_name)
224
+
225
+ return {
226
+ "traditional_rag": traditional_result,
227
+ "graph_rag": graph_result,
228
+ "comparison": {
229
+ "traditional_count": len(traditional_result["results"]),
230
+ "graph_count": len(graph_result["results"]),
231
+ "graph_cross_category": len([r for r in graph_result["results"] if r.get("source") == "图遍历"])
232
+ }
233
+ }
234
+
235
+ def generate_copywriting(self, query: str, product_name: str, style_name: str, use_graph: bool = True) -> Dict:
236
+ """生成文案(使用LLM)"""
237
+ if use_graph:
238
+ retrieval_result = self.graph_rag.retrieve(query, product_name, style_name)
239
+ else:
240
+ retrieval_result = self.traditional_rag.retrieve(query, product_name, style_name)
241
+
242
+ # 获取检索到的参考文案
243
+ retrieved_texts = [r["content"] for r in retrieval_result["results"][:5]] # 取前5个作为参考
244
+
245
+ # 统计信息
246
+ cross_category_count = len([r for r in retrieval_result["results"] if r.get("source") == "图遍历"]) if use_graph else 0
247
+
248
+ # 获取产品特征(用于GraphRAG)
249
+ product_features = []
250
+ if use_graph and retrieval_result.get("product_features"):
251
+ product_features = retrieval_result["product_features"]
252
+
253
+ # 调用LLM生成文案
254
+ try:
255
+ llm_generated = self._call_llm_generate(
256
+ product_name=product_name,
257
+ style_name=style_name,
258
+ reference_texts=retrieved_texts,
259
+ product_features=product_features,
260
+ use_graph=use_graph,
261
+ cross_category_count=cross_category_count
262
+ )
263
+ except Exception as e:
264
+ print(f"LLM生成失败: {e}")
265
+ # 如果LLM失败,使用模板生成
266
+ llm_generated = self._generate_template(retrieved_texts, product_name, style_name)
267
+
268
+ # 组装最终输出
269
+ if use_graph and product_features:
270
+ features = ", ".join(product_features[:3])
271
+ reference_sources = ', '.join([r.get('product', '未知') for r in retrieval_result["results"][:3]])
272
+ generated_text = f"""基于图增强检索生成的文案:
273
+
274
+ ✨ 检索策略:通过图结构找到跨品类的风格相关文案
275
+ 📊 检索结果:找到 {len(retrieved_texts)} 个相关文案,其中 {cross_category_count} 个来自跨品类(通过风格节点关联)
276
+ 🎯 产品特征:{features}
277
+ 📝 参考文案来源:{reference_sources}
278
+
279
+ 【{style_name}风格】{product_name}文案:
280
+
281
+ {llm_generated}
282
+
283
+ 💡 说明:GraphRAG 通过风格节点找到了跨品类的参考文案(如香薰蜡烛的清冷避世风文案),即使产品不同,但风格相通,可以借鉴文案模板。"""
284
+ else:
285
+ generated_text = f"""基于传统语义检索生成的文案:
286
+
287
+ 🔍 检索策略:直接通过语义相似度搜索
288
+ 📊 检索结果:找到 {len(retrieved_texts)} 个语义相似的文案
289
+ ⚠️ 局限性:如果数据库中没有相似内容,可能返回不相关的结果
290
+
291
+ 【{style_name}风格】{product_name}文案:
292
+
293
+ {llm_generated}
294
+
295
+ 💡 说明:传统 RAG 只能找到语义相似的文案,如果数据库中没有该产品的该风格文案,可能无法生成合适的文案。"""
296
+
297
+ return {
298
+ "generated_text": generated_text,
299
+ "retrieval_result": retrieval_result,
300
+ "method": "GraphRAG" if use_graph else "Traditional RAG"
301
+ }
302
+
303
+ def _call_llm_generate(self, product_name: str, style_name: str, reference_texts: List[str],
304
+ product_features: List[str] = None, use_graph: bool = True,
305
+ cross_category_count: int = 0) -> str:
306
+ """调用LLM生成文案"""
307
+ headers = {
308
+ "Content-Type": "application/json",
309
+ "Authorization": f"Bearer {LLM_API_KEY}"
310
+ }
311
+ url = f"{LLM_API_BASE}/chat/completions"
312
+
313
+ # 构建参考文案说明
314
+ reference_context = ""
315
+ if reference_texts:
316
+ reference_context = "\n\n参考文案(用于学习风格和句式):\n"
317
+ for i, text in enumerate(reference_texts[:3], 1):
318
+ reference_context += f"{i}. {text}\n"
319
+ else:
320
+ reference_context = "\n\n⚠️ 注意:没有找到相关参考文案,请根据产品特征和风格要求创作。"
321
+
322
+ # 构建产品特征说明
323
+ features_context = ""
324
+ if product_features:
325
+ features_context = f"\n产品特征:{', '.join(product_features[:5])}"
326
+
327
+ # 构建prompt
328
+ if use_graph and cross_category_count > 0:
329
+ prompt = f"""你是一名擅长小红书文案写作的创意编辑。请根据以下信息,生成一篇适合在小红书发布的文案(200-300字,要求内容丰富、有细节感)。
330
+
331
+ 产品名称:{product_name}
332
+ 目标风格:{style_name}
333
+ {features_context}
334
+
335
+ {reference_context}
336
+
337
+ 重要提示:
338
+ 1. 这些参考文案来自其他产品(跨品类),但风格相同,请学习它们的句式、语气和情感表达方式
339
+ 2. 将参考文案的风格和句式应用到目标产品上
340
+ 3. 文案要有细节感、人情味,符合小红书用户的阅读习惯
341
+ 4. 保持{style_name}的风格特征
342
+ 5. 文案长度要求200-300字,要有丰富的内容和细节描述,可以包含使用场景、情感体验、产品特色等多个方面
343
+ 6. 请确保文案完整,不要被截断,以完整的句子结尾
344
+
345
+ 请直接输出文案内容,不要包含"好的"、"没问题"等前缀,也不要使用markdown格式。只输出文案正文,确保内容完整。"""
346
+ else:
347
+ prompt = f"""你是一名擅长小红书文案写作的创意编辑。请根据以下信息,生成一篇适合在小红书发布的文案(200-300字,要求内容丰富、有细节感)。
348
+
349
+ 产品名称:{product_name}
350
+ 目标风格:{style_name}
351
+ {features_context}
352
+
353
+ {reference_context}
354
+
355
+ 重要提示:
356
+ 1. 参考文案可能有限或不够相关,请根据产品特征和风格要求创作
357
+ 2. 文案要有细节感、人情味,符合小红书用户的阅读习惯
358
+ 3. 保持{style_name}的风格特征
359
+ 4. 文案长度要求200-300字,要有丰富的内容和细节描述,可以包含使用场景、情感体验、产品特色等多个方面
360
+ 5. 请确保文案完整,不要被截断,以完整的句子结尾
361
+
362
+ 请直接输出文案内容,不要包含"好的"、"没问题"等前缀,也不要使用markdown格式。只输出文案正文,确保内容完整。"""
363
+
364
+ body = {
365
+ "model": LLM_MODEL,
366
+ "messages": [
367
+ {
368
+ "role": "system",
369
+ "content": "你是一名擅长文案写作的创意编辑,擅长创作小红书风格的文案。"
370
+ },
371
+ {
372
+ "role": "user",
373
+ "content": prompt
374
+ }
375
+ ],
376
+ "max_tokens": 4000, # 增加token限制以支持更长的文案(200-300字约需要800-1200 tokens,设置4000确保完整输出)
377
+ "temperature": 0.9
378
+ }
379
+
380
+ resp = requests.post(url, headers=headers, json=body, timeout=60)
381
+ resp.raise_for_status()
382
+ data = resp.json()
383
+ generated = data["choices"][0]["message"]["content"].strip()
384
+
385
+ # 清理生成的内容
386
+ # 移除常见的前缀(只移除开头的前缀,不要截断内容)
387
+ prefixes_to_remove = [
388
+ "好的,没问题!",
389
+ "好的,",
390
+ "没问题!",
391
+ "好的!",
392
+ ]
393
+ for prefix in prefixes_to_remove:
394
+ if generated.startswith(prefix):
395
+ generated = generated[len(prefix):].strip()
396
+
397
+ # 移除markdown格式符号(但保留内容)
398
+ generated = generated.replace("**", "").replace("*", "").strip()
399
+
400
+ return generated
401
+
402
+ def _generate_template(self, reference_texts: List[str], product_name: str, style_name: str) -> str:
403
+ """生成文案模板(简化版,实际应调用LLM)"""
404
+ # 如果有参考文案,提取关键句式
405
+ key_phrases = []
406
+ if reference_texts:
407
+ for text in reference_texts[:2]: # 只取前2个参考
408
+ # 提取关键句式(简单提取)
409
+ if "避难所" in text:
410
+ key_phrases.append("避难所")
411
+ if "安静" in text:
412
+ key_phrases.append("安静")
413
+ if "唯一" in text:
414
+ key_phrases.append("唯一")
415
+ if "���绝子" in text:
416
+ key_phrases.append("绝绝子")
417
+
418
+ # 根据风格和产品生成
419
+ if "清冷避世风" in style_name or "深夜emo" in style_name.lower():
420
+ if "眼罩" in product_name:
421
+ if key_phrases:
422
+ # GraphRAG:使用参考文案的句式
423
+ return f"戴上眼罩的这片刻漆黑,是我在繁杂城市里唯一的{'避难所' if '避难所' in key_phrases else '避风港'}。物理意义上的关灯,也是心理上的断联。世界终于{'安静了' if '安静' in key_phrases else '静下来了'},今晚只属于我自己。"
424
+ else:
425
+ # 传统RAG:没有参考,使用通用模板
426
+ return f"这个{product_name}真的很不错,遮光效果好,推荐给大家使用。"
427
+ elif "CCD" in product_name or "相机" in product_name:
428
+ return "深夜拿起它,在颗粒感的画面里,所有的情绪都有了出口。低像素不是缺陷,是另一种真实。"
429
+ else:
430
+ if key_phrases:
431
+ return f"每一个与{product_name}的瞬间,都是我与世界的{'唯一连接' if '唯一' in key_phrases else '连接'}。"
432
+ else:
433
+ return f"这个{product_name}真的很不错,推荐给大家。"
434
+ elif "疯狂种草" in style_name:
435
+ if key_phrases and "绝绝子" in key_phrases:
436
+ # GraphRAG:使用参考文案的语气
437
+ return f"家人们谁懂啊!这个{product_name}真的绝绝子,一秒沦陷!必须人手一个!"
438
+ else:
439
+ # 传统RAG:没有参考,使用通用语气
440
+ return f"这个{product_name}真的很不错,推荐给大家购买!"
441
+ else:
442
+ if key_phrases:
443
+ return f"这个{product_name}真的很不错,{'强烈推荐' if '绝绝子' in key_phrases else '推荐'}给大家!"
444
+ else:
445
+ return f"这个{product_name}真的很不错,推荐给大家!"
446
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi>=0.104.0
2
+ uvicorn[standard]>=0.24.0
3
+ pydantic>=2.0.0
4
+ requests>=2.31.0
5
+ python-multipart>=0.0.6
6
+ chromadb>=0.4.22
7
+ numpy>=1.24.0
8
+ openai>=1.0.0