forked from open-webui/open-webui
fix: chat return type to dict
This commit is contained in:
parent
6350d86bde
commit
2cb0bf4439
3 changed files with 52 additions and 15 deletions
|
@ -44,8 +44,12 @@ class ChatForm(BaseModel):
|
||||||
chat: dict
|
chat: dict
|
||||||
|
|
||||||
|
|
||||||
class ChatUpdateForm(ChatForm):
|
class ChatResponse(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
|
user_id: str
|
||||||
|
title: str
|
||||||
|
chat: dict
|
||||||
|
timestamp: int # timestamp in epoch
|
||||||
|
|
||||||
|
|
||||||
class ChatTitleIdResponse(BaseModel):
|
class ChatTitleIdResponse(BaseModel):
|
||||||
|
@ -77,7 +81,11 @@ class ChatTable:
|
||||||
|
|
||||||
def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
|
def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
|
||||||
try:
|
try:
|
||||||
query = Chat.update(chat=json.dumps(chat)).where(Chat.id == id)
|
query = Chat.update(
|
||||||
|
chat=json.dumps(chat),
|
||||||
|
title=chat["title"] if "title" in chat else "New Chat",
|
||||||
|
timestamp=int(time.time()),
|
||||||
|
).where(Chat.id == id)
|
||||||
query.execute()
|
query.execute()
|
||||||
|
|
||||||
chat = Chat.get(Chat.id == id)
|
chat = Chat.get(Chat.id == id)
|
||||||
|
@ -92,6 +100,7 @@ class ChatTable:
|
||||||
ChatModel(**model_to_dict(chat))
|
ChatModel(**model_to_dict(chat))
|
||||||
for chat in Chat.select()
|
for chat in Chat.select()
|
||||||
.where(Chat.user_id == user_id)
|
.where(Chat.user_id == user_id)
|
||||||
|
.order_by(Chat.timestamp.desc())
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.offset(skip)
|
.offset(skip)
|
||||||
]
|
]
|
||||||
|
@ -109,5 +118,14 @@ class ChatTable:
|
||||||
for chat in Chat.select().limit(limit).offset(skip)
|
for chat in Chat.select().limit(limit).offset(skip)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
|
||||||
|
try:
|
||||||
|
query = Chat.delete().where((Chat.id == id) & (Chat.user_id == user_id))
|
||||||
|
query.execute() # Remove the rows, return number of rows removed.
|
||||||
|
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
Chats = ChatTable(DB)
|
Chats = ChatTable(DB)
|
||||||
|
|
|
@ -27,9 +27,6 @@ class User(Model):
|
||||||
|
|
||||||
|
|
||||||
class UserModel(BaseModel):
|
class UserModel(BaseModel):
|
||||||
class Config:
|
|
||||||
orm_mode = True
|
|
||||||
|
|
||||||
id: str
|
id: str
|
||||||
name: str
|
name: str
|
||||||
email: str
|
email: str
|
||||||
|
|
|
@ -5,12 +5,13 @@ from typing import List, Union, Optional
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
import json
|
||||||
|
|
||||||
from apps.web.models.users import Users
|
from apps.web.models.users import Users
|
||||||
from apps.web.models.chats import (
|
from apps.web.models.chats import (
|
||||||
ChatModel,
|
ChatModel,
|
||||||
|
ChatResponse,
|
||||||
ChatForm,
|
ChatForm,
|
||||||
ChatUpdateForm,
|
|
||||||
ChatTitleIdResponse,
|
ChatTitleIdResponse,
|
||||||
Chats,
|
Chats,
|
||||||
)
|
)
|
||||||
|
@ -46,13 +47,14 @@ async def get_user_chats(skip: int = 0, limit: int = 50, cred=Depends(bearer_sch
|
||||||
############################
|
############################
|
||||||
|
|
||||||
|
|
||||||
@router.post("/new", response_model=Optional[ChatModel])
|
@router.post("/new", response_model=Optional[ChatResponse])
|
||||||
async def create_new_chat(form_data: ChatForm, cred=Depends(bearer_scheme)):
|
async def create_new_chat(form_data: ChatForm, cred=Depends(bearer_scheme)):
|
||||||
token = cred.credentials
|
token = cred.credentials
|
||||||
user = Users.get_user_by_token(token)
|
user = Users.get_user_by_token(token)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
return Chats.insert_new_chat(user.id, form_data)
|
chat = Chats.insert_new_chat(user.id, form_data)
|
||||||
|
return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
@ -65,13 +67,14 @@ async def create_new_chat(form_data: ChatForm, cred=Depends(bearer_scheme)):
|
||||||
############################
|
############################
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{id}", response_model=Optional[ChatModel])
|
@router.get("/{id}", response_model=Optional[ChatResponse])
|
||||||
async def get_chat_by_id(id: str, cred=Depends(bearer_scheme)):
|
async def get_chat_by_id(id: str, cred=Depends(bearer_scheme)):
|
||||||
token = cred.credentials
|
token = cred.credentials
|
||||||
user = Users.get_user_by_token(token)
|
user = Users.get_user_by_token(token)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
return Chats.get_chat_by_id_and_user_id(id, user.id)
|
chat = Chats.get_chat_by_id_and_user_id(id, user.id)
|
||||||
|
return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
@ -84,17 +87,16 @@ async def get_chat_by_id(id: str, cred=Depends(bearer_scheme)):
|
||||||
############################
|
############################
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{id}", response_model=Optional[ChatModel])
|
@router.post("/{id}", response_model=Optional[ChatResponse])
|
||||||
async def update_chat_by_id(
|
async def update_chat_by_id(id: str, form_data: ChatForm, cred=Depends(bearer_scheme)):
|
||||||
id: str, form_data: ChatUpdateForm, cred=Depends(bearer_scheme)
|
|
||||||
):
|
|
||||||
token = cred.credentials
|
token = cred.credentials
|
||||||
user = Users.get_user_by_token(token)
|
user = Users.get_user_by_token(token)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
chat = Chats.get_chat_by_id_and_user_id(id, user.id)
|
chat = Chats.get_chat_by_id_and_user_id(id, user.id)
|
||||||
if chat:
|
if chat:
|
||||||
return Chats.update_chat_by_id(id, form_data.chat)
|
chat = Chats.update_chat_by_id(id, form_data.chat)
|
||||||
|
return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
@ -105,3 +107,23 @@ async def update_chat_by_id(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
############################
|
||||||
|
# DeleteChatById
|
||||||
|
############################
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{id}", response_model=bool)
|
||||||
|
async def delete_chat_by_id(id: str, cred=Depends(bearer_scheme)):
|
||||||
|
token = cred.credentials
|
||||||
|
user = Users.get_user_by_token(token)
|
||||||
|
|
||||||
|
if user:
|
||||||
|
result = Chats.delete_chat_by_id_and_user_id(id, user.id)
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
detail=ERROR_MESSAGES.INVALID_TOKEN,
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue