Skip to main content

Embeddings

The aidial_sdk.embeddings module provides the base class and models for building embedding applications. Register an implementation with app.add_embeddings() to expose it through the Unified API.

from aidial_sdk.embeddings import Embeddings, Request, Response

Embeddings base class​

from aidial_sdk.embeddings import Embeddings

Abstract base class with a single method to implement:

embeddings​

async def embeddings(self, request: Request) -> Response

Receives an embedding request and returns the computed embeddings. Unlike ChatCompletion, this method returns the response directly (no streaming).

Request​

Extends the Azure OpenAI embeddings format with DIAL-specific fields.

Standard fields (Azure OpenAI compatible)​

FieldTypeDefaultDescription
modelstr | NoneNoneModel identifier.
inputstr | list[str] | list[int] | list[list[int]]requiredText or tokenized input to embed.
encoding_format"float" | "base64""float"Output encoding format.
dimensionsint | NoneNoneDesired embedding dimensionality (if the model supports it).
userstr | NoneNoneEnd-user identifier.

DIAL extension fields​

FieldTypeDefaultDescription
custom_inputlist[EmbeddingsMultiModalInput] | NoneNoneMultimodal input (strings and/or attachments).
custom_fieldsEmbeddingsRequestCustomFields | NoneNoneCustom fields (see below).

EmbeddingsRequestCustomFields​

FieldTypeDefaultDescription
typestr | NoneNoneEmbedding type hint (e.g., "query" or "document").
instructionstr | NoneNoneTask instruction for the embedding model.

EmbeddingsMultiModalInput​

Union[str, Attachment, list[str | Attachment]]

Allows mixing text and file attachments in a single embedding request.

Request properties​

PropertyTypeDescription
deployment_idstrThe deployment name from the URL path.
api_key_secretstrThe API key from the api-key header.
jwtstr | NoneThe JWT from the Authorization header, if present.
headersdict[str, str]All HTTP headers from the original request.

Response​

EmbeddingResponse​

FieldTypeDefaultDescription
datalist[Embedding]requiredList of embedding results.
modelstrrequiredModel used to generate the embeddings.
object"list""list"Object type (always "list").
usageUsagerequiredToken usage.

Embedding​

FieldTypeDefaultDescription
embeddingstr | list[float]requiredEmbedding vector (float list) or base64-encoded string.
indexintrequiredIndex of the input this embedding corresponds to.
object"embedding""embedding"Object type.

Usage​

FieldTypeDescription
prompt_tokensintNumber of tokens in the input.
total_tokensintTotal tokens processed.

Example​

import uvicorn
from aidial_sdk import DIALApp
from aidial_sdk.embeddings import Embeddings, Request, Response
from aidial_sdk.embeddings.response import Embedding, Usage


class SimpleEmbeddings(Embeddings):
async def embeddings(self, request: Request) -> Response:
inputs = (
[request.input]
if isinstance(request.input, str)
else request.input
)
return Response(
data=[
Embedding(
embedding=[0.1, 0.2, 0.3],
index=i,
)
for i in range(len(inputs))
],
model="simple-embeddings",
usage=Usage(
prompt_tokens=sum(len(str(inp).split()) for inp in inputs),
total_tokens=sum(len(str(inp).split()) for inp in inputs),
),
)


app = DIALApp()
app.add_embeddings("simple-embeddings", SimpleEmbeddings())

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

Further reading​