Metadata-Version: 2.4
Name: cogmait-langextract
Version: 1.3.1
Summary: Knowledge graph information extraction library based on LangExtract
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: information-extraction,knowledge-graph,nlp,llm,langextract,entity-extraction,relationship-extraction
Author: CogmAIT Team
Requires-Python: >=3.11,<3.13
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Text Processing :: Linguistic
Provides-Extra: dev
Provides-Extra: full
Provides-Extra: langfuse
Requires-Dist: coverage[toml] (>=7.0) ; extra == "dev"
Requires-Dist: coverage[toml] (>=7.0) ; extra == "full"
Requires-Dist: httpx (>=0.25)
Requires-Dist: json-repair (>=0.30)
Requires-Dist: langfuse (>=2.0) ; extra == "full"
Requires-Dist: langfuse (>=2.0) ; extra == "langfuse"
Requires-Dist: litellm (>=1.81,<1.82)
Requires-Dist: loguru (>=0.7)
Requires-Dist: more-itertools (>=9.0)
Requires-Dist: mypy (>=1.10) ; extra == "dev"
Requires-Dist: mypy (>=1.10) ; extra == "full"
Requires-Dist: openai (>=1.0)
Requires-Dist: pandas (>=2.0)
Requires-Dist: pydantic (>=2.0)
Requires-Dist: pytest (>=8.0) ; extra == "dev"
Requires-Dist: pytest (>=8.0) ; extra == "full"
Requires-Dist: pytest-asyncio (>=0.23) ; extra == "dev"
Requires-Dist: pytest-asyncio (>=0.23) ; extra == "full"
Requires-Dist: pytest-cov (>=4.0) ; extra == "dev"
Requires-Dist: pytest-cov (>=4.0) ; extra == "full"
Requires-Dist: python-dotenv (>=1.0)
Requires-Dist: pyyaml (>=6.0)
Requires-Dist: requests (>=2.31)
Requires-Dist: ruff (>=0.5) ; extra == "dev"
Requires-Dist: ruff (>=0.5) ; extra == "full"
Requires-Dist: tqdm (>=4.66)
Requires-Dist: typing-extensions (>=4.9)
Project-URL: Documentation, https://cogmait-langextract.readthedocs.io
Project-URL: Homepage, https://github.com/cogmait/cogmait-langextract
Project-URL: Issues, https://github.com/cogmait/cogmait-langextract/issues
Project-URL: Repository, https://github.com/cogmait/cogmait-langextract
Description-Content-Type: text/markdown

# CogmAIT LangExtract

基于 LangExtract 的知识图谱信息抽取库，默认通过 LiteLLM 调用模型，支持从非结构化文本中提取结构化实体和关系。

## 特性

- 🎯 **多模式抽取**：支持实体抽取、关系抽取、图谱一键抽取
- 🔌 **适配器模式**：可插拔的抽取后端，当前支持 LangExtract
- 📦 **异步支持**：原生异步 API，支持高并发场景
- 🧩 **分块处理**：自动处理长文本，支持并行抽取
- 🎨 **可扩展**：支持自定义 Adapter、自定义 Prompt 模板

## 安装

```bash
# 基础安装
pip install cogmait-langextract

# 完整功能（含 Langfuse 集成）
pip install cogmait-langextract[full]

# 开发安装
pip install cogmait-langextract[dev]
```

## 快速开始

```python
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())
```

## 核心组件

| 组件 | 说明 |
|------|------|
| `GraphExtraction` | 图谱抽取编排器，提供 `extract_graph`、`extract_entities`、`extract_relations` 方法 |
| `InformationExtractionFactory` | 适配器工厂，用于创建和管理抽取适配器 |
| `Entity` / `Relationship` | 数据模型 |
| `GraphNode` / `GraphEdge` | 图谱模型 |
| `LangextractConfig` | LangExtract 配置类 |

## 文档

### 入门指南

| 文档 | 说明 |
|------|------|
| [安装指南](docs/安装指南.md) | 安装方法和环境配置 |
| [快速开始](docs/快速开始.md) | 5 分钟上手指南 |
| [核心概念](docs/核心概念.md) | 架构设计和核心概念 |

### 进阶文档

| 文档 | 说明 |
|------|------|
| [使用指南](docs/使用指南.md) | 高级配置、Langfuse 集成、自定义 Adapter |
| [API 参考](docs/API 参考.md) | 完整 API 接口文档 |
| [示例代码](examples/) | 6 个实用示例代码 |
| [v2 仓库迁移指南](v2 仓库迁移指南.md) | 从旧版迁移到新包的指南 |
| [CI/CD 实施指南](CI-CD 实施指南.md) | GitLab CI/CD 配置指南 |

## License

Apache 2.0

