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.
| Variable | Default | Description |
|---|---|---|
| DIAL_SDK_LOG | WARNING | DIAL SDK log level |
| 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. |
Install the library using pip:
pip install aidial-sdkThe 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)python3 app.pySend 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"
}Important
This project uses Python>=3.9 and Poetry>=2.1.1 as a dependency manager.
Check out Poetry's documentation on how to install it on your system before proceeding.
To install requirements:
poetry installThis will install all requirements for running the package, linting, formatting and tests.
The recommended IDE is VSCode. Open the project in VSCode and install the recommended extensions.
The VSCode is configured to use PEP-8 compatible formatter Black.
Alternatively you can use PyCharm.
Set-up the Black formatter for PyCharm manually or install PyCharm>=2023.2 with built-in Black support.
Run the linting before committing:
make lintTo auto-fix formatting issues run:
make formatRun unit tests locally for available python versions:
make testRun unit tests for the specific python version:
make test PYTHON=3.11To remove the virtual environment and build artifacts run:
make cleanTo build the package run:
make buildTo publish the package to PyPI run:
make publish