It works, poorly, but it works.

This commit is contained in:
Tristan Smith 2024-06-11 00:04:26 -04:00
commit 829294b337
27 changed files with 394 additions and 0 deletions

0
chat/__init__.py Normal file
View file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

3
chat/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
chat/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ChatConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'chat'

View file

Binary file not shown.

3
chat/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat with Ollama</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: hidden;
/* Prevents body from scrolling */
}
.container {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 80%;
max-width: 600px;
max-height: 90vh;
overflow: hidden;
display: flex;
flex-direction: column;
}
h1 {
text-align: center;
color: #444;
}
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 10px;
font-size: 14px;
resize: none;
/* Prevents textarea from being resizable */
}
button {
width: 100%;
padding: 10px;
border: none;
background-color: #5cb85c;
color: white;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
margin-bottom: 10px;
}
button:hover {
background-color: #4cae4c;
}
.response {
flex: 1;
margin-top: 10px;
padding: 10px;
background-color: #f9f9f9;
border-left: 5px solid #5cb85c;
white-space: pre-wrap;
/* Ensure formatting is preserved */
overflow-y: auto;
/* Adds vertical scroll if needed */
font-family: monospace;
}
.loading {
text-align: center;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<h1>Chat with Ollama</h1>
<form id="chat-form" method="post" action="/chat/send/">
{% csrf_token %}
<textarea id="userInput" name="message" rows="4" cols="50" placeholder="Type your message here..."></textarea>
<button type="submit">Send</button>
</form>
<div id="loading" class="loading" style="display: none;">Loading...</div>
<div id="response" class="response"></div>
</div>
<script>
document.getElementById('chat-form').onsubmit = async function (e) {
e.preventDefault();
const message = document.getElementById('userInput').value;
const responseDiv = document.getElementById('response');
const loadingDiv = document.getElementById('loading');
responseDiv.innerText = '';
loadingDiv.style.display = 'block';
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
const response = await fetch('/chat/send/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRFToken': csrftoken
},
body: new URLSearchParams({
'message': message
})
});
const data = await response.json();
loadingDiv.style.display = 'none';
if (data.response) {
responseDiv.innerText = data.response;
} else if (data.error) {
responseDiv.innerText = 'Error: ' + data.error;
}
};
</script>
</body>
</html>

3
chat/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

7
chat/urls.py Normal file
View file

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('send/', views.send_message, name='send_message'),
]

52
chat/views.py Normal file
View file

@ -0,0 +1,52 @@
from django.shortcuts import render
from django.http import JsonResponse
import requests
import json
def index(request):
return render(request, 'chat/index.html')
def send_message(request):
if request.method == 'POST':
user_message = request.POST.get('message')
api_url = 'http://192.168.1.36:11434/api/generate' # Updated URL with the correct port
headers = {
'Authorization': 'Bearer your_api_key_here', # Include your API key if needed
'Content-Type': 'application/json'
}
payload = {
'prompt': user_message,
'model': 'mistral:latest' # Include the model
}
try:
response = requests.post(api_url, json=payload, headers=headers)
response.raise_for_status() # This will raise an HTTPError for bad responses
print(f'Response Content: {response.content}')
# Handle the response correctly by splitting lines and parsing each JSON object
response_lines = response.content.decode('utf-8').splitlines()
combined_response = ""
for line in response_lines:
json_obj = json.loads(line)
if 'response' in json_obj:
combined_response += json_obj['response'] + " "
if json_obj.get('done', False):
break
return JsonResponse({'response': combined_response.strip()})
except requests.exceptions.HTTPError as errh:
print(f'HTTP Error: {errh}')
print(f'Response Content: {response.content}')
return JsonResponse({'error': f'HTTP Error: {errh}', 'details': response.content.decode('utf-8')}, status=400)
except requests.exceptions.ConnectionError as errc:
print(f'Error Connecting: {errc}')
return JsonResponse({'error': f'Error Connecting: {errc}'}, status=500)
except requests.exceptions.Timeout as errt:
print(f'Timeout Error: {errt}')
return JsonResponse({'error': f'Timeout Error: {errt}'}, status=500)
except requests.exceptions.RequestException as err:
print(f'Request Error: {err}')
print(f'Response Content: {response.content}')
return JsonResponse({'error': f'Request Error: {err}', 'details': response.content.decode('utf-8')}, status=500)

BIN
db.sqlite3 Normal file

Binary file not shown.

22
manage.py Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ollama.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()

0
ollama/__init__.py Normal file
View file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

16
ollama/asgi.py Normal file
View file

@ -0,0 +1,16 @@
"""
ASGI config for ollama project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ollama.settings')
application = get_asgi_application()

124
ollama/settings.py Normal file
View file

@ -0,0 +1,124 @@
"""
Django settings for ollama project.
Generated by 'django-admin startproject' using Django 5.0.6.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-b%0==p)98(5sa=z&jx=llq=p!(+3pp4_x410g5j$40ifay-ljq'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'chat',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'ollama.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'ollama.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

9
ollama/urls.py Normal file
View file

@ -0,0 +1,9 @@
# myproject/urls.py
from django.contrib import admin
from django.urls import path, include # Import 'include'
urlpatterns = [
path('admin/', admin.site.urls),
path('chat/', include('chat.urls')), # Include the URLs from the 'chat' app
]

16
ollama/wsgi.py Normal file
View file

@ -0,0 +1,16 @@
"""
WSGI config for ollama project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ollama.settings')
application = get_wsgi_application()