我已经构建了一个 Azure AI GPT4 模型,并且我已经开始通过 API 调用正常使用它,一切都运行良好。我最近创建了一个 Azure 搜索和 ML 索引,以便在聊天中为其提供一些自定义数据。这一切都运行良好,并且可以在 Azure AI Studio 的 Playground 中运行。
但是,当我使用提供的 JSON 代码来设置我的 API 时,我从 API 收到以下返回:
{
"error": {
"requestid": "<request_id>",
"code": 400,
"message": "Failed to access workspace /subscriptions/<subscription_guid>/resourceGroups/<resource_group_name>/providers/Microsoft.MachineLearningServices/workspaces/<workspace_name> due to invalid token. Response: {\"error\":{\"code\":\"InvalidAuthenticationToken\",\"message\":\"The access token is invalid.\"}}"
}
}
Microsoft 在 Azure AI Studio 游乐场中提供的代码告诉您使用
access_token
身份验证,但实际上并不提供值或参数。我在 this 文档中找到了语法,并添加了我的想法,即提供的 access_token
,但它总是失败。
我已经搜索了我能找到的所有文档,并通过 Azure 门户,但我找不到
access token
应该包含在哪里。
如果我将 API 设置为使用 AI 搜索而不是 Azure ML 索引,它可以工作,但不使用 ML 索引,这是不对的。
如何才能使其正常工作,或者在哪里可以找到或生成访问令牌?
下面是我的 API 调用的完整(混淆)代码:
{
"data_sources": [
{
"type": "azure_ml_index",
"parameters": {
"project_resource_id": "/subscriptions/<subscription_guid>/resourceGroups/<resource_group_name>/providers/Microsoft.MachineLearningServices/workspaces/<workspace_name>",
"name": "<index name>",
"version": "1",
"authentication": {
"type": "access_token",
"access_token": "<UNKNOWN>"
},
"query_type": "vector",
"in_scope": true,
"role_information": "<system prompt>",
"strictness": 3,
"top_n_documents": 5,
"endpoint": "<azure search endpoint>",
"key": "<azure search key>",
"indexName": "<gpt4 index name>"
}
}
],
"messages": [
{
"role": "system",
"content": "<system prompt>"
},
{
"role": "user",
"content": "<content prompt>"
},
{
"role": "assistant",
"content": "<response prompt>"
}
],
"deployment": "<gpt4model>",
"temperature": 0.5,
"top_p": 1,
"max_tokens": 1800,
"stop": null,
"stream": true,
"frequency_penalty": 0.25,
"presence_penalty": 0.35,
"azureSearchEndpoint": "<azure search endpoint>",
"azureSearchKey": "<azure search key>"
}
您需要拥有访问令牌才能访问机器学习工作区中的资源。
要获取令牌并在 POST 请求中使用它,请使用下面的 Azure CLI 命令:
token_response=$(az account get-access-token --resource=https://management.core.windows.net/)
access_token=$(echo "$token_response" | jq -r '.accessToken')
az rest --method POST --uri https://jgsopenai.openai.azure.com/openai/deployments/tst/chat/completions?api-version=2024-02-15-preview --resource https://cognitiveservices.azure.com/ --body '
{
"data_sources": [
{
"type": "azure_ml_index",
"parameters": {
"project_resource_id": "/subscriptions/b83c1ed3-xxxxxxxxxx/resourceGroups/<xxx-yyy>/providers/Microsoft.MachineLearningServices/workspaces/<ml-workspace-name>",
"name": "maroon-gyro-lw5ss1dmjt",
"version": "1",
"authentication": {
"type": "access_token",
"access_token": "'"$access_token"'"
}
}
}
],
"messages": [
{
"role": "user",
"content": "What is the amount due paid for the current bill?"
}
]
}
'
在这里,我首先获取访问令牌并在 POST 请求中提供它。另外,请确保您已在
bashCLI 中完成了
az login
。
输出:
您还可以使用服务主体获取令牌。按照 this 使用服务主体获取令牌。
注意:我强烈建议使用托管身份而不是令牌。