lollms-webui/endpoints/chat_bar.py

182 lines
7.2 KiB
Python
Raw Normal View History

2024-01-07 23:22:23 +00:00
"""
project: lollms_webui
file: chat_bar.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 are linked to lollms_webui chatbox
"""
2024-12-19 12:48:57 +00:00
2024-01-07 23:22:23 +00:00
import os
import platform
2024-12-19 12:48:57 +00:00
import shutil
import threading
2024-03-17 02:25:52 +00:00
from datetime import datetime
2024-12-19 12:48:57 +00:00
from functools import partial
from pathlib import Path
from typing import List
from urllib.parse import urlparse
import tqdm
from ascii_colors import ASCIIColors
from fastapi import (APIRouter, FastAPI, File, HTTPException, Request,
UploadFile)
from lollms.databases.discussions_database import DiscussionsDB
from lollms.internet import scrape_and_save
from lollms.main_config import BaseConfig
from lollms.security import forbid_remote_access, sanitize_path
from lollms.types import MSG_OPERATION_TYPE, SENDER_TYPES
from lollms.utilities import (detect_antiprompt,
find_first_available_file_index,
remove_text_from_string, trace_exception)
from pydantic import BaseModel, Field
from starlette.responses import StreamingResponse
from lollms_webui import LOLLMSWebUI
2024-01-07 23:22:23 +00:00
from utilities.execution_engines.latex_execution_engine import execute_latex
2024-12-19 12:48:57 +00:00
from utilities.execution_engines.python_execution_engine import execute_python
2024-01-07 23:22:23 +00:00
from utilities.execution_engines.shell_execution_engine import execute_bash
2024-12-19 12:48:57 +00:00
2024-01-09 22:26:41 +00:00
# ----------------------- Defining router and main class ------------------------------
2024-01-07 23:22:23 +00:00
router = APIRouter()
2024-12-19 12:48:57 +00:00
lollmsElfServer: LOLLMSWebUI = LOLLMSWebUI.get_instance()
2024-03-17 02:25:52 +00:00
class AddWebPageRequest(BaseModel):
2024-12-19 12:48:57 +00:00
client_id: str = Field(...)
2024-03-17 02:25:52 +00:00
url: str = Field(..., description="Url to be used")
2024-12-19 12:48:57 +00:00
2024-03-17 02:25:52 +00:00
class CmdExecutionRequest(BaseModel):
2024-12-19 12:48:57 +00:00
client_id: str = Field(...)
2024-03-17 02:25:52 +00:00
command: str = Field(..., description="Url to be used")
2024-03-28 22:58:51 +00:00
parameters: List[str] = Field(..., description="Command parameters")
2024-03-17 02:25:52 +00:00
2024-03-17 14:10:24 +00:00
"""
2024-03-17 02:25:52 +00:00
@router.post("/execute_personality_command")
async def execute_personality_command(request: CmdExecutionRequest):
client_id = request.client_id
client = lollmsElfServer.session.get_client(client_id)
lollmsElfServer.cancel_gen = False
client.generated_text=""
client.cancel_generation=False
client.continuing=False
client.first_chunk=True
if not lollmsElfServer.model:
ASCIIColors.error("Model not selected. Please select a model")
lollmsElfServer.error("Model not selected. Please select a model", client_id=client_id)
return {'status':False,"error":"Model not selected. Please select a model"}
if not lollmsElfServer.busy:
if lollmsElfServer.session.get_client(client_id).discussion is None:
if lollmsElfServer.db.does_last_discussion_have_messages():
lollmsElfServer.session.get_client(client_id).discussion = lollmsElfServer.db.create_discussion()
else:
lollmsElfServer.session.get_client(client_id).discussion = lollmsElfServer.db.load_last_discussion()
ump = lollmsElfServer.config.discussion_prompt_separator +lollmsElfServer.config.user_name.strip() if lollmsElfServer.config.use_user_name_in_discussions else lollmsElfServer.personality.user_message_prefix
message = lollmsElfServer.session.get_client(client_id).discussion.add_message(
2024-08-14 20:15:45 +00:00
message_type = MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_SET_CONTENT.value,
2024-03-17 02:25:52 +00:00
sender_type = SENDER_TYPES.SENDER_TYPES_USER.value,
sender = ump.replace(lollmsElfServer.config.discussion_prompt_separator,"").replace(":",""),
content="",
metadata=None,
parent_message_id=lollmsElfServer.message_id
)
lollmsElfServer.busy=True
command = request.command
parameters = request.parameters
lollmsElfServer.prepare_reception(client_id)
if lollmsElfServer.personality.processor is not None:
lollmsElfServer.start_time = datetime.now()
2024-08-15 15:28:33 +00:00
lollmsElfServer.personality.processor.callback = partial(lollmsElfServer.process_data, client_id=client_id)
2024-03-17 02:25:52 +00:00
lollmsElfServer.personality.processor.execute_command(command, parameters)
else:
lollmsElfServer.warning("Non scripted personalities do not support commands",client_id=client_id)
lollmsElfServer.close_message(client_id)
lollmsElfServer.busy=False
#tpe = threading.Thread(target=lollmsElfServer.start_message_generation, args=(message, message_id, client_id))
#tpe.start()
else:
lollmsElfServer.error("I am busy. Come back later.", client_id=client_id)
return {'status':False,"error":"I am busy. Come back later."}
lollmsElfServer.busy=False
return {'status':True,}
2024-03-17 14:10:24 +00:00
"""
2024-03-28 22:58:51 +00:00
MAX_PAGE_SIZE = 10000000
2024-03-17 02:25:52 +00:00
2024-12-19 12:48:57 +00:00
2024-03-17 02:25:52 +00:00
@router.post("/add_webpage")
async def add_webpage(request: AddWebPageRequest):
2024-03-28 22:58:51 +00:00
forbid_remote_access(lollmsElfServer)
2024-03-17 02:25:52 +00:00
client = lollmsElfServer.session.get_client(request.client_id)
2024-03-17 14:10:24 +00:00
if client is None:
2024-12-19 12:48:57 +00:00
raise HTTPException(
status_code=400,
detail="Unknown client. This service only accepts lollms webui requests",
)
2024-03-17 14:10:24 +00:00
def do_scraping():
lollmsElfServer.ShowBlockingMessage("Scraping web page\nPlease wait...")
ASCIIColors.yellow("Scaping web page")
client = lollmsElfServer.session.get_client(request.client_id)
url = request.url
2024-12-19 12:48:57 +00:00
index = find_first_available_file_index(
client.discussion.discussion_folder / "text_data", "web_", ".txt"
)
2024-03-31 01:01:12 +00:00
try:
2024-12-19 12:48:57 +00:00
file_path = sanitize_path(
str(
client.discussion.discussion_folder
/ "text_data"
/ f"web_{index}.txt"
),
True,
)
2024-03-31 01:01:12 +00:00
except Exception as ex:
lollmsElfServer.HideBlockingMessage()
raise ex
2024-03-28 22:58:51 +00:00
try:
result = urlparse(url)
if all([result.scheme, result.netloc]): # valid URL
2024-12-19 12:48:57 +00:00
if not scrape_and_save(
url=url, file_path=file_path, max_size=MAX_PAGE_SIZE
):
2024-03-31 01:01:12 +00:00
lollmsElfServer.HideBlockingMessage()
2024-03-28 22:58:51 +00:00
raise HTTPException(status_code=400, detail="Web page too large")
else:
2024-03-31 01:01:12 +00:00
lollmsElfServer.HideBlockingMessage()
2024-03-28 22:58:51 +00:00
raise HTTPException(status_code=400, detail="Invalid URL")
except Exception as e:
2024-03-31 01:01:12 +00:00
trace_exception(e)
lollmsElfServer.HideBlockingMessage()
2024-03-28 22:58:51 +00:00
raise HTTPException(status_code=400, detail=f"Exception : {e}")
2024-12-19 12:48:57 +00:00
2024-03-17 14:10:24 +00:00
try:
2024-12-19 12:48:57 +00:00
client.discussion.add_file(
file_path,
client,
partial(lollmsElfServer.process_data, client_id=request.client_id),
)
# File saved successfully
2024-03-17 14:10:24 +00:00
lollmsElfServer.HideBlockingMessage()
lollmsElfServer.refresh_files()
except Exception as e:
# Error occurred while saving the file
lollmsElfServer.HideBlockingMessage()
lollmsElfServer.refresh_files()
2024-12-19 12:48:57 +00:00
return {"status": False, "error": str(e)}
2024-03-17 14:10:24 +00:00
client.generation_thread = threading.Thread(target=do_scraping)
client.generation_thread.start()
2024-12-19 12:48:57 +00:00
return {"status": True}