Azure OpenAI assistant query input binding for Azure Functions

Important

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

The Azure OpenAI assistant query input binding allows you to integrate Assistants API queries into your code executions.

For information on setup and configuration details of the Azure OpenAI extension, see Azure OpenAI extensions for Azure Functions. To learn more about Azure OpenAI assistants, see Azure OpenAI Assistants API.

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 demonstrates the creation process, where the HTTP GET function that queries the conversation history of the assistant chat bot. The response to the prompt is returned in the HTTP response.

    public static async Task<IActionResult> GetChatState(
       [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "assistants/{assistantId}")] HttpRequestData req,
       string assistantId,
       [AssistantQueryInput("{assistantId}", TimestampUtc = "{Query.timestampUTC}", ChatStorageConnectionSetting = DefaultChatStorageConnectionSetting, CollectionName = DefaultCollectionName)] AssistantState state)
    {
        return new OkObjectResult(state);
    }
}

This example demonstrates the creation process, where the HTTP GET function that queries the conversation history of the assistant chat bot. The response to the prompt is returned in the HTTP response.

                "Ask for clarification if a user request is ambiguous.";

        AssistantCreateRequest assistantCreateRequest = new AssistantCreateRequest(assistantId, instructions);
        assistantCreateRequest.setChatStorageConnectionSetting(DEFAULT_CHATSTORAGE);
        assistantCreateRequest.setCollectionName(DEFAULT_COLLECTION);

        message.setValue(assistantCreateRequest);
        JSONObject response = new JSONObject();
        response.put("assistantId", assistantId);
        
        return request.createResponseBuilder(HttpStatus.CREATED)
            .header("Content-Type", "application/json")
            .body(response.toString())
            .build();    
}

Examples aren't yet available.

This example demonstrates the creation process, where the HTTP GET function that queries the conversation history of the assistant chat bot. The response to the prompt is returned in the HTTP response.

            }
        };
    }
})


const chatBotQueryInput = input.generic({
    type: 'assistantQuery',
    id: '{assistantId}',
    timestampUtc: '{Query.timestampUTC}',
    chatStorageConnectionSetting: CHAT_STORAGE_CONNECTION_SETTING,
    collectionName: COLLECTION_NAME
})
app.http('GetChatState', {
    methods: ['GET'],

This example demonstrates the creation process, where the HTTP GET function that queries the conversation history of the assistant chat bot. The response to the prompt is returned in the HTTP response.

Here's the function.json file for Get Chat State:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "route": "assistants/{assistantId}",
      "methods": [
        "get"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    },
    {
      "name": "State",
      "type": "assistantQuery",
      "direction": "in",
      "dataType": "string",
      "id": "{assistantId}",
      "timestampUtc": "{Query.timestampUTC}",
      "chatStorageConnectionSetting": "AzureWebJobsStorage",
      "collectionName": "ChatState"
    }
  ]
}

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

using namespace System.Net

param($Request, $TriggerMetadata, $State)

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

This example demonstrates the creation process, where the HTTP GET function that queries the conversation history of the assistant chat bot. The response to the prompt is returned in the HTTP response.

    recent_message_content = data['recentMessages'][0]['content']
    return func.HttpResponse(recent_message_content, status_code=200, mimetype="text/plain")


@apis.function_name("GetChatState")

Attributes

Apply the AssistantQuery attribute to define an assistant query input binding, which supports these parameters:

Parameter Description
Id Gets the ID of the assistant to query.
TimeStampUtc Optional. Gets or sets the timestamp of the earliest message in the chat history to fetch. The timestamp should be in ISO 8601 format - for example, 2023-08-01T00:00:00Z.

Annotations

The assistantQuery annotation enables you to define an assistant query input binding, which supports these parameters:

Element Description
name Gets or sets the name of the input binding.
id Gets the ID of the assistant to query.
timeStampUtc Optional. Gets or sets the timestamp of the earliest message in the chat history to fetch. The timestamp should be in ISO 8601 format - for example, 2023-08-01T00:00:00Z.

Decorators

During the preview, define the input binding as a generic_input_binding binding of type assistantQuery, which supports these parameters:

Parameter Description
arg_name The name of the variable that represents the binding parameter.
id Gets the ID of the assistant to query.
time_stamp_utc Optional. Gets or sets the timestamp of the earliest message in the chat history to fetch. The timestamp should be in ISO 8601 format - for example, 2023-08-01T00:00:00Z.

Configuration

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

Property Description
type Must be assistantQuery.
direction Must be in.
name The name of the input binding.
id Gets the ID of the assistant to query.
timeStampUtc Optional. Gets or sets the timestamp of the earliest message in the chat history to fetch. The timestamp should be in ISO 8601 format - for example, 2023-08-01T00:00:00Z.

Configuration

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

Property Description
id Gets the ID of the assistant to query.
timeStampUtc Optional. Gets or sets the timestamp of the earliest message in the chat history to fetch. The timestamp should be in ISO 8601 format - for example, 2023-08-01T00:00:00Z.

Usage

See the Example section for complete examples.