Azure OpenAI assistant post input binding for Azure Functions
Important
The Azure OpenAI extension for Azure Functions is currently in preview.
The Azure OpenAI assistant post input binding lets you send prompts to assistant chat bots.
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](../ai-services/openai/
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 POST function that sends user prompts to the assistant chat bot. The response to the prompt is returned in the HTTP response.
/// <summary>
/// HTTP POST function that sends user prompts to the assistant chat bot.
/// </summary>
[Function(nameof(PostUserQuery))]
public static async Task<IActionResult> PostUserQuery(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "assistants/{assistantId}")] HttpRequestData req,
string assistantId,
[AssistantPostInput("{assistantId}", "{Query.message}", Model = "%CHAT_MODEL_DEPLOYMENT_NAME%", ChatStorageConnectionSetting = DefaultChatStorageConnectionSetting, CollectionName = DefaultCollectionName)] AssistantState state)
{
return new OkObjectResult(state.RecentMessages.Any() ? state.RecentMessages[state.RecentMessages.Count - 1].Content : "No response returned.");
}
/// <summary>
/// HTTP GET function that queries the conversation history of the assistant chat bot.
/// </summary>
[Function(nameof(GetChatState))]
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)
{
This example demonstrates the creation process, where the HTTP POST function that sends user prompts to the assistant chat bot. The response to the prompt is returned in the HTTP response.
public HttpResponseMessage getChatState(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS,
route = "assistants/{assistantId}")
HttpRequestMessage<Optional<String>> request,
@BindingName("assistantId") String assistantId,
@AssistantQuery(name = "AssistantState", id = "{assistantId}", timestampUtc = "{Query.timestampUTC}", chatStorageConnectionSetting = DEFAULT_CHATSTORAGE, collectionName = DEFAULT_COLLECTION) AssistantState state,
final ExecutionContext context) {
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "application/json")
.body(state)
.build();
}
/*
* HTTP POST function that sends user prompts to the assistant chat bot.
*/
@FunctionName("PostUserResponse")
public HttpResponseMessage postUserResponse(
Examples aren't yet available.
This example demonstrates the creation process, where the HTTP POST function that sends user prompts to the assistant chat bot. The response to the prompt is returned in the HTTP response.
}
})
const assistantPostInput = input.generic({
type: 'assistantPost',
id: '{assistantId}',
model: '%CHAT_MODEL_DEPLOYMENT_NAME%',
userMessage: '{Query.message}',
chatStorageConnectionSetting: CHAT_STORAGE_CONNECTION_SETTING,
collectionName: COLLECTION_NAME
})
app.http('PostUserResponse', {
methods: ['POST'],
route: 'assistants/{assistantId}',
authLevel: 'anonymous',
extraInputs: [assistantPostInput],
handler: async (_, context) => {
const chatState: any = context.extraInputs.get(assistantPostInput)
This example demonstrates the creation process, where the HTTP POST function that sends user prompts to the assistant chat bot. The response to the prompt is returned in the HTTP response.
Here's the function.json file for post user query:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"route": "assistants/{assistantId}",
"methods": [
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
},
{
"name": "State",
"type": "assistantPost",
"direction": "in",
"dataType": "string",
"id": "{assistantId}",
"userMessage": "{Query.message}",
"model": "%CHAT_MODEL_DEPLOYMENT_NAME%",
"chatStorageConnectionSetting": "AzureWebJobsStorage",
"collectionName": "ChatState"
}
]
}
For more information about function.json file properties, see the Configuration section.
using namespace System.Net
param($Request, $TriggerMetadata, $State)
$recent_message_content = "No recent messages!"
if ($State.recentMessages.Count -gt 0) {
$recent_message_content = $State.recentMessages[0].content
}
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $recent_message_content
Headers = @{
"Content-Type" = "text/plain"
}
})
This example demonstrates the creation process, where the HTTP POST function that sends user prompts to the assistant chat bot. The response to the prompt is returned in the HTTP response.
response_json = {"assistantId": assistantId}
return func.HttpResponse(json.dumps(response_json), status_code=202, mimetype="application/json")
@apis.function_name("PostUserQuery")
@apis.route(route="assistants/{assistantId}", methods=["POST"])
@apis.assistant_post_input(arg_name="state", id="{assistantId}", user_message="{Query.message}", model="%CHAT_MODEL_DEPLOYMENT_NAME%", chat_storage_connection_setting=DEFAULT_CHAT_STORAGE_SETTING, collection_name=DEFAULT_CHAT_COLLECTION_NAME)
def post_user_response(req: func.HttpRequest, state: str) -> func.HttpResponse:
# Parse the JSON string into a dictionary
data = json.loads(state)
Attributes
Apply the PostUserQuery
attribute to define an assistant post input binding, which supports these parameters:
Parameter | Description |
---|---|
Id | The ID of the assistant to update. |
Model | The name of the OpenAI chat model to use. For Azure OpenAI, this value is the name of the model deployment. |
Annotations
The PostUserQuery
annotation enables you to define an assistant post input binding, which supports these parameters:
Element | Description |
---|---|
name | The name of the output binding. |
id | The ID of the assistant to update. |
model | The name of the OpenAI chat model to use. For Azure OpenAI, this value is the name of the model deployment. |
Decorators
During the preview, define the output binding as a generic_output_binding
binding of type postUserQuery
, which supports these parameters:
Parameter | Description |
---|---|
arg_name | The name of the variable that represents the binding parameter. |
id | The ID of the assistant to update. |
model | The name of the OpenAI chat model to use. For Azure OpenAI, this value is the name of the model deployment. |
Configuration
The binding supports these configuration properties that you set in the function.json file.
Property | Description |
---|---|
type | Must be PostUserQuery . |
direction | Must be out . |
name | The name of the output binding. |
id | The ID of the assistant to update. |
model | The name of the OpenAI chat model to use. For Azure OpenAI, this value is the name of the model deployment. |
Configuration
The binding supports these properties, which are defined in your code:
Property | Description |
---|---|
id | The ID of the assistant to update. |
model | The name of the OpenAI chat model to use. For Azure OpenAI, this value is the name of the model deployment. |
Usage
See the Example section for complete examples.