commit 829294b337c4a6118273bd52060fff61c12825e9 Author: Tristan Smith Date: Tue Jun 11 00:04:26 2024 -0400 It works, poorly, but it works. diff --git a/chat/__init__.py b/chat/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chat/__pycache__/__init__.cpython-312.pyc b/chat/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..0f56387 Binary files /dev/null and b/chat/__pycache__/__init__.cpython-312.pyc differ diff --git a/chat/__pycache__/admin.cpython-312.pyc b/chat/__pycache__/admin.cpython-312.pyc new file mode 100644 index 0000000..06c6249 Binary files /dev/null and b/chat/__pycache__/admin.cpython-312.pyc differ diff --git a/chat/__pycache__/apps.cpython-312.pyc b/chat/__pycache__/apps.cpython-312.pyc new file mode 100644 index 0000000..705ef2f Binary files /dev/null and b/chat/__pycache__/apps.cpython-312.pyc differ diff --git a/chat/__pycache__/models.cpython-312.pyc b/chat/__pycache__/models.cpython-312.pyc new file mode 100644 index 0000000..d91a507 Binary files /dev/null and b/chat/__pycache__/models.cpython-312.pyc differ diff --git a/chat/__pycache__/urls.cpython-312.pyc b/chat/__pycache__/urls.cpython-312.pyc new file mode 100644 index 0000000..7eefe9d Binary files /dev/null and b/chat/__pycache__/urls.cpython-312.pyc differ diff --git a/chat/__pycache__/views.cpython-312.pyc b/chat/__pycache__/views.cpython-312.pyc new file mode 100644 index 0000000..518487b Binary files /dev/null and b/chat/__pycache__/views.cpython-312.pyc differ diff --git a/chat/admin.py b/chat/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/chat/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/chat/apps.py b/chat/apps.py new file mode 100644 index 0000000..2fe899a --- /dev/null +++ b/chat/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChatConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'chat' diff --git a/chat/migrations/__init__.py b/chat/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chat/migrations/__pycache__/__init__.cpython-312.pyc b/chat/migrations/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..9e46368 Binary files /dev/null and b/chat/migrations/__pycache__/__init__.cpython-312.pyc differ diff --git a/chat/models.py b/chat/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/chat/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/chat/templates/chat/index.html b/chat/templates/chat/index.html new file mode 100644 index 0000000..88ac321 --- /dev/null +++ b/chat/templates/chat/index.html @@ -0,0 +1,133 @@ + + + + + + + Chat with Ollama + + + + +
+

Chat with Ollama

+
+ {% csrf_token %} + + +
+ +
+
+ + + + + \ No newline at end of file diff --git a/chat/tests.py b/chat/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/chat/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/chat/urls.py b/chat/urls.py new file mode 100644 index 0000000..ece36c3 --- /dev/null +++ b/chat/urls.py @@ -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'), +] \ No newline at end of file diff --git a/chat/views.py b/chat/views.py new file mode 100644 index 0000000..9bac006 --- /dev/null +++ b/chat/views.py @@ -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) diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..851c8cb Binary files /dev/null and b/db.sqlite3 differ diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..1dd9824 --- /dev/null +++ b/manage.py @@ -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() diff --git a/ollama/__init__.py b/ollama/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ollama/__pycache__/__init__.cpython-312.pyc b/ollama/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..738bbd5 Binary files /dev/null and b/ollama/__pycache__/__init__.cpython-312.pyc differ diff --git a/ollama/__pycache__/settings.cpython-312.pyc b/ollama/__pycache__/settings.cpython-312.pyc new file mode 100644 index 0000000..f03eb5c Binary files /dev/null and b/ollama/__pycache__/settings.cpython-312.pyc differ diff --git a/ollama/__pycache__/urls.cpython-312.pyc b/ollama/__pycache__/urls.cpython-312.pyc new file mode 100644 index 0000000..0dfb95d Binary files /dev/null and b/ollama/__pycache__/urls.cpython-312.pyc differ diff --git a/ollama/__pycache__/wsgi.cpython-312.pyc b/ollama/__pycache__/wsgi.cpython-312.pyc new file mode 100644 index 0000000..9948dca Binary files /dev/null and b/ollama/__pycache__/wsgi.cpython-312.pyc differ diff --git a/ollama/asgi.py b/ollama/asgi.py new file mode 100644 index 0000000..ed20efe --- /dev/null +++ b/ollama/asgi.py @@ -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() diff --git a/ollama/settings.py b/ollama/settings.py new file mode 100644 index 0000000..072dd8b --- /dev/null +++ b/ollama/settings.py @@ -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' diff --git a/ollama/urls.py b/ollama/urls.py new file mode 100644 index 0000000..99c9c9b --- /dev/null +++ b/ollama/urls.py @@ -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 +] diff --git a/ollama/wsgi.py b/ollama/wsgi.py new file mode 100644 index 0000000..d247f02 --- /dev/null +++ b/ollama/wsgi.py @@ -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()