You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

app_agent.html 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>AutoGen FastAPI Sample: Agent</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. margin: 0;
  11. padding: 0;
  12. display: flex;
  13. flex-direction: column;
  14. align-items: center;
  15. justify-content: center;
  16. height: 100vh;
  17. background-color: #f0f0f0;
  18. }
  19. #chat-container {
  20. width: 90%;
  21. max-width: 600px;
  22. background-color: #fff;
  23. border-radius: 8px;
  24. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  25. padding: 20px;
  26. }
  27. #messages {
  28. height: 600px;
  29. overflow-y: auto;
  30. border-bottom: 1px solid #ddd;
  31. margin-bottom: 20px;
  32. }
  33. .message {
  34. margin: 10px 0;
  35. }
  36. .message.user {
  37. text-align: right;
  38. }
  39. .message.assistant {
  40. text-align: left;
  41. }
  42. .message.error {
  43. color: #721c24;
  44. background-color: #f8d7da;
  45. border: 1px solid #f5c6cb;
  46. padding: 10px;
  47. border-radius: 4px;
  48. margin: 10px 0;
  49. }
  50. .message.system {
  51. color: #0c5460;
  52. background-color: #d1ecf1;
  53. border: 1px solid #bee5eb;
  54. padding: 10px;
  55. border-radius: 4px;
  56. margin: 10px 0;
  57. }
  58. #input-container input:disabled,
  59. #input-container button:disabled {
  60. background-color: #e0e0e0;
  61. cursor: not-allowed;
  62. }
  63. #input-container {
  64. display: flex;
  65. }
  66. #input-container input {
  67. flex: 1;
  68. padding: 10px;
  69. border: 1px solid #ddd;
  70. border-radius: 4px;
  71. }
  72. #input-container button {
  73. padding: 10px 20px;
  74. border: none;
  75. background-color: #007bff;
  76. color: #fff;
  77. border-radius: 4px;
  78. cursor: pointer;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div id="chat-container">
  84. <div id="messages"></div>
  85. <div id="input-container">
  86. <input type="text" id="message-input" placeholder="Type a message...">
  87. <button onclick="sendMessage()">Send</button>
  88. </div>
  89. </div>
  90. <script>
  91. document.getElementById('message-input').addEventListener('keydown', function (event) {
  92. if (event.key === 'Enter') {
  93. sendMessage();
  94. }
  95. });
  96. async function sendMessage() {
  97. const input = document.getElementById('message-input');
  98. const button = document.querySelector('#input-container button');
  99. const message = input.value;
  100. if (!message) return;
  101. // Display user message
  102. displayMessage(message, 'user');
  103. // Clear input and disable controls
  104. input.value = '';
  105. input.disabled = true;
  106. button.disabled = true;
  107. try {
  108. const response = await fetch('http://localhost:8001/chat', {
  109. method: 'POST',
  110. headers: {
  111. 'Content-Type': 'application/json'
  112. },
  113. body: JSON.stringify({ content: message, source: 'user' })
  114. });
  115. const data = await response.json();
  116. if (!response.ok) {
  117. // Handle error response
  118. if (data.detail && data.detail.type === 'error') {
  119. displayMessage(data.detail.content, 'error');
  120. } else {
  121. displayMessage('Error: ' + (data.detail || 'Unknown error'), 'error');
  122. }
  123. } else {
  124. displayMessage(data.content, 'assistant');
  125. }
  126. } catch (error) {
  127. console.error('Error:', error);
  128. displayMessage('Error: Could not reach the server.', 'error');
  129. } finally {
  130. // Re-enable controls
  131. input.disabled = false;
  132. button.disabled = false;
  133. input.focus();
  134. }
  135. }
  136. function displayMessage(content, source) {
  137. const messagesContainer = document.getElementById('messages');
  138. const messageElement = document.createElement('div');
  139. messageElement.className = `message ${source}`;
  140. const labelElement = document.createElement('span');
  141. labelElement.className = 'label';
  142. labelElement.textContent = source;
  143. const contentElement = document.createElement('div');
  144. contentElement.className = 'content';
  145. contentElement.textContent = content;
  146. messageElement.appendChild(labelElement);
  147. messageElement.appendChild(contentElement);
  148. messagesContainer.appendChild(messageElement);
  149. messagesContainer.scrollTop = messagesContainer.scrollHeight;
  150. }
  151. async function loadHistory() {
  152. try {
  153. const response = await fetch('http://localhost:8001/history');
  154. if (!response.ok) {
  155. throw new Error('Network response was not ok');
  156. }
  157. const history = await response.json();
  158. history.forEach(message => {
  159. displayMessage(message.content, message.source);
  160. });
  161. } catch (error) {
  162. console.error('Error loading history:', error);
  163. }
  164. }
  165. // Load chat history when the page loads
  166. window.onload = loadHistory;
  167. </script>
  168. </body>
  169. </html>