2024-01-07 02:18:13 +00:00
|
|
|
"""
|
|
|
|
project: lollms
|
|
|
|
file: lollms_generation_events.py
|
|
|
|
author: ParisNeo
|
|
|
|
description:
|
|
|
|
This module contains a set of Socketio routes that provide information about the Lord of Large Language and Multimodal Systems (LoLLMs) Web UI
|
|
|
|
application. These routes are specific to text generation operation
|
|
|
|
|
|
|
|
"""
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import threading
|
|
|
|
from datetime import datetime
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import List
|
|
|
|
|
2024-01-07 02:18:13 +00:00
|
|
|
import pkg_resources
|
2024-12-19 12:48:57 +00:00
|
|
|
import socketio
|
|
|
|
from ascii_colors import ASCIIColors
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
2024-01-07 02:18:13 +00:00
|
|
|
from fastapi.responses import FileResponse
|
|
|
|
from lollms.binding import BindingBuilder, InstallOption
|
2024-08-14 20:15:45 +00:00
|
|
|
from lollms.personality import AIPersonality
|
2024-03-28 22:58:51 +00:00
|
|
|
from lollms.security import forbid_remote_access
|
2024-12-19 12:48:57 +00:00
|
|
|
from lollms.server.elf_server import LOLLMSElfServer
|
|
|
|
from lollms.types import MSG_OPERATION_TYPE, SENDER_TYPES
|
|
|
|
from lollms.utilities import (convert_language_name,
|
|
|
|
find_first_available_file_index, gc, load_config,
|
|
|
|
trace_exception)
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2024-01-07 02:18:13 +00:00
|
|
|
from lollms_webui import LOLLMSWebUI
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
lollmsElfServer = LOLLMSWebUI.get_instance()
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------- events -----------------------------------------
|
2024-12-19 12:48:57 +00:00
|
|
|
def add_events(sio: socketio):
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
@sio.on("generate_msg")
|
|
|
|
def handle_generate_msg(sid, data, use_threading=True):
|
2024-01-07 02:18:13 +00:00
|
|
|
client_id = sid
|
|
|
|
lollmsElfServer.cancel_gen = False
|
2024-02-26 00:55:44 +00:00
|
|
|
client = lollmsElfServer.session.get_client(client_id)
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
client.generated_text = ""
|
|
|
|
client.cancel_generation = False
|
|
|
|
client.continuing = False
|
|
|
|
client.first_chunk = True
|
|
|
|
|
2024-01-07 02:18:13 +00:00
|
|
|
if not lollmsElfServer.model:
|
|
|
|
ASCIIColors.error("Model not selected. Please select a model")
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.error(
|
|
|
|
"Model not selected. Please select a model", client_id=client_id
|
|
|
|
)
|
2024-01-07 02:18:13 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not lollmsElfServer.busy:
|
2024-02-26 00:55:44 +00:00
|
|
|
if lollmsElfServer.session.get_client(client_id).discussion is None:
|
2024-01-07 02:18:13 +00:00
|
|
|
if lollmsElfServer.db.does_last_discussion_have_messages():
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.session.get_client(client_id).discussion = (
|
|
|
|
lollmsElfServer.db.create_discussion()
|
|
|
|
)
|
2024-01-07 02:18:13 +00:00
|
|
|
else:
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.session.get_client(client_id).discussion = (
|
|
|
|
lollmsElfServer.db.load_last_discussion()
|
|
|
|
)
|
2024-01-07 02:18:13 +00:00
|
|
|
|
|
|
|
prompt = data["prompt"]
|
2024-04-20 22:13:15 +00:00
|
|
|
try:
|
|
|
|
nb_tokens = len(lollmsElfServer.model.tokenize(prompt))
|
|
|
|
except:
|
|
|
|
nb_tokens = None
|
2024-12-19 12:48:57 +00:00
|
|
|
created_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
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(
|
|
|
|
message_type=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_SET_CONTENT.value,
|
|
|
|
sender_type=SENDER_TYPES.SENDER_TYPES_USER.value,
|
|
|
|
sender=ump.replace(
|
|
|
|
lollmsElfServer.config.discussion_prompt_separator, ""
|
|
|
|
).replace(":", ""),
|
2024-01-07 02:18:13 +00:00
|
|
|
content=prompt,
|
2024-08-14 20:15:45 +00:00
|
|
|
steps=[],
|
2024-01-07 02:18:13 +00:00
|
|
|
metadata=None,
|
2024-04-20 22:13:15 +00:00
|
|
|
parent_message_id=lollmsElfServer.message_id,
|
|
|
|
created_at=created_at,
|
2024-12-19 12:48:57 +00:00
|
|
|
nb_tokens=nb_tokens,
|
2024-01-07 02:18:13 +00:00
|
|
|
)
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
ASCIIColors.green(
|
|
|
|
"Starting message generation by " + lollmsElfServer.personality.name
|
|
|
|
)
|
2024-05-22 13:55:43 +00:00
|
|
|
if use_threading:
|
2024-12-19 12:48:57 +00:00
|
|
|
client.generation_thread = threading.Thread(
|
|
|
|
target=lollmsElfServer.start_message_generation,
|
|
|
|
args=(message, message.id, client_id),
|
|
|
|
)
|
2024-05-22 13:55:43 +00:00
|
|
|
client.generation_thread.start()
|
|
|
|
else:
|
|
|
|
lollmsElfServer.start_message_generation(message, message.id, client_id)
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-01-18 07:57:00 +00:00
|
|
|
# lollmsElfServer.sio.sleep(0.01)
|
2024-01-07 02:18:13 +00:00
|
|
|
ASCIIColors.info("Started generation task")
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.busy = True
|
|
|
|
# tpe = threading.Thread(target=lollmsElfServer.start_message_generation, args=(message, message_id, client_id))
|
|
|
|
# tpe.start()
|
2024-01-07 02:18:13 +00:00
|
|
|
else:
|
|
|
|
lollmsElfServer.error("I am busy. Come back later.", client_id=client_id)
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
@sio.on("generate_msg_with_internet")
|
|
|
|
def generate_msg_with_internet(sid, data):
|
2024-03-15 00:34:21 +00:00
|
|
|
client_id = sid
|
|
|
|
lollmsElfServer.cancel_gen = False
|
|
|
|
client = lollmsElfServer.session.get_client(client_id)
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
client.generated_text = ""
|
|
|
|
client.cancel_generation = False
|
|
|
|
client.continuing = False
|
|
|
|
client.first_chunk = True
|
2024-03-15 00:34:21 +00:00
|
|
|
|
|
|
|
if not lollmsElfServer.model:
|
|
|
|
ASCIIColors.error("Model not selected. Please select a model")
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.error(
|
|
|
|
"Model not selected. Please select a model", client_id=client_id
|
|
|
|
)
|
2024-03-15 00:34:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not lollmsElfServer.busy:
|
|
|
|
if lollmsElfServer.session.get_client(client_id).discussion is None:
|
|
|
|
if lollmsElfServer.db.does_last_discussion_have_messages():
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.session.get_client(client_id).discussion = (
|
|
|
|
lollmsElfServer.db.create_discussion()
|
|
|
|
)
|
2024-03-15 00:34:21 +00:00
|
|
|
else:
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.session.get_client(client_id).discussion = (
|
|
|
|
lollmsElfServer.db.load_last_discussion()
|
|
|
|
)
|
2024-03-15 00:34:21 +00:00
|
|
|
|
|
|
|
prompt = data["prompt"]
|
2024-04-20 22:13:15 +00:00
|
|
|
try:
|
|
|
|
nb_tokens = len(lollmsElfServer.model.tokenize(prompt))
|
|
|
|
except:
|
|
|
|
nb_tokens = None
|
2024-12-19 12:48:57 +00:00
|
|
|
created_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
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(
|
|
|
|
message_type=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_SET_CONTENT.value,
|
|
|
|
sender_type=SENDER_TYPES.SENDER_TYPES_USER.value,
|
|
|
|
sender=ump.replace(
|
|
|
|
lollmsElfServer.config.discussion_prompt_separator, ""
|
|
|
|
).replace(":", ""),
|
|
|
|
content=prompt,
|
|
|
|
steps=[],
|
|
|
|
metadata=None,
|
2024-04-20 22:13:15 +00:00
|
|
|
parent_message_id=lollmsElfServer.message_id,
|
|
|
|
created_at=created_at,
|
2024-12-19 12:48:57 +00:00
|
|
|
nb_tokens=nb_tokens,
|
2024-03-15 00:34:21 +00:00
|
|
|
)
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
ASCIIColors.green(
|
|
|
|
"Starting message generation by " + lollmsElfServer.personality.name
|
|
|
|
)
|
|
|
|
|
|
|
|
client.generation_thread = threading.Thread(
|
|
|
|
target=lollmsElfServer.start_message_generation,
|
|
|
|
args=(message, message.id, client_id, False, None, True),
|
|
|
|
)
|
2024-03-15 00:34:21 +00:00
|
|
|
client.generation_thread.start()
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-03-15 00:34:21 +00:00
|
|
|
# lollmsElfServer.sio.sleep(0.01)
|
|
|
|
ASCIIColors.info("Started generation task")
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.busy = True
|
|
|
|
# tpe = threading.Thread(target=lollmsElfServer.start_message_generation, args=(message, message_id, client_id))
|
|
|
|
# tpe.start()
|
2024-03-15 00:34:21 +00:00
|
|
|
else:
|
|
|
|
lollmsElfServer.error("I am busy. Come back later.", client_id=client_id)
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
@sio.on("generate_msg_from")
|
2024-01-07 14:27:14 +00:00
|
|
|
def handle_generate_msg_from(sid, data):
|
2024-01-07 02:18:13 +00:00
|
|
|
client_id = sid
|
2024-02-26 00:55:44 +00:00
|
|
|
client = lollmsElfServer.session.get_client(client_id)
|
2024-01-07 02:18:13 +00:00
|
|
|
lollmsElfServer.cancel_gen = False
|
2024-12-19 12:48:57 +00:00
|
|
|
client.continuing = False
|
|
|
|
client.first_chunk = True
|
|
|
|
|
2024-02-26 00:55:44 +00:00
|
|
|
if lollmsElfServer.session.get_client(client_id).discussion is None:
|
2024-01-07 02:18:13 +00:00
|
|
|
ASCIIColors.warning("Please select a discussion")
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.error(
|
|
|
|
"Please select a discussion first", client_id=client_id
|
|
|
|
)
|
2024-01-07 02:18:13 +00:00
|
|
|
return
|
2024-12-19 12:48:57 +00:00
|
|
|
id_ = data["id"]
|
|
|
|
generation_type = data.get("msg_type", None)
|
|
|
|
if id_ == -1:
|
|
|
|
message = lollmsElfServer.session.get_client(
|
|
|
|
client_id
|
|
|
|
).discussion.current_message
|
2024-01-07 02:18:13 +00:00
|
|
|
else:
|
2024-12-19 12:48:57 +00:00
|
|
|
message = lollmsElfServer.session.get_client(
|
|
|
|
client_id
|
|
|
|
).discussion.load_message(id_)
|
2024-01-07 02:18:13 +00:00
|
|
|
if message is None:
|
2024-12-19 12:48:57 +00:00
|
|
|
return
|
|
|
|
client.generation_thread = threading.Thread(
|
|
|
|
target=lollmsElfServer.start_message_generation,
|
|
|
|
args=(message, message.id, client_id, False, generation_type),
|
|
|
|
)
|
2024-02-26 00:55:44 +00:00
|
|
|
client.generation_thread.start()
|
2024-01-07 02:18:13 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
@sio.on("continue_generate_msg_from")
|
2024-01-07 14:27:14 +00:00
|
|
|
def handle_continue_generate_msg_from(sid, data):
|
2024-01-07 02:18:13 +00:00
|
|
|
client_id = sid
|
2024-02-26 00:55:44 +00:00
|
|
|
client = lollmsElfServer.session.get_client(client_id)
|
2024-01-07 02:18:13 +00:00
|
|
|
lollmsElfServer.cancel_gen = False
|
2024-12-19 12:48:57 +00:00
|
|
|
client.continuing = True
|
|
|
|
client.first_chunk = True
|
|
|
|
|
2024-02-26 00:55:44 +00:00
|
|
|
if lollmsElfServer.session.get_client(client_id).discussion is None:
|
2024-01-07 02:18:13 +00:00
|
|
|
ASCIIColors.yellow("Please select a discussion")
|
|
|
|
lollmsElfServer.error("Please select a discussion", client_id=client_id)
|
|
|
|
return
|
2024-12-19 12:48:57 +00:00
|
|
|
id_ = data["id"]
|
|
|
|
if id_ == -1:
|
|
|
|
message = lollmsElfServer.session.get_client(
|
|
|
|
client_id
|
|
|
|
).discussion.current_message
|
2024-01-07 02:18:13 +00:00
|
|
|
else:
|
2024-12-19 12:48:57 +00:00
|
|
|
message = lollmsElfServer.session.get_client(
|
|
|
|
client_id
|
|
|
|
).discussion.load_message(id_)
|
2024-01-07 02:18:13 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
client.generated_text = message.content
|
|
|
|
client.generation_thread = threading.Thread(
|
|
|
|
target=lollmsElfServer.start_message_generation,
|
|
|
|
args=(message, message.id, client_id, True),
|
|
|
|
)
|
2024-02-26 00:55:44 +00:00
|
|
|
client.generation_thread.start()
|
2024-01-07 02:18:13 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
# add functions to lollm
|
|
|
|
lollmsElfServer.handle_generate_msg = handle_generate_msg
|
|
|
|
lollmsElfServer.generate_msg_with_internet = generate_msg_with_internet
|
|
|
|
lollmsElfServer.handle_continue_generate_msg_from = (
|
|
|
|
handle_continue_generate_msg_from
|
|
|
|
)
|