CogmAIT LangExtract
基于 LangExtract 的知识图谱信息抽取库,默认通过 LiteLLM 调用模型,支持从非结构化文本中提取结构化实体和关系。
特性
- 🎯 多模式抽取:支持实体抽取、关系抽取、图谱一键抽取
- 🔌 适配器模式:可插拔的抽取后端,当前支持 LangExtract
- 📦 异步支持:原生异步 API,支持高并发场景
- 🧩 分块处理:自动处理长文本,支持并行抽取
- 🎨 可扩展:支持自定义 Adapter、自定义 Prompt 模板
安装
# 基础安装
pip install cogmait-langextract
# 完整功能(含 Langfuse 集成)
pip install cogmait-langextract[full]
# 开发安装
pip install cogmait-langextract[dev]
快速开始
import asyncio
from cogmait_langextract import (
GraphExtraction,
LangextractConfig,
)
async def main():
# 1. 配置
config = LangextractConfig(
model_name="qwen-long",
api_key="litellm-api-key",
api_base="http://localhost:4000",
)
# 2. 创建抽取器
extractor = GraphExtraction(
node_config={"config": config},
edge_config={"config": config},
)
# 3. 执行抽取
result = await extractor.extract_graph(
prompt="从文本中提取知识图谱",
schema={
"nodes": """
## 公司
### 描述
公司是指依法设立的,以营利为目的从事商业经营活动的企业法人。
### 属性
- 经营范围:公司依法被允许从事的经营项目描述。
## 品牌
### 描述
品牌是指用于识别特定企业、产品或服务的名称、术语、符号、设计或其组合,旨在与竞争对手形成区分。
### 属性
- 品牌名称:品牌的官方全称,如“小米”。
""",
"edges": """
## 隶属
### 描述
表示品牌与公司之间的所属关系,即某一品牌归属于特定公司实体所有。
### 关系三元组
- 品牌-隶属-公司
"""
},
input_text="华为技术有限公司,主要从事信息与通信技术(ICT)解决方案的研究、开发、销售与服务,其旗下品牌为华为。",
)
# 4. 处理结果
for node in result["nodes"]:
print(f"节点:{node.node_name} ({node.node_type})")
for edge in result["edges"]:
print(f"关系:{edge.source_id} -{edge.relation_type}-> {edge.target_id}")
asyncio.run(main())
核心组件
1.5.0 粗细溯源行为变化
trace_flag=False(也是图抽取默认值)现在返回分块来源,不再表示关闭来源信息;
它跳过基础和模糊对齐。trace_flag=True 额外执行原文对齐,并兼容旧位置字段。
两个模式均返回 text_classes,节点和关系均带 source_entries。
条目的 text 是未经核验的模型摘录;位置可空,不能据此伪造精确原文。
这是公开行为变化,旧调用方升级前需阅读粗细溯源迁移指南。
| 组件 | 说明 |
|---|---|
GraphExtraction |
图谱抽取编排器,提供 extract_graph、extract_entities、extract_relations 方法 |
InformationExtractionFactory |
适配器工厂,用于创建和管理抽取适配器 |
Entity / Relationship |
数据模型 |
SourceEntry / TextClass |
模型摘录、分块关联与可选位置 |
GraphNode / GraphEdge |
图谱模型 |
LangextractConfig |
LangExtract 配置类 |
模型输出容错
JSON 输出允许完整的 json 或无语言名代码围栏,以及外部尾标记 <|COMPLETE|>。
字符串内部的同名标记、围栏和公式保留。解析前检查唯一响应边界,多个顶层 JSON、
未知前后缀、未闭合容器和无法确认的引号边界会失败,不截取第一段冒充完整结果。
已知格式异常优先用标准 JSON 解码;字符串内原始控制字符使用标准库非严格解码,
其他可修复语法错误沿用 json_repair,不增加模型纠错调用。YAML 行为不变。
extractions 中的非对象项记录警告后过滤,剩余对象继续原字段校验、转换和建图。
原数组非空却全部被过滤时抛出 ResolverParsingError;正常空数组保持原行为。
警告包含原始、保留和丢弃数量,以及前 5 个异常项的索引、类型和每项最多 160 字符摘要。
这是有损容错:即使 100 项中丢弃 90 项,只要剩余项通过后续流程仍可返回结果, 不新增过滤比例阈值、返回字段或界面提示。处理成功不代表抽取完整或事实正确。 排查结果偏少时应同时检查过滤警告;后续属性校验或建图失败仍按原规则失败。
文档
入门指南
| 文档 | 说明 |
|---|---|
| 安装指南 | 安装方法和环境配置 |
| 快速开始 | 5 分钟上手指南 |
| 核心概念 | 架构设计和核心概念 |
进阶文档
| 文档 | 说明 |
|---|---|
| 使用指南 | 高级配置、Langfuse 集成、自定义 Adapter |
| [API 参考](docs/API 参考.md) | 完整 API 接口文档 |
| 示例代码 | 6 个实用示例代码 |
| [v2 仓库迁移指南](v2 仓库迁移指南.md) | 从旧版迁移到新包的指南 |
| [CI/CD 实施指南](CI-CD 实施指南.md) | GitLab CI/CD 配置指南 |
License
Apache 2.0
Metadata
Release files for cogmait-langextract 1.5.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cogmait_langextract-1.5.3.tar.gz | 82.4 kB | Without provenance | Details for cogmait_langextract-1.5.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cogmait_langextract-1.5.3-py3-none-any.whl | Python 3 | none | any | Details for cogmait_langextract-1.5.3-py3-none-any.whl |
Total release size: 178.6 kB
Release files / cogmait_langextract-1.5.3.tar.gz
| Download URL | cogmait_langextract-1.5.3.tar.gz |
|---|---|
| Size | 82.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38adab4a6d81654d2c933bb880e2871a857e89ff61be18f8b0d74e05cd9effa5
|
|
BLAKE2b-256 checksum How to use checksums |
cb132ba0ee593e5d05845374234409d586b59380a5201f6604f4e1a7833d5c6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.11
|
Release files / cogmait_langextract-1.5.3-py3-none-any.whl
| Download URL | cogmait_langextract-1.5.3-py3-none-any.whl |
|---|---|
| Size | 96.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b9ca9e720912627aedf5a68cf028b4129ca46c267133f036a6049e09096b69b
|
|
BLAKE2b-256 checksum How to use checksums |
5195b5569bbb283515c92f461c42f3a42ff6228d769fd0647a8be29c4d0eb31c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.11
|
Security for cogmait-langextract 1.5.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.