1

我正在使用 V3 API 从 LUIS 端点获取预测,我需要一种方法来告诉 LUIS 我的时区,以便通过以下方式正确解析相对时间表达式(例如“过去两个小时内”、“10 分钟内”) datetimeV2 实体。

如果我将 V2 API 与 timezoneOffset 选项一起使用,一切都会完美运行,但我无法使 V3 API 与新选项 datetimeReference(它应该替换 timezoneOffset)一起工作。实际上,我什至不知道应该为 datetimeReference 设置哪个值(整数?日期时间?)。

这是我对 Python 的尝试。谁能告诉我有什么问题吗?

from datetime import datetime
import requests

appId           = # my app id
subscriptionKey = # my subscription key

query = "tra 10 minuti" # = "in 10 minutes" (my app speaks Italian)

# ATTEMPT 1
# based on https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-concept-data-alteration?tabs=V2#change-time-zone-of-prebuilt-datetimev2-entity,
# assuming it works the same way as timezoneOffset
endpoint = 'https://westeurope.api.cognitive.microsoft.com/luis/prediction/v3.0/apps/{appId}/slots/staging/predict?datetimeReference=120&subscription-key={subscriptionKey}&query={query}'
endpoint = endpoint.format(appId = appId, subscriptionKey = subscriptionKey, query = query)

response = requests.get(endpoint)

# ATTEMPT 2
# according to https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-migration-api-v3
endpoint = 'https://westeurope.api.cognitive.microsoft.com/luis/prediction/v3.0/apps/{appId}/slots/staging/predict?'
endpoint = endpoint.format(appId = appId)

json = {
    "query" : query,
    "options":{
        "datetimeReference": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), # e.g. "2020-05-07T13:54:33". Not clear if that's what it wants
        "preferExternalEntities": True
    },
    "externalEntities":[],
    "dynamicLists":[]
}

response = requests.post(endpoint, json, headers = {'Ocp-Apim-Subscription-Key' : subscriptionKey})

更新:在 ATTEMPT 2 中发送请求的正确方法是

response = requests.post(endpoint, json = json, headers = {'Ocp-Apim-Subscription-Key' : subscriptionKey})
4

1 回答 1

0

正如您所发现的,您的 JSON 应该放在json参数中,而不是data参数中:

response = requests.post(endpoint, json = json, headers = {'Ocp-Apim-Subscription-Key' : subscriptionKey})
于 2020-05-11T17:18:07.767 回答