From 9da9801da00e92727d85a20d6715a77e790337c0 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sun, 2 Jun 2024 12:31:51 +0200 Subject: [PATCH] Change dimensions of VectorField, move logic from view to controler. Zbudowalismy go --- api/bot/admin.py | 2 -- api/bot/controllers.py | 25 ++++++++++++++++++++++--- api/bot/migrations/__init__.py | 0 api/bot/models.py | 5 ----- api/bot/schemas.py | 8 ++++++-- api/bot/views.py | 23 +++++------------------ api/chunks/controllers.py | 2 +- api/chunks/migrations/0001_initial.py | 9 +++++---- api/chunks/models.py | 2 +- 9 files changed, 40 insertions(+), 36 deletions(-) delete mode 100644 api/bot/migrations/__init__.py delete mode 100644 api/bot/models.py diff --git a/api/bot/admin.py b/api/bot/admin.py index 7263774..c6fe108 100644 --- a/api/bot/admin.py +++ b/api/bot/admin.py @@ -1,4 +1,2 @@ from django.contrib import admin -from .models import Bot -admin.site.register(Bot) diff --git a/api/bot/controllers.py b/api/bot/controllers.py index 6b3b0f1..22f112b 100644 --- a/api/bot/controllers.py +++ b/api/bot/controllers.py @@ -1,9 +1,28 @@ from http import HTTPStatus -from bot.models import Bot +import requests +from pgvector.django import L2Distance + from bot.schemas import BotIn, BotOut +from chunks.models import Chunk def query_llm_controller(payload: BotIn) -> tuple[HTTPStatus, BotOut]: - bot = Bot(**payload.dict()) - return HTTPStatus.CREATED, bot + # TODO: payload na embeding -> Vector -> Szukamy w bazie podobne -> dokument do payloada + embeddings_body = { + "input": payload.input + } + response = requests.post("http://192.168.0.3:9000/v1/embeddings/", json=embeddings_body) + input_embedding = response.json()['data'][0]['embedding'] + similar_chunk = Chunk.objects.order_by(L2Distance('embedding', input_embedding))[0] + print(similar_chunk.text) + llm_body = { + "prompt": "\n\n### Instructions:\nOdpowiedz na pytanie "+ payload.input +"\n\nWiedząc że"+similar_chunk.text+"\n\n### Response:\n", + "stop": [ + "\n", + "###" + ] + } + llm_response = requests.post("http://192.168.0.3:9000/v1/completions/", json=llm_body) + llm_response = llm_response.json()['choices'][0]['text'] + return HTTPStatus.OK, BotOut(output=str(llm_response)) diff --git a/api/bot/migrations/__init__.py b/api/bot/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/api/bot/models.py b/api/bot/models.py deleted file mode 100644 index 6dca41c..0000000 --- a/api/bot/models.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.db import models - - -class Bot(models.Model): - text = models.TextField[str, str]() diff --git a/api/bot/schemas.py b/api/bot/schemas.py index 44c50a4..0ffcfe7 100644 --- a/api/bot/schemas.py +++ b/api/bot/schemas.py @@ -2,8 +2,12 @@ class BotIn(Schema): - text: str + input: str class BotOut(Schema): - text: str + output: str + + +class BotError(Schema): + pass \ No newline at end of file diff --git a/api/bot/views.py b/api/bot/views.py index 8a77c0c..02fa5eb 100644 --- a/api/bot/views.py +++ b/api/bot/views.py @@ -1,27 +1,14 @@ -import requests - from http import HTTPStatus + from django.http import HttpRequest -from pgvector.django import L2Distance +from ninja import Router -from bot.schemas import BotIn, BotOut from bot.controllers import query_llm_controller -from chunks.models import Chunk -from ninja import Router +from bot.schemas import BotIn, BotOut, BotError router = Router(tags=["Bot"]) -@router.post("/bot/", response={HTTPStatus.CREATED: BotOut}) +@router.post("/bot/", response={HTTPStatus.OK: BotOut}) def query_llm(request: HttpRequest, payload: BotIn): - # TODO: payload na embeding -> Vector -> Szukamy w bazie podobne -> dokument do payloada - response = requests.post("http://192.168.0.3:9000/v1/embeddings", data=payload.text) - - if not response.ok: - return HTTPStatus.INTERNAL_SERVER_ERROR - - print(response.content) - - # Chunk.objects.order_by(L2Distance('embedding', response)) - response = requests.post("0.0.0.0:9000/v1/completions", data=payload) - return query_llm_controller(response) + return query_llm_controller(payload) diff --git a/api/chunks/controllers.py b/api/chunks/controllers.py index 3019df4..9594c73 100644 --- a/api/chunks/controllers.py +++ b/api/chunks/controllers.py @@ -16,7 +16,7 @@ def create_chunk_controller(payload: ChunkIn) -> tuple[HTTPStatus, ChunkOut]: chunk_data["document_idx"] = document_instance response = requests.post( - url="http://0.0.0.0:9000/v1/embeddings", + url="http://0.0.0.0:9000/v1/embeddings/", data={"input": payload.text}) if response.ok: diff --git a/api/chunks/migrations/0001_initial.py b/api/chunks/migrations/0001_initial.py index 2c8fc6f..caa92cd 100644 --- a/api/chunks/migrations/0001_initial.py +++ b/api/chunks/migrations/0001_initial.py @@ -1,15 +1,16 @@ -# Generated by Django 5.0.2 on 2024-03-29 09:07 +# Generated by Django 4.2.13 on 2024-06-02 10:10 +from django.db import migrations, models import django.db.models.deletion import pgvector.django -from django.db import migrations, models class Migration(migrations.Migration): + initial = True dependencies = [ - ("documents", "0001_initial"), + ("documents", "0002_remove_document_embedding"), ] operations = [ @@ -26,7 +27,7 @@ class Migration(migrations.Migration): ), ), ("text", models.CharField(max_length=100)), - ("embedding", pgvector.django.VectorField(dimensions=10)), + ("embedding", pgvector.django.VectorField(dimensions=4096)), ("chunk_idx", models.IntegerField()), ("start_char", models.IntegerField()), ("end_char", models.IntegerField()), diff --git a/api/chunks/models.py b/api/chunks/models.py index 142fc34..5dab3a4 100644 --- a/api/chunks/models.py +++ b/api/chunks/models.py @@ -5,7 +5,7 @@ class Chunk(models.Model): text = models.CharField[str, str](max_length=100) - embedding = VectorField[list[float], VectorField](dimensions=10) + embedding = VectorField[list[float], VectorField](dimensions=4096) chunk_idx = models.IntegerField[int, int]() start_char = models.IntegerField[int, int]() end_char = models.IntegerField[int, int]()