Skip to main content

A Python SDK for Hume AI

Project description

Hume AI Python SDK

Integrate Hume APIs directly into your Python application



Documentation

API reference documentation is available here.

Installation

pip install hume
# or
poetry add hume

Other Resources

from hume.client import HumeClient

client = HumeClient(
    api_key="YOUR_API_KEY", # Defaults to HUME_API_KEY
)
client.empathic_voice.configs.list_configs()

Async Client

The SDK also exports an async client so that you can make non-blocking calls to our API.

import asyncio

from hume.client import AsyncHumeClient

client = AsyncHumeClient(
    api_key="YOUR_API_KEY",
)

async def main() -> None:
    await client.empathic_voice.configs.list_configs()

asyncio.run(main())

Writing File

Writing files with an async stream of bytes can be tricky in Python! aiofiles can simplify this some. For example, you can download your job artifacts like so:

import aiofiles

from hume import AsyncHumeClient

client = AsyncHumeClient()
async with aiofiles.open('artifacts.zip', mode='wb') as file:
    async for chunk in client.expression_measurement.batch.get_job_artifacts(id="my-job-id"):
        await file.write(chunk)

Legacy SDK

If you want to continue using the legacy SDKs, simply import them from the hume.legacy module.

from hume import HumeVoiceClient, VoiceConfig

client = HumeVoiceClient("<your-api-key>")
config = client.empathic_voice.configs.get_config_version(
    id="id",
    version=1,
)

Namespaces

This SDK contains the APIs for expression measurement, empathic voice and custom models. Even if you do not plan on using more than one API to start, the SDK provides easy access in case you find additional APIs in the future.

Each API is namespaced accordingly:

from hume.client import HumeClient

client = HumeClient(
    api_key="YOUR_API_KEY",
)

client.expression_measurement. # APIs specific to Expression Measurement

client.emapthic_voice.         # APIs specific to Empathic Voice

Exception Handling

All errors thrown by the SDK will be subclasses of ApiError.

import hume

try:
  client.text_gen.create_chat_completion(...)
except hume.core.ApiError as e: # Handle all errors
  print(e.status_code)
  print(e.body)

Pagination

Paginated requests will return a SyncPager or AsyncPager, which can be used as generators for the underlying object. For example, list_tools will return a generator over ReturnUserDefinedTool and handle the pagination behind the scenes:

import hume.client

client = HumeClient(
    api_key="YOUR_API_KEY",
)

for tool in client.empathic_voice.tools.list_tools():
  print(tool)

you could also iterate page-by-page:

for page in client.empathic_voice.tools.list_tools().iter_pages():
  print(page.items)

or manually:

pager = client.empathic_voice.tools.list_tools()
# First page
print(pager.items)
# Second page
pager = pager.next_page()
print(pager.items)

WebSockets

We expose a websocket client for interacting with the EVI API as well as Expression Measurement.

When interacting with these clients, you can use them very similarly to how you'd use the common websockets library:

from hume import StreamDataModels

client = AsyncHumeClient(api_key=os.getenv("HUME_API_KEY"))

async with client.expression_measurement.stream.connect(
    options={"config": StreamDataModels(...)}
) as hume_socket:
    print(await hume_socket.get_job_details())

The underlying connection, in this case hume_socket, will support intellisense/autocomplete for the different functions that are available on the socket!

Advanced

Retries

The Hume SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit.

A request is deemed retriable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 409 (Conflict)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the max_retries request option to configure this behavior.

from hume.client import HumeClient

client = HumeClient(...)

# Override retries for a specific method
client.text_gen.create_chat_completion(..., {
    max_retries=5
})

Timeouts

By default, requests time out after 60 seconds. You can configure this with a timeout option at the client or request level.

from hume.client import HumeClient

client = HumeClient(
    # All timeouts are 20 seconds
    timeout=20.0,
)

# Override timeout for a specific method
client.text_gen.create_chat_completion(..., {
    timeout_in_seconds=20.0
})

Custom HTTP client

You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies and transports.

import httpx

from hume.client import HumeClient

client = HumeClient(
    http_client=httpx.Client(
        proxies="http://my.test.proxy.example.com",
        transport=httpx.HTTPTransport(local_address="0.0.0.0"),
    ),
)

Beta Status

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version. This way, you can install the same version each time without breaking changes.

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

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

hume-0.7.0rc2.tar.gz (108.8 kB view details)

Uploaded Source

Built Distribution

hume-0.7.0rc2-py3-none-any.whl (273.5 kB view details)

Uploaded Python 3

File details

Details for the file hume-0.7.0rc2.tar.gz.

File metadata

  • Download URL: hume-0.7.0rc2.tar.gz
  • Upload date:
  • Size: 108.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.19 Linux/5.15.0-1071-azure

File hashes

Hashes for hume-0.7.0rc2.tar.gz
Algorithm Hash digest
SHA256 8e34b1356190d153f536802f32ef3410f6d53019d65ea74d69663bf3a5593a68
MD5 ee61a496ebdeba6953c0316606606eaa
BLAKE2b-256 01c8204508a87d6380ead69366ff5d5fd32c6c1a383c126084224ff3dc786307

See more details on using hashes here.

Provenance

File details

Details for the file hume-0.7.0rc2-py3-none-any.whl.

File metadata

  • Download URL: hume-0.7.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 273.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.19 Linux/5.15.0-1071-azure

File hashes

Hashes for hume-0.7.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 d28de72e68ae5bd85909f6d4a538296cc12d66d7135452e8da8d9c5515a9405e
MD5 92fecfa1dc9565d1fd4eb3f5a46446ce
BLAKE2b-256 312bb986efbce4e075f45e3ce9d6d356a8dd7446900c5fa5afe1ee371d03d608

See more details on using hashes here.

Provenance

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