Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-9hee committed Jan 18, 2025
2 parents 0bdd2ef + 44c17db commit 65d4ae6
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ RUN mkdir -p /code

WORKDIR /code

COPY . /code
RUN pip install poetry
COPY pyproject.toml poetry.lock /code/
RUN poetry config virtualenvs.create false
RUN poetry install --only main --no-root --no-interaction
COPY . /code
RUN #poetry install --only main --no-root --no-interaction
RUN pip install -r /code/requirements.txt

EXPOSE 8000

Expand Down
Empty file added apps/answer/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions apps/answer/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AnswerConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.answer"
5 changes: 5 additions & 0 deletions apps/answer/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ninja import FilterSchema


class AnswerFilter(FilterSchema):
pass
41 changes: 41 additions & 0 deletions apps/answer/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 5.1.5 on 2025-01-18 02:43

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='UserAnswer',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('question', models.CharField(max_length=150)),
],
options={
'db_table': 'user_answer',
},
),
migrations.CreateModel(
name='ModelAnswer',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('model_name', models.CharField(max_length=20)),
('answer', models.TextField()),
('user_answer', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='model_answers', to='answer.useranswer')),
],
options={
'db_table': 'model_answer',
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.5 on 2025-01-18 05:05

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('answer', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='modelanswer',
old_name='model_name',
new_name='model_id',
),
]
Empty file.
18 changes: 18 additions & 0 deletions apps/answer/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from contrib.django import models



class UserAnswer(models.BaseDateTimeModel):
question = models.CharField(max_length=150)

class Meta:
db_table = 'user_answer'


class ModelAnswer(models.BaseDateTimeModel):
model_id = models.CharField(max_length=20)
answer = models.TextField()
user_answer = models.ForeignKey('answer.UserAnswer', on_delete=models.DO_NOTHING, related_name='model_answers')

class Meta:
db_table = 'model_answer'
13 changes: 13 additions & 0 deletions apps/answer/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ninja import Schema


class ModelAnswerSchema(Schema):
model_id: str
answer: str


class UserAnswerSchema(Schema):
id: int
question: str
model_answers: list[ModelAnswerSchema]

15 changes: 15 additions & 0 deletions apps/answer/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from ninja import Router
from ninja.pagination import paginate

from apps.answer.models import UserAnswer
from apps.answer.schemas import UserAnswerSchema
from contrib.django.ninja.pagination import ContribPageNumberPagination

router = Router(tags=["Answer"])


@router.get('/answers', response=list[UserAnswerSchema])
@paginate(ContribPageNumberPagination, page_size=4)
def get_answers(request):
answers = UserAnswer.objects.all()
return answers
1 change: 1 addition & 0 deletions apps/chat/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MessageSchema(Schema):
message: str
models: list[str] = ['gpt-4o']


@router.post("/chat/stream")
def chat_stream(request, message_data: MessageSchema):
def event_stream():
Expand Down
15 changes: 14 additions & 1 deletion apps/chat/templates/chat/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,20 @@ <h3>모델 선택</h3>
<input type="text" id="message" placeholder="메시지를 입력하세요" />
<button onclick="sendMessage()">전송</button>
</div>
<div class="responses-container" id="responses-container"></div>
<div class="responses-container">
<div class="model-response">
<div class="model-title">GPT-4o</div>
<div id="gpt-4o-response" class="response-content"></div>
</div>
<div class="model-response">
<div class="model-title">GPT-4o mini</div>
<div id="gpt-4o-mini-response" class="response-content"></div>
</div>
<div class="model-response">
<div class="model-title">GPT-3.5 Turbo</div>
<div id="gpt-3.5-turbo-response" class="response-content"></div>
</div>
</div>
</div>

<script>
Expand Down
Empty file added apps/core/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions contrib/django/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from django.db.models import *
from .base import BaseDateTimeModel
Empty file.
31 changes: 31 additions & 0 deletions contrib/django/ninja/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any

from django.db.models import QuerySet
from ninja.pagination import PageNumberPagination


class ContribPageNumberPagination(PageNumberPagination):

class Input(PageNumberPagination.Input):
pass

class Output(PageNumberPagination.Output):
current_page: int
has_next: bool

def __init__(self, page_size: int = 10) -> None:
super().__init__(page_size)

def paginate_queryset(
self, queryset: QuerySet, pagination: Input, **params: Any
) -> Any:
offset = (pagination.page - 1) * self.page_size
items = queryset[offset : offset + self.page_size]
count = self._items_count(queryset)
return {
"items": items,
"count": count,
"current_page": pagination.page,
"has_next": self.page_size * pagination.page < count,
} # noqa: E203
return super().paginate_queryset(queryset, pagination, **params)
3 changes: 2 additions & 1 deletion django_project/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sentry_sdk.integrations.django import DjangoIntegration

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
Expand All @@ -42,6 +42,7 @@
"django.contrib.staticfiles",
"corsheaders",
'apps.chat', # chat 앱 등록
"apps.answer",
]

MIDDLEWARE = [
Expand Down
8 changes: 5 additions & 3 deletions django_project/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

ENVIRONMENT = "dev"

DEBUG = True


DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "postgres",
"HOST": "db.tkfurcrlobggbeotpevg.supabase.co",
"USER": "postgres",
"PASSWORD": "fAfQ6wutJVDRByjS",
"HOST": "fly-0-nrt.pooler.supabase.com",
"USER": "postgres.aqzyuyngwfdahoeccreq",
"PASSWORD": "UbsziHCGbA8ASkO4",
"PORT": "5432",
}
}
Expand Down
1 change: 1 addition & 0 deletions django_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

app.add_router("v1", "apps.core.views.router")
app.add_router("v1", "apps.chat.endpoints.router") # chat 앱의 router 추가
app.add_router("v1", "apps.answer.views.router")

urlpatterns = [
path("admin/", admin.site.urls),
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
annotated-types==0.7.0
anthropic==0.43.1
anyio==4.8.0
asgiref==3.8.1
cachetools==5.5.0
Expand All @@ -20,6 +19,7 @@ google-generativeai==0.8.3
googleapis-common-protos==1.66.0
grpcio==1.69.0
grpcio-status==1.69.0
gunicorn==23.0.0
h11==0.14.0
httpcore==1.0.7
httplib2==0.22.0
Expand All @@ -31,6 +31,8 @@ proto-plus==1.25.0
protobuf==5.29.3
pyasn1==0.6.1
pyasn1_modules==0.4.1
packaging==24.2
psycopg2-binary==2.9.10
pydantic==2.10.5
pydantic_core==2.27.2
pyparsing==3.2.1
Expand Down

0 comments on commit 65d4ae6

Please sign in to comment.