parent
57a1ed2410
commit
5c3cb5344a
9 changed files with 146 additions and 0 deletions
@ -0,0 +1,3 @@ |
||||
from django.contrib import admin |
||||
|
||||
# Register your models here. |
@ -0,0 +1,6 @@ |
||||
from django.apps import AppConfig |
||||
|
||||
|
||||
class AppConfig(AppConfig): |
||||
default_auto_field = 'django.db.models.BigAutoField' |
||||
name = 'app' |
@ -0,0 +1,3 @@ |
||||
from django.db import models |
||||
|
||||
# Create your models here. |
@ -0,0 +1,113 @@ |
||||
<!-- chat/templates/chat/index.html --> |
||||
|
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
<title>Chat App</title> |
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/15.0.7/marked.min.js" integrity="sha512-rPuOZPx/WHMHNx2RoALKwiCDiDrCo4ekUctyTYKzBo8NGA79NcTW2gfrbcCL2RYL7RdjX2v9zR0fKyI4U4kPew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> |
||||
<style> |
||||
body { |
||||
font-family: Arial, sans-serif; |
||||
max-width: 800px; |
||||
margin: 0 auto; |
||||
padding: 20px; |
||||
} |
||||
#chat-container { |
||||
border: 1px solid #ccc; |
||||
border-radius: 5px; |
||||
padding: 20px; |
||||
margin-bottom: 20px; |
||||
} |
||||
#prompt-input { |
||||
width: 100%; |
||||
} |
||||
#response-container { |
||||
min-height: 100px; |
||||
border: 1px solid #eee; |
||||
color: black; |
||||
padding: 10px; |
||||
margin-top: 10px; |
||||
border-radius: 5px; |
||||
} |
||||
input { |
||||
padding: 8px; |
||||
width: 70%; |
||||
} |
||||
button { |
||||
padding: 8px 16px; |
||||
background-color: #4CAF50; |
||||
color: white; |
||||
border: none; |
||||
border-radius: 4px; |
||||
cursor: pointer; |
||||
} |
||||
button:hover { |
||||
background-color: #45a049; |
||||
} |
||||
</style> |
||||
</head> |
||||
<body> |
||||
<h1>Simple Chat App</h1> |
||||
|
||||
<div id="chat-container"> |
||||
<h3>Enter your message:</h3> |
||||
<textarea type="text" id="prompt-input" placeholder="Type your message..." value="hello"> |
||||
</textarea> |
||||
<button onclick="sendPrompt()">Send</button> |
||||
|
||||
<div id="response-container"> |
||||
<div id="response-text">Response will appear here...</div> |
||||
</div> |
||||
</div> |
||||
|
||||
<script> |
||||
const responseText = document.querySelector('#response-text'); |
||||
// Function to send the prompt and handle streaming response |
||||
responseText.innerHTML = marked.parse(""); |
||||
async function sendPrompt() { |
||||
const promptInput = document.getElementById('prompt-input'); |
||||
const content = promptInput.value; |
||||
|
||||
// Clear previous response |
||||
responseText.textContent = ''; |
||||
|
||||
function base64ToUtf8(bytes) { |
||||
// Decode UTF-8 bytes to a JavaScript string |
||||
return (new TextDecoder()).decode(bytes, { stream: true }); |
||||
} |
||||
try { |
||||
// Make the fetch request |
||||
const response = await fetch('/api/conversations/14/prompt/', { |
||||
method: 'POST', |
||||
headers: { |
||||
'Content-Type': 'application/json', |
||||
}, |
||||
body: JSON.stringify({ content }), |
||||
}); |
||||
|
||||
// Handle the event stream |
||||
const reader = response.body.getReader(); |
||||
const decoder = new TextDecoder(); |
||||
let buffer = ''; |
||||
function readChunk() { |
||||
reader.read().then(({ done, value }) => { |
||||
if (done) return; |
||||
console.log(value); |
||||
buffer += atob((new TextDecoder()).decode(value, { stream: true })) |
||||
console.log(buffer) |
||||
responseText.innerHTML = marked.parse(buffer); |
||||
readChunk(); |
||||
}); |
||||
} |
||||
readChunk(); |
||||
|
||||
} catch (error) { |
||||
console.error('Error:', error); |
||||
responseText.textContent = `Error: ${error.message}`; |
||||
} |
||||
} |
||||
</script> |
||||
</body> |
||||
</html> |
@ -0,0 +1,3 @@ |
||||
from django.test import TestCase |
||||
|
||||
# Create your tests here. |
@ -0,0 +1,9 @@ |
||||
# conversation_app/urls.py |
||||
|
||||
from django.contrib import admin |
||||
from django.urls import path, include |
||||
from app import views |
||||
|
||||
urlpatterns = [ |
||||
path('', views.index, name='index'), |
||||
] |
@ -0,0 +1,9 @@ |
||||
from django.shortcuts import render |
||||
import json |
||||
import time |
||||
|
||||
def index(request): |
||||
""" |
||||
Render the main chat page |
||||
""" |
||||
return render(request, 'index.html') |
Loading…
Reference in New Issue