ContextGem

ContextGem:轻松从文档中提取 LLM

测试 覆盖范围 文档 文档 执照 PyPI Python 版本 代码安全 代码样式:黑色 进口:isort Pydantic v2 诗 预先提交 贡献者契约 深度维基

 

ContextGem 是一个免费的开源 LLM 框架,它可以让您以最少的代码更轻松地从文档中提取结构化数据和见解。

💎 为什么选择 ContextGem?

大多数流行的 LLM 框架用于从文档中提取结构化数据,即使是提取基本信息,也需要大量的样板代码。这大大增加了开发时间和复杂性。

ContextGem 通过提供灵活直观的框架来应对这一挑战,该框架能够以最小的投入从文档中提取结构化数据和洞察。复杂且耗时的部分由强大的抽象功能处理,从而消除了样板代码并降低了开发成本。

⭐ 主要特点

内置抽象 ContextGem 其他 LLM 框架*
自动动态提示 🟢
自动化数据建模和验证器 🟢
精确的粒度参考映射(段落和句子) 🟢
理由(提取背后的推理) 🟢
神经分割(SaT) 🟢
多语言支持(无提示输入/输出) 🟢
单一、统一的提取管道(声明式、可重用、完全可序列化) 🟢 🟡
分组法学硕士课程,包含特定角色的任务 🟢 🟡
嵌套上下文提取 🟢 🟡
统一的、完全可序列化的结果存储模型(文档) 🟢 🟡
提取任务校准示例 🟢 🟡
内置并发 I/O 处理 🟢 🟡
自动使用和成本跟踪 🟢 🟡
回退和重试逻辑 🟢 🟢
多家 LLM 提供商 🟢 🟢

🟢 - 完全支持 - 无需额外设置
🟡 - 部分支持 - 需要额外设置
◯ - 不支持 - 需要自定义逻辑

* 查看ContextGem 抽象的描述以及使用 ContextGem 和其他流行的开源 LLM 框架的具体实现示例的比较。

💡 使用最少的代码,您可以:

  • 从文档(文本、图像)中提取结构化数据
  • 识别并分析文档中的关键方面(主题、主题、类别)
  • 从文档中提取特定概念(实体、事实、结论、评估)
  • 通过简单、直观的 API构建复杂的提取工作流程
  • 创建多级提取管道(包含概念的方面、分层方面)

 

ContextGem 提取示例

📦安装

pip install -U contextgem
 

🚀 快速入门

# Quick Start Example - Extracting anomalies from a document, with source references and justifications

import os

from contextgem import Document, DocumentLLM, StringConcept

# Sample document text (shortened for brevity)
doc = Document(
    raw_text=(
        "Consultancy Agreement\n"
        "This agreement between Company A (Supplier) and Company B (Customer)...\n"
        "The term of the agreement is 1 year from the Effective Date...\n"
        "The Supplier shall provide consultancy services as described in Annex 2...\n"
        "The Customer shall pay the Supplier within 30 calendar days of receiving an invoice...\n"
        "The purple elephant danced gracefully on the moon while eating ice cream.\n"  # 💎 anomaly
        "This agreement is governed by the laws of Norway...\n"
    ),
)

# Attach a document-level concept
doc.concepts = [
    StringConcept(
        name="Anomalies",  # in longer contexts, this concept is hard to capture with RAG
        description="Anomalies in the document",
        add_references=True,
        reference_depth="sentences",
        add_justifications=True,
        justification_depth="brief",
        # see the docs for more configuration options
    )
    # add more concepts to the document, if needed
    # see the docs for available concepts: StringConcept, JsonObjectConcept, etc.
]
# Or use `doc.add_concepts([...])`

# Define an LLM for extracting information from the document
llm = DocumentLLM(
    model="openai/gpt-4o-mini",  # or another provider/LLM
    api_key=os.environ.get(
        "CONTEXTGEM_OPENAI_API_KEY"
    ),  # your API key for the LLM provider
    # see the docs for more configuration options
)

# Extract information from the document
doc = llm.extract_all(doc)  # or use async version `await llm.extract_all_async(doc)`

# Access extracted information in the document object
print(
    doc.concepts[0].extracted_items
)  # extracted items with references & justifications
# or `doc.get_concept_by_name("Anomalies").extracted_items`
 

在 Colab 中打开


请参阅文档中的更多示例:

基本使用示例

高级用法示例

🔄 文档转换器

要创建用于 LLM 分析的 ContextGem 文档,您可以直接传递原始文本,也可以使用处理各种文件格式的内置转换器。

📄 DOCX 转换器

ContextGem 提供内置转换器,可轻松将 DOCX 文件转换为 LLM 就绪数据。

  • 提取其他开源工具通常无法捕获的信息:未对齐的表格、注释、脚注、文本框、页眉/页脚和嵌入图像
  • 保留具有丰富元数据的文档结构,以改进 LLM 分析
# Using ContextGem's DocxConverter

from contextgem import DocxConverter

converter = DocxConverter()

# Convert a DOCX file to an LLM-ready ContextGem Document
# from path
document = converter.convert("path/to/document.docx")
# or from file object
with open("path/to/document.docx", "rb") as docx_file_object:
    document = converter.convert(docx_file_object)

# You can also use it as a standalone text extractor
docx_text = converter.convert_to_text_format(
    "path/to/document.docx",
    output_format="markdown",  # or "raw"
)
 

在文档中了解有关DOCX 转换器功能的更多信息。

🎯 重点文档分析

ContextGem 利用 LLM 的长上下文窗口,从单个文档中提取出卓越的准确率。与 RAG 方法(通常难以处理复杂概念和细微洞察)不同,ContextGem 充分利用了持续扩展的上下文容量、不断改进的 LLM 功能以及降低的成本。这种专注的方法能够直接从完整文档中提取信息,消除检索不一致,同时针对深入的单文档分析进行优化。虽然这可以提高单个文档的准确率,但 ContextGem 目前不支持跨文档查询或全语料库检索——对于这些用例,现代 RAG 系统(例如 LlamaIndex、Haystack)仍然更为合适。

🤖 支持

ContextGem 通过LiteLLM集成支持基于云和本地的 LLM :

  • 云端法学硕士:OpenAI、Anthropic、Google、Azure OpenAI 等
  • 本地 LLM:使用 Ollama、LM Studio 等提供商在本地运行模型。
  • 模型架构:适用于推理/CoT 功能(例如 o4-mini)和非推理模型(例如 gpt-4.1)
  • 简单的 API:所有 LLM 的统一接口,可轻松切换提供商

在文档中了解有关支持的 LLM 提供程序和模型以及如何配置 LLM 的更多信息。

⚡ 优化

ContextGem 文档提供了有关优化策略的指导,以最大限度地提高性能、最大限度地降低成本并提高提取准确性:

💾 序列化结果

ContextGem 允许您使用内置序列化方法保存和加载 Document 对象、管道和 LLM 配置:

  • 保存已处理的文档以避免重复昂贵的 LLM 调用
  • 在系统之间传输提取结果
  • 保留管道和 LLM 配置以供以后重用

在文档中了解有关序列化选项的更多信息。

📚 文档

完整文档可在contextgem.dev上找到。

完整文档的原始文本版本可在 处获取docs/docs-raw-for-llm.txt。此文件自动生成,包含所有文档,其格式已针对 LLM 导入进行了优化(例如,用于问答)。