Azure OpenAI embeddings store output binding for Azure Functions

Important

The Azure OpenAI extension for Azure Functions is currently in preview.

The Azure OpenAI embeddings store output binding allows you to write files to a semantic document store that can be referenced later in a semantic search.

For information on setup and configuration details of the Azure OpenAI extension, see Azure OpenAI extensions for Azure Functions. To learn more about semantic ranking in Azure AI Search, see Semantic ranking in Azure AI Search.

Note

References and examples are only provided for the Node.js v4 model.

Note

References and examples are only provided for the Python v2 model.

Note

While both C# process models are supported, only isolated worker model examples are provided.

Example

This example writes an HTTP input stream to a semantic document store at the provided URL.

[Function("IngestFile")]
public static async Task<EmbeddingsStoreOutputResponse> IngestFile(
    [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
{
    ArgumentNullException.ThrowIfNull(req);

    using StreamReader reader = new(req.Body);
    string request = await reader.ReadToEndAsync();

    if (string.IsNullOrWhiteSpace(request))
    {
        throw new ArgumentException("Request body is empty.");
    }

    EmbeddingsRequest? requestBody = JsonSerializer.Deserialize<EmbeddingsRequest>(request);

    if (string.IsNullOrWhiteSpace(requestBody?.Url))
    {
        throw new ArgumentException("Invalid request body. Make sure that you pass in {\"Url\": value } as the request body.");
    }

    if (!Uri.TryCreate(requestBody.Url, UriKind.Absolute, out Uri? uri))
    {
        throw new ArgumentException("Invalid Url format.");
    }

    string filename = Path.GetFileName(uri.AbsolutePath);

    return new EmbeddingsStoreOutputResponse
    {
        HttpResponse = new OkObjectResult(new { status = HttpStatusCode.OK }),
        SearchableDocument = new SearchableDocument(filename)
    };

This example writes an HTTP input stream to a semantic document store at the provided URL.

import com.microsoft.azure.functions.openai.annotation.search.SemanticSearch;
import com.sun.jndi.toolkit.url.Uri;

public class FilePrompt {

    @FunctionName("IngestFile")
    public HttpResponseMessage ingestFile(
        @HttpTrigger(
            name = "req", 
            methods = {HttpMethod.POST},
            authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<EmbeddingsRequest> request,
        @EmbeddingsStoreOutput(name="EmbeddingsStoreOutput", input = "{Url}", inputType = InputType.Url,
                connectionName = "AISearchEndpoint", collection = "openai-index",
                model = "%EMBEDDING_MODEL_DEPLOYMENT_NAME%") OutputBinding<EmbeddingsStoreOutputResponse> output,
        final ExecutionContext context) throws MalformedURLException {

        if (request.getBody() == null || request.getBody().getUrl() == null)
        {
            throw new IllegalArgumentException("Invalid request body. Make sure that you pass in {\"Url\": value } as the request body.");
        }

        Uri uri = new Uri(request.getBody().getUrl());
        String filename = Paths.get(uri.getPath()).getFileName().toString();

        EmbeddingsStoreOutputResponse embeddingsStoreOutputResponse = new EmbeddingsStoreOutputResponse(new SearchableDocument(filename));

        output.setValue(embeddingsStoreOutputResponse);

        JSONObject response = new JSONObject();
        response.put("status", "success");
        response.put("title", filename);

        return request.createResponseBuilder(HttpStatus.CREATED)
                .header("Content-Type", "application/json")
                .body(response)
                .build();
    }

    public class EmbeddingsStoreOutputResponse {
        private SearchableDocument searchableDocument;

        public EmbeddingsStoreOutputResponse(SearchableDocument searchableDocument) {
            this.searchableDocument = searchableDocument;
        }

Examples aren't yet available.

This example writes an HTTP input stream to a semantic document store at the provided URL.

const embeddingsHttpInput = input.generic({
    input: '{RawText}',
    inputType: 'RawText',
    type: 'embeddings',
    model: '%EMBEDDING_MODEL_DEPLOYMENT_NAME%'
})

app.http('generateEmbeddings', {
    methods: ['POST'],
    route: 'embeddings',
    authLevel: 'function',
    extraInputs: [embeddingsHttpInput],
    handler: async (request, context) => {
        let requestBody: EmbeddingsHttpRequest = await request.json();
        let response: any = context.extraInputs.get(embeddingsHttpInput);

        context.log(
            `Received ${response.count} embedding(s) for input text containing ${requestBody.RawText.length} characters.`
        );
        
        // TODO: Store the embeddings into a database or other storage.

        return {status: 202}
    }
});

interface EmbeddingsFilePath {
    FilePath?: string;
}

const embeddingsFilePathInput = input.generic({
    input: '{FilePath}',

This example writes an HTTP input stream to a semantic document store at the provided URL.

Here's the function.json file for ingesting files:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    },
    {
      "name": "EmbeddingsStoreOutput",
      "type": "embeddingsStore",
      "direction": "out",
      "input": "{Url}",
      "inputType": "Url",
      "connectionName": "AISearchEndpoint",
      "collection": "openai-index",
      "model": "%EMBEDDING_MODEL_DEPLOYMENT_NAME%"
    }
  ]
}

For more information about function.json file properties, see the Configuration section.

using namespace System.Net

param($Request, $TriggerMetadata)

$ErrorActionPreference = 'Stop'

$inputJson = $Request.Body

if (-not $inputJson -or -not $inputJson.Url) {
    throw 'Invalid request body. Make sure that you pass in {\"Url\": value } as the request body.'
}

$uri = [URI]$inputJson.Url
$filename = [System.IO.Path]::GetFileName($uri.AbsolutePath)


Push-OutputBinding -Name EmbeddingsStoreOutput -Value @{
    "title" = $filename
}

$response = @{
    "status" = "success"
    "title" = $filename
}

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK
        Body = $response
        Headers    = @{
            "Content-Type" = "application/json"
        }
})

This example writes an HTTP input stream to a semantic document store at the provided URL.

@app.function_name("IngestFile")
@app.route(methods=["POST"])
@app.embeddings_store_output(arg_name="requests", input="{url}", input_type="url", connection_name="AISearchEndpoint", collection="openai-index", model="%EMBEDDING_MODEL_DEPLOYMENT_NAME%")
def ingest_file(req: func.HttpRequest, requests: func.Out[str]) -> func.HttpResponse:
    user_message = req.get_json()
    if not user_message:
        return func.HttpResponse(json.dumps({"message": "No message provided"}), status_code=400, mimetype="application/json")
    file_name_with_extension = os.path.basename(user_message["Url"])
    title = os.path.splitext(file_name_with_extension)[0]
    create_request = {
        "title": title
    }
    requests.set(json.dumps(create_request))
    response_json = {
        "status": "success",
        "title": title
    }
    return func.HttpResponse(json.dumps(response_json), status_code=200, mimetype="application/json")

Attributes

Apply the EmbeddingsStoreOutput attribute to define an embeddings store output binding, which supports these parameters:

Parameter Description
Input The input string for which to generate embeddings.
Model Optional. The ID of the model to use, which defaults to text-embedding-ada-002. You shouldn't change the model for an existing database. For more information, see Usage.
MaxChunkLength Optional. The maximum number of characters used for chunking the input. For more information, see Usage.
MaxOverlap Optional. Gets or sets the maximum number of characters to overlap between chunks.
InputType Optional. Gets the type of the input.
ConnectionName The name of an app setting or environment variable that contains the connection string value. This property supports binding expressions.
Collection The name of the collection or table or index to search. This property supports binding expressions.

Annotations

The EmbeddingsStoreOutput annotation enables you to define an embeddings store output binding, which supports these parameters:

Element Description
name Gets or sets the name of the output binding.
input The input string for which to generate embeddings.
model Optional. The ID of the model to use, which defaults to text-embedding-ada-002. You shouldn't change the model for an existing database. For more information, see Usage.
maxChunkLength Optional. The maximum number of characters used for chunking the input. For more information, see Usage.
maxOverlap Optional. Gets or sets the maximum number of characters to overlap between chunks.
inputType Optional. Gets the type of the input.
connectionName The name of an app setting or environment variable that contains the connection string value. This property supports binding expressions.
collection The name of the collection or table or index to search. This property supports binding expressions.

Decorators

During the preview, define the output binding as a generic_output_binding binding of type semanticSearch, which supports these parameters:

Parameter Description
arg_name The name of the variable that represents the binding parameter.
input The input string for which to generate embeddings.
model Optional. The ID of the model to use, which defaults to text-embedding-ada-002. You shouldn't change the model for an existing database. For more information, see Usage.
maxChunkLength Optional. The maximum number of characters used for chunking the input. For more information, see Usage.
max_overlap Optional. Gets or sets the maximum number of characters to overlap between chunks.
input_type Gets the type of the input.
connection_name The name of an app setting or environment variable that contains the connection string value. This property supports binding expressions.
collection The name of the collection or table or index to search. This property supports binding expressions.

Configuration

The binding supports these configuration properties that you set in the function.json file.

Property Description
type Must be embeddingsStore.
direction Must be out.
name The name of the output binding.
input The input string for which to generate embeddings.
model Optional. The ID of the model to use, which defaults to text-embedding-ada-002. You shouldn't change the model for an existing database. For more information, see Usage.
maxChunkLength Optional. The maximum number of characters used for chunking the input. For more information, see Usage.
maxOverlap Optional. Gets or sets the maximum number of characters to overlap between chunks.
inputType Optional. Gets the type of the input.
connectionName The name of an app setting or environment variable that contains the connection string value. This property supports binding expressions.
collection The name of the collection or table or index to search. This property supports binding expressions.

Configuration

The binding supports these properties, which are defined in your code:

Property Description
input The input string for which to generate embeddings.
model Optional. The ID of the model to use, which defaults to text-embedding-ada-002. You shouldn't change the model for an existing database. For more information, see Usage.
maxChunkLength Optional. The maximum number of characters used for chunking the input. For more information, see Usage.
maxOverlap Optional. Gets or sets the maximum number of characters to overlap between chunks.
inputType Optional. Gets the type of the input.
connectionName The name of an app setting or environment variable that contains the connection string value. This property supports binding expressions.
collection The name of the collection or table or index to search. This property supports binding expressions.

Usage

See the Example section for complete examples.