Azure Functions 的 Azure OpenAI 助手查询输入绑定

重要

适用于 Azure Functions 的 Azure OpenAI 扩展目前为预览版。

利用 Azure OpenAI 助手查询输入绑定,可以将助手 API 查询集成到代码执行中。

要了解 Azure OpenAI 扩展的设置和配置详细信息,请参阅适用于 Azure Functions 的 Azure OpenAI 扩展。 若要了解有关 Azure OpenAI 助手的详细信息,请参阅 Azure OpenAI 助手 API

注意

仅提供了适用于 Node.js v4 模型的参考和示例。

注意

仅提供了适用于 Python v2 模型的参考和示例。

注意

虽然支持这两个 C# 进程模型,但仅 提供了独立的辅助角色模型 示例。

示例

此示例演示了创建过程,该过程中 HTTP GET 函数查询助手聊天机器人的对话历史记录。 对提示的回复在 HTTP 响应中返回。

/// <summary>
/// HTTP GET function that queries the conversation history of the assistant chat bot.
/// </summary>
[Function(nameof(GetChatState))]
public static 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);
}

此示例演示了创建过程,该过程中 HTTP GET 函数查询助手聊天机器人的对话历史记录。 对提示的回复在 HTTP 响应中返回。

/*
 * HTTP GET function that queries the conversation history of the assistant chat bot.
 */   
@FunctionName("GetChatState")
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 GET 函数查询助手聊天机器人的对话历史记录。 对提示的回复在 HTTP 响应中返回。

const { app, input, output } = require("@azure/functions");

const chatBotQueryInput = input.generic({
    type: 'assistantQuery',
    id: '{assistantId}',
    timestampUtc: '{Query.timestampUTC}',
    chatStorageConnectionSetting: CHAT_STORAGE_CONNECTION_SETTING,
    collectionName: COLLECTION_NAME
})
app.http('GetChatState', {
    methods: ['GET'],
    route: 'assistants/{assistantId}',
    authLevel: 'anonymous',
    extraInputs: [chatBotQueryInput],
    handler: async (_, context) => {
        const state = context.extraInputs.get(chatBotQueryInput)
        return { status: 200, jsonBody: state }
    }
})

此示例演示了创建过程,该过程中 HTTP GET 函数查询助手聊天机器人的对话历史记录。 对提示的回复在 HTTP 响应中返回。

import { HttpRequest, InvocationContext, app, input, output } from "@azure/functions"

const chatBotQueryInput = input.generic({
    type: 'assistantQuery',
    id: '{assistantId}',
    timestampUtc: '{Query.timestampUTC}',
    chatStorageConnectionSetting: CHAT_STORAGE_CONNECTION_SETTING,
    collectionName: COLLECTION_NAME
})
app.http('GetChatState', {
    methods: ['GET'],
    route: 'assistants/{assistantId}',
    authLevel: 'anonymous',
    extraInputs: [chatBotQueryInput],
    handler: async (_, context) => {
        const state: any = context.extraInputs.get(chatBotQueryInput)
        return { status: 200, jsonBody: state }
    }
})

此示例演示了创建过程,该过程中 HTTP GET 函数查询助手聊天机器人的对话历史记录。 对提示的回复在 HTTP 响应中返回。

下面是 获取聊天状态的function.json 文件:

{
  "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"
    }
  ]
}

有关 function.json 文件属性的详细信息,请参阅配置部分。

using namespace System.Net

param($Request, $TriggerMetadata, $State)

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

此示例演示了创建过程,该过程中 HTTP GET 函数查询助手聊天机器人的对话历史记录。 对提示的回复在 HTTP 响应中返回。

@apis.function_name("GetChatState")
@apis.route(route="assistants/{assistantId}", methods=["GET"])
@apis.assistant_query_input(
    arg_name="state",
    id="{assistantId}",
    timestamp_utc="{Query.timestampUTC}",
    chat_storage_connection_setting=DEFAULT_CHAT_STORAGE_SETTING,
    collection_name=DEFAULT_CHAT_COLLECTION_NAME,
)
def get_chat_state(req: func.HttpRequest, state: str) -> func.HttpResponse:
    return func.HttpResponse(state, status_code=200, mimetype="application/json")

特性

应用 AssistantQuery 特性以定义助手查询输入绑定,该绑定支持以下参数:

参数 说明
Id 要查询的助手的 ID。
TimeStampUtc 可选。 获取或设置要提取的聊天历史记录中最早消息的时间戳。 时间戳应采用 ISO 8601 格式,例如 2023-08-01T00:00:00Z。

批注

通过 assistantQuery 注释,可定义助手查询输入绑定,该绑定支持以下参数:

元素 说明
名字 获取或设置输入绑定的名称。
id 要查询的助手的 ID。
timeStampUtc 可选。 获取或设置要提取的聊天历史记录中最早消息的时间戳。 时间戳应采用 ISO 8601 格式,例如 2023-08-01T00:00:00Z。

修饰符

在预览期间,将输入绑定定义为 generic_input_binding 类型的 assistantQuery 绑定,该绑定支持以下参数:

参数 说明
arg_name 表示绑定参数的变量的名称。
id 要查询的助手的 ID。
time_stamp_utc 可选。 获取或设置要提取的聊天历史记录中最早消息的时间戳。 时间戳应采用 ISO 8601 格式,例如 2023-08-01T00:00:00Z。

配置

绑定支持在 function.json 文件中设置的这些配置属性。

properties 说明
type 必须是 assistantQuery
方向 必须是 in
名字 输入绑定的名称。
id 要查询的助手的 ID。
timeStampUtc 可选。 获取或设置要提取的聊天历史记录中最早消息的时间戳。 时间戳应采用 ISO 8601 格式,例如 2023-08-01T00:00:00Z。

配置

绑定支持以下属性,这些属性在代码中定义:

properties 说明
id 要查询的助手的 ID。
timeStampUtc 可选。 获取或设置要提取的聊天历史记录中最早消息的时间戳。 时间戳应采用 ISO 8601 格式,例如 2023-08-01T00:00:00Z。

使用情况

有关完整示例,请参阅示例部分