与Angular的对话流实现集成没有提供正确的回复



与Angular集成的对话流聊天机器人无法提供正确的回复。

我已经将Dialog流与Angular集成。我正在使用Firebase作为后端。遵循此链接中的命令https://firebase.google.com/docs/functions/get-started?authuser=2

聊天机器人在前三个意图中都能正常工作,但之后它就不会提供经过训练的回复了。我是个初学者,不知道哪里出了问题。

这是我集成到我的Angular项目中的index.js文件

'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function checkHousingProblems(agent) {
let HousingProblems = agent.parameters.HousingProblems;
if (HousingProblems=='2'){
agent.add('Do you live in residence or off campus?');
}else if (HousingProblems=='3'){
agent.add('Do you live in residence or off campus?');
}else if (HousingProblems=='quite a bit'){
agent.add('Do you live in residence or off campus?');
}else if (HousingProblems=='extremely'){
agent.add('Do you live in residence or off campus?');
}else {
agent.add('How much do the following issues contribute to your stress? • Legal issues  0 = not at all  1 = somewhat  2 = quite a bit  3 = extremely');
}
}
function checkHousingStatus(agent) {
let HousingStatus = agent.parameters.HousingStatus;
if (HousingStatus=='residence'){
agent.add('For concerns about residence contact residence services.');
}else if (HousingStatus=='off campus'){
agent.add('For concerns related to living off campus');
}
agent.add('How much do the following issues contribute to your stress? • Legal issues 0 = not at all  1 = somewhat  2 = quite a bit  3 = extremely');
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('LifeProblemsQ2', checkHousingProblems);
intentMap.set('LifeProblemsQ2PartB', checkHousingStatus);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});

这是我的聊天服务代码

export class ChatService {
private token = environment.dialogflow.sciofreliefbot;
private client = new ApiAiClient({accessToken: this.token});
conversation = new BehaviorSubject<Message[]>([]);
constructor() { }
update(msg: Message) {
this.conversation.next([msg]);
}
converse(msg: string) {
const userMessage = new Message(msg, 'user');
this.update(userMessage);
return this.client.textRequest(msg)
.then(res => {
const speech = res.result.fulfillment.speech;
const botMessage = new Message(speech, 'bot');
this.update(botMessage);
});
}
talk() {
this.client.textRequest('Hi')
.then(res => console.log(res));
}

聊天-数字组件.ts代码

export class ChatDialogComponent implements OnInit {
messages: Observable<Message[]>;
formValue: string;
constructor(public chat: ChatService) { }
ngOnInit() {
this.messages = this.chat.conversation.asObservable()
.scan((acc, val) => acc.concat(val));
}
sendMessage() {
this.chat.converse(this.formValue);
this.formValue = '';
}
}

chat-dialog.component.html代码

<h1> Sci of Relief Chat bot</h1>
<ng-container *ngFor="let message of messages | async">
<div class="message" [ngClass]="{'from': message.sentBy === 'bot',
'to': message.sentBy === 'user'}">
{{ message.content }}
</div>
</ng-container>
<label for="nameField"> Your Message </label>
<input [(ngModel)] ="formValue" (keyup.enter)="sendMessage()" type="text"><br>
<button (click)="sendMessage()">Send</button> 

当我在对话框流中使用webdemo链接时,我得到了以下输出

用户:3

博特:你住在宿舍还是校外?

用户:住宅

Bot:有关住宅的问题,请联系住宅服务。

当我在我的Angular网站上使用时,我在"住宅"后没有得到回复

如果有人能告诉我哪里出了问题,那会很有帮助。

如果没有原始交互日志,这是非常困难的。但也许每次调用DialogFlow时,您的会话ID都不同。将SessionId硬代码放在ChatService类的ApiAiClient的构造函数上。

export class ChatService {
private token = environment.dialogflow.sciofreliefbot;
private client = new ApiAiClient({accessToken: this.token, sessionId  : 'sessionId' });
...
}

如果有效,请检查ChatService类的生命周期,以便在每个Angular应用程序上只创建一次。或者发送与客户端相关的sessionId(如cookie(

错误是由于Dialog流fullfilment函数将两个响应作为单个文本返回,而我为显示它们而编写的函数无法识别它们。我通过更改履行功能以返回正确的消息来解决此问题。

最新更新