mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-16 21:20:39 +03:00
add question count table (#73)
* update admin url * update log level * add user question count table * rename ChatGpt to ChatGptModels * change user to root in ci tests * add chatgpt_shared volume
This commit is contained in:
@@ -20,3 +20,10 @@ class AccessToken(SQLAlchemyBaseAccessTokenTable[Mapped[int]], Base):
|
||||
@declared_attr
|
||||
def user_id(cls) -> Mapped[int]:
|
||||
return mapped_column(INTEGER, ForeignKey("users.id", ondelete="cascade"), nullable=False)
|
||||
|
||||
|
||||
class UserQuestionCount(Base):
|
||||
__tablename__ = "user_question_count" # type: ignore[assignment]
|
||||
|
||||
user_id: Mapped[int] = mapped_column(INTEGER, ForeignKey("users.id", ondelete="cascade"), primary_key=True)
|
||||
question_count: Mapped[int] = mapped_column(INTEGER, default=0, nullable=False)
|
||||
|
||||
@@ -3,10 +3,12 @@ from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from infra.database.base import Base
|
||||
|
||||
__slots__ = ("ChatGpt",)
|
||||
__slots__ = ("ChatGptModels",)
|
||||
|
||||
|
||||
class ChatGpt(Base):
|
||||
class ChatGptModels(Base):
|
||||
__tablename__ = "chatgpt_models" # type: ignore[assignment]
|
||||
|
||||
id: Mapped[int] = mapped_column("id", INTEGER(), primary_key=True, autoincrement=True)
|
||||
model: Mapped[str] = mapped_column("model", VARCHAR(length=256), nullable=False, unique=True)
|
||||
priority: Mapped[int] = mapped_column("priority", SMALLINT(), default=0)
|
||||
@@ -10,7 +10,7 @@ from sqlalchemy import delete, desc, select, update
|
||||
from sqlalchemy.dialects.sqlite import insert
|
||||
|
||||
from constants import INVALID_GPT_REQUEST_MESSAGES
|
||||
from core.bot.models.chat_gpt import ChatGpt
|
||||
from core.bot.models.chatgpt import ChatGptModels
|
||||
from infra.database.db_adapter import Database
|
||||
from settings.config import AppSettings
|
||||
|
||||
@@ -20,29 +20,29 @@ class ChatGPTRepository:
|
||||
settings: AppSettings
|
||||
db: Database
|
||||
|
||||
async def get_chatgpt_models(self) -> Sequence[ChatGpt]:
|
||||
query = select(ChatGpt).order_by(desc(ChatGpt.priority))
|
||||
async def get_chatgpt_models(self) -> Sequence[ChatGptModels]:
|
||||
query = select(ChatGptModels).order_by(desc(ChatGptModels.priority))
|
||||
|
||||
async with self.db.session() as session:
|
||||
result = await session.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
async def change_chatgpt_model_priority(self, model_id: int, priority: int) -> None:
|
||||
query = update(ChatGpt).values(priority=priority).filter(ChatGpt.id == model_id)
|
||||
query = update(ChatGptModels).values(priority=priority).filter(ChatGptModels.id == model_id)
|
||||
async with self.db.get_transaction_session() as session:
|
||||
await session.execute(query)
|
||||
|
||||
async def reset_all_chatgpt_models_priority(self) -> None:
|
||||
query = update(ChatGpt).values(priority=0)
|
||||
query = update(ChatGptModels).values(priority=0)
|
||||
|
||||
async with self.db.session() as session:
|
||||
await session.execute(query)
|
||||
|
||||
async def add_chatgpt_model(self, model: str, priority: int) -> dict[str, str | int]:
|
||||
query = (
|
||||
insert(ChatGpt)
|
||||
insert(ChatGptModels)
|
||||
.values(
|
||||
{ChatGpt.model: model, ChatGpt.priority: priority},
|
||||
{ChatGptModels.model: model, ChatGptModels.priority: priority},
|
||||
)
|
||||
.prefix_with("OR IGNORE")
|
||||
)
|
||||
@@ -52,13 +52,13 @@ class ChatGPTRepository:
|
||||
return {"model": model, "priority": priority}
|
||||
|
||||
async def delete_chatgpt_model(self, model_id: int) -> None:
|
||||
query = delete(ChatGpt).filter_by(id=model_id)
|
||||
query = delete(ChatGptModels).filter_by(id=model_id)
|
||||
|
||||
async with self.db.session() as session:
|
||||
await session.execute(query)
|
||||
|
||||
async def get_current_chatgpt_model(self) -> str:
|
||||
query = select(ChatGpt.model).order_by(desc(ChatGpt.priority)).limit(1)
|
||||
query = select(ChatGptModels.model).order_by(desc(ChatGptModels.priority)).limit(1)
|
||||
|
||||
async with self.db.session() as session:
|
||||
result = await session.execute(query)
|
||||
|
||||
@@ -15,7 +15,7 @@ from speech_recognition import (
|
||||
)
|
||||
|
||||
from constants import AUDIO_SEGMENT_DURATION
|
||||
from core.bot.models.chat_gpt import ChatGpt
|
||||
from core.bot.models.chatgpt import ChatGptModels
|
||||
from core.bot.repository import ChatGPTRepository
|
||||
from infra.database.db_adapter import Database
|
||||
from settings.config import settings
|
||||
@@ -90,7 +90,7 @@ class SpeechToTextService:
|
||||
class ChatGptService:
|
||||
repository: ChatGPTRepository
|
||||
|
||||
async def get_chatgpt_models(self) -> Sequence[ChatGpt]:
|
||||
async def get_chatgpt_models(self) -> Sequence[ChatGptModels]:
|
||||
return await self.repository.get_chatgpt_models()
|
||||
|
||||
async def request_to_chatgpt(self, question: str | None) -> str:
|
||||
|
||||
Reference in New Issue
Block a user