Skip to content

Commit

Permalink
Maintain chat history in llm
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Sep 19, 2024
1 parent bff742a commit 89759ba
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 36 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ max-complexity = 10
per-file-ignores =
/**/tests/*_mock_data.py: E501
**/snap_test_*.py: E501
/**/apps.py: F401
3 changes: 0 additions & 3 deletions content/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
class ContentConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "content"

def ready(self):
import content.signals
3 changes: 3 additions & 0 deletions content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.translation import gettext_lazy as _

from common.models import UserResource
from content.tasks import create_embedding_for_content_task


class Tag(models.Model):
Expand Down Expand Up @@ -46,4 +47,6 @@ def save(self, *args, **kwargs):
if self.pk is None:
if self.document_type == self.DocumentType.TEXT:
self.extracted_file = self.document_file
if self.document_status == self.DocumentStatus.PENDING:
create_embedding_for_content_task(self.id)
super().save(*args, **kwargs)
10 changes: 0 additions & 10 deletions content/signals.py

This file was deleted.

10 changes: 6 additions & 4 deletions content/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

@shared_task(blind=True)
def create_embedding_for_content_task(content_id):
from .models import Content
from content.models import Content

content = Content.objects.get(id=content_id)
url = settings.EMBEDDING_MODEL_URL
headers = {"Content-Type": "application/json"}
data = content.extracted_file.read()
loader = LoaderFromText(text=data)
Expand All @@ -22,15 +21,18 @@ def create_embedding_for_content_task(content_id):
"name_model": settings.EMBEDDING_MODEL_NAME,
"texts": [split_docs[i].page_content for i in range(len(split_docs))],
}
response = requests.post(url, headers=headers, json=payload)
response = requests.post(settings.EMBEDDING_MODEL_URL, headers=headers, json=payload)
metadata = [
{"source": "plain-text", "page_content": split_docs[i].page_content, "uuid": content.content_id}
for i in range(len(split_docs))
]
if response.status_code == 200:
db = QdrantDatabase(host="qdrant", port=settings.QDRANT_DB_PORT, collection_name=settings.QDRANT_DB_COLLECTION_NAME)
db = QdrantDatabase(
host=settings.QDRANT_DB_HOST, port=settings.QDRANT_DB_PORT, collection_name=settings.QDRANT_DB_COLLECTION_NAME
)
db.set_collection()
db.store_data(zip(response.json(), metadata))
content.document_status = Content.DocumentStatus.ADDED_TO_VECTOR
else:
content.document_status = Content.DocumentStatus.FAILURE
content.save()
7 changes: 0 additions & 7 deletions content/urls.py

This file was deleted.

12 changes: 12 additions & 0 deletions content/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Create your views here.
from rest_framework.decorators import api_view
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response

from chatbotcore.llm import OllamaHandler
Expand All @@ -14,3 +15,14 @@ def chat(request):
result = data.execute_chain(request.data["query"])
return Response(result)
return Response(serializer.errors)


class UserQuery(GenericAPIView):
llm = OllamaHandler()

def post(self, request, *arg, **kwargs):
serializer = UserQuerySerializer(data=request.data)
if serializer.is_valid():
result = self.llm.execute_chain(request.data["query"])
return Response(result)
return Response(serializer.errors, 422)
8 changes: 2 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ x-server: &base_server_setup
APP_ENVIRONMENT: ${APP_ENVIRONMENT:-development}
APP_TYPE: web
DJANGO_DEBUG: ${DJANGO_DEBUG:-true}
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY?error}
DJANGO_TIME_ZONE: ${DJANGO_TIME_ZONE:-Asia/Kathmandu}
# -- Domain configurations
DJANGO_ALLOWED_HOSTS: ${DJANGO_ALLOWED_HOSTS:-*}
Expand Down Expand Up @@ -84,7 +84,7 @@ services:
# https://github.com/qdrant/qdrant/blob/master/config/config.yaml
QDRANT__SERVICE__HOST: 0.0.0.0
QDRANT__SERVICE__HTTP_PORT: 6333

web:
<<: *base_server_setup
command: bash -c 'python manage.py runserver 0.0.0.0:8001'
Expand All @@ -99,10 +99,6 @@ services:
worker:
<<: *base_server_setup
command: celery -A main worker --loglevel=info
depends_on:
- redis
- web


volumes:
postgres-data16:
Expand Down
9 changes: 4 additions & 5 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.urls import path

urlpatterns = [
path("admin/", admin.site.urls),
path("chat_message", include("content.urls")),
]
from content.views import UserQuery

urlpatterns = [path("admin/", admin.site.urls), path("chat_message", UserQuery.as_view())]
if settings.DEBUG:

# Static and media file URLs
Expand Down

0 comments on commit 89759ba

Please sign in to comment.