remove models update from migrations (#80)

* add bot models update script

* add last question at field

* update README.md
This commit is contained in:
Dmitry Afanasyev
2024-01-08 21:33:35 +03:00
committed by GitHub
parent 7cbe7b7c50
commit 28895f3510
16 changed files with 191 additions and 145 deletions

View File

@@ -19,9 +19,7 @@ class User(Base):
hashed_password: Mapped[str] = mapped_column(String(length=1024), nullable=False)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
created_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), index=True, nullable=False, default=datetime.now
)
created_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False, default=datetime.now)
user_question_count: Mapped["UserQuestionCount"] = relationship(
"UserQuestionCount",
@@ -38,6 +36,12 @@ class User(Base):
return self.user_question_count.question_count
return 0
@property
def last_question_at(self) -> str | None:
if self.user_question_count:
return self.user_question_count.last_question_at.strftime("%Y-%m-%d %H:%M:%S")
return None
@classmethod
def build(
cls,
@@ -70,9 +74,7 @@ class AccessToken(Base):
user_id = mapped_column(INTEGER, ForeignKey("users.id", ondelete="cascade"), nullable=False)
token: Mapped[str] = mapped_column(String(length=42), primary_key=True, default=lambda: str(uuid.uuid4()))
created_at: Mapped[datetime] = mapped_column(
TIMESTAMP(timezone=True), index=True, nullable=False, default=datetime.now
)
created_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False, default=datetime.now)
user: Mapped["User"] = relationship(
"User",
@@ -94,3 +96,4 @@ class UserQuestionCount(Base):
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)
last_question_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False, default=datetime.now)

View File

@@ -1,6 +1,6 @@
from dataclasses import dataclass
from sqlalchemy import select
from sqlalchemy import func, select
from sqlalchemy.dialects.sqlite import insert
from sqlalchemy.orm import load_only
@@ -69,7 +69,8 @@ class UserRepository:
UserQuestionCount.get_real_column_name(
UserQuestionCount.question_count.key
): UserQuestionCount.question_count
+ 1
+ 1,
UserQuestionCount.get_real_column_name(UserQuestionCount.last_question_at.key): func.now(),
},
)
)

View File

@@ -0,0 +1,13 @@
import asyncio
import sys
from pathlib import Path
from loguru import logger
if __name__ == "__main__":
sys.path.append(str(Path(__file__).parent.parent.parent.parent))
from core.bot.services import ChatGptService
chatgpt_service = ChatGptService.build()
asyncio.run(chatgpt_service.update_chatgpt_models())
logger.info("chatgpt models has been updated")

View File

@@ -38,6 +38,18 @@ class ChatGPTRepository:
async with self.db.session() as session:
await session.execute(query)
async def delete_all_chatgpt_models(self) -> None:
query = delete(ChatGptModels)
async with self.db.session() as session:
await session.execute(query)
async def bulk_insert_chatgpt_models(self, models_priority: list[dict[str, Any]]) -> None:
models = [ChatGptModels(**model_priority) for model_priority in models_priority]
async with self.db.session() as session:
session.add_all(models)
await session.commit()
async def add_chatgpt_model(self, model: str, priority: int) -> dict[str, str | int]:
query = (
insert(ChatGptModels)

View File

@@ -14,7 +14,7 @@ from speech_recognition import (
UnknownValueError as SpeechRecognizerError,
)
from constants import AUDIO_SEGMENT_DURATION
from constants import AUDIO_SEGMENT_DURATION, ChatGptModelsEnum
from core.auth.models.users import User
from core.auth.repository import UserRepository
from core.auth.services import UserService
@@ -24,6 +24,77 @@ from infra.database.db_adapter import Database
from settings.config import settings
@dataclass
class ChatGptService:
repository: ChatGPTRepository
user_service: UserService
@classmethod
def build(cls) -> "ChatGptService":
db = Database(settings=settings)
repository = ChatGPTRepository(settings=settings, db=db)
user_repository = UserRepository(db=db)
user_service = UserService(repository=user_repository)
return ChatGptService(repository=repository, user_service=user_service)
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:
question = question or "Привет!"
chatgpt_model = await self.get_current_chatgpt_model()
return await self.repository.ask_question(question=question, chatgpt_model=chatgpt_model)
async def request_to_chatgpt_microservice(self, question: str) -> Response:
chatgpt_model = await self.get_current_chatgpt_model()
return await self.repository.request_to_chatgpt_microservice(question=question, chatgpt_model=chatgpt_model)
async def get_current_chatgpt_model(self) -> str:
return await self.repository.get_current_chatgpt_model()
async def change_chatgpt_model_priority(self, model_id: int, priority: int) -> None:
return await self.repository.change_chatgpt_model_priority(model_id=model_id, priority=priority)
async def update_chatgpt_models(self) -> None:
await self.repository.delete_all_chatgpt_models()
models = ChatGptModelsEnum.base_models_priority()
await self.repository.bulk_insert_chatgpt_models(models)
async def reset_all_chatgpt_models_priority(self) -> None:
return await self.repository.reset_all_chatgpt_models_priority()
async def add_chatgpt_model(self, gpt_model: str, priority: int) -> dict[str, str | int]:
return await self.repository.add_chatgpt_model(model=gpt_model, priority=priority)
async def delete_chatgpt_model(self, model_id: int) -> None:
return await self.repository.delete_chatgpt_model(model_id=model_id)
async def get_or_create_bot_user(
self,
user_id: int,
email: str | None = None,
username: str | None = None,
first_name: str | None = None,
last_name: str | None = None,
ban_reason: str | None = None,
is_active: bool = True,
is_superuser: bool = False,
) -> User:
return await self.user_service.get_or_create_user_by_id(
user_id=user_id,
email=email,
username=username,
first_name=first_name,
last_name=last_name,
ban_reason=ban_reason,
is_active=is_active,
is_superuser=is_superuser,
)
async def update_bot_user_message_count(self, user_id: int) -> None:
await self.user_service.update_user_message_count(user_id)
class SpeechToTextService:
def __init__(self, filename: str) -> None:
self.filename = filename
@@ -87,69 +158,3 @@ class SpeechToTextService:
except SpeechRecognizerError as error:
logger.error("error recognizing text with google", error=error)
raise
@dataclass
class ChatGptService:
repository: ChatGPTRepository
user_service: UserService
@classmethod
def build(cls) -> "ChatGptService":
db = Database(settings=settings)
repository = ChatGPTRepository(settings=settings, db=db)
user_repository = UserRepository(db=db)
user_service = UserService(repository=user_repository)
return ChatGptService(repository=repository, user_service=user_service)
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:
question = question or "Привет!"
chatgpt_model = await self.get_current_chatgpt_model()
return await self.repository.ask_question(question=question, chatgpt_model=chatgpt_model)
async def request_to_chatgpt_microservice(self, question: str) -> Response:
chatgpt_model = await self.get_current_chatgpt_model()
return await self.repository.request_to_chatgpt_microservice(question=question, chatgpt_model=chatgpt_model)
async def get_current_chatgpt_model(self) -> str:
return await self.repository.get_current_chatgpt_model()
async def change_chatgpt_model_priority(self, model_id: int, priority: int) -> None:
return await self.repository.change_chatgpt_model_priority(model_id=model_id, priority=priority)
async def reset_all_chatgpt_models_priority(self) -> None:
return await self.repository.reset_all_chatgpt_models_priority()
async def add_chatgpt_model(self, gpt_model: str, priority: int) -> dict[str, str | int]:
return await self.repository.add_chatgpt_model(model=gpt_model, priority=priority)
async def delete_chatgpt_model(self, model_id: int) -> None:
return await self.repository.delete_chatgpt_model(model_id=model_id)
async def get_or_create_bot_user(
self,
user_id: int,
email: str | None = None,
username: str | None = None,
first_name: str | None = None,
last_name: str | None = None,
ban_reason: str | None = None,
is_active: bool = True,
is_superuser: bool = False,
) -> User:
return await self.user_service.get_or_create_user_by_id(
user_id=user_id,
email=email,
username=username,
first_name=first_name,
last_name=last_name,
ban_reason=ban_reason,
is_active=is_active,
is_superuser=is_superuser,
)
async def update_bot_user_message_count(self, user_id: int) -> None:
await self.user_service.update_user_message_count(user_id)