Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

chatbot.js 3.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  1. let typed;
  2. let allowQuestion = true;
  3. if(typed != null) {
  4. typed.destroy();
  5. typed = null;
  6. }
  7. $("#gpt-button").click(function() {
  8. if($("#chat-input").val() != "" && allowQuestion) {
  9. askQuestion();
  10. }
  11. }); // ask chatGPT when user clicks button
  12. $(document).on('keypress',function(e) { // ask chatGpt when user presses enter
  13. if(e.which == 13 && $("#chat-input").val() != "" && allowQuestion) {
  14. askQuestion();
  15. }
  16. });
  17. function askQuestion(){
  18. allowQuestion = false;
  19. let question = $("#chat-input").val();
  20. let userHtmlData = '';
  21. let chatHtmlData = '';
  22. userHtmlData += `
  23. <div id="list-group" class="list-group w-100">
  24. <div class="userResponse">
  25. <a class="listItem list-group-item d-flex gap-3 border-0 w-60">
  26. <div class="d-flex gap-2 w-100 justify-content-end">
  27. <p class="mb-0 opacity-75">${question}</p>
  28. </div>
  29. <div class="vertical-bar"></div>
  30. <img src="../static/images/user.png" alt="twbs" width="80" height="80" class="rounded-circle flex-shrink-0 align-self-center">
  31. </a>
  32. </div>
  33. </div>
  34. `;
  35. chatHtmlData += `
  36. <div id="list-group" class="list-group w-100">
  37. <div class="chatResponse">
  38. <a class="listItem list-group-item d-flex gap-3 border-0 w-60 justify-content-between">
  39. <img src="../static/images/chat.png" alt="twbs" width="80" height="80" class="rounded-circle flex-shrink-0">
  40. <div class="vertical-bar"></div>
  41. <div class="d-flex gap-2 w-100 justify-content-between">
  42. <p class="mb-0 opacity-75" id="gpt-answer"></p>
  43. </div>
  44. </a>
  45. </div>
  46. </div>
  47. `;
  48. $("#chat-input").val('');
  49. $("#list-group").append(userHtmlData);
  50. setTimeout(function() {
  51. $("#list-group").append(chatHtmlData);
  52. }, 50);
  53. window.scrollTo(0, document.body.scrollHeight);
  54. let dotAnimation;
  55. $("#gpt-answer").ready(function() {
  56. dotAnimation = setInterval(function() {
  57. var th = $('#gpt-answer');
  58. if (th.text().length < 4) {
  59. th.text(th.text() + ".");
  60. } else {
  61. th.text("");
  62. }
  63. }, 150);
  64. });
  65. $.ajax({
  66. type: 'POST',
  67. url: '/chatbot',
  68. data: {'prompt': question},
  69. success: function (data) {
  70. clearInterval(dotAnimation);
  71. // $("#gpt-answer").text(data.answer);
  72. typed = new Typed("#gpt-answer", {
  73. strings: [data.answer],
  74. typeSpeed: 0,
  75. showCursor: false
  76. });
  77. $("#gpt-answer").removeAttr("id");
  78. allowQuestion = true;
  79. }
  80. });
  81. }
  82. const inputBox = document.getElementById('chat-input');
  83. const inputArea = document.querySelector('.input-area');
  84. let chatEl = document.querySelector('.container');
  85. if (chatEl) {
  86. chatEl.scrollTop = chatEl.scrollHeight;
  87. }
  88. // PROMPT FUNCTIONALITY
  89. let prompts = document.querySelectorAll('.prompt');
  90. for (let prompt of prompts) {
  91. prompt.addEventListener('click', () => {
  92. inputBox.value = prompt.innerText;
  93. askQuestion();
  94. });
  95. }
  96. // AUTO SCROLL FUNCTIONALITY
  97. const listGroup = document.getElementById('list-group');
  98. const observer = new ResizeObserver(entries => {
  99. for (let entry of entries) {
  100. if (entry.contentBoxSize) {
  101. window.scrollTo(0, document.body.scrollHeight);
  102. }
  103. }
  104. });
  105. observer.observe(listGroup);
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...