trustwise


Nametrustwise JSON
Version 1.0.0b10 PyPI version JSON
download
home_pageNone
SummaryTrustwise Python SDK for interacting with the Trustwise APIs
upload_time2025-10-07 21:51:08
maintainerNone
docs_urlNone
authorNone
requires_python<3.14,>=3.11
licenseNone
keywords ai alignment evaluation metrics safety
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🦉 Trustwise Python SDK

[![PyPI version](https://img.shields.io/pypi/v/trustwise.svg)](https://pypi.org/project/trustwise/)

Trustwise Python SDK provides convenient access to the Trustwise metrics for any Python (3.11, 3.12, 3.13) application. The library includes type definitions for all request params and response fields, and offers both synchronous and asynchronous clients.

## 📦 Installation

```bash
pip install trustwise
```

## 📚 Documentation

For comprehensive documentation, including detailed API references, usage examples, and metric descriptions, visit our [official SDK documentation](https://trustwiseai.github.io/trustwise/).

## 🚀 Quick Start

Get started with Trustwise in just a few lines of code:

```python
import os
from trustwise.sdk import TrustwiseSDK
from trustwise.sdk.config import TrustwiseConfig

# Initialize the SDK
config = TrustwiseConfig(api_key=os.getenv("TW_API_KEY"))
trustwise = TrustwiseSDK(config)
```

### 🛡️ Trustwise Metrics

```python
# Example context
context = [{
    "node_text": "Paris is the capital of France.",
    "node_score": 0.95,
    "node_id": "doc:idx:1"
}]

# Evaluate faithfulness
result = trustwise.metrics.faithfulness.evaluate(
    query="What is the capital of France?",
    response="The capital of France is Paris.",
    context=context
)
print(f"Faithfulness score: {result.score}")

# Evaluate PII detection
pii_result = trustwise.metrics.pii.evaluate(
    text="My email is john@example.com and my phone is 123-456-7890",
    allowlist=["john@example.com"],  # (Optionally) Allow specific PII patterns
    blocklist=["123-456-7890"]       # (Optionally) Block specific PII patterns
)
print(f"PII detection result: {pii_result}")
```

```python
# Evaluate cost for OpenAI model
cost_result = trustwise.metrics.cost.evaluate(
    model_name="gpt-3.5-turbo",
    model_type="LLM",
    model_provider="openai",
    number_of_queries=5,
    total_prompt_tokens=950,
    total_completion_tokens=50
)
print(f"Cost per run: ${cost_result.cost_estimate_per_run}")
print(f"Total project cost: ${cost_result.total_project_cost_estimate}")

# Evaluate carbon emissions
carbon_result = trustwise.metrics.carbon.evaluate(
    processor_name="AMD A10-9700",
    provider_name="aws",
    provider_region="us-east-1",
    instance_type="p4d.24xlarge",
    average_latency=100
)
print(f"Carbon emissions: {carbon_result.carbon_emitted} kg CO2e")
```

### 🚧 Guardrails

Create guardrails to automatically validate responses:

```python
# Create a multi-metric guardrail
guardrail = trustwise.guardrails(
    thresholds={
        "faithfulness": 0.8,
        "answer_relevancy": 0.7,
        "clarity": 0.7
    },
    block_on_failure=True
)

# Evaluate with multiple metrics
evaluation = guardrail.evaluate(
    query="What is the capital of France?",
    response="The capital of France is Paris.",
    context=context
)

print("Guardrail Evaluation:", evaluation)
print("Guardrail Evaluation:", evaluation.to_json())
```

## 🔐 API Key Setup

Get your API Key by logging in to Trustwise Portal. Sign up to get access on the [Trustwise Website](http://trustwise.ai)

The SDK requires an API key to authenticate requests. You can provide the API key in several ways:

```python
# Method 1: Using environment variable (recommended)
config = TrustwiseConfig()  # Automatically uses TW_API_KEY from environment
trustwise = TrustwiseSDK(config)

# Method 2: Direct initialization with API key
config_direct = TrustwiseConfig(api_key=os.environ["TW_API_KEY"])
trustwise_direct = TrustwiseSDK(config_direct)

# Method 3: Custom configuration with specific base URL
config_custom = TrustwiseConfig(
    api_key=os.environ["TW_API_KEY"],
    base_url="https://api.trustwise.ai"
)
trustwise_custom = TrustwiseSDK(config_custom)
```

## ⚡ Async Support

For async applications, simply use `TrustwiseSDKAsync` instead of `TrustwiseSDK`:

```python
import os
import asyncio
from trustwise.sdk import TrustwiseSDKAsync
from trustwise.sdk.config import TrustwiseConfig

# Initialize with TrustwiseConfig
trustwise = TrustwiseSDKAsync(TrustwiseConfig(api_key=os.getenv("TW_API_KEY")))

async def main():
    # Example: Evaluate clarity asynchronously
    result = await trustwise.metrics.clarity.evaluate(
        query="What is the capital of France?",
        response="The capital of France is Paris."
    )
    print(f"Clarity score: {result.score}")

if __name__ == "__main__":
    asyncio.run(main())
```

## 📊 Available Metrics

- Faithfulness
- Answer Relevancy
- Context Relevancy
- Summarization
- Prompt Injection Detection
- Clarity
- Helpfulness
- Toxicity
- Tone
- Formality
- Simplicity
- Sensitivity
- Cost Estimation
- Carbon Emissions

## 📝 License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

## 🤝 Contributing

We welcome contributions! If you find a bug, have a feature request, or want to contribute code, please create an issue or submit a pull request.

### Git Hooks

This repository includes git hooks to ensure code quality. To install them:

```bash
# Make sure you're in the repository root
./scripts/install-hooks.sh
```

The hooks will run tests, linting, and documentation checks before each push to ensure code quality.



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "trustwise",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.14,>=3.11",
    "maintainer_email": null,
    "keywords": "ai, alignment, evaluation, metrics, safety",
    "author": null,
    "author_email": "Mayank Chutani <mayank@trustwise.ai>",
    "download_url": null,
    "platform": null,
    "description": "# \ud83e\udd89 Trustwise Python SDK\n\n[![PyPI version](https://img.shields.io/pypi/v/trustwise.svg)](https://pypi.org/project/trustwise/)\n\nTrustwise Python SDK provides convenient access to the Trustwise metrics for any Python (3.11, 3.12, 3.13) application. The library includes type definitions for all request params and response fields, and offers both synchronous and asynchronous clients.\n\n## \ud83d\udce6 Installation\n\n```bash\npip install trustwise\n```\n\n## \ud83d\udcda Documentation\n\nFor comprehensive documentation, including detailed API references, usage examples, and metric descriptions, visit our [official SDK documentation](https://trustwiseai.github.io/trustwise/).\n\n## \ud83d\ude80 Quick Start\n\nGet started with Trustwise in just a few lines of code:\n\n```python\nimport os\nfrom trustwise.sdk import TrustwiseSDK\nfrom trustwise.sdk.config import TrustwiseConfig\n\n# Initialize the SDK\nconfig = TrustwiseConfig(api_key=os.getenv(\"TW_API_KEY\"))\ntrustwise = TrustwiseSDK(config)\n```\n\n### \ud83d\udee1\ufe0f Trustwise Metrics\n\n```python\n# Example context\ncontext = [{\n    \"node_text\": \"Paris is the capital of France.\",\n    \"node_score\": 0.95,\n    \"node_id\": \"doc:idx:1\"\n}]\n\n# Evaluate faithfulness\nresult = trustwise.metrics.faithfulness.evaluate(\n    query=\"What is the capital of France?\",\n    response=\"The capital of France is Paris.\",\n    context=context\n)\nprint(f\"Faithfulness score: {result.score}\")\n\n# Evaluate PII detection\npii_result = trustwise.metrics.pii.evaluate(\n    text=\"My email is john@example.com and my phone is 123-456-7890\",\n    allowlist=[\"john@example.com\"],  # (Optionally) Allow specific PII patterns\n    blocklist=[\"123-456-7890\"]       # (Optionally) Block specific PII patterns\n)\nprint(f\"PII detection result: {pii_result}\")\n```\n\n```python\n# Evaluate cost for OpenAI model\ncost_result = trustwise.metrics.cost.evaluate(\n    model_name=\"gpt-3.5-turbo\",\n    model_type=\"LLM\",\n    model_provider=\"openai\",\n    number_of_queries=5,\n    total_prompt_tokens=950,\n    total_completion_tokens=50\n)\nprint(f\"Cost per run: ${cost_result.cost_estimate_per_run}\")\nprint(f\"Total project cost: ${cost_result.total_project_cost_estimate}\")\n\n# Evaluate carbon emissions\ncarbon_result = trustwise.metrics.carbon.evaluate(\n    processor_name=\"AMD A10-9700\",\n    provider_name=\"aws\",\n    provider_region=\"us-east-1\",\n    instance_type=\"p4d.24xlarge\",\n    average_latency=100\n)\nprint(f\"Carbon emissions: {carbon_result.carbon_emitted} kg CO2e\")\n```\n\n### \ud83d\udea7 Guardrails\n\nCreate guardrails to automatically validate responses:\n\n```python\n# Create a multi-metric guardrail\nguardrail = trustwise.guardrails(\n    thresholds={\n        \"faithfulness\": 0.8,\n        \"answer_relevancy\": 0.7,\n        \"clarity\": 0.7\n    },\n    block_on_failure=True\n)\n\n# Evaluate with multiple metrics\nevaluation = guardrail.evaluate(\n    query=\"What is the capital of France?\",\n    response=\"The capital of France is Paris.\",\n    context=context\n)\n\nprint(\"Guardrail Evaluation:\", evaluation)\nprint(\"Guardrail Evaluation:\", evaluation.to_json())\n```\n\n## \ud83d\udd10 API Key Setup\n\nGet your API Key by logging in to Trustwise Portal. Sign up to get access on the [Trustwise Website](http://trustwise.ai)\n\nThe SDK requires an API key to authenticate requests. You can provide the API key in several ways:\n\n```python\n# Method 1: Using environment variable (recommended)\nconfig = TrustwiseConfig()  # Automatically uses TW_API_KEY from environment\ntrustwise = TrustwiseSDK(config)\n\n# Method 2: Direct initialization with API key\nconfig_direct = TrustwiseConfig(api_key=os.environ[\"TW_API_KEY\"])\ntrustwise_direct = TrustwiseSDK(config_direct)\n\n# Method 3: Custom configuration with specific base URL\nconfig_custom = TrustwiseConfig(\n    api_key=os.environ[\"TW_API_KEY\"],\n    base_url=\"https://api.trustwise.ai\"\n)\ntrustwise_custom = TrustwiseSDK(config_custom)\n```\n\n## \u26a1 Async Support\n\nFor async applications, simply use `TrustwiseSDKAsync` instead of `TrustwiseSDK`:\n\n```python\nimport os\nimport asyncio\nfrom trustwise.sdk import TrustwiseSDKAsync\nfrom trustwise.sdk.config import TrustwiseConfig\n\n# Initialize with TrustwiseConfig\ntrustwise = TrustwiseSDKAsync(TrustwiseConfig(api_key=os.getenv(\"TW_API_KEY\")))\n\nasync def main():\n    # Example: Evaluate clarity asynchronously\n    result = await trustwise.metrics.clarity.evaluate(\n        query=\"What is the capital of France?\",\n        response=\"The capital of France is Paris.\"\n    )\n    print(f\"Clarity score: {result.score}\")\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n```\n\n## \ud83d\udcca Available Metrics\n\n- Faithfulness\n- Answer Relevancy\n- Context Relevancy\n- Summarization\n- Prompt Injection Detection\n- Clarity\n- Helpfulness\n- Toxicity\n- Tone\n- Formality\n- Simplicity\n- Sensitivity\n- Cost Estimation\n- Carbon Emissions\n\n## \ud83d\udcdd License\n\nThis project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.\n\n## \ud83e\udd1d Contributing\n\nWe welcome contributions! If you find a bug, have a feature request, or want to contribute code, please create an issue or submit a pull request.\n\n### Git Hooks\n\nThis repository includes git hooks to ensure code quality. To install them:\n\n```bash\n# Make sure you're in the repository root\n./scripts/install-hooks.sh\n```\n\nThe hooks will run tests, linting, and documentation checks before each push to ensure code quality.\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Trustwise Python SDK for interacting with the Trustwise APIs",
    "version": "1.0.0b10",
    "project_urls": {
        "Documentation": "https://trustwiseai.github.io/trustwise/",
        "Homepage": "https://trustwise.ai",
        "Issues": "https://github.com/trustwiseai/trustwise/issues",
        "Repository": "https://github.com/trustwiseai/trustwise"
    },
    "split_keywords": [
        "ai",
        " alignment",
        " evaluation",
        " metrics",
        " safety"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "db86d7240ccb94fb5e20522c431eff84ed84b2380ba54321011c0ec118228786",
                "md5": "12a1c3738d70fab393dd7f56209d2958",
                "sha256": "41aeffce72a26fc150993509d0a486ab53c2d12ef35946bce1c2cd3efb0b4ad8"
            },
            "downloads": -1,
            "filename": "trustwise-1.0.0b10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "12a1c3738d70fab393dd7f56209d2958",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.14,>=3.11",
            "size": 85273,
            "upload_time": "2025-10-07T21:51:08",
            "upload_time_iso_8601": "2025-10-07T21:51:08.856451Z",
            "url": "https://files.pythonhosted.org/packages/db/86/d7240ccb94fb5e20522c431eff84ed84b2380ba54321011c0ec118228786/trustwise-1.0.0b10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-07 21:51:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "trustwiseai",
    "github_project": "trustwise",
    "github_not_found": true,
    "lcname": "trustwise"
}
        
Elapsed time: 0.97147s