Skip to main content

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 配置类

文档

入门指南

文档 说明
安装指南 安装方法和环境配置
快速开始 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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cogmait-langextract 1.5.1
File Size Uploaded Provenance Actions
cogmait_langextract-1.5.1.tar.gz 79.3 kB Without provenance Details for cogmait_langextract-1.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cogmait-langextract 1.5.1
File Interpreter ABI Platform Actions
cogmait_langextract-1.5.1-py3-none-any.whl Python 3 none any Details for cogmait_langextract-1.5.1-py3-none-any.whl

Total release size: 173.2 kB

Release files / cogmait_langextract-1.5.1.tar.gz

Download URL cogmait_langextract-1.5.1.tar.gz
Size 79.3 kB
Tags Source
SHA-256 checksum
How to use checksums
29c873d0b2a5b4ed23966ad99e165818b7d499a7fc7505c0209ec99f078379d6
BLAKE2b-256 checksum
How to use checksums
0ac4e34e69ef5b5f154884f572472f73c50a3f4d4209fea0b1a492914e092462
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.1-py3-none-any.whl

Download URL cogmait_langextract-1.5.1-py3-none-any.whl
Size 93.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b47a8056213d30592c02f1b8b3d8ce4faad1fe220000b91b13399b30f5b5d8a
BLAKE2b-256 checksum
How to use checksums
0ac337244acdbac32d15254f99c9eb7edfdd05a5ef83a54468667d505646c63e
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.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page