hammaad commited on
Commit
f87b457
·
1 Parent(s): 80d1dad

updated the fronend with the streaming effect

Browse files
src/app/features/home/home.component.ts CHANGED
@@ -19,78 +19,97 @@ import { SourceDocumentModel } from 'src/app/core/models/source-document.model';
19
  styleUrls: ['./home.component.scss']
20
  })
21
 
22
-
23
  export class HomeComponent implements OnInit {
24
-
25
-
26
  chats: Array<ChatModel> = [];
27
  selectedChatIndex: number;
28
  userQuestion: string;
29
  isLoading: boolean = false;
30
  isContainerHidden: boolean = false;
31
 
32
-
33
- constructor(private historyService : HistoryService,
34
- private datePipe: DatePipe,
35
  private chatService: ChatService,
36
- private toastr: ToastrService) { }
 
37
 
38
- ngOnInit(): void {
39
 
40
- }
41
  copyQuestion(question: string) {
42
- // Copy the question to the clipboard
43
  const textField = document.createElement('textarea');
44
  textField.innerText = question;
45
  document.body.appendChild(textField);
46
  textField.select();
47
  document.execCommand('copy');
48
  textField.remove();
49
-
50
- // Show a toast message
51
  this.toastr.success('Question copied: ' + question, 'Copied');
52
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  onQuestionSubmit() {
55
- if(this.userQuestion){
56
- console.log("row user question :",this.userQuestion);
57
  const questionModel: QuestionModel = new QuestionModel();
58
-
59
-
60
  questionModel.user_question = this.userQuestion;
61
 
62
  this.isLoading = true;
63
 
64
- this.chatService.generateAnswer(questionModel).subscribe((response) => {
65
- console.log(response);
66
-
67
- const chatModel: ChatModel = new ChatModel();
68
- console.log("chatmodel :",chatModel);
69
- const user_question = response["user_input"];
70
- console.log("user input :",user_question);
71
- const arr= response["format_data"];
72
- console.log("arr :",arr);
73
- const Question_model= {
74
- user_question: user_question
75
- };
76
- chatModel.questionModel = Question_model;
77
- console.log("user question :",chatModel.questionModel);
78
-
79
- const Answer_Model ={
80
- content: response.bot_response,
81
- sourceDocuments: response.format_data
82
- }
83
- chatModel.answerModel = Answer_Model;
84
- this.chats.unshift(chatModel);
85
- this.selectedChatIndex = 0;
86
- this.isLoading = false;
87
- this.userQuestion = '';
88
- //this.getChatHistoryByUser(this.user.userId);
89
- }, error => {
90
- this.isLoading = false;
91
- });
 
 
 
 
 
 
 
 
 
 
 
92
  this.isContainerHidden = true;
93
- }
94
  }
95
 
96
  handleQuestionSelect(index: number): void {
 
19
  styleUrls: ['./home.component.scss']
20
  })
21
 
 
22
  export class HomeComponent implements OnInit {
 
 
23
  chats: Array<ChatModel> = [];
24
  selectedChatIndex: number;
25
  userQuestion: string;
26
  isLoading: boolean = false;
27
  isContainerHidden: boolean = false;
28
 
29
+ constructor(
30
+ private historyService: HistoryService,
31
+ private datePipe: DatePipe,
32
  private chatService: ChatService,
33
+ private toastr: ToastrService
34
+ ) {}
35
 
36
+ ngOnInit(): void {}
37
 
 
38
  copyQuestion(question: string) {
 
39
  const textField = document.createElement('textarea');
40
  textField.innerText = question;
41
  document.body.appendChild(textField);
42
  textField.select();
43
  document.execCommand('copy');
44
  textField.remove();
 
 
45
  this.toastr.success('Question copied: ' + question, 'Copied');
46
+ }
47
+
48
+ // Step 2: Add the streaming text method
49
+ streamResponseText(responseText: string, chatIndex: number): void {
50
+ const answerModel = this.chats[chatIndex].answerModel;
51
+ answerModel.content = ''; // Clear the content before streaming
52
+
53
+ let index = 0;
54
+ const intervalId = setInterval(() => {
55
+ if (index < responseText.length) {
56
+ answerModel.content += responseText[index];
57
+ index++;
58
+ } else {
59
+ clearInterval(intervalId); // Stop the interval when text is fully displayed
60
+ }
61
+ }, 10); // Adjust delay as needed (30ms)
62
+ }
63
 
64
  onQuestionSubmit() {
65
+ if (this.userQuestion) {
66
+ console.log("Row user question:", this.userQuestion);
67
  const questionModel: QuestionModel = new QuestionModel();
 
 
68
  questionModel.user_question = this.userQuestion;
69
 
70
  this.isLoading = true;
71
 
72
+ this.chatService.generateAnswer(questionModel).subscribe(
73
+ (response) => {
74
+ console.log("API Response:", response);
75
+
76
+ const chatModel: ChatModel = new ChatModel();
77
+
78
+ const Question_model = {
79
+ user_question: response["user_input"],
80
+ };
81
+ chatModel.questionModel = Question_model;
82
+
83
+ console.log("User question:", chatModel.questionModel);
84
+
85
+ const formattedResponse = response["bot_response"]
86
+ .replace(/\/n|\n/g, "<br>") // Handle newlines
87
+ .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
88
+ .replace(/###\s(.*?):/g, '<br><h6>$1:</h6>'); // Handle bold text
89
+
90
+ const Answer_Model = {
91
+ content: '', // Start with empty content for streaming
92
+ sourceDocuments: response.format_data,
93
+ };
94
+
95
+ chatModel.answerModel = Answer_Model;
96
+ this.chats.unshift(chatModel);
97
+ this.selectedChatIndex = 0;
98
+
99
+ // Step 3: Use the streaming effect
100
+ const currentChatIndex = this.selectedChatIndex;
101
+ this.streamResponseText(formattedResponse, currentChatIndex);
102
+
103
+ this.isLoading = false;
104
+ this.userQuestion = '';
105
+ },
106
+ (error) => {
107
+ this.isLoading = false;
108
+ }
109
+ );
110
+
111
  this.isContainerHidden = true;
112
+ }
113
  }
114
 
115
  handleQuestionSelect(index: number): void {