【问题标题】:Microsoft LUIS: unable to set time zone (datetimeReference) for datetimeV2 entitiesMicrosoft LUIS:无法为 datetimeV2 实体设置时区 (datetimeReference)
【发布时间】:2020-05-07 12:13:03
【问题描述】:

我正在使用 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})

【问题讨论】:

  • 您已经链接到一个文档,该文档解释了 datetimeReference 应该采用 ISO 8601 格式并且应该进入请求正文,就像您在尝试 2 中所做的那样。什么究竟是不清楚的文件?如果文档有问题,那么您应该考虑将其作为反馈发布在页面本身上。但是,既然您已经在这里发布了一个问题,您能解释一下您期望发生的事情与实际发生的事情吗?
  • 该文档没有明确说明 datetimeReference 是什么(我已经发送了反馈),尽管我假设它是我们希望 LUIS 在解析相对时间表达式时将其视为“现在”的日期时间。例如:如果现在在我的时区是 2020-05-08T10:08:23,我假设将它作为 datetimeReference 与“在 10 分钟内”的查询一起传递将返回一个实体 datetimeV2,解析为 2020-05-08 10:18 :23。但事实并非如此。相反,LUIS 返回 2020-05-08 08:18:23,好像它认为我位于 UTC+0(它似乎完全忽略了我的 datetimeReference 选项)
  • 我刚刚将您问题中的 JSON 粘贴到 Postman 中,并在属性中填写了“10 分钟内”和“2020-05-08T10:08:23”。返回的实体如您预期的那样显示“2020-05-08T10:18:23”。您可以尝试在 Postman 中的请求进行验证吗?你能想到你可能会做不同的事情吗?您还没有显示如何处理响应的代码,那么问题可能是您将字符串从返回的实体转换为某种日期时间对象吗?
  • 在 Postman 中尝试了请求并看到它工作后,我修改了我的 Python 请求,发现我只是在错误的参数中传递了 json(数据而不是 json)。它现在完美运行,这也证实了我对 datetimeReference 参数的解释。感谢您的提示!
  • 这很奇怪。如果 LUIS 无法看到您的请求正文,我想知道它是如何读取您的查询的

标签: python azure chatbot azure-language-understanding luis.ai


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多