Skip to main content

Sema4AI Document Intelligence API client

Project description

Document Intelligence Client

This Python package offers a robust client for seamless interaction with the Document Intelligence API. It enables you to efficiently retrieve, manage, and process document data within your workspace.

Installation

To install the package directly from GitHub using pip, run the following command:

pip install sema4ai-di-client

Usage

After installing the package, you can import it and start using the DocumentIntelligenceClient class.

Importing the Package

from sema4ai.di_client import DocumentIntelligenceClient

Getting a Document Work Item

To retrieve a document work item, make sure you've set the required environment variables before initializing the client. Specifically, ensure that DOCUMENT_INTELLIGENCE_SERVICE_URL and AGENTS_EVENTS_SERVICE_URL is set.

When running in Sema4.ai Control Room these are all handled by the platform.

  • Environment Variables: Make sure the required environment variables (DOCUMENT_INTELLIGENCE_SERVICE_URL, AGENTS_EVENTS_SERVICE_URL) are set in your environment before running the code.
  • Workspace ID: If you are developing on local, then make sure to set workspace_id in DocumentIntelligenceClient(workspace_id='<your_workspace_id>'), which is optional and deduced on non-local environments from the URL passed.
  • Initialize the Client: The client will automatically read the environment variables and initialize the connection.
  • Error Handling: The example includes error handling and ensures the client is properly closed at the end.

Here's an example:

from sema4ai.di_client import DocumentIntelligenceClient

# Ensure environment variables are set for the service URLs
# Example:
# export DOCUMENT_INTELLIGENCE_SERVICE_URL='https://api.yourdomain.com'
# export AGENTS_EVENTS_SERVICE_URL='https://agents.yourdomain.com'

# Initialize the client
client = DocumentIntelligenceClient()

# Specify the document ID you want to retrieve
document_id = 'your_document_id'

# Get the document work item
try:
    document_work_item = client.get_document_work_item(document_id)
    if document_work_item:
        print("Document Work Item:")
        print(document_work_item)
    else:
        print("No document work item found for the given document ID.")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    client.close()  # Make sure to close the client connection

Available Methods

The DocumentIntelligenceClient offers several methods to interact with the Document Intelligence API and manage work items. Below are examples of the available operations:

  • Get Document Type: Retrieve details about a specific document type.

    document_type = client.get_document_type(document_type_name)
    
  • Get Document Format: Fetch the format of a document based on its type and class.

    document_format = client.get_document_format(document_type_name, document_class_name)
    
  • Store Extracted Content: Store extracted content after processing a document.

    client.store_extracted_content(extracted_content)
    
  • Store Transformed Content: Save content that has been transformed by a process.

    client.store_transformed_content(transformed_content)
    
  • Store Computed Content: Submit content that has been computed after analysis.

    client.store_computed_content(computed_content)
    
  • Get Document Content: Retrieve document content in various states, such as raw, extracted, transformed, or computed.

    content = client.get_document_content(document_id, content_state)
    
  • Remove Document Content: Delete content for a document in a specific state.

    client.remove_document_content(document_id, content_state)
    
  • Complete Work Item Stage: Mark a work item’s current stage as complete and move to the next stage.

    response = client.work_items_complete_stage(
        work_item_id='your_work_item_id',
        status='SUCCESS',  # or 'FAILURE'
        status_reason='optional_reason',  # Optional
        log_details_path='optional_log_path'  # Optional
    )
    

Dependencies

The package requires the following dependencies:

  • urllib3 >= 1.25.3, < 2.1.0
  • python-dateutil
  • pydantic >= 2
  • typing-extensions >= 4.7.1

These should be installed automatically when you install the package via pip.

Example Code Snippet

Below is an example code snippet if you are testing on prod.

from sema4ai.di_client import DocumentIntelligenceClient

client = DocumentIntelligenceClient()

# Fetch and print the document type
try:
    document_type = client.get_document_type("CounterParty Reconciliation")
    print(document_type)
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    client.close()  # Ensure proper resource cleanup

Below is an example code snippet if you are testing on local.

from sema4ai.di_client import DocumentIntelligenceClient
import os

# Set the required environment variables within the Python code
os.environ['DOCUMENT_INTELLIGENCE_SERVICE_URL'] = 'http://127.0.0.1:9080'
os.environ['AGENTS_EVENTS_SERVICE_URL'] = 'http://127.0.0.1:9080'

workspace_id = "<your Sema4.ai Control Room Workspace ID>"
client = DocumentIntelligenceClient(workspace_id=workspace_id)

# Fetch and print the document type
try:
    document_type = client.get_document_type("CounterParty Reconciliation")
    print(document_type)
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    client.close()  # Ensure proper resource cleanup

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sema4ai_di_client-1.0.8.tar.gz (63.1 kB view details)

Uploaded Source

Built Distribution

sema4ai_di_client-1.0.8-py3-none-any.whl (128.4 kB view details)

Uploaded Python 3

File details

Details for the file sema4ai_di_client-1.0.8.tar.gz.

File metadata

  • Download URL: sema4ai_di_client-1.0.8.tar.gz
  • Upload date:
  • Size: 63.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for sema4ai_di_client-1.0.8.tar.gz
Algorithm Hash digest
SHA256 afd59bb5bc7287cf00423a0231fa119e6b3e2b41020c43665d582255ed3b5dd4
MD5 ab8d80baa4fd26c3e61b894f658c4451
BLAKE2b-256 dbbcfcbde36dd988b6cae62fbb429f67802b45edc1cd6c79ec9a31a5aea482f7

See more details on using hashes here.

File details

Details for the file sema4ai_di_client-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: sema4ai_di_client-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 128.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for sema4ai_di_client-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 dca5c3b83ed19c15d30ec157dc459a9f4d1cd9e1f8b93c8623e398f96eb1dfcb
MD5 31457e9f93278ea240bae02167314ec6
BLAKE2b-256 99e8aa2743b6091ef6e05306b3f45f7d15b333da3b5e946503d5e1974136602e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page