-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CharlieKC/issue3 #19
Merged
Merged
CharlieKC/issue3 #19
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
Reformat files
- Loading branch information
commit 58fbc411c1bcc51b8619851816be07c9ba240e6c
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,99 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<h1>Conversation: {{ request.session.conversation_id }} </h1> | ||
|
||
{% for msg in messages %} | ||
<p>{{ msg.text }}</p> | ||
{% endfor %} | ||
|
||
<div id="chat-ui" class="mx-auto w-11/12"> | ||
<!-- Messages will go here --> | ||
<div id="chat-box" class=""></div> | ||
</div> | ||
|
||
<!-- Ensure space at bottom of page --> | ||
<div class="fixed bottom-0 my-10"></div> | ||
|
||
<div class="fixed bottom-0 left-0 w-full border-t"> | ||
<div class="flex justify-center my-2"> | ||
<textarea | ||
id="user-input" | ||
rows="1" | ||
class="textarea textarea-bordered w-11/12 text-xl" | ||
style="resize: none"></textarea> | ||
</div> | ||
<h1>Conversation: {{ request.session.conversation_id }}</h1> | ||
{% for msg in messages %}<p>{{ msg.text }}</p>{% endfor %} | ||
<div id="chat-ui" class="mx-auto w-11/12"> | ||
<!-- Messages will go here --> | ||
<div id="chat-box"></div> | ||
</div> | ||
<!-- Ensure space at bottom of page --> | ||
<div class="fixed bottom-0 my-10"></div> | ||
<div class="fixed bottom-0 left-0 w-full border-t"> | ||
<div class="flex justify-center my-2"> | ||
<textarea id="user-input" | ||
rows="1" | ||
class="textarea textarea-bordered w-11/12 text-xl" | ||
style="resize: none"></textarea> | ||
</div> | ||
|
||
|
||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> | ||
<script> | ||
</div> | ||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> | ||
<script> | ||
// Function to get CSRF token from cookies | ||
function getCookie(name) { | ||
let cookieValue = null; | ||
if (document.cookie && document.cookie !== '') { | ||
const cookies = document.cookie.split(';'); | ||
for (let i = 0; i < cookies.length; i++) { | ||
const cookie = cookies[i].trim(); | ||
if (cookie.substring(0, name.length + 1) === (name + '=')) { | ||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
break; | ||
} | ||
} | ||
let cookieValue = null; | ||
if (document.cookie && document.cookie !== '') { | ||
const cookies = document.cookie.split(';'); | ||
for (let i = 0; i < cookies.length; i++) { | ||
const cookie = cookies[i].trim(); | ||
if (cookie.substring(0, name.length + 1) === (name + '=')) { | ||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
break; | ||
} | ||
} | ||
return cookieValue; | ||
} | ||
return cookieValue; | ||
} | ||
|
||
const csrftoken = getCookie('csrftoken'); | ||
|
||
function appendMessage(who, text) { | ||
let left_or_right = "end"; | ||
let color = "success"; | ||
if (who === "user") { | ||
left_or_right = "start"; | ||
color = "primary"; | ||
} | ||
let left_or_right = "end"; | ||
let color = "success"; | ||
if (who === "user") { | ||
left_or_right = "start"; | ||
color = "primary"; | ||
} | ||
|
||
|
||
// const message = `<div class="chat chat-${left_or_right}"><div class="chat-header mx-1">${who}</div><div class="chat-bubble chat-bubble-${color}">${text}</div></div>`; | ||
const message = `<div class="chat chat-${left_or_right}"><div class="chat-header mx-1">${who}</div><div class="chat-bubble chat-bubble-${color}">${text}</div></div>`; | ||
$("#chat-box").append(message); | ||
// const message = `<div class="chat chat-${left_or_right}"><div class="chat-header mx-1">${who}</div><div class="chat-bubble chat-bubble-${color}">${text}</div></div>`; | ||
const message = `<div class="chat chat-${left_or_right}"><div class="chat-header mx-1">${who}</div><div class="chat-bubble chat-bubble-${color}">${text}</div></div>`; | ||
$("#chat-box").append(message); | ||
Check warning Code scanning / CodeQL DOM text reinterpreted as HTML Medium DOM text Error loading related location Loading |
||
} | ||
|
||
// const existingMessages = JSON.parse(document.getElementById('hello-data').message-data); | ||
// existingMessages.forEach(element => { | ||
// appendMessage(element.role, element.text); | ||
|
||
// }); | ||
|
||
|
||
function sendMessage() { | ||
const userInput = $("#user-input").val(); | ||
appendMessage("user", userInput); | ||
|
||
$.ajax({ | ||
url: "/api/chat/", | ||
method: "POST", | ||
headers: { "X-CSRFToken": csrftoken }, | ||
data: { text: userInput }, | ||
success: function(response) { | ||
appendMessage("assistant", response.text); | ||
} | ||
}); | ||
const userInput = $("#user-input").val(); | ||
appendMessage("user", userInput); | ||
|
||
$.ajax({ | ||
url: "/api/chat/", | ||
method: "POST", | ||
headers: { | ||
"X-CSRFToken": csrftoken | ||
}, | ||
data: { | ||
text: userInput | ||
}, | ||
success: function(response) { | ||
appendMessage("assistant", response.text); | ||
} | ||
}); | ||
|
||
$("#user-input").val(""); | ||
$("#user-input").val(""); | ||
} | ||
|
||
|
||
const userInput = document.getElementById('user-input'); | ||
|
||
// Add event listener for the 'keydown' event | ||
userInput.addEventListener('keydown', function(event) { | ||
// Reset rows to 1 to calculate the correct scrollHeight | ||
if (event.key === 'Enter') { | ||
if (event.shiftKey) { | ||
userInput.rows++; | ||
return; | ||
} | ||
event.preventDefault(); // Prevent the default action (new line or form submission) | ||
userInput.rows = 1; | ||
sendMessage(); | ||
} | ||
}); | ||
const userInput = document.getElementById('user-input'); | ||
|
||
</script> | ||
|
||
{% endblock content %} | ||
// Add event listener for the 'keydown' event | ||
userInput.addEventListener('keydown', function(event) { | ||
// Reset rows to 1 to calculate the correct scrollHeight | ||
if (event.key === 'Enter') { | ||
if (event.shiftKey) { | ||
userInput.rows++; | ||
return; | ||
} | ||
event.preventDefault(); // Prevent the default action (new line or form submission) | ||
userInput.rows = 1; | ||
sendMessage(); | ||
} | ||
}); | ||
</script> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Inclusion of functionality from an untrusted source Medium