lollms-webui/endpoints/lollms_message.py

103 lines
3.9 KiB
Python
Raw Normal View History

2024-01-07 14:27:14 +00:00
"""
project: lollms_message
file: lollms_discussion.py
author: ParisNeo
description:
This module contains a set of FastAPI routes that provide information about the Lord of Large Language and Multimodal Systems (LoLLMs) Web UI
application. These routes allow users to manipulate the message elements.
"""
2024-02-18 23:23:15 +00:00
from fastapi import APIRouter, Body, Request
2024-02-17 01:08:14 +00:00
from pydantic import Field
2024-01-07 14:27:14 +00:00
from lollms_webui import LOLLMSWebUI
from pydantic import BaseModel
from starlette.responses import StreamingResponse
from lollms.types import MSG_TYPE
2024-01-07 19:44:59 +00:00
from lollms.utilities import detect_antiprompt, remove_text_from_string, trace_exception
2024-01-07 14:27:14 +00:00
from ascii_colors import ASCIIColors
from lollms.databases.discussions_database import DiscussionsDB
2024-03-28 22:58:51 +00:00
from lollms.security import forbid_remote_access
2024-01-07 14:27:14 +00:00
import tqdm
2024-02-17 23:14:52 +00:00
from typing import Any, Optional
from pydantic import BaseModel, ValidationError
2024-02-18 23:23:15 +00:00
import json
2024-01-09 22:26:41 +00:00
# ----------------------- Defining router and main class ------------------------------
2024-01-07 14:27:14 +00:00
router = APIRouter()
2024-01-07 19:44:59 +00:00
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
2024-01-07 14:27:14 +00:00
2024-01-07 19:44:59 +00:00
class EditMessageParameters(BaseModel):
2024-02-17 01:08:14 +00:00
client_id: str = Field(..., min_length=1)
2024-02-18 23:23:15 +00:00
id: int = Field(...)
message: str = Field(...)
metadata: list = Field(default=[])
2024-01-07 19:44:59 +00:00
2024-02-17 23:14:52 +00:00
@router.post("/edit_message")
2024-02-17 01:08:14 +00:00
async def edit_message(edit_params: EditMessageParameters):
2024-03-28 22:58:51 +00:00
forbid_remote_access(lollmsElfServer)
2024-02-17 01:08:14 +00:00
client_id = edit_params.client_id
message_id = edit_params.id
new_message = edit_params.message
2024-02-18 23:23:15 +00:00
metadata = json.dumps(edit_params.metadata,indent=4)
2024-01-07 19:44:59 +00:00
try:
lollmsElfServer.session.get_client(client_id).discussion.edit_message(message_id, new_message, new_metadata=metadata)
2024-01-07 19:44:59 +00:00
return {"status": True}
except Exception as ex:
2024-02-17 01:08:14 +00:00
trace_exception(ex) # Assuming 'trace_exception' function logs the error
return {"status": False, "error": "There was an error editing the message"}
2024-01-07 19:44:59 +00:00
2024-02-17 01:08:14 +00:00
class MessageRankParameters(BaseModel):
client_id: str = Field(..., min_length=1)
2024-02-18 23:23:15 +00:00
id: int = Field(...)
2024-01-07 19:44:59 +00:00
2024-02-17 23:14:52 +00:00
@router.post("/message_rank_up")
2024-02-17 01:08:14 +00:00
async def message_rank_up(rank_params: MessageRankParameters):
2024-03-28 22:58:51 +00:00
forbid_remote_access(lollmsElfServer)
2024-02-17 01:08:14 +00:00
client_id = rank_params.client_id
2024-02-17 23:14:52 +00:00
message_id = rank_params.id
2024-02-17 01:08:14 +00:00
2024-01-07 19:44:59 +00:00
try:
new_rank = lollmsElfServer.session.get_client(client_id).discussion.message_rank_up(message_id)
2024-01-07 19:44:59 +00:00
return {"status": True, "new_rank": new_rank}
except Exception as ex:
2024-02-17 01:08:14 +00:00
trace_exception(ex) # Assuming 'trace_exception' function logs the error
return {"status": False, "error": "There was an error ranking up the message"}
2024-01-07 19:44:59 +00:00
2024-02-17 23:14:52 +00:00
@router.post("/message_rank_down")
2024-02-17 01:08:14 +00:00
def message_rank_down(rank_params: MessageRankParameters):
2024-03-28 22:58:51 +00:00
forbid_remote_access(lollmsElfServer)
2024-02-17 01:08:14 +00:00
client_id = rank_params.client_id
2024-02-17 23:14:52 +00:00
message_id = rank_params.id
2024-01-07 19:44:59 +00:00
try:
new_rank = lollmsElfServer.session.get_client(client_id).discussion.message_rank_down(message_id)
2024-01-07 19:44:59 +00:00
return {"status": True, "new_rank": new_rank}
except Exception as ex:
return {"status": False, "error":str(ex)}
2024-02-17 01:08:14 +00:00
class MessageDeleteParameters(BaseModel):
client_id: str = Field(..., min_length=1)
2024-02-18 23:23:15 +00:00
id: int = Field(...)
2024-02-17 01:08:14 +00:00
2024-02-17 23:14:52 +00:00
@router.post("/delete_message")
2024-02-17 01:08:14 +00:00
async def delete_message(delete_params: MessageDeleteParameters):
2024-03-28 22:58:51 +00:00
forbid_remote_access(lollmsElfServer)
2024-02-17 01:08:14 +00:00
client_id = delete_params.client_id
2024-02-17 23:14:52 +00:00
message_id = delete_params.id
2024-02-17 01:08:14 +00:00
if lollmsElfServer.session.get_client(client_id).discussion is None:
2024-01-07 19:44:59 +00:00
return {"status": False,"message":"No discussion is selected"}
else:
2024-02-17 01:08:14 +00:00
try:
new_rank = lollmsElfServer.session.get_client(client_id).discussion.delete_message(message_id)
2024-02-17 01:08:14 +00:00
ASCIIColors.yellow("Message deleted")
return {"status":True,"new_rank": new_rank}
except Exception as ex:
trace_exception(ex) # Assuming 'trace_exception' function logs the error
return {"status": False, "error": "There was an error deleting the message"}
2024-02-17 23:14:52 +00:00