Skip to content

Commit

Permalink
Change dimensions of VectorField, move logic from view to controler. …
Browse files Browse the repository at this point in the history
…Zbudowalismy go
  • Loading branch information
TheJimmyNowak committed Jun 2, 2024
1 parent 47a2263 commit 9da9801
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 36 deletions.
2 changes: 0 additions & 2 deletions api/bot/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from django.contrib import admin
from .models import Bot

admin.site.register(Bot)
25 changes: 22 additions & 3 deletions api/bot/controllers.py
Original file line number Diff line number Diff line change
@@ -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))
Empty file removed api/bot/migrations/__init__.py
Empty file.
5 changes: 0 additions & 5 deletions api/bot/models.py

This file was deleted.

8 changes: 6 additions & 2 deletions api/bot/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@


class BotIn(Schema):
text: str
input: str


class BotOut(Schema):
text: str
output: str


class BotError(Schema):
pass
23 changes: 5 additions & 18 deletions api/bot/views.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion api/chunks/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions api/chunks/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion api/chunks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
Expand Down

0 comments on commit 9da9801

Please sign in to comment.