重要
適用於 Azure Functions 的 Azure OpenAI 延伸模組目前為預覽狀態。
Azure OpenAI 助理查詢輸入繫結可讓您將助理 API 查詢整合到程式碼執行中。
如需 Azure OpenAI 延伸模組的安裝和設定詳細資訊,請參閱適用於 Azure Functions 的 Azure OpenAI 延伸模組。 若要深入了解 Azure OpenAI 助理,請參閱 Azure OpenAI 助理 API。
注意
參考和範例僅適用於 Node.js v4 模型。
注意
參考和範例僅適用於 Python v2 模型。
注意
雖然支援這兩個 C# 進程模型,但只會 提供隔離的背景工作模型 範例。
範例
目前這款綁定沒有 Go 支援。
此範例示範建立程序,其中 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.Function, "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.FUNCTION,
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: 'function',
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: 'function',
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 屬性來定義助理查詢輸入繫結,其支援下列參數:
| 參數 | 描述 |
|---|---|
| 標識碼 | 取得要查詢之助理的識別碼。 |
| 時間戳UTC | 選擇性。 取得或設定要擷取之聊天歷程記錄中最早訊息的時間戳記。 時間戳應為 ISO 8601 格式,例如 2023-08-01T00:00:00Z。 |
註釋
assistantQuery 註釋可讓您定義助理查詢輸入繫結,其支援下列參數:
| 元素 | 描述 |
|---|---|
| 名字 | 取得或設定輸入繫結的名稱。 |
| 識別碼 | 取得要查詢之助理的識別碼。 |
| 時間戳UTC | 選擇性。 取得或設定要擷取之聊天歷程記錄中最早訊息的時間戳記。 時間戳應為 ISO 8601 格式,例如 2023-08-01T00:00:00Z。 |
裝飾項目
在預覽期間,將輸入繫結定義為 generic_input_binding 類型的 assistantQuery 繫結,其支援下列參數:
| 參數 | 描述 |
|---|---|
| arg_name | 代表繫結參數的變數名稱。 |
| 識別碼 | 取得要查詢之助理的識別碼。 |
| time_stamp_utc | 選擇性。 取得或設定要擷取之聊天歷程記錄中最早訊息的時間戳記。 時間戳應為 ISO 8601 格式,例如 2023-08-01T00:00:00Z。 |
組態
繫結支援您在 function.json 檔案中設定的下列組態屬性。
| 屬性 | 描述 |
|---|---|
| 類型 | 必須是 assistantQuery。 |
| 方向 | 必須是 in。 |
| 名字 | 輸入繫結的名稱。 |
| 識別碼 | 取得要查詢之助理的識別碼。 |
| 時間戳UTC | 選擇性。 取得或設定要擷取之聊天歷程記錄中最早訊息的時間戳記。 時間戳應為 ISO 8601 格式,例如 2023-08-01T00:00:00Z。 |
組態
繫結支援您在程式碼中定義的下列屬性:
| 屬性 | 描述 |
|---|---|
| 識別碼 | 取得要查詢之助理的識別碼。 |
| 時間戳UTC | 選擇性。 取得或設定要擷取之聊天歷程記錄中最早訊息的時間戳記。 時間戳應為 ISO 8601 格式,例如 2023-08-01T00:00:00Z。 |
使用方式
如需完整範例,請參閱範例一節。
連結
要使用 Azure OpenAI 綁定擴充功能,你需要指定一個連接到 OpenAI 模型定義的連結。 請使用以下其中一種方法在綁定中設定 OpenAI 模型連線:
- 使用
AIConnectionNamebinding 屬性(Azure OpenAI 偏好)。 - 在應用程式內設定並設定
AZURE_OPENAI_ENDPOINTAZURE_OPENAI_KEY(針對 Azure OpenAI)。 - 只
Open_API_Key在應用程式設定中設定(針對https://api.openai.com)。
連線的設定方式取決於模型 API 與認證方法,如下表所示:
| 認證/模型 API | Azure OpenAI | OpenAI (https://api.openai.com) |
|---|---|---|
| 管理身份連線 | AIConnectionName |
不支援 |
| 金鑰保存庫 參考 | AZURE_OPENAI_ENDPOINTAZURE_OPENAI_KEY |
Open_API_Key |
| 應用程式配置參考 | AZURE_OPENAI_ENDPOINTAZURE_OPENAI_KEY |
Open_API_Key |
| 共用密碼 | AZURE_OPENAI_ENDPOINTAZURE_OPENAI_KEY |
Open_API_Key |
使用基於管理身份的連線和屬性 AIConnectionName 。
當你使用 AIConnectionName時,這個屬性設定的值會依連接類型而異:
-
管理身份連線:此
AIConnectionName屬性由<CONNECTION_NAME_PREFIX>一組設定共享,這些設定共同定義了與 Azure OpenAI 的身份基礎連線。 欲了解更多資訊,請參閱定義身份連結。 -
金鑰保存庫 參考:
AIConnectionName屬性設定回傳一個 Azure Key Vault 參考,指向 API 金鑰集中維護的位置。 欲了解更多資訊,請參閱定義 金鑰保存庫 連接。 -
App Configuration 參考:
AIConnectionName屬性設定會回傳一個 Azure 應用程式組態 參考,該參考會回傳 API 金鑰或 金鑰保存庫 參考。 欲了解更多資訊,請參閱連接條目中的 Azure 應用程式組態。 -
API 金鑰:
AIConnectionName屬性設定會解析成包含端點與金鑰的應用程式設定。 由於共享金鑰可能被入侵,請盡可能使用受管理身份連線。 欲了解更多資訊,請參閱定義連結。
想了解更多關於綁定連接的資訊,請參閱 Azure Functions 中的 Manage connections 。
OpenAI 綁定包含一個AIConnectionName屬性,你可以用來指定定義與 Azure OpenAI 連結的應用程式設定群組:<ConnectionNamePrefix>
當你將屬性設定 AIConnectionName 為 myAzureOpenAI:
myAzureOpenAI__endpoint=https://contoso.openai.azure.com/myAzureOpenAI__credential=managedidentitymyAzureOpenAI__clientId=aaaaaaaa-bbbb-cccc-1111-222222222222
執行時,主機將這些設定解讀為單一 myAzureOpenAI 設定:
"myAzureOpenAI":
{
"endpoint": "https://contoso.openai.azure.com/",
"credential": "managedidentity",
"clientId": "aaaaaaaa-bbbb-cccc-1111-222222222222"
}
使用受管理身份時,務必將你的身份加入認知 服務 OpenAI 使用者 角色。
本地執行時,請將這些設定加入 local.settings.json 專案檔案。 如需詳細資訊,請參閱 使用身分識別型連線進行本機開發。
如需詳細資訊,請參閱使用應用程式設定。