Skip to main content

AI DIAL Python SDK

About DIALX

PyPI version Discord


Overview

Framework to create applications and model adapters for AI DIAL.

Applications and model adapters implemented using this framework will be compatible with AI DIAL API that was designed based on Azure OpenAI API.


Environment Variables

Variable Default Description
DIAL_SDK_HEADERS_TO_PROXY `` A comma-separated list of headers that should be proxied from incoming requests to outgoing requests to the DIAL API. By default, no headers are proxied.
DIAL_SDK_SSE_HEARTBEAT_INTERVAL When set, the SDK inserts ping comments into streaming chat completion responses after the response has been idle for the specified number of seconds, helping prevent read timeouts when the DIAL application isn't responsive.
PYDANTIC_V2 False When True and Pydantic V2 is installed, DIAL SDK classes for requests/responses will be based on Pydantic V2 BaseModel. Otherwise, they will be based on Pydantic V1 BaseModel.

Logging-related environment variables (log level, console format, and trace/span correlation) are documented in docs/logging.md.


Usage

Install the library using pip:

pip install aidial-sdk

Echo application example

The echo application example replies to the user by repeating their last message:

# Save this as app.py
import uvicorn

from aidial_sdk import DIALApp
from aidial_sdk.chat_completion import ChatCompletion, Request, Response


# ChatCompletion is an abstract class for applications and model adapters
class EchoApplication(ChatCompletion):
    async def chat_completion(self, request: Request, response: Response) -> None:
        # Get last message (the newest) from the history
        last_user_message = request.messages[-1]

        # Generate response with a single choice
        with response.create_single_choice() as choice:
            # Fill the content of the response with the last user's content
            choice.append_content(last_user_message.text())


# DIALApp extends FastAPI to provide a user-friendly interface for routing requests to your applications
app = DIALApp()
app.add_chat_completion("echo", EchoApplication())

# Run built app
if __name__ == "__main__":
    uvicorn.run(app, port=5000)

Run

python3 app.py

Check

Send the next request:

curl http://127.0.0.1:5000/openai/deployments/echo/chat/completions \
  -H "Content-Type: application/json" \
  -H "Api-Key: DIAL_API_KEY" \
  -d '{
    "messages": [{"role": "user", "content": "Repeat me!"}]
  }'

You will see the JSON response as:

{
    "choices":[
        {
            "index": 0,
            "finish_reason": "stop",
            "message": {
                "role": "assistant",
                "content": "Repeat me!"
            }
        }
    ],
    "usage": null,
    "id": "d08cfda2-d7c8-476f-8b95-424195fcdafe",
    "created": 1695298034,
    "object": "chat.completion"
}

Endpoints

Every deployment is exposed under two base paths:

Base path Description
/openai/deployments/{deployment_name} The DIAL API path. The deployment name is a part of the path.
/openai/v1 The Azure OpenAI v1 API path. The deployment name comes from the request headers.

The following endpoints are served under each of them:

  • POST <base path>/chat/completions
  • POST <base path>/rate
  • POST <base path>/tokenize
  • POST <base path>/truncate_prompt
  • GET <base path>/configuration
  • POST <base path>/embeddings

request.deployment_id is resolved from the first of the following sources that is set:

  1. the X-DIAL-OVERRIDE-NAME header,
  2. the deployment name from the path, for the /openai/deployments/{deployment_name} requests,
  3. the X-DIAL-DEPLOYMENT-ID header, which DIAL Core sets when it calls the /openai/v1 endpoints.

An /openai/v1 request with none of the headers set fails with 500 Internal Server Error.


Development

Development Environment

This project requires Python ≥3.11 and Poetry ≥2.1.1 for dependency management.

Setup

  1. Install Poetry. See the official installation guide.

  2. (Optional) Specify custom Python or Poetry executables in .env.dev. This is useful if multiple versions are installed. By default, python and poetry are used.

    POETRY_PYTHON=path-to-python-exe
    POETRY=path-to-poetry-exe
    
  3. Create and activate the virtual environment:

    make init_env
    source .venv/bin/activate
    
  4. Install project dependencies (including linting, formatting, and test tools):

    make install
    

IDE configuration

The recommended IDE is VSCode. Open the project in VSCode and install the recommended extensions. VS Code is configured to use the Ruff formatter.

Alternatively you can use PyCharm that has built-in Ruff support.

Lint

Run the linting before committing:

make lint

To auto-fix formatting issues run:

make format

Test

Run unit tests locally for available python versions:

make test

Run unit tests for the specific python version:

make test PYTHON=3.11

Git hooks

You may optionally install Git hooks that will automatically run the linting step on Git push. You only need to do it once for the given repository.

make install_git_hooks

[!IMPORTANT] This command doesn't work if you have already installed Git hooks locally or globally.

Clean

To remove the virtual environment and build artifacts run:

make clean

Build

To build the package run:

make build

Publish

To publish the package to PyPI run:

make publish

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aidial_sdk-0.42.0.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

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

aidial_sdk-0.42.0-py3-none-any.whl (61.3 kB view details)

Uploaded Python 3

File details

Details for the file aidial_sdk-0.42.0.tar.gz.

File metadata

  • Download URL: aidial_sdk-0.42.0.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.21 Linux/6.17.0-1022-azure

File hashes

Hashes for aidial_sdk-0.42.0.tar.gz
Algorithm Hash digest
SHA256 b482d9ab8916ee2724d1b979c3d6770248e0241b2d92d94890f6cf13062c9a9f
MD5 6d2360c758db5799899744e50164ab07
BLAKE2b-256 d253079fafca716c11e07d6db8594d22679dd7fc43cbf65ca323e544bb178131

See more details on using hashes here.

File details

Details for the file aidial_sdk-0.42.0-py3-none-any.whl.

File metadata

  • Download URL: aidial_sdk-0.42.0-py3-none-any.whl
  • Upload date:
  • Size: 61.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.21 Linux/6.17.0-1022-azure

File hashes

Hashes for aidial_sdk-0.42.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab03a37fc777b26aa8f7d0a386b7b9f7b7db9137e9db6e5ee4eac29d0e4b41cf
MD5 8744f64816e23adb4cc62bd9c541874d
BLAKE2b-256 c9a4cd90bd2c39afd0c06fb80863597945acf221077f3c19d5e592559e31e5dc

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.42.0 This release

2 files

0.41.0

2 files

0.40.0

2 files

0.39.0

2 files

0.38.0

2 files

0.37.0

2 files

0.36.0

2 files

0.35.0

2 files

0.34.0

2 files

0.33.1

2 files

0.33.0

2 files

0.32.0

2 files

0.31.0

2 files

0.30.0

2 files

0.29.0

2 files

0.28.0

2 files

0.27.0

2 files

0.26.0

2 files

0.25.1

2 files

0.25.0

2 files

0.24.0

2 files

0.23.0

2 files

0.22.1

2 files

0.22.0

2 files

0.21.0

2 files

0.20.0

2 files

0.19.0

2 files

0.18.0

2 files

0.17.0

2 files

0.16.0

2 files

0.15.0

2 files

0.14.0

2 files

0.13.0

2 files

0.12.0

2 files

0.11.0

2 files

0.10.1

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page