From f6893edcc2b1b25b6de94822d8fc208338892fb6 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 22 Oct 2024 23:24:49 -0700 Subject: [PATCH] feat: leaderboard rating --- .../open_webui/apps/webui/models/feedbacks.py | 14 +- .../apps/webui/routers/evaluations.py | 23 ++- src/lib/components/admin/Evaluations.svelte | 157 +++++++++++++++--- 3 files changed, 159 insertions(+), 35 deletions(-) diff --git a/backend/open_webui/apps/webui/models/feedbacks.py b/backend/open_webui/apps/webui/models/feedbacks.py index 7255b48fd..72b5fd79b 100644 --- a/backend/open_webui/apps/webui/models/feedbacks.py +++ b/backend/open_webui/apps/webui/models/feedbacks.py @@ -147,21 +147,29 @@ class FeedbackTable: with get_db() as db: return [ FeedbackModel.model_validate(feedback) - for feedback in db.query(Feedback).all() + for feedback in db.query(Feedback) + .order_by(Feedback.updated_at.desc()) + .all() ] def get_feedbacks_by_type(self, type: str) -> list[FeedbackModel]: with get_db() as db: return [ FeedbackModel.model_validate(feedback) - for feedback in db.query(Feedback).filter_by(type=type).all() + for feedback in db.query(Feedback) + .filter_by(type=type) + .order_by(Feedback.updated_at.desc()) + .all() ] def get_feedbacks_by_user_id(self, user_id: str) -> list[FeedbackModel]: with get_db() as db: return [ FeedbackModel.model_validate(feedback) - for feedback in db.query(Feedback).filter_by(user_id=user_id).all() + for feedback in db.query(Feedback) + .filter_by(user_id=user_id) + .order_by(Feedback.updated_at.desc()) + .all() ] def update_feedback_by_id( diff --git a/backend/open_webui/apps/webui/routers/evaluations.py b/backend/open_webui/apps/webui/routers/evaluations.py index f0e9236dc..9a6845efa 100644 --- a/backend/open_webui/apps/webui/routers/evaluations.py +++ b/backend/open_webui/apps/webui/routers/evaluations.py @@ -2,7 +2,7 @@ from typing import Optional from fastapi import APIRouter, Depends, HTTPException, status, Request from pydantic import BaseModel - +from open_webui.apps.webui.models.users import Users, UserModel from open_webui.apps.webui.models.feedbacks import ( FeedbackModel, FeedbackForm, @@ -67,18 +67,27 @@ async def delete_feedbacks(user=Depends(get_verified_user)): return success +class FeedbackUserModel(FeedbackModel): + user: Optional[UserModel] = None + + +@router.get("/feedbacks/all", response_model=list[FeedbackUserModel]) +async def get_all_feedbacks(user=Depends(get_admin_user)): + feedbacks = Feedbacks.get_all_feedbacks() + return [ + FeedbackUserModel( + **feedback.model_dump(), user=Users.get_user_by_id(feedback.user_id) + ) + for feedback in feedbacks + ] + + @router.delete("/feedbacks/all") async def delete_all_feedbacks(user=Depends(get_admin_user)): success = Feedbacks.delete_all_feedbacks() return success -@router.get("/feedbacks/all", response_model=list[FeedbackModel]) -async def get_all_feedbacks(user=Depends(get_admin_user)): - feedbacks = Feedbacks.get_all_feedbacks() - return feedbacks - - @router.post("/feedback", response_model=FeedbackModel) async def create_feedback( request: Request, diff --git a/src/lib/components/admin/Evaluations.svelte b/src/lib/components/admin/Evaluations.svelte index c9b678ffa..700659564 100644 --- a/src/lib/components/admin/Evaluations.svelte +++ b/src/lib/components/admin/Evaluations.svelte @@ -1,30 +1,107 @@