8000 basic communication flow with questions · SashaKryzh/Book-AI@1b1e7b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
basic communication flow with questions
Browse files Browse the repository at this point in the history
  • Loading branch information
denitdao committed Jan 23, 2022
1 parent da66d6a commit 1b1e7b8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 51 deletions.
11 changes: 5 additions & 6 deletions lib/models/book_types.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
enum BookType {
undefined,
procrastination,
anxiety,
communicationProblems,
workLifeBalance,
selfCare,
burnout,
eatingDisorder,
// burnout,
// anxiety,
// communicationProblems,
// workLifeBalance,
// eatingDisorder,
}
2 changes: 1 addition & 1 deletion lib/models/chat_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ChatResponse {
ChatResponse(
{required this.message,
required this.parameters,
this.sentiment = 0.5,
this.sentiment = 0,
this.isFinal = false,
this.isError = false});
}
2 changes: 1 addition & 1 deletion lib/models/dialogue_summary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:int20h_app/models/mood_types.dart';

class DialogueSummary {
MoodType moodType = MoodType.undefined;
BookType bookType = BookType.undefined;
BookType? bookType;
double overallSentiment = 0.5;
bool isFinalReached = false;
List<String> bookList = ['Book1', 'Book2', 'Book3'];
Expand Down
34 changes: 24 additions & 10 deletions lib/services/dialogflow_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:ffi';

import 'package:dialog_flowtter/dialog_flowtter.dart';
import 'package:injectable/injectable.dart';
import 'package:int20h_app/models/book_types.dart';
Expand All @@ -16,14 +14,8 @@ class DialogFlowService {
}

Future<ChatResponse> sendIntent(
String userMessage, bool isQuestionContext) async {
String userMessage) async {
final response = await (await _dialogFlowInstance).detectIntent(
queryParams: QueryParameters(
contexts: isQuestionContext
? [
Context(name: 'myQuestion', lifespanCount: 1),
]
: null),
queryInput: QueryInput(
text: TextInput(text: userMessage, languageCode: 'en'),
),
Expand All @@ -44,7 +36,7 @@ class DialogFlowService {
var isEnd =
response.queryResult?.diagnosticInfo?['end_conversation'] ?? false;

var isError = response.queryResult?.action == 'input.welcome';
var isError = response.queryResult?.action == 'input.unknown';

return ChatResponse(
message: textResponse,
Expand All @@ -54,4 +46,26 @@ class DialogFlowService {
isError: isError,
);
}

Future<ChatResponse> triggerBookIntent(BookType bookType) async {
final response = await (await _dialogFlowInstance).detectIntent(
queryInput: QueryInput(
text: TextInput(text: bookType.name, languageCode: 'en'),
),
);

var textResponse = response.text ?? 'Error';
if (response.message == null) {
return ChatResponse(
message: textResponse,
parameters: Map.identity(),
isError: true,
);
}

return ChatResponse(
message: textResponse,
parameters: Map.identity(),
);
}
}
79 changes: 46 additions & 33 deletions lib/services/dialogue_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import 'package:flutter_chat_types/flutter_chat_types.dart';
class DialogueService {
DialogueService(this._dialogFlowService);

List<BookType> _userQuestionTypes = BookType.values;
Map<BookType, double> _userWeights = Map.fromEntries([
MapEntry(BookType.burnout, 0),
MapEntry(BookType.procrastination, 0),
MapEntry(BookType.workLifeBalance, 0),
MapEntry(BookType.selfCare, 0),
// MapEntry(BookType.workLifeBalance, 0),
]);

List<MapEntry<String, BookType>> _userQuestions = List.from([
MapEntry("Do you feel like burnout?", BookType.burnout),
MapEntry("Do you feel like procrastination?", BookType.procrastination),
MapEntry("Do you feel like workLifeBalance?", BookType.workLifeBalance),
]);
bool isQuestioning = false;
bool isWaitingForResponse = false;

var questionNumber = 0;

Expand All @@ -38,22 +36,37 @@ class DialogueService {
void sendMessage(String text) async {
_addUserMessage(text);

if (questionNumber == _userQuestions.length || _summary.isFinalReached) {
// find result
_addFinalAIMessage();
return;
}
if (isQuestioning) {
var answer = await _dialogFlowService.sendIntent(text);

if (answer.isError) {
_addAIMessage(answer.message); // say that didn't get it
isWaitingForResponse = true;
} else {
_processAnswer(answer);

if (!isWaitingForResponse) {
questionNumber++;

ChatResponse answer = await _dialogFlowService.sendIntent(text, true);
if (answer.isError) {
_addAIMessage(answer.message);
// ask question again
_addAIMessage(_userQuestions[questionNumber].key);
if (questionNumber == _userQuestionTypes.length) {
isQuestioning = false;
_addFinalAIMessage();
return;
}
}
}
} else {
_processAnswer(answer); // update weights here
questionNumber++;
_addAIMessage(answer.message);
if (_isEnoughDataAboutUser()) {
var answer = await _dialogFlowService.sendIntent(text);
_addAIMessage(answer.message);
return;
} else {
isQuestioning = true;
}
}
var question = await _dialogFlowService
.triggerBookIntent(_userQuestionTypes[questionNumber]);
_addAIMessage(question.message);
}

Future<List<Message>> getMessages() async {
Expand All @@ -62,26 +75,30 @@ class DialogueService {

void _processAnswer(ChatResponse answer) {
if (answer.parameters.isNotEmpty) {
var agreeParam = answer.parameters['agree'] ?? false;
var agreeParam = answer.parameters['questionResult'];

if (agreeParam) {
var type = _userQuestions[questionNumber].value;
if (agreeParam == 'true') {
var type = _userQuestionTypes[questionNumber];
_userWeights[type] = (_userWeights[type] ?? 0) + 1;
}

isWaitingForResponse = false;
}

if (answer.isFinal) {
_summary.isFinalReached = true;
}
// _addAIMessage("Your sentiment is " + answer.sentiment.toString());

// todo get all the data about parameters, mood, sentiment...
}

bool _isEnoughDataAboutUser() {
return _summary.isFinalReached &&
_summary.moodType != MoodType.undefined &&
_summary.bookType != BookType.undefined;
return questionNumber == _userQuestionTypes.length;
}

void _addFinalAIMessage() {
_addAIMessage("I've collected enough data.");
_addAIMessage("You can say 'Finish' to get the result");
_addAIMessage('Or continue with the small talk :)');
_addMessage(_userWeights.toString(), aiUser);
}

void _addUserMessage(String message) {
Expand All @@ -92,10 +109,6 @@ class DialogueService {
_addMessage(message, aiUser);
}

void _addFinalAIMessage() {
_addMessage("Я собрал достаточно параметров", aiUser);
}

void _addMessage(String message, User user) {
var object = TextMessage(
author: user,
Expand Down

0 comments on commit 1b1e7b8

Please sign in to comment.
0