我有一个Flutter应用程序,可以将用户查询发送到Dialogflow。我的目标不仅是发送来自用户的查询,还包括从我的应用程序设置上下文,以便Dialogflow根据给定的上下文匹配意图。
当我测试下面的代码时,它返回一个aiResponse的实例:
{responseId: null, queryResult: null, webhookStatus: null}
但是其中的每个元素都是空的,所以有些东西不能正常工作。我希望看到一个包含所有必要信息的结果,最重要的是queryResult.fulfillmentText,这是来自Dialogflow的答案。
我使用这个插件:https://pub.dev/packages/flutter_dialogflow_v2
这是我的代码:
List<Context> contexts = [
Context(name: 'start', lifespanCount: 10),
];
DetectIntentResponse aiResponse = await dialogflow.detectIntent(
DetectIntentRequest(
queryInput: QueryInput(
text: TextInput(
text: finalQuery,
languageCode: Language.english,
)),
queryParams: QueryParameters(
contexts: contexts,
),
),
);
print(aiResponse.queryResult.fulfillmentText); //throws error because queryResult is null
我非常确定它不能工作,因为我在其中设置了上下文。当我从我的代码中删除它们时,它工作得很好。也许我设置上下文列表的方式是错误的?
我还可以在Dialogflow中的fulfillment函数中设置上下文。然后,我必须向Dialogflow发送一个查询,这会触发一个意图,然后为来自用户的实际查询设置上下文。这不是我想要的方式,因为插件已经提供了这个功能。
我希望有人可以帮助我解决我的问题,因为我不确定是插件还是只有我。
发布于 2021-05-27 17:16:02
所以我修好了它。我现在使用的是一个不同的插件,但它可能也适用于我以前使用过的插件。
我现在使用的是dialog_flowtter:
dialog_flowtter: ^0.2.0
这是我的代码:
void response(query) async {
DialogAuthCredentials credentials =
await DialogAuthCredentials.fromFile("assets/service.json");
final DialogFlowtter dialogFlowtter = DialogFlowtter(
credentials: credentials,
sessionId: "12345678",
);
final QueryInput queryInput = QueryInput(
text: TextInput(
text: query,
languageCode: "en",
),
);
final params = QueryParameters(
contexts: [
Context(
name:
"projects/{project_id=*}/locations/{location_id=*}/agent/environments/{environment_id=*}/users/{user_id=*}/sessions/{session_id=*}/contexts/start",
lifespanCount: 10),
],
);
DetectIntentResponse response = await dialogFlowtter.detectIntent(
queryInput: queryInput,
queryParams: params,
);
print(response.text);
}
上下文名称位于上下文名称字符串的末尾,我将其命名为"start“,生命周期为10。
https://stackoverflow.com/questions/67720695
复制相似问题