知识图谱
LlamaIndex 数据结构。
KnowledgeGraphIndex #
Bases: BaseIndex[KG]
知识图谱索引。
通过提取三元组构建知识图谱,并在查询时利用知识图谱。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
kg_triplet_extract_template
|
BasePromptTemplate
|
用于提取三元组的 prompt。 |
无
|
max_triplets_per_chunk
|
int
|
要提取的最大三元组数量。 |
10
|
storage_context
|
Optional[StorageContext]
|
要使用的存储上下文。 |
无
|
graph_store
|
Optional[GraphStore]
|
要使用的图存储。 |
必需的 |
show_progress
|
bool
|
是否显示 tqdm 进度条。默认为 False。 |
False
|
include_embeddings
|
bool
|
是否在索引中包含嵌入。默认为 False。 |
False
|
max_object_length
|
int
|
三元组中对象的最大长度。默认为 128。 |
128
|
kg_triplet_extract_fn
|
Optional[Callable]
|
用于提取三元组的函数。默认为 None。 |
无
|
源代码位于 llama-index-core/llama_index/core/indices/knowledge_graph/base.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
|
upsert_triplet #
upsert_triplet(triplet: Tuple[str, str, str], include_embeddings: bool = False) -> None
插入三元组,并可选地插入嵌入。
用于手动插入知识图谱三元组(形式为 (subject, relationship, object))。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
triplet
|
tuple
|
知识三元组 |
必需的 |
embedding
|
Any
|
三元组的嵌入选项。默认为 None。 |
必需的 |
源代码位于 llama-index-core/llama_index/core/indices/knowledge_graph/base.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
add_node #
add_node(keywords: List[str], node: BaseNode) -> None
添加节点。
用于手动插入节点(以关键字为键)。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
keywords
|
List[str]
|
用于索引节点的关键字。 |
必需的 |
node
|
Node
|
要索引的节点。 |
必需的 |
源代码位于 llama-index-core/llama_index/core/indices/knowledge_graph/base.py
277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
upsert_triplet_and_node #
upsert_triplet_and_node(triplet: Tuple[str, str, str], node: BaseNode, include_embeddings: bool = False) -> None
Upsert 知识图谱三元组和节点。
同时调用 upsert_triplet 和 add_node。行为是幂等的;如果节点已存在,则只添加三元组。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
keywords
|
List[str]
|
用于索引节点的关键字。 |
必需的 |
node
|
Node
|
要索引的节点。 |
必需的 |
include_embeddings
|
bool
|
添加三元组嵌入的选项。默认为 False |
False
|
源代码位于 llama-index-core/llama_index/core/indices/knowledge_graph/base.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
get_networkx_graph #
get_networkx_graph(limit: int = 100) -> Any
获取图结构的 networkx 表示。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
limit
|
int
|
图中包含的起始节点数量。 |
100
|
注意:此函数需要安装 networkx。注意:这是一个 Beta 功能。
源代码位于 llama-index-core/llama_index/core/indices/knowledge_graph/base.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|