API client for Python
This page covers the official Python API client for Decthings. Using it, you can communicate with Decthings from your own Python code.
Installation
The package is available for installation on PyPI. To install, install Python and pip, and then run pip install decthings-api. From your own code, import the client and create a new client instance:
from decthings_api.client import DecthingsClient
client = DecthingsClient(api_key="<myapikey>")
An API key is used to add authentication to the Decthings requests, so that you for example can access your own private models. To create a new API key, navigate to the API keys page. Click create, and a new key will be generated for you. Copy the key and add it to your script:
Replace <myapikey> in the example above with your own key.
Important: Make sure to keep your key safe! Never commit your key to source control such as git, and if you share your code, make sure you don't include your key.
Synchronous client
Python has support for async/await syntax, which simplifies input/output programming such as networking. Using async/await is the recommended way to use the Decthings API.
However, because many Python programs don't use async/await, there is also a synchronous API client:
from decthings_api.client_sync import DecthingsClientSync
client = DecthingsClient(api_key="<myapikey>")
Protocol
The client is smart in choosing the HTTP or WebSocket API - it will use HTTP until an event is being listened to, in which case it will switch to WebSocket and use that until no event is being listened to.
Async example
Below are some examples of how you can call methods. Navigate to the desired methods to see more details.
import asyncio
from decthings_api.client import DecthingsClient
from decthings_api.tensor import DecthingsTensor
client = DecthingsClient(api_key="<myapikey>")
async def main():
get_models_result = await client.model.get_models({})
evaluate_result = await client.model.evaluate({
"modelId": "<modelid>",
"params": [{
"name": "<parameter>",
"data": [DecthingsTensor(["My input text"], "string", [])]
}]
})
add_entries_result = await client.dataset.add_entries({
"datasetId": "<datasetid>",
"keys": [
{
"key": "<key>",
"data": [DecthingsTensor([], "i32", [])]
}
]
})
asyncio.run(main())
Sync example
from decthings_api.client_sync import DecthingsClientSync
from decthings_api.tensor import DecthingsTensor
client = DecthingsClientSync(api_key="<myapikey>")
def main():
get_models_result = await client.model.get_models({})
evaluate_result = await client.model.evaluate({
"modelId": "<modelid>",
"params": [{
"name": "<parameter>",
"data": [DecthingsTensor(["My input text"], "string", [])]
}]
})
add_entries_result = await client.dataset.add_entries({
"datasetId": "<datasetid>",
"keys": [
{
"key": "<key>",
"data": [DecthingsTensor([7], "i32", [])]
}
]
})
main()