lollms-webui/endpoints/lollms_message.py

80 lines
2.8 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.
"""
from fastapi import APIRouter
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 api.db import DiscussionsDB
from safe_store.text_vectorizer import TextVectorizer, VectorizationMethod, VisualizationMethod
import tqdm
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):
client_id :str
id :int
message :str
metadata :dict
@router.get("/edit_message")
def edit_message(client_id:str,id:int, message:str,metadata:dict=None):
message_id = id
new_message = message
metadata = metadata
try:
lollmsElfServer.connections[client_id]["current_discussion"].edit_message(message_id, new_message,new_metadata=metadata)
return {"status": True}
except Exception as ex:
trace_exception(ex)
return {"status": False, "error":str(ex)}
class MessageRankParameters(BaseModel):
client_id :str
id :int
class MessageDeleteParameters(BaseModel):
client_id :str
id :int
@router.get("/message_rank_up")
def message_rank_up(client_id:str, id:int):
discussion_id = id
try:
new_rank = lollmsElfServer.connections[client_id]["current_discussion"].message_rank_up(discussion_id)
return {"status": True, "new_rank": new_rank}
except Exception as ex:
return {"status": False, "error":str(ex)}
@router.get("/message_rank_down")
def message_rank_down(client_id:str, id:int):
discussion_id = id
try:
new_rank = lollmsElfServer.connections[client_id]["current_discussion"].message_rank_down(discussion_id)
return {"status": True, "new_rank": new_rank}
except Exception as ex:
return {"status": False, "error":str(ex)}
@router.get("/delete_message")
def delete_message(client_id:str, id:int):
discussion_id = id
if lollmsElfServer.connections[client_id]["current_discussion"] is None:
return {"status": False,"message":"No discussion is selected"}
else:
new_rank = lollmsElfServer.connections[client_id]["current_discussion"].delete_message(discussion_id)
ASCIIColors.yellow("Message deleted")
return {"status":True,"new_rank": new_rank}