ContextGem

repository·main·Indexed 23 days ago

https://github.com/shcherbak-ai/contextgem

An open-source LLM framework for structured data extraction from documents. It abstracts prompt engineering and validation to provide granular references and justifications for extracted insights. ContextGem supports a variety of concept types—including StringConcept, JsonObjectConcept, BooleanConcept, NumericalConcept, RatingConcept, DateConcept, and LabelConcept—and integrates with cloud and local LLMs via LiteLLM. It allows for the definition of hierarchical Aspects and reusable ExtractionPipelines for consistent analysis across multiple documents.

Tokens
52.1K
Snippets
80
Records
233
Agent score
81%

What's inside contextgem

  1. Overview of ContextGem capabilities

    main

    ContextGem is an open-source LLM framework designed for effortless structured data extraction from documents with minimal code. It provides several core capabilities:

    • Aspect Extraction: Identify and extract specific document sections such as clauses, chapters, or terms.
    • Concept Extraction: Infer structured data like JSON objects, strings, numbers, dates, booleans, ratings, and labels.
    • Extraction Pipelines: Create reusable pipelines that combine aspects and concepts for consistent analysis across multiple files.
    • LLM Integration: Support for various cloud LLM providers and local models.
    • Serialization: Ability to save and load ContextGem objects for storage or transfer.
  2. Review ContextGem project licenses

    main
    The ContextGem project is licensed under Apache-2.0. A license analysis report is available which details the licenses of all production dependencies. Most dependencies use permissive licenses (MIT, BSD-3-Clause, Apache-2.0), but there are specific warnings for packages using weak-copyleft licenses or unknown licenses.
  3. What is LabelConcept and how does it work?

    main

    LabelConcept is a classification concept type used to categorize documents or content into predefined categories. It supports two distinct modes of operation:

    1. Multi-class classification (classification_type="multi_class"):

      • Selects exactly one label from the predefined set.
      • Labels are mutually exclusive.
      • A label is always returned, even if no perfect match exists.
      • Best Practice: Include a general "other" label (e.g., "N/A", "misc") to handle edge cases where content doesn't fit specific categories.
    2. Multi-label classification (classification_type="multi_label"):

      • Selects zero, one, or multiple labels from the predefined set.
      • Labels are non-exclusive.
      • If no labels apply, the extracted_items list will be empty.

    Use LabelConcept for tasks like document type classification, topic tagging, quality assessment, or risk categorization.

  4. What is RatingConcept and when to use it

    main

    The RatingConcept is a specialized concept type used to calculate, infer, and derive numerical rating values from documents within a defined scale.

    Use RatingConcept to:

    • Derive implicit ratings: Calculate ratings based on sentiment, key criteria, or contextual evaluation.
    • Generate evaluative scores: Produce numerical assessments for quality, relevance, or performance.
    • Normalize diverse signals: Convert qualitative assessments into consistent numerical ratings.
    • Synthesize overall scores: Combine multiple factors into a single assessment.

    Common use cases include quantifying product reviews, performance assessments, risk evaluations (severity/probability), and content analyses.

  5. What is JsonObjectConcept?

    main

    The JsonObjectConcept is used to extract complex, structured information from unstructured text into JSON format. It is ideal for:

    • Nested data structures: Hierarchical information and related attributes.
    • Standardized formats: Extracting data following predefined schemas for reliable downstream processing.
    • Complex entity extraction: Capturing entities with multiple attributes and relationships.

    It allows you to define precise schemas that ensure extracted information maintains structural integrity.

  6. Use StringConcept for text-based extraction

    main

    The StringConcept is used to extract text-based information from documents. It is suitable for:

    • Simple fields: names, titles, descriptions, identifiers.
    • Complex analyses: conclusions, assessments, recommendations, summaries.
    • Detected elements: anomalies, patterns, key findings, critical insights.

    It can extract both factual information and interpretive content that requires advanced understanding.

  7. Supported LLM providers and models

    main

    ContextGem uses LiteLLM to provide a unified interface for various LLM providers. This allows for easy switching between different models and providers without changing your core logic.

    Supported Providers

    • Cloud LLMs: OpenAI, Anthropic, Google, Azure OpenAI, xAI, and more.
    • Local LLMs: Models run locally via providers like Ollama or LM Studio.

    Model Recommendations

    • Reasoning Models: Supports both reasoning/CoT-capable models (e.g., gpt-5) and non-reasoning models (e.g., gpt-4.1).
    • Structured Extraction: For reliable structured data extraction, it is recommended to use models with performance equivalent to or exceeding gpt-4o-mini.
    • Small Models: Smaller models (e.g., 8B parameter models) may struggle with the detailed extraction instructions used by ContextGem. If you experience issues, refer to the optimization guides.
  8. Extracting Document-level and Aspect-specific Concepts

    main

    ContextGem allows you to extract two types of information from a single document:

    1. Document-level concepts: These apply to the entire document (e.g., 'Is Privacy Policy' or 'Last Updated Date').
    2. Aspect-specific concepts: These are tied to particular sections or themes within the document.

    When working with multiple aspects and concepts, using concurrency can significantly speed up the extraction process.

  9. Serialize and persist ContextGem objects

    main

    ContextGem provides built-in serialization methods to save and load the following objects:

    • Document objects: Save processed documents to avoid repeating expensive LLM calls.
    • Pipelines: Persist extraction pipelines for later reuse.
    • LLM configurations: Save your LLM settings to transfer extraction results between different systems.
  10. Understand the Core Components: Document, Aspect, and Concept

    main

    ContextGem uses a hierarchical model to perform structured extraction from documents:

    • Document (contextgem.public.documents.Document): The source material containing text and/or visual content (e.g., Contracts, Invoices, CVs).
    • Aspect (contextgem.public.aspects.Aspect): A defined area or topic within a document (or within another aspect) that requires focus. Aspects can be nested to create a hierarchy for granular extraction.
    • Concept (contextgem.public.concepts.*): The actual unit of information extracted. Concepts are attached to either a Document (for broad analysis) or an Aspect (for targeted extraction within a specific context).

    Supported Concept Types

    • StringConcept
    • BooleanConcept
    • NumericalConcept
    • DateConcept
    • JsonObjectConcept
    • RatingConcept
    • LabelConcept