pvd as deps arg

This commit is contained in:
Jeremy Yin 2024-03-15 15:01:45 +08:00
parent 0769004ebb
commit 14c6b5933c
4 changed files with 23 additions and 9 deletions

View File

@ -1,6 +1,10 @@
from pydantic import BaseModel from pydantic import BaseModel
class Context(BaseModel):
user_name: str
class GetAiChatResponseInput(BaseModel): class GetAiChatResponseInput(BaseModel):
message: str message: str
user_name: str user_name: str

View File

@ -4,13 +4,14 @@ from fastapi import FastAPI, Request
from dotenv import load_dotenv from dotenv import load_dotenv
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
from simplylab.entity import GetAiChatResponseInput from simplylab.entity import GetAiChatResponseInput, Context
from simplylab.entity import GetAiChatResponseOutput from simplylab.entity import GetAiChatResponseOutput
from simplylab.entity import GetUserChatHistoryInput from simplylab.entity import GetUserChatHistoryInput
from simplylab.entity import GetUserChatHistoryOutput from simplylab.entity import GetUserChatHistoryOutput
from simplylab.entity import GetChatStatusTodayInput from simplylab.entity import GetChatStatusTodayInput
from simplylab.entity import GetChatStatusTodayOutput from simplylab.entity import GetChatStatusTodayOutput
from simplylab.error import Error from simplylab.error import Error
from simplylab.providers import Providers
from simplylab.services import Services from simplylab.services import Services
load_dotenv() load_dotenv()
@ -32,20 +33,26 @@ async def hi():
@app.post("/api/v1/get_ai_chat_response") @app.post("/api/v1/get_ai_chat_response")
async def get_ai_chat_response(req: GetAiChatResponseInput) -> GetAiChatResponseOutput: async def get_ai_chat_response(req: GetAiChatResponseInput) -> GetAiChatResponseOutput:
svc = Services(req) ctx = Context(user_name=req.user_name)
pvd = Providers()
svc = Services(ctx, pvd)
res = await svc.chat.get_ai_chat_response(req) res = await svc.chat.get_ai_chat_response(req)
return res return res
@app.post("/api/v1/get_user_chat_history") @app.post("/api/v1/get_user_chat_history")
async def get_user_chat_history(req: GetUserChatHistoryInput) -> GetUserChatHistoryOutput: async def get_user_chat_history(req: GetUserChatHistoryInput) -> GetUserChatHistoryOutput:
svc = Services(req) ctx = Context(user_name=req.user_name)
pvd = Providers()
svc = Services(ctx, pvd)
res = await svc.chat.get_user_chat_history(req) res = await svc.chat.get_user_chat_history(req)
return res return res
@app.post("/api/v1/get_chat_status_today") @app.post("/api/v1/get_chat_status_today")
async def get_chat_status_today(req: GetChatStatusTodayInput) -> GetChatStatusTodayOutput: async def get_chat_status_today(req: GetChatStatusTodayInput) -> GetChatStatusTodayOutput:
svc = Services(req) ctx = Context(user_name=req.user_name)
pvd = Providers()
svc = Services(ctx, pvd)
res = await svc.chat.get_chat_status_today(req) res = await svc.chat.get_chat_status_today(req)
return res return res

View File

@ -1,12 +1,15 @@
from typing import Any from typing import Any
from simplylab.entity import Context
from simplylab.providers import Providers
from simplylab.services.chat import ChatService from simplylab.services.chat import ChatService
class Services: class Services:
def __init__(self, ctx: Any): def __init__(self, ctx: Context, providers: Providers):
self.ctx = ctx self.ctx = ctx
self.pvd = providers
@property @property
def chat(self): def chat(self):
return ChatService(self.ctx) return ChatService(self.ctx, self.pvd)

View File

@ -1,14 +1,14 @@
from typing import Any from typing import Any
from simplylab.entity import GetAiChatResponseInput, GetUserChatHistoryInput, GetChatStatusTodayInput, UserChatMessage, \ from simplylab.entity import GetAiChatResponseInput, GetUserChatHistoryInput, GetChatStatusTodayInput, UserChatMessage, \
GetChatStatusTodayOutput, GetAiChatResponseOutput, GetUserChatHistoryOutput GetChatStatusTodayOutput, GetAiChatResponseOutput, GetUserChatHistoryOutput, Context
from simplylab.providers import Providers from simplylab.providers import Providers
class ChatService: class ChatService:
def __init__(self, ctx: Any): def __init__(self, ctx: Context, provider: Providers):
self.ctx = ctx self.ctx = ctx
self.pvd = Providers() self.pvd = provider
async def get_ai_chat_response(self, req: GetAiChatResponseInput) -> GetAiChatResponseOutput: async def get_ai_chat_response(self, req: GetAiChatResponseInput) -> GetAiChatResponseOutput:
message = req.message message = req.message