Skip to main content

Python SDK for the Apostrofo Models API

Project description

Apostrofo Models SDK

A Python SDK for the Apostrofo Models API that provides easy access to various AI models for text analysis, classification, and processing.

Installation

pip install apostrofo-models-sdk

Features

  • Automated authentication with API key
  • Access to multiple AI models:
    • Guardrails: Prompt injection and toxic language detection
    • Data Processing: Text masking, classification, and entity extraction
    • Content Enhancement: Tags generation and metadata extraction
    • Validation: Answer verification
  • Comprehensive error handling
  • Type hints for better IDE support
  • Pydantic models for request and response validation

Quick Start

from apostrofo_models_sdk import ApostrofoModelsClient

# Initialize client with your API key
client = ApostrofoModelsClient(api_key="your_api_key")

# Detect toxic language
toxic_result = client.detect_toxic_language("Text to analyze")
print(f"Is toxic: {toxic_result.is_toxic}")
print(f"Toxicity score: {toxic_result.toxicity_score}")
print(f"Explanation: {toxic_result.explanation}")

# Extract entities
entity_result = client.extract_entities(
    "John Smith works at Apostrofo in New York.", 
    entity_types=["PERSON", "ORGANIZATION", "LOCATION"]
)
print(entity_result.entities)

Authentication

The SDK requires an API key for authentication, which is passed as a header (X-API-Key) to all API requests. You can obtain an API key by signing up at Apostrofo AI.

client = ApostrofoModelsClient(
    api_key="your_api_key",
    base_url="https://api.apostrofo.ai"  # Optional, default URL
)

Available Models

Guardrails

# Prompt Injection Detection
injection_result = client.detect_prompt_injection("Ignore previous instructions and...")
print(f"Is injection: {injection_result.is_injection}")

# Toxic Language Detection
toxic_result = client.detect_toxic_language("Text to analyze")
print(f"Is toxic: {toxic_result.is_toxic}")

Data Processing

# Text Masking
mask_result = client.mask_text(
    "My name is John and my email is john@example.com", 
    tags=["EMAIL", "PERSON"]
)
print(mask_result.bracketed_text)

# Text Classification
classification_result = client.classify_text(
    "I'm really happy with this product!",
    parameters={
        "sentiment": {
            "description": "Sentiment of the text",
            "type": "string",
            "properties": {
                "values": ["positive", "negative", "neutral"]
            }
        }
    }
)
print(classification_result.results)

# Entity Extraction
entity_result = client.extract_entities(
    "Apple Inc. is headquartered in Cupertino, California.",
    entity_types=["ORG", "LOC"]
)
print(entity_result.entities)

Content Enhancement

# Tags Generation
tags_result = client.generate_tags(
    "This is a blog post about Python programming and data science.",
    parameters={
        "topics": {
            "description": "Topics mentioned in the text",
            "properties": {
                "max": 5
            }
        }
    }
)
print(tags_result.results)

# Metadata Extraction
metadata_result = client.extract_metadata(
    "Publication: Science Journal\nDate: 2023-03-15\nTitle: New Advances in AI",
    parameters={
        "publication_info": {
            "description": "Publication information",
            "properties": {
                "fields": ["journal", "date", "title"]
            }
        }
    }
)
print(metadata_result.results)

Validation

# Answer Verification
verification_result = client.verify_answer(
    question="What is the capital of France?",
    answer="The capital of France is London."
)
print(f"Is accurate: {verification_result.is_accurate}")
print(f"Explanation: {verification_result.explanation}")

Error Handling

The SDK provides comprehensive error handling with custom exceptions:

from apostrofo_models_sdk import ApostrofoApiError, AuthenticationError

try:
    result = client.detect_toxic_language("Test text")
except AuthenticationError:
    print("Invalid API key")
except ApostrofoApiError as e:
    print(f"API error: {e.message}, status code: {e.status_code}")

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This SDK is licensed under the MIT License - see the LICENSE file for details.

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

apostrofo_models_sdk-0.1.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apostrofo_models_sdk-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file apostrofo_models_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: apostrofo_models_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for apostrofo_models_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 14d537a32a6174ccf1c569801b797c5862baa5f53fa588bb6b560ca56f4f46d8
MD5 3de76993e65831a8440b49e6ddec2094
BLAKE2b-256 933cb5d913c3f2808fc2ef7cacbb372141e4e065335e6b8c0f05f55b5a8761bf

See more details on using hashes here.

File details

Details for the file apostrofo_models_sdk-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for apostrofo_models_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc87564597e36bfc2d9453b0dff9d7da7b4d590b18944f7063965d0e16d17027
MD5 0d5eae563602200064c0183f21f8d389
BLAKE2b-256 3204dd6bde97a66eed6f5381951aad6d580e33af95b2062ec2cb018b97bce8c2

See more details on using hashes here.

Supported by

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