Skip to main content

A Python client for the Truestate API

Project description

TrueState Python SDK

The TrueState Python SDK is a Python library that allows you to interact with the TrueState API. The SDK provides a convenient way to access the TrueState API from your Python application.

For documentation on the TrueState API, visit the API documentation.

Installation

You can install the TrueState Python SDK using pip:

pip install truestate

Once installed, you must configure the following environment variables:

export TRUESTATE_API_KEY="your-api-key"
export TRUESTATE_ORGANISATION_ID="your-organisation-id"

Inference

The TrueState Python SDK provides a simple way to build and deploy robust natural language AI systems. The inference SDK provides a high-level API for making predictions on your models.

Univesal Classification

Universal classification is an advanced type of NLP classification that can be used to classify any type of text data. The TrueState Python SDK provides a high-level API for making predictions on your universal classification models.

from truestate.inference import classify

sample_text = "You should invest all of your money on the stock market!"

result = classify(sample_text, choices=["the text contains financial advice"])

print(result)
#{'the text contains financial advice': 0.9951813}

Semantic Routing

Semantic routing is a powerful feature that allows you to route incoming messages to the appropriate department or team based on the content of the message. The TrueState Python SDK provides a high-level API for making predictions on your semantic routing models.

from truestate.inference import route

sample_text = "I have a question about my order."


def send_to_order_agent(text: str):
    return f"Sending message to order agent: '{text}'"

def send_to_support_agent(text: str):
    return f"Sending message to support agent: '{text}'"

def send_to_general_agent(text: str):
    return f"Would send message to general agent: '{text}'"

routing_strategies = {
    "the user message is about an order": send_to_order_agent,
    "the user message is about a problem with a product / service": send_to_support_agent,
    "__default__": send_to_general_agent
}

response = route(sample_text, routing_strategies, threshold=0.5)

print(response)
# Sending message to order agent: I have a question about my order....

Natural Language Search

Natural language search allows you to search for information using natural language queries, offering considerable performance gains above traditional search algorithms. The TrueState Python SDK provides a high-level API for making predictions on your searchable datasets.

To create a searchable dataset, visit the Workflows documentation.

from truestate.inference import search

query = "lightweight camping gear for backpacking"

dataset_id = "1783d3ff-178e-485d-a43b-23759cc6bdf3"

results = search("Com", dataset_id)

print(results)
# [
#     {
#         "id": "UL001",
#         "name": "Featherlight Pro X2 Tent",
#         "description": "Ultra-lightweight 2-person tent weighing 1.75 lbs (794g). Made with Dyneema Composite Fabric for ultimate strength-to-weight ratio. Ideal for thru-hikers on long-distance trails."
#     },
#     {
#         "id": "UL002",
#         "name": "AeroDown Ultralight 5°F Sleeping Bag",
#         "description": "Premium 5°F (-15°C) rated sleeping bag weighing only 1.2 lbs (544g). Features 950+ fill power down with water-repellent treatment. Perfect for long-distance backpacking in varied conditions."
#     },
#     {
#         "id": "UL003",
#         "name": "TitaniumFlame Micro Stove System",
#         "description": "Integrated stove system weighing 5.6 oz (159g) including pot. Boils water in 100 seconds and nests with a fuel canister. Engineered for ultralight long-distance hikers."
#     }
# ]

Hierarchy Classification

In some instances it's necessary to classify text data into a hierarchy of categories. The TrueState Python SDK provides a high-level API for making predictions on your hierarchy classification models.

from truestate.inference import hierarchy_classificaiton

sample_text = "Amazon is a multinational technology company that focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. The primary business of Amazon.com is the sale of consumer goods and subscriptions."

hierarchy = ClassHierarchy(
    name="company-categorisation",
    choices=[
        Category(
            label="Software company",
            criteria="this text contains a description of a software company",
            subcategories=[
                Category(
                    label="Ecommerce",
                    criteria="this text contains a description of an ecommerce company",
                ),
                Category(
                    label="Blockchain",
                    criteria="this text contains a description of an web3.0 / blockchain company",
                ),
            ],
        ),
    ],
)

result = hierarchy_classificaiton(sample_text, hierarchy)
print(result)
# {
#     "text": "Amazon is a multinational technology company that focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. The primary business of Amazon.com is the sale of consumer goods and subscriptions.",
#    "categories": {
#       "category_1": "Software company",
#       "category_2": "Ecommerce"
# }

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

truestate-0.5.62.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

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

truestate-0.5.62-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file truestate-0.5.62.tar.gz.

File metadata

  • Download URL: truestate-0.5.62.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for truestate-0.5.62.tar.gz
Algorithm Hash digest
SHA256 3bd8dae3dda0c895824b177f9c193707b966f5a9d0ae9e21c80aa5e87f9989cc
MD5 0eddc48e8ad340ef4936dd24752ac001
BLAKE2b-256 15e63145bfe8df1108399bce2a91e4f58bc240c642a7e39972b7c4121c3a03aa

See more details on using hashes here.

File details

Details for the file truestate-0.5.62-py3-none-any.whl.

File metadata

  • Download URL: truestate-0.5.62-py3-none-any.whl
  • Upload date:
  • Size: 32.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for truestate-0.5.62-py3-none-any.whl
Algorithm Hash digest
SHA256 b5602fed46aeabac2d5cdc0cf279554d26424bfd348223b13f8da13a5241df2a
MD5 3a1df0ca57532ad1c2f763ae09d0e1ea
BLAKE2b-256 d08ec518c73db3bb4997ffeba7b492a176193a0c8060e0e224401b258437f742

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