b4f7574f
tangwang
feat(agent): add ...
|
1
|
# Redis 在线索引契约
|
12118125
tangwang
offline tasks: me...
|
2
|
|
b4f7574f
tangwang
feat(agent): add ...
|
3
|
本文描述 `scripts/load_index_to_redis.py` 当前实际写入的 Redis 契约。在线服务、诊断脚本和 Agent 工具均应以本文及加载器代码为准。
|
12118125
tangwang
offline tasks: me...
|
4
|
|
b4f7574f
tangwang
feat(agent): add ...
|
5
|
## 1. 权威约定
|
12118125
tangwang
offline tasks: me...
|
6
|
|
b4f7574f
tangwang
feat(agent): add ...
|
7
8
9
10
11
12
|
- 加载器取离线 TSV 的第一列作为 key 后缀,最后一列作为 value。
- value 原样写入 Redis,不会转换成 JSON。
- 当前标准 value 是按排名降序的文本:`item_id:score,item_id:score,...`。
- 所有索引的默认 TTL 都由 `--expire-days` 控制,当前默认值为 60 天。
- Redis 地址、端口、数据库和认证信息由部署配置决定,文档不固定数据库编号,也不保存凭据。
- 商品 ID 在接口中建议使用字符串,避免跨语言传递时的数值精度问题。
|
12118125
tangwang
offline tasks: me...
|
13
|
|
b4f7574f
tangwang
feat(agent): add ...
|
14
|
示例:
|
12118125
tangwang
offline tasks: me...
|
15
|
|
b4f7574f
tangwang
feat(agent): add ...
|
16
17
18
|
```text
Key: item:similar:swing:3141390
Value: 3264320:1.2656,3264284:1.2656,3562128:0.4000
|
a1c26d3d
tangwang
add cpp swing for...
|
19
20
|
```
|
b4f7574f
tangwang
feat(agent): add ...
|
21
|
value 不是下面这种 JSON:
|
a1c26d3d
tangwang
add cpp swing for...
|
22
|
|
a1c26d3d
tangwang
add cpp swing for...
|
23
|
```json
|
b4f7574f
tangwang
feat(agent): add ...
|
24
|
[[3264320, 1.2656], [3264284, 1.2656]]
|
a1c26d3d
tangwang
add cpp swing for...
|
25
26
|
```
|
b4f7574f
tangwang
feat(agent): add ...
|
27
|
历史环境可能仍保留 JSON pair 数组、JSON ID 数组或旧的 `item:similar:w2v:*` key。新代码不应继续写这些格式;`agent_tools` 的读取器仅为平滑迁移而兼容它们。
|
a1c26d3d
tangwang
add cpp swing for...
|
28
|
|
b4f7574f
tangwang
feat(agent): add ...
|
29
|
## 2. I2I 索引
|
a1c26d3d
tangwang
add cpp swing for...
|
30
|
|
b4f7574f
tangwang
feat(agent): add ...
|
31
32
33
34
35
36
37
38
39
|
| 离线模块 | 输入文件 | Redis key | value |
| --- | --- | --- | --- |
| C++ Swing | `collaboration/output/swing_similar.txt` | `item:similar:swing_cpp:{item_id}` | `item_id:score,...` |
| Python Swing | `output/i2i_swing_YYYYMMDD.txt` | `item:similar:swing:{item_id}` | `item_id:score,...` |
| Session W2V | `output/i2i_session_w2v_YYYYMMDD.txt` | `item:similar:session_w2v:{item_id}` | `item_id:score,...` |
| DeepWalk | `output/i2i_deepwalk_YYYYMMDD.txt` | `item:similar:deepwalk:{item_id}` | `item_id:score,...` |
| 名称内容相似 | `output/i2i_content_name_YYYYMMDD.txt` | `item:similar:content_name:{item_id}` | `item_id:score,...` |
| 图片内容相似 | `output/i2i_content_pic_YYYYMMDD.txt` | `item:similar:content_pic:{item_id}` | `item_id:score,...` |
| 行为相似 | `output/i2i_item_behavior_YYYYMMDD.txt` | `item:similar:item_behavior:{item_id}` | `item_id:score,...` |
|
a1c26d3d
tangwang
add cpp swing for...
|
40
|
|
b4f7574f
tangwang
feat(agent): add ...
|
41
|
`item:similar:w2v:{item_id}` 是历史别名,不是当前加载器的写入目标。迁移期间可以读它作为 `session_w2v` 的后备 key,但新生产者和新调用方必须使用 `item:similar:session_w2v:{item_id}`。
|
12118125
tangwang
offline tasks: me...
|
42
|
|
b4f7574f
tangwang
feat(agent): add ...
|
43
|
各算法原始 score 的量纲不同,例如 Swing 原始分数、向量分数和行为聚合分数不能直接相加。跨算法融合应使用排名融合或先逐路校准;Agent 工具默认使用加权 RRF。
|
12118125
tangwang
offline tasks: me...
|
44
|
|
b4f7574f
tangwang
feat(agent): add ...
|
45
|
## 3. 兴趣聚合索引
|
12118125
tangwang
offline tasks: me...
|
46
|
|
b4f7574f
tangwang
feat(agent): add ...
|
47
|
完整 key 格式为:
|
12118125
tangwang
offline tasks: me...
|
48
|
|
b4f7574f
tangwang
feat(agent): add ...
|
49
50
|
```text
interest:{list_type}:{dimension_key}
|
12118125
tangwang
offline tasks: me...
|
51
52
|
```
|
b4f7574f
tangwang
feat(agent): add ...
|
53
|
`list_type` 有四种:
|
12118125
tangwang
offline tasks: me...
|
54
|
|
b4f7574f
tangwang
feat(agent): add ...
|
55
56
57
58
59
60
|
| 类型 | 数据范围 |
| --- | --- |
| `hot` | 最近窗口内的多行为加权结果 |
| `cart` | 加购、入池行为结果 |
| `new` | 最近上架商品结果 |
| `global` | 完整回看窗口内的多行为加权结果 |
|
a1c26d3d
tangwang
add cpp swing for...
|
61
|
|
b4f7574f
tangwang
feat(agent): add ...
|
62
|
`global` 是列表类型,不是自动生成的 dimension。因此现有生产器会生成 `interest:global:platform:essaone` 等 key,不会生成 `interest:global:global`。若业务需要真正的无条件全局兜底,必须在离线侧新增 `global` dimension 后再建立该契约。
|
12118125
tangwang
offline tasks: me...
|
63
|
|
b4f7574f
tangwang
feat(agent): add ...
|
64
|
单维度:
|
a1c26d3d
tangwang
add cpp swing for...
|
65
|
|
b4f7574f
tangwang
feat(agent): add ...
|
66
67
68
69
70
71
72
73
|
```text
platform:{platform}
client_platform:{client_platform}
supplier:{supplier_id}
category_level1:{category_id}
category_level2:{category_id}
category_level3:{category_id}
category_level4:{category_id}
|
12118125
tangwang
offline tasks: me...
|
74
75
|
```
|
b4f7574f
tangwang
feat(agent): add ...
|
76
|
组合维度:
|
12118125
tangwang
offline tasks: me...
|
77
|
|
b4f7574f
tangwang
feat(agent): add ...
|
78
79
80
81
82
83
84
|
```text
platform_client:{platform}_{client_platform}
platform_category2:{platform}_{category_level2_id}
platform_category3:{platform}_{category_level3_id}
client_category2:{client_platform}_{category_level2_id}
platform:{platform};country_code:{country_code}
platform:{platform};country_code:{country_code};customer_type:{customer_type}
|
12118125
tangwang
offline tasks: me...
|
85
|
```
|
12118125
tangwang
offline tasks: me...
|
86
|
|
b4f7574f
tangwang
feat(agent): add ...
|
87
|
国家及客户类型组合当前只为 `essa`、`essaone` 生成。具体数据来源和语义限制见 [新维度说明.md](./新维度说明.md)。
|
12118125
tangwang
offline tasks: me...
|
88
|
|
b4f7574f
tangwang
feat(agent): add ...
|
89
|
兴趣索引同样保留 score:
|
12118125
tangwang
offline tasks: me...
|
90
|
|
b4f7574f
tangwang
feat(agent): add ...
|
91
92
93
|
```text
Key: interest:hot:platform_category2:essaone_29
Value: 3482467:156.2000,2708138:143.0000,1008982:132.5000
|
12118125
tangwang
offline tasks: me...
|
94
95
|
```
|
b4f7574f
tangwang
feat(agent): add ...
|
96
|
## 4. 标准解析
|
12118125
tangwang
offline tasks: me...
|
97
|
|
b4f7574f
tangwang
feat(agent): add ...
|
98
|
仅消费当前契约时可使用:
|
a1c26d3d
tangwang
add cpp swing for...
|
99
100
|
```python
|
b4f7574f
tangwang
feat(agent): add ...
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
def parse_ranked_value(value):
if not value:
return []
if isinstance(value, bytes):
value = value.decode('utf-8')
result = []
for token in value.split(','):
token = token.strip()
if not token or ':' not in token:
continue
item_id, score = token.rsplit(':', 1)
try:
result.append((item_id, float(score)))
except ValueError:
continue
return result
```
需要兼容历史 JSON 格式时,统一调用:
|
a1c26d3d
tangwang
add cpp swing for...
|
121
|
|
12118125
tangwang
offline tasks: me...
|
122
|
```python
|
b4f7574f
tangwang
feat(agent): add ...
|
123
|
from agent_tools.parsing import parse_ranked_items
|
12118125
tangwang
offline tasks: me...
|
124
|
|
b4f7574f
tangwang
feat(agent): add ...
|
125
|
items = parse_ranked_items(redis_client.get(key))
|
12118125
tangwang
offline tasks: me...
|
126
127
|
```
|
b4f7574f
tangwang
feat(agent): add ...
|
128
|
不要在每个在线召回分支各写一套解析逻辑。
|
12118125
tangwang
offline tasks: me...
|
129
|
|
b4f7574f
tangwang
feat(agent): add ...
|
130
|
## 5. 加载与验证
|
12118125
tangwang
offline tasks: me...
|
131
|
|
b4f7574f
tangwang
feat(agent): add ...
|
132
|
加载指定日期的全部索引:
|
12118125
tangwang
offline tasks: me...
|
133
|
|
12118125
tangwang
offline tasks: me...
|
134
|
```bash
|
b4f7574f
tangwang
feat(agent): add ...
|
135
136
137
|
cd offline_tasks
python3 scripts/load_index_to_redis.py --date YYYYMMDD
```
|
12118125
tangwang
offline tasks: me...
|
138
|
|
b4f7574f
tangwang
feat(agent): add ...
|
139
|
该命令会连接外部服务并写 Redis。执行前必须确认 `config/offline_config.py` 或显式参数中的目标地址、数据库、TTL 和日期。不要为了加载新数据执行 `--flush-db`,除非已确认整个目标数据库都可删除。
|
12118125
tangwang
offline tasks: me...
|
140
|
|
b4f7574f
tangwang
feat(agent): add ...
|
141
|
在线抽样:
|
12118125
tangwang
offline tasks: me...
|
142
|
|
b4f7574f
tangwang
feat(agent): add ...
|
143
144
145
146
147
148
|
```bash
redis-cli --scan --pattern 'item:similar:swing:*' | head
redis-cli --scan --pattern 'interest:hot:platform:*' | head
redis-cli GET 'item:similar:swing:3141390'
redis-cli TTL 'item:similar:swing:3141390'
```
|
801fb682
tangwang
add cpp swing for...
|
149
|
|
b4f7574f
tangwang
feat(agent): add ...
|
150
|
认证信息通过 `REDISCLI_AUTH` 或安全的凭据注入方式传递,不要写入命令历史、脚本或文档。
|
801fb682
tangwang
add cpp swing for...
|
151
|
|
b4f7574f
tangwang
feat(agent): add ...
|
152
|
建议按索引族记录以下指标,而不是依赖文档中的静态数量:
|
12118125
tangwang
offline tasks: me...
|
153
|
|
b4f7574f
tangwang
feat(agent): add ...
|
154
155
156
157
158
|
- 生成文件行数、合法行比例和空 value 比例;
- Redis 写入成功数、跳过数和 TTL;
- key 覆盖商品数、每个 key 的候选数量分布;
- 抽样解析成功率和候选商品实时可售率;
- 在线每路 hit rate、召回候选数和延迟。
|
12118125
tangwang
offline tasks: me...
|
159
|
|
b4f7574f
tangwang
feat(agent): add ...
|
160
|
## 6. 变更规则
|
12118125
tangwang
offline tasks: me...
|
161
|
|
b4f7574f
tangwang
feat(agent): add ...
|
162
|
以下任一变化都属于在线契约变更,必须同步修改加载器、读取器、测试和本文:
|
12118125
tangwang
offline tasks: me...
|
163
|
|
b4f7574f
tangwang
feat(agent): add ...
|
164
165
166
167
168
|
- key 前缀或算法名;
- value 序列化格式;
- dimension key 的拼接方式;
- score 含义或排序方向;
- TTL、数据库或双写/迁移策略。
|
12118125
tangwang
offline tasks: me...
|
169
|
|
b4f7574f
tangwang
feat(agent): add ...
|
170
|
更新日期:2026-08-23。
|