2024-01-01 03:18:49 +00:00
"""
File : lollms_web_ui . py
Author : ParisNeo
Description : Singleton class for the LoLLMS web UI .
This class provides a singleton instance of the LoLLMS web UI , allowing access to its functionality and data across multiple endpoints .
"""
2024-01-07 14:27:14 +00:00
import asyncio
2024-01-07 02:18:13 +00:00
import ctypes
2024-12-19 12:48:57 +00:00
import gc
2024-01-07 02:18:13 +00:00
import json
2024-12-19 12:48:57 +00:00
import os
2024-01-07 02:18:13 +00:00
import re
2024-12-19 12:48:57 +00:00
import shutil
2024-01-07 02:18:13 +00:00
import string
2024-12-19 12:48:57 +00:00
import sys
import threading
2024-01-07 02:18:13 +00:00
import time
2024-12-19 12:48:57 +00:00
import traceback
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Any , Callable , List , Tuple
import git
2024-01-07 02:18:13 +00:00
import numpy as np
2024-12-19 12:48:57 +00:00
import requests
from lollms . app import LollmsApplication
from lollms . binding import ( BindingBuilder , BindingType , LLMBinding ,
LOLLMSConfig , ModelBuilder )
from lollms . client_session import Client
from lollms . com import LoLLMsCom , NotificationDisplayType , NotificationType
from lollms . config import InstallOption
from lollms . databases . discussions_database import Discussion , DiscussionsDB
from lollms . generation import ( RECEPTION_MANAGER , ROLE_CHANGE_DECISION ,
ROLE_CHANGE_OURTPUT )
from lollms . helpers import ASCIIColors , trace_exception
from lollms . paths import LollmsPaths
from lollms . personality import AIPersonality , PersonalityBuilder
from lollms . server . elf_server import LOLLMSElfServer
from lollms . types import ( CONTENT_OPERATION_TYPES , MSG_OPERATION_TYPE ,
MSG_TYPE , SENDER_TYPES )
from lollms . utilities import ( File64BitsManager , PackageManager ,
PromptReshaper , convert_language_name ,
find_first_available_file_index ,
is_asyncio_loop_running , process_ai_output ,
run_async , yes_or_no_input )
from tqdm import tqdm
2024-01-07 02:18:13 +00:00
if not PackageManager . check_package_installed ( " requests " ) :
PackageManager . install_package ( " requests " )
if not PackageManager . check_package_installed ( " bs4 " ) :
PackageManager . install_package ( " beautifulsoup4 " )
import requests
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
def terminate_thread ( thread ) :
if thread :
if not thread . is_alive ( ) :
ASCIIColors . yellow ( " Thread not alive " )
return
thread_id = thread . ident
exc = ctypes . py_object ( SystemExit )
res = ctypes . pythonapi . PyThreadState_SetAsyncExc ( thread_id , exc )
if res > 1 :
ctypes . pythonapi . PyThreadState_SetAsyncExc ( thread_id , None )
del thread
gc . collect ( )
raise SystemError ( " Failed to terminate the thread. " )
else :
2024-12-19 12:48:57 +00:00
ASCIIColors . yellow (
" Canceled successfully "
) # The current version of the webui
2024-02-15 00:02:40 +00:00
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
lollms_webui_version = " v16 alpha (codename Nexus 🌀) "
2024-01-07 02:18:13 +00:00
2024-01-01 03:18:49 +00:00
2024-01-04 02:57:42 +00:00
class LOLLMSWebUI ( LOLLMSElfServer ) :
2024-01-05 02:40:55 +00:00
__instance = None
@staticmethod
def build_instance (
config : LOLLMSConfig ,
lollms_paths : LollmsPaths ,
load_binding = True ,
load_model = True ,
load_voice_service = True ,
load_sd_service = True ,
try_select_binding = False ,
try_select_model = False ,
callback = None ,
2024-01-15 19:38:54 +00:00
args = None ,
2024-12-19 12:48:57 +00:00
sio = None ,
2024-01-05 02:40:55 +00:00
) :
if LOLLMSWebUI . __instance is None :
LOLLMSWebUI (
config ,
lollms_paths ,
load_binding = load_binding ,
load_model = load_model ,
load_sd_service = load_sd_service ,
load_voice_service = load_voice_service ,
try_select_binding = try_select_binding ,
try_select_model = try_select_model ,
callback = callback ,
2024-01-15 19:38:54 +00:00
args = args ,
2024-12-19 12:48:57 +00:00
sio = sio ,
2024-01-05 02:40:55 +00:00
)
2024-12-19 12:48:57 +00:00
return LOLLMSWebUI . __instance
2024-01-01 03:18:49 +00:00
def __init__ (
self ,
config : LOLLMSConfig ,
lollms_paths : LollmsPaths ,
load_binding = True ,
load_model = True ,
2024-01-04 02:57:42 +00:00
load_voice_service = True ,
load_sd_service = True ,
2024-01-01 03:18:49 +00:00
try_select_binding = False ,
try_select_model = False ,
callback = None ,
2024-01-15 19:38:54 +00:00
args = None ,
2024-12-19 12:48:57 +00:00
sio = None ,
2024-01-01 03:18:49 +00:00
) - > None :
super ( ) . __init__ (
config ,
lollms_paths ,
load_binding = load_binding ,
load_model = load_model ,
try_select_binding = try_select_binding ,
try_select_model = try_select_model ,
callback = callback ,
2024-12-19 12:48:57 +00:00
sio = sio ,
2024-01-01 03:18:49 +00:00
)
2024-12-19 12:48:57 +00:00
self . app_name : str = " LOLLMSWebUI "
self . version : str = lollms_webui_version
2024-01-15 19:38:54 +00:00
self . args = args
2024-01-01 03:18:49 +00:00
2024-01-05 02:40:55 +00:00
self . busy = False
self . nb_received_tokens = 0
2024-12-19 12:48:57 +00:00
2024-01-05 02:40:55 +00:00
self . config_file_path = config . file_path
self . cancel_gen = False
2024-12-19 12:48:57 +00:00
2024-01-11 17:00:11 +00:00
if self . config . auto_update :
if self . check_update_ ( ) :
ASCIIColors . info ( " New version found. Updating! " )
self . run_update_script ( )
2024-01-05 02:40:55 +00:00
# Keeping track of current discussion and message
self . _current_user_message_id = 0
self . _current_ai_message_id = 0
self . _message_id = 0
2024-02-18 23:23:15 +00:00
# migrate old databases to new ones:
2024-12-19 12:48:57 +00:00
databases_path = self . lollms_paths . personal_path / " databases "
if (
databases_path . exists ( )
and len ( [ f for f in databases_path . iterdir ( ) if f . suffix == " .db " ] ) > 0
) :
if yes_or_no_input (
" Old databases have been spotted on your system. Do you want me to migrate them to the new format? "
) :
2024-02-18 23:23:15 +00:00
databases_found = False
for database_path in databases_path . iterdir ( ) :
2024-12-19 12:48:57 +00:00
if database_path . suffix == " .db " :
ASCIIColors . red (
f " Found old discussion database format : { database_path } "
)
ASCIIColors . red ( f " Migrating to new format... " , end = " " )
new_db_path = (
self . lollms_paths . personal_discussions_path
/ database_path . stem
)
2024-02-18 23:23:15 +00:00
new_db_path . mkdir ( exist_ok = True , parents = True )
try :
2024-12-19 12:48:57 +00:00
shutil . copy ( database_path , new_db_path / " database.db " )
2024-02-18 23:23:15 +00:00
ASCIIColors . green ( " ok " )
databases_found = True
except Exception as ex :
ASCIIColors . warning ( ex )
if databases_found :
2024-12-19 12:48:57 +00:00
ASCIIColors . green (
f " Databases are migrated from { databases_path } to the new { self . lollms_paths . personal_discussions_path } path "
)
if yes_or_no_input (
" Databases are migrated to the new format. Do you want me to delete the previous version? "
) :
2024-02-18 23:23:15 +00:00
for database_path in databases_path . iterdir ( ) :
2024-12-19 12:48:57 +00:00
if database_path . suffix == " .db " :
2024-02-18 23:23:15 +00:00
ASCIIColors . red ( f " Deleting { database_path } " )
database_path . unlink ( )
if config [ " discussion_db_name " ] . endswith ( " .db " ) :
2024-12-19 12:48:57 +00:00
config [ " discussion_db_name " ] = config [ " discussion_db_name " ] . replace (
" .db " , " "
)
2024-02-18 23:23:15 +00:00
config . save_config ( )
self . discussion_db_name = config [ " discussion_db_name " ]
# Create database object
2024-05-02 22:58:18 +00:00
self . db = DiscussionsDB ( self , self . lollms_paths , self . discussion_db_name )
2024-01-05 02:40:55 +00:00
# If the database is empty, populate it with tables
2024-12-19 12:48:57 +00:00
ASCIIColors . info ( " Checking discussions database... " , end = " " )
2024-01-05 02:40:55 +00:00
self . db . create_tables ( )
self . db . add_missing_columns ( )
ASCIIColors . success ( " ok " )
2024-12-19 12:48:57 +00:00
# This is used to keep track of messages
self . download_infos = { }
2024-01-07 02:18:13 +00:00
# Define a WebSocket event handler
2024-01-16 23:06:38 +00:00
@sio.event
2024-01-07 02:18:13 +00:00
async def connect ( sid , environ ) :
2024-02-26 00:55:44 +00:00
self . session . add_client ( sid , sid , self . db . load_last_discussion ( ) , self . db )
2024-12-19 12:48:57 +00:00
await self . sio . emit ( " connected " , to = sid )
ASCIIColors . success ( f " Client { sid } connected " )
2024-01-07 02:18:13 +00:00
2024-01-16 23:06:38 +00:00
@sio.event
2024-01-07 02:18:13 +00:00
def disconnect ( sid ) :
try :
2024-12-19 12:48:57 +00:00
self . session . add_client (
sid , sid , self . db . load_last_discussion ( ) , self . db
)
2024-02-26 00:55:44 +00:00
if self . session . get_client ( sid ) . processing :
2024-12-19 12:48:57 +00:00
self . session . get_client ( sid ) . schedule_for_deletion = True
2024-02-26 00:55:44 +00:00
else :
2024-05-30 17:31:32 +00:00
# Clients are now kept forever
2024-12-19 12:48:57 +00:00
pass # self.session.remove_client(sid, sid)
2024-01-07 02:18:13 +00:00
except Exception as ex :
pass
2024-12-19 12:48:57 +00:00
ASCIIColors . error ( f " Client { sid } disconnected " )
2024-01-07 02:18:13 +00:00
# generation status
2024-12-19 12:48:57 +00:00
self . generating = False
ASCIIColors . blue ( f " Your personal data is stored here : " , end = " " )
2024-01-07 02:18:13 +00:00
ASCIIColors . green ( f " { self . lollms_paths . personal_path } " )
2024-02-19 21:40:28 +00:00
self . start_servers ( )
2024-01-07 02:18:13 +00:00
2024-02-18 23:23:15 +00:00
def get_uploads_path ( self , client_id ) :
2024-12-19 12:48:57 +00:00
return self . session . get_client (
client_id
) . discussion_path # self.db.discussion_db_path/f'{["discussion"].discussion_id}'
2024-01-01 03:18:49 +00:00
# Other methods and properties of the LoLLMSWebUI singleton class
2024-01-06 01:05:07 +00:00
def check_module_update_ ( self , repo_path , branch_name = " main " ) :
try :
# Open the repository
ASCIIColors . yellow ( f " Checking for updates from { repo_path } " )
repo = git . Repo ( repo_path )
2024-12-19 12:48:57 +00:00
2024-01-06 01:05:07 +00:00
# Fetch updates from the remote for the specified branch
2024-12-19 12:48:57 +00:00
repo . remotes . origin . fetch (
refspec = f " refs/heads/ { branch_name } :refs/remotes/origin/ { branch_name } "
)
2024-01-06 01:05:07 +00:00
# Compare the local and remote commit IDs for the specified branch
local_commit = repo . head . commit
remote_commit = repo . remotes . origin . refs [ branch_name ] . commit
2024-12-19 12:48:57 +00:00
2024-01-06 01:05:07 +00:00
# Check if the local branch is behind the remote branch
2024-12-19 12:48:57 +00:00
is_behind = (
repo . is_ancestor ( local_commit , remote_commit )
and local_commit != remote_commit
)
2024-01-06 01:05:07 +00:00
ASCIIColors . yellow ( f " update availability: { is_behind } " )
2024-12-19 12:48:57 +00:00
2024-01-06 01:05:07 +00:00
# Return True if the local branch is behind the remote branch
return is_behind
except Exception as e :
# Handle any errors that may occur during the fetch process
# trace_exception(e)
2024-12-19 12:48:57 +00:00
return False
2024-01-06 01:05:07 +00:00
def check_update_ ( self , branch_name = " main " ) :
try :
# Open the repository
2024-12-19 12:48:57 +00:00
repo_path = str ( Path ( __file__ ) . parent / " lollms_core " )
2024-01-06 01:05:07 +00:00
if self . check_module_update_ ( repo_path , branch_name ) :
return True
2024-08-29 06:40:46 +00:00
repo_path = str ( Path ( __file__ ) . parent )
2024-01-06 01:05:07 +00:00
if self . check_module_update_ ( repo_path , branch_name ) :
return True
return False
except Exception as e :
# Handle any errors that may occur during the fetch process
# trace_exception(e)
return False
2024-12-19 12:48:57 +00:00
2024-01-06 01:05:07 +00:00
def run_update_script ( self , args = None ) :
2024-06-07 07:12:05 +00:00
# deactivate trust store for github and pip package install
2024-12-19 12:48:57 +00:00
if " REQUESTS_CA_BUNDLE " in os . environ :
del os . environ [ " REQUESTS_CA_BUNDLE " ]
update_script = Path ( __file__ ) . parent / " update_script.py "
2024-01-06 01:05:07 +00:00
# Convert Namespace object to a dictionary
if args :
args_dict = vars ( args )
else :
args_dict = { }
# Filter out any key-value pairs where the value is None
2024-12-19 12:48:57 +00:00
valid_args = {
key : value for key , value in args_dict . items ( ) if value is not None
}
2024-01-06 01:05:07 +00:00
# Save the arguments to a temporary file
2024-12-19 12:48:57 +00:00
temp_file = Path ( __file__ ) . parent / " temp_args.txt "
2024-01-06 01:05:07 +00:00
with open ( temp_file , " w " ) as file :
# Convert the valid_args dictionary to a string in the format "key1 value1 key2 value2 ..."
2024-12-19 12:48:57 +00:00
arg_string = " " . join (
[ f " -- { key } { value } " for key , value in valid_args . items ( ) ]
)
2024-01-06 01:05:07 +00:00
file . write ( arg_string )
os . system ( f " python { update_script } " )
2024-01-07 02:18:13 +00:00
sys . exit ( 0 )
2024-01-07 21:34:33 +00:00
def run_restart_script ( self , args ) :
2024-12-19 12:48:57 +00:00
restart_script = Path ( __file__ ) . parent / " restart_script.py "
2024-01-07 21:34:33 +00:00
# Convert Namespace object to a dictionary
args_dict = vars ( args )
2024-01-07 02:18:13 +00:00
2024-01-07 21:34:33 +00:00
# Filter out any key-value pairs where the value is None
2024-12-19 12:48:57 +00:00
valid_args = {
key : value for key , value in args_dict . items ( ) if value is not None
}
2024-01-07 21:34:33 +00:00
# Save the arguments to a temporary file
2024-12-19 12:48:57 +00:00
temp_file = Path ( __file__ ) . parent / " temp_args.txt "
2024-01-07 21:34:33 +00:00
with open ( temp_file , " w " ) as file :
# Convert the valid_args dictionary to a string in the format "key1 value1 key2 value2 ..."
2024-12-19 12:48:57 +00:00
arg_string = " " . join (
[ f " -- { key } { value } " for key , value in valid_args . items ( ) ]
)
2024-01-07 21:34:33 +00:00
file . write ( arg_string )
os . system ( f " python { restart_script } " )
sys . exit ( 0 )
2024-01-07 02:18:13 +00:00
2024-01-10 20:22:17 +00:00
def audio_callback ( self , text ) :
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
if self . summoned :
client_id = 0
self . cancel_gen = False
2024-02-26 00:55:44 +00:00
client = self . 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 self . model :
ASCIIColors . error ( " Model not selected. Please select a model " )
2024-12-19 12:48:57 +00:00
self . error (
" Model not selected. Please select a model " , client_id = client_id
)
2024-01-07 02:18:13 +00:00
return
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
if not self . busy :
2024-02-26 00:55:44 +00:00
if client . discussion is None :
2024-01-07 02:18:13 +00:00
if self . db . does_last_discussion_have_messages ( ) :
2024-02-26 00:55:44 +00:00
client . discussion = self . db . create_discussion ( )
2024-01-07 02:18:13 +00:00
else :
2024-02-26 00:55:44 +00:00
client . discussion = self . db . load_last_discussion ( )
2024-01-07 02:18:13 +00:00
prompt = text
2024-04-20 01:07:29 +00:00
try :
nb_tokens = len ( self . model . tokenize ( prompt ) )
except :
nb_tokens = None
2024-02-26 00:55:44 +00:00
message = client . discussion . add_message (
2024-12-19 12:48:57 +00:00
message_type = MSG_TYPE . MSG_TYPE_CONTENT . value ,
sender_type = SENDER_TYPES . SENDER_TYPES_USER . value ,
sender = (
self . config . user_name . strip ( )
if self . config . use_user_name_in_discussions
else self . config . user_name
) ,
content = prompt ,
metadata = None ,
2024-04-20 01:07:29 +00:00
parent_message_id = self . message_id ,
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 " + self . personality . name
)
client . generation_thread = threading . Thread (
target = self . start_message_generation ,
args = ( message , message . id , client_id ) ,
)
2024-02-26 00:55:44 +00:00
client . generation_thread . start ( )
2024-12-19 12:48:57 +00:00
2024-01-16 23:06:38 +00:00
self . 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
self . busy = True
# tpe = threading.Thread(target=self.start_message_generation, args=(message, message_id, client_id))
# tpe.start()
2024-01-07 02:18:13 +00:00
else :
self . error ( " I am busy. Come back later. " , client_id = client_id )
else :
2024-02-10 11:12:44 +00:00
if " lollms " in text . lower ( ) :
2024-01-07 02:18:13 +00:00
self . summoned = True
2024-03-17 14:10:24 +00:00
# def scrape_and_save(self, url, file_path):
# # Send a GET request to the URL
# response = requests.get(url)
2024-12-19 12:48:57 +00:00
2024-03-17 14:10:24 +00:00
# # Parse the HTML content using BeautifulSoup
# soup = BeautifulSoup(response.content, 'html.parser')
2024-12-19 12:48:57 +00:00
2024-03-17 14:10:24 +00:00
# # Find all the text content in the webpage
# text_content = soup.get_text()
2024-12-19 12:48:57 +00:00
2024-03-17 14:10:24 +00:00
# # Remove extra returns and spaces
# text_content = ' '.join(text_content.split())
2024-12-19 12:48:57 +00:00
2024-03-17 14:10:24 +00:00
# # Save the text content as a text file
# with open(file_path, 'w', encoding="utf-8") as file:
# file.write(text_content)
2024-05-18 18:56:11 +00:00
2024-12-19 12:48:57 +00:00
# self.info(f"Webpage content saved to {file_path}")
2024-05-18 18:56:11 +00:00
2024-01-07 02:18:13 +00:00
def rebuild_personalities ( self , reload_all = False ) :
if reload_all :
2024-12-19 12:48:57 +00:00
self . mounted_personalities = [ ]
2024-01-07 02:18:13 +00:00
loaded = self . mounted_personalities
2024-12-19 12:48:57 +00:00
loaded_names = [
f " { p . category } / { p . personality_folder_name } " for p in loaded if p is not None
]
mounted_personalities = [ ]
2024-01-07 02:18:13 +00:00
ASCIIColors . success ( f " ╔══════════════════════════════════════════════════╗ " )
ASCIIColors . success ( f " ║ Building mounted Personalities ║ " )
ASCIIColors . success ( f " ╚══════════════════════════════════════════════════╝ " )
2024-12-19 12:48:57 +00:00
to_remove = [ ]
for i , personality in enumerate ( self . config [ " personalities " ] ) :
if i == self . config [ " active_personality_id " ] :
2024-01-07 02:18:13 +00:00
ASCIIColors . red ( " * " , end = " " )
ASCIIColors . green ( f " { personality } " )
else :
ASCIIColors . yellow ( f " { personality } " )
if personality in loaded_names :
mounted_personalities . append ( loaded [ loaded_names . index ( personality ) ] )
else :
2024-11-05 00:44:58 +00:00
personality_path = f " { personality } "
2024-01-07 02:18:13 +00:00
try :
2024-12-19 12:48:57 +00:00
personality = AIPersonality (
personality_path ,
self . lollms_paths ,
self . config ,
model = self . model ,
app = self ,
selected_language = self . config . current_language ,
run_scripts = True ,
)
2024-02-11 17:29:24 +00:00
2024-01-07 02:18:13 +00:00
mounted_personalities . append ( personality )
2024-12-19 12:48:57 +00:00
if self . config . auto_read and len ( personality . audio_samples ) > 0 :
2024-01-07 02:18:13 +00:00
try :
2024-12-19 12:48:57 +00:00
from lollms . services . tts . xtts . lollms_xtts import \
LollmsXTTS
2024-01-07 02:18:13 +00:00
if self . tts is None :
2024-12-19 12:48:57 +00:00
voice = self . config . xtts_current_voice
if voice != " main_voice " :
2024-05-05 00:23:07 +00:00
voices_folder = self . lollms_paths . custom_voices_path
else :
2024-12-19 12:48:57 +00:00
voices_folder = (
Path ( __file__ ) . parent . parent . parent
/ " services/xtts/voices "
)
2024-05-05 00:23:07 +00:00
2024-05-01 18:02:57 +00:00
self . tts = LollmsXTTS (
2024-12-19 12:48:57 +00:00
self ,
voices_folders = [
voices_folder ,
Path ( __file__ ) . parent . parent . parent
/ " services/xtts/voices " ,
] ,
freq = self . config . xtts_freq ,
)
2024-07-14 22:38:59 +00:00
except Exception as ex :
trace_exception ( ex )
2024-12-19 12:48:57 +00:00
self . warning (
f " Personality { personality . name } request using custom voice but couldn ' t load XTTS "
)
2024-01-07 02:18:13 +00:00
except Exception as ex :
2024-12-19 12:48:57 +00:00
ASCIIColors . error (
f " Personality file not found or is corrupted ( { personality_path } ). \n Returned the following exception: { ex } \n Please verify that the personality you have selected exists or select another personality. Some updates may lead to change in personality name or category, so check the personality selection in settings to be sure. "
)
2024-01-07 02:18:13 +00:00
ASCIIColors . info ( " Trying to force reinstall " )
if self . config [ " debug " ] :
print ( ex )
try :
personality = AIPersonality (
2024-12-19 12:48:57 +00:00
personality_path ,
self . lollms_paths ,
self . config ,
self . model ,
app = self ,
run_scripts = True ,
selected_language = self . config . current_language ,
installation_option = InstallOption . FORCE_INSTALL ,
)
2024-01-07 02:18:13 +00:00
mounted_personalities . append ( personality )
if personality . processor :
personality . processor . mounted ( )
except Exception as ex :
2024-12-19 12:48:57 +00:00
ASCIIColors . error (
f " Couldn ' t load personality at { personality_path } "
)
2024-01-07 02:18:13 +00:00
trace_exception ( ex )
ASCIIColors . info ( f " Unmounting personality " )
to_remove . append ( i )
2024-12-19 12:48:57 +00:00
personality = AIPersonality (
None ,
self . lollms_paths ,
self . config ,
self . model ,
app = self ,
run_scripts = True ,
installation_option = InstallOption . FORCE_INSTALL ,
)
2024-01-07 02:18:13 +00:00
mounted_personalities . append ( personality )
if personality . processor :
personality . processor . mounted ( )
ASCIIColors . info ( " Reverted to default personality " )
2024-12-19 12:48:57 +00:00
if self . config [ " active_personality_id " ] > = 0 and self . config [
" active_personality_id "
] < len ( self . config [ " personalities " ] ) :
ASCIIColors . success (
f ' selected model : { self . config [ " personalities " ] [ self . config [ " active_personality_id " ] ] } '
)
2024-01-07 02:18:13 +00:00
else :
2024-12-19 12:48:57 +00:00
ASCIIColors . warning (
" An error was encountered while trying to mount personality "
)
2024-01-07 02:18:13 +00:00
ASCIIColors . success ( f " ╔══════════════════════════════════════════════════╗ " )
ASCIIColors . success ( f " ║ Done ║ " )
ASCIIColors . success ( f " ╚══════════════════════════════════════════════════╝ " )
# Sort the indices in descending order to ensure correct removal
to_remove . sort ( reverse = True )
# Remove elements from the list based on the indices
for index in to_remove :
if 0 < = index < len ( mounted_personalities ) :
mounted_personalities . pop ( index )
self . config [ " personalities " ] . pop ( index )
ASCIIColors . info ( f " removed personality { personality_path } " )
2024-12-19 12:48:57 +00:00
if self . config [ " active_personality_id " ] > = len ( self . config [ " personalities " ] ) :
self . config [ " active_personality_id " ] = 0
2024-01-07 02:18:13 +00:00
return mounted_personalities
# ================================== LOLLMSApp
2024-12-19 12:48:57 +00:00
# properties
2024-01-07 02:18:13 +00:00
@property
def message_id ( self ) :
return self . _message_id
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
@message_id.setter
def message_id ( self , id ) :
2024-12-19 12:48:57 +00:00
self . _message_id = id
2024-01-07 02:18:13 +00:00
@property
def current_user_message_id ( self ) :
return self . _current_user_message_id
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
@current_user_message_id.setter
def current_user_message_id ( self , id ) :
2024-12-19 12:48:57 +00:00
self . _current_user_message_id = id
2024-01-07 02:18:13 +00:00
self . _message_id = id
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
@property
def current_ai_message_id ( self ) :
return self . _current_ai_message_id
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
@current_ai_message_id.setter
def current_ai_message_id ( self , id ) :
2024-12-19 12:48:57 +00:00
self . _current_ai_message_id = id
2024-01-07 02:18:13 +00:00
self . _message_id = id
def download_file ( self , url , installation_path , callback = None ) :
"""
Downloads a file from a URL , reports the download progress using a callback function , and displays a progress bar .
Args :
url ( str ) : The URL of the file to download .
installation_path ( str ) : The path where the file should be saved .
callback ( function , optional ) : A callback function to be called during the download
with the progress percentage as an argument . Defaults to None .
"""
try :
response = requests . get ( url , stream = True )
# Get the file size from the response headers
2024-12-19 12:48:57 +00:00
total_size = int ( response . headers . get ( " content-length " , 0 ) )
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
with open ( installation_path , " wb " ) as file :
2024-01-07 02:18:13 +00:00
downloaded_size = 0
2024-12-19 12:48:57 +00:00
with tqdm (
total = total_size , unit = " B " , unit_scale = True , ncols = 80
) as progress_bar :
2024-01-07 02:18:13 +00:00
for chunk in response . iter_content ( chunk_size = 8192 ) :
if chunk :
file . write ( chunk )
downloaded_size + = len ( chunk )
if callback is not None :
callback ( downloaded_size , total_size )
progress_bar . update ( len ( chunk ) )
if callback is not None :
callback ( total_size , total_size )
print ( " File downloaded successfully " )
except Exception as e :
print ( " Couldn ' t download file: " , str ( e ) )
def clean_string ( self , input_string ) :
# Remove extra spaces by replacing multiple spaces with a single space
2024-12-19 12:48:57 +00:00
# cleaned_string = re.sub(r'\s+', ' ', input_string)
2024-01-07 02:18:13 +00:00
# Remove extra line breaks by replacing multiple consecutive line breaks with a single line break
2024-12-19 12:48:57 +00:00
cleaned_string = re . sub ( r " \ n \ s* \ n " , " \n " , input_string )
2024-01-07 02:18:13 +00:00
# Create a string containing all punctuation characters
2024-12-19 12:48:57 +00:00
punctuation_chars = string . punctuation
2024-01-07 02:18:13 +00:00
# Define a regular expression pattern to match and remove non-alphanumeric characters
2024-12-19 12:48:57 +00:00
# pattern = f'[^a-zA-Z0-9\s{re.escape(punctuation_chars)}]' # This pattern matches any character that is not a letter, digit, space, or punctuation
pattern = f " [^a-zA-Z0-9 \u00C0 - \u017F \ s { re . escape ( punctuation_chars ) } ] "
2024-01-07 02:18:13 +00:00
# Use re.sub to replace the matched characters with an empty string
2024-12-19 12:48:57 +00:00
cleaned_string = re . sub ( pattern , " " , cleaned_string )
2024-01-07 02:18:13 +00:00
return cleaned_string
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
def make_discussion_title ( self , discussion , client_id = None ) :
"""
Builds a title for a discussion
"""
2024-05-29 15:21:37 +00:00
2024-01-07 02:18:13 +00:00
# Get the list of messages
messages = discussion . get_messages ( )
2024-06-27 22:39:42 +00:00
discussion_messages = f " { self . start_header_id_template } instruction { self . end_header_id_template } Create a short title to this discussion \n Your response should only contain the title without any comments. \n "
discussion_title = f " \n { self . start_header_id_template } Discussion title { self . end_header_id_template } "
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
available_space = (
self . config . ctx_size
- 150
- len ( self . model . tokenize ( discussion_messages ) )
- len ( self . model . tokenize ( discussion_title ) )
)
2024-01-07 02:18:13 +00:00
# Initialize a list to store the full messages
2024-12-19 12:48:57 +00:00
full_message_list = [ ]
2024-01-07 02:18:13 +00:00
# Accumulate messages until the cumulative number of tokens exceeds available_space
tokens_accumulated = 0
# Accumulate messages starting from message_index
for message in messages :
# Check if the message content is not empty and visible to the AI
2024-12-19 12:48:57 +00:00
if message . content != " " and (
message . message_type
< = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT_INVISIBLE_TO_USER . value
and message . message_type
!= MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT_INVISIBLE_TO_AI . value
) :
2024-01-07 02:18:13 +00:00
# Tokenize the message content
message_tokenized = self . model . tokenize (
2024-12-19 12:48:57 +00:00
" \n "
+ self . config . discussion_prompt_separator
+ message . sender
+ " : "
+ message . content . strip ( )
)
2024-01-07 02:18:13 +00:00
# Check if adding the message will exceed the available space
if tokens_accumulated + len ( message_tokenized ) > available_space :
break
# Add the tokenized message to the full_message_list
full_message_list . insert ( 0 , message_tokenized )
# Update the cumulative number of tokens
tokens_accumulated + = len ( message_tokenized )
# Build the final discussion messages by detokenizing the full_message_list
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
for message_tokens in full_message_list :
discussion_messages + = self . model . detokenize ( message_tokens )
discussion_messages + = discussion_title
title = [ " " ]
2024-12-19 12:48:57 +00:00
def receive ( chunk : str , message_type : MSG_OPERATION_TYPE ) :
2024-01-07 02:18:13 +00:00
if chunk :
title [ 0 ] + = chunk
antiprompt = self . personality . detect_antiprompt ( title [ 0 ] )
if antiprompt :
2024-04-19 20:30:51 +00:00
ASCIIColors . warning ( f " \n { antiprompt } detected. Stopping generation " )
2024-12-19 12:48:57 +00:00
title [ 0 ] = self . remove_text_from_string ( title [ 0 ] , antiprompt )
2024-01-07 02:18:13 +00:00
return False
else :
return True
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
self . _generate ( discussion_messages , 150 , client_id , receive )
ASCIIColors . info ( title [ 0 ] )
return title [ 0 ]
2024-12-19 12:48:57 +00:00
def get_discussion_to ( self , client_id , message_id = - 1 ) :
2024-02-26 00:55:44 +00:00
messages = self . session . get_client ( client_id ) . discussion . get_messages ( )
2024-01-07 02:18:13 +00:00
full_message_list = [ ]
2024-06-27 22:39:42 +00:00
ump = f " { self . start_header_id_template } { self . config . user_name . strip ( ) if self . config . use_user_name_in_discussions else self . personality . user_message_prefix } { self . end_header_id_template } "
2024-01-07 02:18:13 +00:00
for message in messages :
2024-12-19 12:48:57 +00:00
if message [ " id " ] < = message_id or message_id == - 1 :
if (
message [ " type " ]
!= MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT_INVISIBLE_TO_USER
) :
if message [ " sender " ] == self . personality . name :
full_message_list . append (
self . config . discussion_prompt_separator
+ self . personality . ai_message_prefix
+ message [ " content " ]
)
2024-01-07 02:18:13 +00:00
else :
full_message_list . append ( ump + message [ " content " ] )
2024-12-19 12:48:57 +00:00
link_text = " \n " # self.personality.link_text
2024-01-07 02:18:13 +00:00
if len ( full_message_list ) > self . config [ " nb_messages_to_remember " ] :
2024-12-19 12:48:57 +00:00
discussion_messages = (
self . config . discussion_prompt_separator
+ self . personality . personality_conditioning
+ link_text . join (
full_message_list [ - self . config [ " nb_messages_to_remember " ] : ]
)
)
2024-01-07 02:18:13 +00:00
else :
2024-12-19 12:48:57 +00:00
discussion_messages = (
self . config . discussion_prompt_separator
+ self . personality . personality_conditioning
+ link_text . join ( full_message_list )
)
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
return discussion_messages # Removes the last return
def set_message_content (
self ,
full_text : str ,
callback : (
Callable [ [ str | list | None , MSG_OPERATION_TYPE , str , Any | None ] , bool ]
| None
) = None ,
client_id = 0 ,
) :
2024-05-02 22:58:18 +00:00
""" This sends full text to front end
Args :
step_text ( dict ) : The step text
callback ( callable , optional ) : A callable with this signature ( str , MSG_TYPE ) to send the text to . Defaults to None .
"""
if not callback :
2024-12-19 12:48:57 +00:00
callback = partial ( self . process_data , client_id = client_id )
2024-05-02 22:58:18 +00:00
if callback :
2024-08-14 20:15:45 +00:00
callback ( full_text , MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT )
2024-05-02 22:58:18 +00:00
2024-05-26 22:36:45 +00:00
def emit_socket_io_info ( self , name , data , client_id ) :
2024-12-19 12:48:57 +00:00
run_async ( partial ( self . sio . emit , name , data , to = client_id ) )
2024-05-26 22:36:45 +00:00
2024-01-07 02:18:13 +00:00
def notify (
2024-12-19 12:48:57 +00:00
self ,
content ,
notification_type : NotificationType = NotificationType . NOTIF_SUCCESS ,
duration : int = 4 ,
client_id = None ,
display_type : NotificationDisplayType = NotificationDisplayType . TOAST ,
verbose : bool | None = None ,
) :
2024-01-24 19:34:23 +00:00
if verbose is None :
verbose = self . verbose
2024-12-19 12:48:57 +00:00
run_async (
partial (
self . sio . emit ,
" notification " ,
{
" content " : content ,
" notification_type " : notification_type . value ,
" duration " : duration ,
" display_type " : display_type . value ,
} ,
to = client_id ,
)
2024-01-07 10:45:10 +00:00
)
2024-01-07 02:18:13 +00:00
if verbose :
2024-12-19 12:48:57 +00:00
if notification_type == NotificationType . NOTIF_SUCCESS :
2024-01-07 02:18:13 +00:00
ASCIIColors . success ( content )
2024-12-19 12:48:57 +00:00
elif notification_type == NotificationType . NOTIF_INFO :
2024-01-07 02:18:13 +00:00
ASCIIColors . info ( content )
2024-12-19 12:48:57 +00:00
elif notification_type == NotificationType . NOTIF_WARNING :
2024-01-07 02:18:13 +00:00
ASCIIColors . warning ( content )
else :
ASCIIColors . red ( content )
2024-03-17 14:15:34 +00:00
2024-03-17 14:10:24 +00:00
def refresh_files ( self , client_id = None ) :
2024-12-19 12:48:57 +00:00
run_async ( partial ( self . sio . emit , " refresh_files " , to = client_id ) )
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
def new_message (
self ,
client_id ,
sender = None ,
content = " " ,
message_type : MSG_OPERATION_TYPE = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
sender_type : SENDER_TYPES = SENDER_TYPES . SENDER_TYPES_AI ,
open = False ,
) :
2024-02-26 00:55:44 +00:00
client = self . session . get_client ( client_id )
2024-12-19 12:48:57 +00:00
# self.close_message(client_id)
if sender == None :
sender = self . personality . name
2024-02-26 00:55:44 +00:00
msg = client . discussion . add_message (
2024-12-19 12:48:57 +00:00
message_type = message_type . value ,
sender_type = sender_type . value ,
sender = sender ,
content = content ,
steps = [ ] ,
metadata = None ,
ui = None ,
rank = 0 ,
parent_message_id = (
client . discussion . current_message . id
if client . discussion . current_message is not None
else 0
) ,
binding = self . config [ " binding_name " ] ,
model = self . config [ " model_name " ] ,
personality = self . config [ " personalities " ] [
self . config [ " active_personality_id " ]
] ,
created_at = datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " ) ,
) # first the content is empty, but we'll fill it at the end
run_async (
partial (
self . sio . emit ,
" new_message " ,
{
" sender " : sender ,
" message_type " : message_type . value ,
" sender_type " : SENDER_TYPES . SENDER_TYPES_AI . value ,
" content " : content ,
" metadata " : None ,
" ui " : None ,
" id " : msg . id ,
" parent_message_id " : msg . parent_message_id ,
" binding " : self . binding . binding_folder_name ,
" model " : self . model . model_name ,
" personality " : self . personality . name ,
" created_at " : client . discussion . current_message . created_at ,
" started_generating_at " : client . discussion . current_message . started_generating_at ,
" finished_generating_at " : client . discussion . current_message . finished_generating_at ,
" nb_tokens " : client . discussion . current_message . nb_tokens ,
" open " : open ,
} ,
to = client_id ,
2024-01-07 14:27:14 +00:00
)
2024-12-19 12:48:57 +00:00
)
def new_block (
self ,
client_id ,
sender = None ,
content = " " ,
parameters = None ,
metadata = None ,
ui = None ,
message_type : MSG_OPERATION_TYPE = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
sender_type : SENDER_TYPES = SENDER_TYPES . SENDER_TYPES_AI ,
open = False ,
) :
2024-05-19 12:35:05 +00:00
# like new_message but without adding the information to the database
client = self . session . get_client ( client_id )
2024-12-19 12:48:57 +00:00
run_async (
partial (
self . sio . emit ,
" new_message " ,
{
" sender " : sender ,
" message_type " : message_type . value ,
" sender_type " : SENDER_TYPES . SENDER_TYPES_AI . value ,
" content " : content ,
" parameters " : parameters ,
" metadata " : metadata ,
" ui " : ui ,
" id " : 0 ,
" parent_message_id " : 0 ,
" binding " : self . binding . binding_folder_name ,
" model " : self . model . model_name ,
" personality " : self . personality . name ,
" created_at " : client . discussion . current_message . created_at ,
" started_generating_at " : client . discussion . current_message . started_generating_at ,
" finished_generating_at " : client . discussion . current_message . finished_generating_at ,
" nb_tokens " : client . discussion . current_message . nb_tokens ,
" open " : open ,
} ,
to = client_id ,
)
)
2024-04-21 00:50:43 +00:00
def send_refresh ( self , client_id ) :
client = self . session . get_client ( client_id )
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" update_message " ,
{
" sender " : client . discussion . current_message . sender ,
" id " : client . discussion . current_message . id ,
" content " : client . discussion . current_message . content ,
" discussion_id " : client . discussion . discussion_id ,
" operation_type " : MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT . value ,
" message_type " : client . discussion . current_message . message_type ,
" created_at " : client . discussion . current_message . created_at ,
" started_generating_at " : client . discussion . current_message . started_generating_at ,
" finished_generating_at " : client . discussion . current_message . finished_generating_at ,
" nb_tokens " : client . discussion . current_message . nb_tokens ,
" binding " : self . binding . binding_folder_name ,
" model " : self . model . model_name ,
" personality " : self . personality . name ,
} ,
to = client_id ,
)
2024-04-21 00:50:43 +00:00
)
2024-08-14 20:15:45 +00:00
2024-12-19 12:48:57 +00:00
def update_message (
self ,
client_id ,
chunk ,
parameters = None ,
metadata = [ ] ,
ui = None ,
operation_type : MSG_OPERATION_TYPE = None ,
) :
2024-02-26 00:55:44 +00:00
client = self . session . get_client ( client_id )
2024-12-19 12:48:57 +00:00
client . discussion . current_message . finished_generating_at = (
datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " )
)
2024-04-20 01:07:29 +00:00
client . discussion . current_message . nb_tokens = self . nb_received_tokens
2024-12-19 12:48:57 +00:00
mtdt = (
json . dumps ( metadata , indent = 4 )
if metadata is not None and type ( metadata ) == list
else metadata
)
2024-07-07 17:14:18 +00:00
2024-12-19 12:48:57 +00:00
if self . nb_received_tokens == 1 :
client . discussion . current_message . started_generating_at = (
datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " )
)
self . update_message_step (
client_id ,
" 🔥 warming up ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
self . update_message_step (
client_id ,
" ✍ generating ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
2024-01-07 14:27:14 +00:00
2024-01-09 00:15:51 +00:00
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" update_message " ,
{
" sender " : self . personality . name ,
" id " : client . discussion . current_message . id ,
" content " : chunk ,
" ui " : client . discussion . current_message . ui if ui is None else ui ,
" discussion_id " : client . discussion . discussion_id ,
" operation_type " : (
operation_type . value
if operation_type is not None
else (
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_ADD_CHUNK . value
if self . nb_received_tokens > 1
else MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT . value
)
) ,
" message_type " : MSG_TYPE . MSG_TYPE_CONTENT . value ,
" created_at " : client . discussion . current_message . created_at ,
" started_generating_at " : client . discussion . current_message . started_generating_at ,
" finished_generating_at " : client . discussion . current_message . finished_generating_at ,
" nb_tokens " : client . discussion . current_message . nb_tokens ,
" parameters " : parameters ,
" metadata " : metadata ,
" binding " : self . binding . binding_folder_name ,
" model " : self . model . model_name ,
" personality " : self . personality . name ,
} ,
to = client_id ,
)
2024-01-07 14:27:14 +00:00
)
2024-12-19 12:48:57 +00:00
if (
operation_type
and operation_type . value < MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_INFO . value
) :
client . discussion . update_message (
client . generated_text ,
new_metadata = mtdt ,
new_ui = ui ,
started_generating_at = client . discussion . current_message . started_generating_at ,
nb_tokens = client . discussion . current_message . nb_tokens ,
)
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
def update_message_content (
self ,
client_id ,
chunk ,
operation_type : MSG_OPERATION_TYPE = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
message_type : MSG_TYPE = None ,
) :
2024-08-14 20:15:45 +00:00
client = self . session . get_client ( client_id )
2024-12-19 12:48:57 +00:00
client . discussion . current_message . finished_generating_at = (
datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " )
)
2024-08-14 20:15:45 +00:00
client . discussion . current_message . nb_tokens = self . nb_received_tokens
2024-12-19 12:48:57 +00:00
if self . nb_received_tokens == 1 :
client . discussion . current_message . started_generating_at = (
datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " )
)
2024-08-14 20:15:45 +00:00
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" update_message " ,
{
" sender " : self . personality . name ,
" id " : client . discussion . current_message . id ,
" content " : chunk ,
" discussion_id " : client . discussion . discussion_id ,
" operation_type " : operation_type . value ,
" message_type " : (
client . discussion . current_message . message_type
if message_type is None
else message_type
) ,
" created_at " : client . discussion . current_message . created_at ,
" started_generating_at " : client . discussion . current_message . started_generating_at ,
" finished_generating_at " : client . discussion . current_message . finished_generating_at ,
" nb_tokens " : client . discussion . current_message . nb_tokens ,
" binding " : self . binding . binding_folder_name ,
" model " : self . model . model_name ,
" personality " : self . personality . name ,
} ,
to = client_id ,
)
2024-08-14 20:15:45 +00:00
)
2024-12-19 12:48:57 +00:00
client . discussion . update_message_content (
client . generated_text ,
started_generating_at = client . discussion . current_message . started_generating_at ,
nb_tokens = client . discussion . current_message . nb_tokens ,
)
2024-08-14 20:15:45 +00:00
2024-12-19 12:48:57 +00:00
def update_message_step (
self , client_id , step_text , msg_operation_type : MSG_OPERATION_TYPE = None
) :
2024-08-14 20:15:45 +00:00
client = self . session . get_client ( client_id )
if msg_operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP :
2024-12-19 12:48:57 +00:00
client . discussion . current_message . add_step ( step_text , " instant " , True , True )
2024-08-14 20:15:45 +00:00
elif msg_operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_START :
2024-12-19 12:48:57 +00:00
client . discussion . current_message . add_step (
step_text , " start_end " , True , False
)
elif (
msg_operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS
) :
client . discussion . current_message . add_step (
step_text , " start_end " , True , True
)
elif (
msg_operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_FAILURE
) :
client . discussion . current_message . add_step (
step_text , " start_end " , False , True
)
2024-08-14 20:15:45 +00:00
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" update_message " ,
{
" id " : client . discussion . current_message . id ,
" discussion_id " : client . discussion . discussion_id ,
" operation_type " : msg_operation_type . value ,
" steps " : client . discussion . current_message . steps ,
} ,
to = client_id ,
)
2024-08-14 20:15:45 +00:00
)
def update_message_metadata ( self , client_id , metadata ) :
client = self . session . get_client ( client_id )
2024-12-19 12:48:57 +00:00
md = (
json . dumps ( metadata )
if type ( metadata ) == dict or type ( metadata ) == list
else metadata
)
2024-08-14 20:15:45 +00:00
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" update_message " ,
{
" sender " : self . personality . name ,
" id " : client . discussion . current_message . id ,
" metadata " : md ,
" discussion_id " : client . discussion . discussion_id ,
" operation_type " : MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_JSON_INFOS . value ,
} ,
to = client_id ,
)
2024-08-14 20:15:45 +00:00
)
client . discussion . update_message_metadata ( metadata )
def update_message_ui ( self , client_id , ui ) :
client = self . session . get_client ( client_id )
2024-08-18 20:49:21 +00:00
2024-08-14 20:15:45 +00:00
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" update_message " ,
{
" sender " : self . personality . name ,
" id " : client . discussion . current_message . id ,
" ui " : ui ,
" discussion_id " : client . discussion . discussion_id ,
" operation_type " : MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_UI . value ,
} ,
to = client_id ,
)
2024-08-14 20:15:45 +00:00
)
client . discussion . update_message_ui ( ui )
2024-01-07 02:18:13 +00:00
def close_message ( self , client_id ) :
2024-02-26 00:55:44 +00:00
client = self . session . get_client ( client_id )
2024-08-14 20:15:45 +00:00
for msg in client . discussion . messages :
if msg . steps is not None :
for step in msg . steps :
2024-12-19 12:48:57 +00:00
step [ " done " ] = True
2024-02-26 00:55:44 +00:00
if not client . discussion :
2024-01-07 02:18:13 +00:00
return
2024-12-19 12:48:57 +00:00
# fix halucination
if len ( client . generated_text ) > 0 and len ( self . start_header_id_template ) > 0 :
client . generated_text = client . generated_text . split (
f " { self . start_header_id_template } "
) [ 0 ]
2024-01-07 02:18:13 +00:00
# Send final message
2024-12-19 12:48:57 +00:00
client . discussion . current_message . finished_generating_at = (
datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " )
)
2024-04-20 01:07:29 +00:00
try :
2024-12-19 12:48:57 +00:00
client . discussion . current_message . nb_tokens = len (
self . model . tokenize ( client . generated_text )
)
2024-04-20 01:07:29 +00:00
except :
client . discussion . current_message . nb_tokens = None
2024-01-09 00:15:51 +00:00
run_async (
2024-12-19 12:48:57 +00:00
partial (
self . sio . emit ,
" close_message " ,
{
" sender " : self . personality . name ,
" id " : client . discussion . current_message . id ,
" content " : client . generated_text ,
" binding " : self . binding . binding_folder_name ,
" model " : self . model . model_name ,
" personality " : self . personality . name ,
" created_at " : client . discussion . current_message . created_at ,
" started_generating_at " : client . discussion . current_message . started_generating_at ,
" finished_generating_at " : client . discussion . current_message . finished_generating_at ,
" nb_tokens " : client . discussion . current_message . nb_tokens ,
} ,
to = client_id ,
)
2024-01-07 14:27:14 +00:00
)
2024-01-07 02:18:13 +00:00
2024-08-15 15:28:33 +00:00
def process_data (
2024-12-19 12:48:57 +00:00
self ,
data : str | list | None ,
operation_type : MSG_OPERATION_TYPE ,
client_id : str = 0 ,
personality : AIPersonality = None ,
) :
2024-01-07 02:18:13 +00:00
"""
2024-08-15 15:28:33 +00:00
Processes a data of generated text
2024-01-07 02:18:13 +00:00
"""
2024-02-26 00:55:44 +00:00
client = self . session . get_client ( client_id )
2024-12-19 12:48:57 +00:00
if data is None and operation_type in [
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_ADD_CHUNK ,
] :
2024-05-05 18:57:11 +00:00
return
2024-12-19 12:48:57 +00:00
2024-08-15 15:28:33 +00:00
if data is not None :
2024-05-02 22:58:18 +00:00
if not client_id in list ( self . session . clients . keys ( ) ) :
self . error ( " Connection lost " , client_id = client_id )
return
2024-08-14 20:15:45 +00:00
if operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP :
2024-12-19 12:48:57 +00:00
ASCIIColors . info ( " --> Step: " + data )
2024-09-29 10:10:18 +00:00
self . update_message_step ( client_id , data , operation_type )
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_START :
2024-12-19 12:48:57 +00:00
ASCIIColors . info ( " --> Step started: " + data )
2024-08-15 15:28:33 +00:00
self . update_message_step ( client_id , data , operation_type )
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS :
2024-12-19 12:48:57 +00:00
ASCIIColors . success ( " --> Step ended: " + data )
2024-08-15 15:28:33 +00:00
self . update_message_step ( client_id , data , operation_type )
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_FAILURE :
2024-12-19 12:48:57 +00:00
ASCIIColors . success ( " --> Step ended: " + data )
2024-08-15 15:28:33 +00:00
self . update_message_step ( client_id , data , operation_type )
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_WARNING :
2024-12-19 12:48:57 +00:00
self . warning ( data , client_id = client_id )
ASCIIColors . error ( " --> Exception from personality: " + data )
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_EXCEPTION :
2024-08-15 15:28:33 +00:00
self . error ( data , client_id = client_id )
2024-12-19 12:48:57 +00:00
ASCIIColors . error ( " --> Exception from personality: " + data )
2024-06-28 00:45:21 +00:00
return
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_INFO :
2024-08-15 15:28:33 +00:00
self . info ( data , client_id = client_id )
2024-12-19 12:48:57 +00:00
ASCIIColors . info ( " --> Info: " + data )
2024-06-28 00:45:21 +00:00
return
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_UI :
2024-08-15 15:28:33 +00:00
self . update_message_ui ( client_id , data )
return
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_JSON_INFOS :
2024-08-15 15:28:33 +00:00
self . update_message_metadata ( client_id , data )
return
2024-09-29 10:10:18 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_NEW_MESSAGE :
2024-01-07 02:18:13 +00:00
self . nb_received_tokens = 0
self . start_time = datetime . now ( )
2024-12-19 12:48:57 +00:00
self . update_message_step (
client_id ,
" 🔥 warming up ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
self . update_message_step (
client_id ,
" ✍ generating ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
2024-01-09 23:08:41 +00:00
self . new_message (
2024-12-19 12:48:57 +00:00
client_id ,
self . personality . name if personality is None else personality . name ,
data ,
message_type = MSG_TYPE . MSG_TYPE_CONTENT ,
)
2024-08-15 15:28:33 +00:00
return
2024-08-14 20:15:45 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_FINISHED_MESSAGE :
2024-01-07 02:18:13 +00:00
self . close_message ( client_id )
2024-08-15 15:28:33 +00:00
return
2024-08-14 20:15:45 +00:00
elif operation_type == MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_ADD_CHUNK :
2024-12-19 12:48:57 +00:00
if self . nb_received_tokens == 0 :
2024-01-07 02:18:13 +00:00
self . start_time = datetime . now ( )
2024-02-11 17:29:24 +00:00
try :
2024-12-19 12:48:57 +00:00
self . update_message_step (
client_id ,
" 🔥 warming up ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
self . update_message_step (
client_id ,
" ✍ generating ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
2024-02-11 17:29:24 +00:00
except Exception as ex :
2024-08-14 20:15:45 +00:00
trace_exception ( ex )
2024-02-11 17:29:24 +00:00
ASCIIColors . warning ( " Couldn ' t send status update to client " )
2024-12-19 12:48:57 +00:00
dt = ( datetime . now ( ) - self . start_time ) . seconds
if dt == 0 :
dt = 1
spd = self . nb_received_tokens / dt
2024-06-27 22:39:42 +00:00
if self . config . debug_show_chunks :
2024-12-19 12:48:57 +00:00
print ( data , end = " " , flush = True )
# ASCIIColors.green(f"Received {self.nb_received_tokens} tokens (speed: {spd:.2f}t/s) ",end="\r",flush=True)
2024-01-07 02:18:13 +00:00
sys . stdout = sys . __stdout__
sys . stdout . flush ( )
2024-08-15 15:28:33 +00:00
if data :
client . generated_text + = data
2024-02-26 00:55:44 +00:00
antiprompt = self . personality . detect_antiprompt ( client . generated_text )
2024-01-07 02:18:13 +00:00
if antiprompt :
2024-04-19 20:30:51 +00:00
ASCIIColors . warning ( f " \n { antiprompt } detected. Stopping generation " )
2024-12-19 12:48:57 +00:00
client . generated_text = self . remove_text_from_string (
client . generated_text , antiprompt
)
self . update_message_content (
client_id ,
client . generated_text ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
)
2024-01-07 02:18:13 +00:00
return False
else :
self . nb_received_tokens + = 1
2024-02-26 00:55:44 +00:00
if client . continuing and client . first_chunk :
2024-12-19 12:48:57 +00:00
self . update_message_content (
client_id ,
client . generated_text ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
)
2024-01-07 02:18:13 +00:00
else :
2024-12-19 12:48:57 +00:00
self . update_message_content (
client_id , data , MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_ADD_CHUNK
)
2024-08-14 20:15:45 +00:00
2024-12-19 12:48:57 +00:00
client . first_chunk = False
2024-01-07 02:18:13 +00:00
# if stop generation is detected then stop
if not self . cancel_gen :
return True
else :
self . cancel_gen = False
ASCIIColors . warning ( " Generation canceled " )
return False
# Stream the generated text to the main process
2024-12-19 12:48:57 +00:00
elif operation_type in [
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT_INVISIBLE_TO_AI ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT_INVISIBLE_TO_USER ,
] :
if self . nb_received_tokens == 0 :
2024-06-25 23:27:29 +00:00
self . start_time = datetime . now ( )
try :
2024-12-19 12:48:57 +00:00
self . update_message_step (
client_id ,
" 🔥 warming up ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
self . update_message_step (
client_id ,
" ✍ generating ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
2024-08-14 20:15:45 +00:00
2024-06-25 23:27:29 +00:00
except Exception as ex :
ASCIIColors . warning ( " Couldn ' t send status update to client " )
2024-08-15 15:28:33 +00:00
client . generated_text = data
2024-02-26 00:55:44 +00:00
antiprompt = self . personality . detect_antiprompt ( client . generated_text )
2024-01-07 02:18:13 +00:00
if antiprompt :
2024-04-19 20:30:51 +00:00
ASCIIColors . warning ( f " \n { antiprompt } detected. Stopping generation " )
2024-12-19 12:48:57 +00:00
client . generated_text = self . remove_text_from_string (
client . generated_text , antiprompt
)
self . update_message_content (
client_id , client . generated_text , operation_type
)
2024-01-07 02:18:13 +00:00
return False
2024-08-15 15:28:33 +00:00
self . update_message_content ( client_id , data , operation_type )
2024-01-07 02:18:13 +00:00
return True
# Stream the generated text to the frontend
else :
2024-08-15 15:28:33 +00:00
self . update_message_content ( client_id , data , operation_type )
2024-01-07 02:18:13 +00:00
return True
2024-12-19 12:48:57 +00:00
def generate ( self , context_details , is_continue , client_id , callback = None ) :
full_prompt , tokens = self . personality . build_context (
context_details , is_continue , True
)
2024-11-30 23:46:28 +00:00
n_predict = self . personality . compute_n_predict ( tokens )
2024-06-27 22:39:42 +00:00
if self . config . debug and self . config . debug_show_final_full_prompt :
2024-12-19 12:48:57 +00:00
ASCIIColors . highlight (
full_prompt ,
[
r
for r in [
2024-06-27 22:39:42 +00:00
self . config . discussion_prompt_separator ,
self . config . start_header_id_template ,
self . config . end_header_id_template ,
self . config . separator_template ,
self . config . start_user_header_id_template ,
self . config . end_user_header_id_template ,
self . config . end_user_message_id_template ,
self . config . start_ai_header_id_template ,
self . config . end_ai_header_id_template ,
self . config . end_ai_message_id_template ,
self . config . system_message_template ,
2024-12-19 12:48:57 +00:00
]
if r != " " and r != " \n "
] ,
)
2024-08-06 09:47:49 +00:00
if self . config . use_smart_routing :
2024-12-19 12:48:57 +00:00
if (
self . config . smart_routing_router_model != " "
and len ( self . config . smart_routing_models_description ) > = 2
) :
2024-08-06 09:47:49 +00:00
ASCIIColors . yellow ( " Using smart routing " )
self . personality . step_start ( " Routing request " )
2024-12-19 12:48:57 +00:00
self . back_model = (
f " { self . binding . binding_folder_name } :: { self . model . model_name } "
)
2024-08-06 09:47:49 +00:00
try :
2024-12-19 12:48:57 +00:00
if not hasattr ( self , " routing_model " ) or self . routing_model is None :
binding , model_name = self . model_path_to_binding_model (
self . config . smart_routing_router_model
)
2024-11-18 22:09:35 +00:00
self . select_model ( binding , model_name )
self . routing_model = self . model
else :
self . set_active_model ( self . routing_model )
2024-12-19 12:48:57 +00:00
models = [
f " { k } "
for k , v in self . config . smart_routing_models_description . items ( )
]
output_id , explanation = self . personality . multichoice_question (
" Select most suitable model to answer the user request given the context. Answer with the selected model index followed by an explanation in a new line. " ,
[
f " { k } : { v } "
for k , v in self . config . smart_routing_models_description . items ( )
] ,
" !@>user prompt: " + context_details [ " prompt " ] ,
return_explanation = True ,
)
if output_id > = 0 and output_id < len ( models ) :
binding , model_name = self . model_path_to_binding_model (
models [ output_id ]
)
self . select_model (
binding , model_name , destroy_previous_model = False
)
2024-08-06 09:47:49 +00:00
self . personality . step_end ( " Routing request " )
2024-11-18 22:33:14 +00:00
self . personality . step ( f " Choice explanation: { explanation } " )
self . personality . step ( f " Selected { models [ output_id ] } " )
else :
2024-12-19 12:48:57 +00:00
ASCIIColors . error (
" Model failed to find the most suited model for your request "
)
self . info (
" Model failed to find the most suited model for your request "
)
binding , model_name = self . model_path_to_binding_model (
models [ 0 ]
)
self . select_model (
binding , model_name , destroy_previous_model = False
)
2024-11-18 22:33:14 +00:00
self . personality . step_end ( " Routing request " )
2024-09-29 10:10:18 +00:00
self . personality . step ( f " Complexity level: { output_id } " )
2024-11-17 23:47:48 +00:00
self . personality . step ( f " Selected { models [ output_id ] } " )
2024-08-06 09:47:49 +00:00
except Exception as ex :
self . error ( " Failed to route beceause of this error : " + str ( ex ) )
self . personality . step_end ( " Routing request " , False )
else :
2024-12-19 12:48:57 +00:00
ASCIIColors . yellow (
" Warning! Smart routing is active but one of the following requirements are not met "
)
2024-08-06 09:47:49 +00:00
ASCIIColors . yellow ( " - smart_routing_router_model must be set correctly " )
2024-12-19 12:48:57 +00:00
ASCIIColors . yellow (
" - smart_routing_models_description must contain at least one model "
)
2024-08-06 09:47:49 +00:00
2024-01-07 02:18:13 +00:00
if self . personality . processor is not None :
ASCIIColors . info ( " Running workflow " )
try :
self . personality . callback = callback
2024-05-03 12:52:42 +00:00
client = self . session . get_client ( client_id )
self . personality . vectorizer = client . discussion . vectorizer
self . personality . text_files = client . discussion . text_files
self . personality . image_files = client . discussion . image_files
self . personality . audio_files = client . discussion . audio_files
2024-12-19 12:48:57 +00:00
output = self . personality . processor . run_workflow (
context_details , client , callback
)
2024-01-07 02:18:13 +00:00
except Exception as ex :
trace_exception ( ex )
# Catch the exception and get the traceback as a list of strings
2024-12-19 12:48:57 +00:00
traceback_lines = traceback . format_exception (
type ( ex ) , ex , ex . __traceback__
)
2024-01-07 02:18:13 +00:00
# Join the traceback lines into a single string
2024-12-19 12:48:57 +00:00
traceback_text = " " . join ( traceback_lines )
2024-01-07 02:18:13 +00:00
ASCIIColors . error ( f " Workflow run failed. \n Error: { ex } " )
ASCIIColors . error ( traceback_text )
if callback :
2024-12-19 12:48:57 +00:00
callback (
f " Workflow run failed \n Error: { ex } " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_EXCEPTION ,
)
return
2024-01-07 02:18:13 +00:00
print ( " Finished executing the workflow " )
2024-05-05 18:57:11 +00:00
return output
2024-01-07 02:18:13 +00:00
2024-05-05 18:57:11 +00:00
txt = self . _generate ( full_prompt , n_predict , client_id , callback )
2024-01-07 02:18:13 +00:00
ASCIIColors . success ( " \n Finished executing the generation " )
2024-08-06 09:47:49 +00:00
2024-12-19 12:48:57 +00:00
if (
self . config . use_smart_routing
and self . config . restore_model_after_smart_routing
) :
if (
self . config . smart_routing_router_model != " "
and len ( self . config . smart_routing_models_description ) > = 2
) :
2024-08-06 09:47:49 +00:00
ASCIIColors . yellow ( " Restoring model " )
self . personality . step_start ( " Restoring main model " )
binding , model_name = self . model_path_to_binding_model ( self . back_model )
self . select_model ( binding , model_name )
self . personality . step_end ( " Restoring main model " )
2024-05-05 18:57:11 +00:00
return txt
2024-01-07 02:18:13 +00:00
def _generate ( self , prompt , n_predict , client_id , callback = None ) :
2024-03-10 02:18:17 +00:00
client = self . session . get_client ( client_id )
2024-08-15 15:28:33 +00:00
if client is None :
return None
2024-01-07 02:18:13 +00:00
self . nb_received_tokens = 0
self . start_time = datetime . now ( )
if self . model is not None :
2024-12-19 12:48:57 +00:00
if (
self . model . binding_type == BindingType . TEXT_IMAGE
and len ( client . discussion . image_files ) > 0
) :
2024-01-07 02:18:13 +00:00
if self . config [ " override_personality_model_parameters " ] :
output = self . model . generate_with_images (
prompt ,
2024-05-04 23:17:41 +00:00
client . discussion . image_files ,
2024-01-07 02:18:13 +00:00
callback = callback ,
n_predict = n_predict ,
2024-12-19 12:48:57 +00:00
temperature = self . config [ " temperature " ] ,
top_k = self . config [ " top_k " ] ,
top_p = self . config [ " top_p " ] ,
repeat_penalty = self . config [ " repeat_penalty " ] ,
repeat_last_n = self . config [ " repeat_last_n " ] ,
seed = self . config [ " seed " ] ,
n_threads = self . config [ " n_threads " ] ,
2024-01-07 02:18:13 +00:00
)
else :
2024-12-19 12:48:57 +00:00
prompt = " \n " . join (
[
f " { self . start_header_id_template } { self . system_message_template } { self . end_header_id_template } I am an AI assistant that can converse and analyze images. When asked to locate something in an image you send, I will reply with: " ,
" boundingbox(image_index, label, left, top, width, height) " ,
" Where: " ,
" image_index: 0-based index of the image " ,
" label: brief description of what is located " ,
" left, top: x,y coordinates of top-left box corner (0-1 scale) " ,
" width, height: box dimensions as fraction of image size " ,
" Coordinates have origin (0,0) at top-left, (1,1) at bottom-right. " ,
" For other queries, I will respond conversationally to the best of my abilities. " ,
prompt ,
]
)
2024-01-07 02:18:13 +00:00
output = self . model . generate_with_images (
prompt ,
2024-05-04 23:17:41 +00:00
client . discussion . image_files ,
2024-01-07 02:18:13 +00:00
callback = callback ,
2024-05-29 00:53:18 +00:00
n_predict = n_predict ,
2024-01-07 02:18:13 +00:00
temperature = self . personality . model_temperature ,
top_k = self . personality . model_top_k ,
top_p = self . personality . model_top_p ,
repeat_penalty = self . personality . model_repeat_penalty ,
2024-12-19 12:48:57 +00:00
repeat_last_n = self . personality . model_repeat_last_n ,
seed = self . config [ " seed " ] ,
n_threads = self . config [ " n_threads " ] ,
2024-03-10 02:18:17 +00:00
)
try :
2024-12-19 12:48:57 +00:00
post_processed_output = process_ai_output (
output ,
client . discussion . image_files ,
client . discussion . discussion_folder ,
)
if len ( post_processed_output ) != output :
self . process_data (
post_processed_output ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
client_id = client_id ,
)
2024-03-10 02:18:17 +00:00
except Exception as ex :
2024-12-19 12:48:57 +00:00
ASCIIColors . error ( str ( ex ) )
2024-01-07 02:18:13 +00:00
else :
if self . config [ " override_personality_model_parameters " ] :
output = self . model . generate (
prompt ,
callback = callback ,
n_predict = n_predict ,
2024-12-19 12:48:57 +00:00
temperature = self . config [ " temperature " ] ,
top_k = self . config [ " top_k " ] ,
top_p = self . config [ " top_p " ] ,
repeat_penalty = self . config [ " repeat_penalty " ] ,
repeat_last_n = self . config [ " repeat_last_n " ] ,
seed = self . config [ " seed " ] ,
n_threads = self . config [ " n_threads " ] ,
2024-01-07 02:18:13 +00:00
)
else :
output = self . model . generate (
prompt ,
callback = callback ,
2024-05-29 00:53:18 +00:00
n_predict = n_predict ,
2024-01-07 02:18:13 +00:00
temperature = self . personality . model_temperature ,
top_k = self . personality . model_top_k ,
top_p = self . personality . model_top_p ,
repeat_penalty = self . personality . model_repeat_penalty ,
2024-12-19 12:48:57 +00:00
repeat_last_n = self . personality . model_repeat_last_n ,
seed = self . config [ " seed " ] ,
n_threads = self . config [ " n_threads " ] ,
2024-01-07 02:18:13 +00:00
)
else :
2024-12-19 12:48:57 +00:00
print (
" No model is installed or selected. Please make sure to install a model and select it inside your configuration before attempting to communicate with the model. "
)
2024-01-07 02:18:13 +00:00
print ( " To do this: Install the model to your models/<binding name> folder. " )
2024-12-19 12:48:57 +00:00
print (
" Then set your model information in your local configuration file that you can find in configs/local_config.yaml "
)
2024-01-07 02:18:13 +00:00
print ( " You can also use the ui to set your model in the settings page. " )
output = " "
return output
2024-12-19 12:48:57 +00:00
def start_message_generation (
self ,
message ,
message_id ,
client_id ,
is_continue = False ,
generation_type = None ,
force_using_internet = False ,
) :
2024-02-26 00:55:44 +00:00
client = self . session . get_client ( client_id )
2024-01-07 02:18:13 +00:00
if self . personality is None :
self . warning ( " Select a personality " )
return
ASCIIColors . info ( f " Text generation requested by client: { client_id } " )
# send the message to the bot
2024-02-26 00:55:44 +00:00
if client . discussion :
2024-01-07 02:18:13 +00:00
try :
2024-12-19 12:48:57 +00:00
ASCIIColors . info (
f " Received message : { message . content } ( { len ( self . model . tokenize ( message . content ) ) } ) "
)
2024-02-11 01:09:31 +00:00
# First we need to send the new message ID to the client
if is_continue :
2024-02-26 00:55:44 +00:00
client . discussion . load_message ( message_id )
client . generated_text = message . content
2024-02-11 01:09:31 +00:00
else :
2024-04-21 00:50:43 +00:00
self . send_refresh ( client_id )
2024-02-11 01:09:31 +00:00
self . new_message ( client_id , self . personality . name , " " )
2024-12-19 12:48:57 +00:00
self . update_message_step (
client_id ,
" 🔥 warming up ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_START ,
)
2024-02-11 01:09:31 +00:00
# prepare query and reception
2024-12-19 12:48:57 +00:00
context_details = self . prepare_query (
client_id ,
message_id ,
is_continue ,
n_tokens = self . config . min_n_predict ,
generation_type = generation_type ,
force_using_internet = force_using_internet ,
previous_chunk = client . generated_text if is_continue else " " ,
)
ASCIIColors . info (
f " prompt has { self . config . ctx_size - context_details [ ' available_space ' ] } tokens "
)
ASCIIColors . info (
f " warmup for generating up to { min ( context_details [ ' available_space ' ] , self . config . max_n_predict ) } tokens "
)
2024-02-11 01:09:31 +00:00
self . prepare_reception ( client_id )
self . generating = True
2024-12-19 12:48:57 +00:00
client . processing = True
2024-02-11 01:09:31 +00:00
try :
2024-12-19 12:48:57 +00:00
self . generate (
context_details ,
client_id = client_id ,
is_continue = is_continue ,
callback = partial ( self . process_data , client_id = client_id ) ,
)
if (
self . tts
and self . config . auto_read
and len ( self . personality . audio_samples ) > 0
) :
2024-02-11 01:09:31 +00:00
try :
2024-12-19 12:48:57 +00:00
self . process_data (
" Generating voice output " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_START ,
client_id = client_id ,
)
from lollms . services . tts . xtts . lollms_xtts import \
LollmsXTTS
voice = self . config . xtts_current_voice
if voice != " main_voice " :
2024-05-05 00:23:07 +00:00
voices_folder = self . lollms_paths . custom_voices_path
else :
2024-12-19 12:48:57 +00:00
voices_folder = (
Path ( __file__ ) . parent . parent . parent
/ " services/xtts/voices "
)
2024-05-05 00:23:07 +00:00
2024-05-18 18:56:11 +00:00
if self . xtts . ready :
2024-12-19 12:48:57 +00:00
language = convert_language_name (
self . personality . language
)
self . xtts . set_speaker_folder (
Path ( self . personality . audio_samples [ 0 ] ) . parent
)
fn = (
self . personality . name . lower ( )
. replace ( " " , " _ " )
. replace ( " . " , " " )
)
2024-05-04 23:17:41 +00:00
fn = f " { fn } _ { message_id } .wav "
url = f " audio/ { fn } "
2024-12-19 12:48:57 +00:00
self . xtts . tts_file (
client . generated_text ,
Path ( self . personality . audio_samples [ 0 ] ) . name ,
f " { fn } " ,
language = language ,
)
fl = f " \n " . join (
[
f " <audio controls> " ,
f ' <source src= " { url } " type= " audio/wav " > ' ,
f " Your browser does not support the audio element. " ,
f " </audio> " ,
]
)
self . process_data (
" Generating voice output " ,
operation_type = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
client_id = client_id ,
)
self . process_data (
fl ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_UI ,
client_id = client_id ,
)
2024-02-11 01:09:31 +00:00
else :
2024-12-19 12:48:57 +00:00
self . InfoMessage (
" xtts is not up yet. \n Please wait for it to load then try again. This may take some time. "
)
2024-02-11 01:09:31 +00:00
except Exception as ex :
ASCIIColors . error ( " Couldn ' t read " )
trace_exception ( ex )
print ( )
ASCIIColors . success ( " ## Done Generation ## " )
print ( )
except Exception as ex :
trace_exception ( ex )
print ( )
ASCIIColors . error ( " ## Generation Error ## " )
print ( )
self . cancel_gen = False
2024-06-25 23:27:29 +00:00
sources_text = " "
if len ( context_details [ " documentation_entries " ] ) > 0 :
sources_text + = ' <div class= " text-gray-400 mr-10px " >Sources:</div> '
sources_text + = ' <div class= " mt-4 flex flex-col items-start gap-x-2 gap-y-1.5 text-sm " style= " max-height: 500px; overflow-y: auto; " > '
for source in context_details [ " documentation_entries " ] :
title = source [ " document_title " ]
path = source [ " document_path " ]
content = source [ " chunk_content " ]
size = source [ " chunk_size " ]
2024-12-15 18:00:46 +00:00
similarity = source [ " similarity " ]
2024-12-19 12:48:57 +00:00
sources_text + = f """
2024-06-25 23:27:29 +00:00
< div class = " source-item " >
2024-12-15 20:02:27 +00:00
< button onclick = " var details = document.getElementById( ' source-details- {title} - {message_id} ' ); details.style.display = details.style.display === ' none ' ? ' block ' : ' none ' ; " style = " text-align: left; font-weight: bold; " > < strong > { title } < / strong > - ( { similarity * 100 : .2 f } % ) < / button >
< div id = " source-details- {title} - {message_id} " style = " display:none; " >
2024-07-10 00:18:00 +00:00
< div style = " max-height: 200px; overflow-y: auto; " >
< p > < strong > Path : < / strong > { path } < / p >
< p > < strong > Content : < / strong > { content } < / p >
< p > < strong > Size : < / strong > { size } < / p >
2024-12-15 18:00:46 +00:00
< p > < strong > Similarity : < / strong > { similarity } < / p >
2024-07-10 00:18:00 +00:00
< / div >
2024-06-25 23:27:29 +00:00
< / div >
< / div >
2024-12-19 12:48:57 +00:00
"""
sources_text + = " </div> "
self . personality . ui ( sources_text )
2024-07-10 00:18:00 +00:00
if len ( context_details [ " skills " ] ) > 0 :
sources_text + = ' <div class= " text-gray-400 mr-10px " >Memories:</div> '
2024-12-15 01:17:42 +00:00
sources_text + = ' <div class= " mt-4 w-full flex flex-col items-start gap-x-2 gap-y-1.5 text-sm " style= " max-height: 500px; overflow-y: auto; " > '
2024-07-10 00:18:00 +00:00
ind = 0
for skill in context_details [ " skills " ] :
2024-12-19 12:48:57 +00:00
sources_text + = f """
2024-07-10 00:18:00 +00:00
< div class = " source-item " >
2024-12-15 20:02:27 +00:00
< button onclick = " var details = document.getElementById( ' source-details- {ind} - {message_id} ' ); details.style.display = details.style.display === ' none ' ? ' block ' : ' none ' ; " style = " text-align: left; font-weight: bold; " > < strong > Memory { ind } : { skill [ ' title ' ] } < / strong > - ( { skill [ ' similarity ' ] * 100 : .2 f } % ) < / button >
< div id = " source-details- {ind} - {message_id} " style = " display:none; " >
2024-12-15 01:17:42 +00:00
< div class = " w-full " style = " max-height: 200px; overflow-y: auto; " >
2024-12-03 00:03:26 +00:00
< pre > { skill [ ' content ' ] } < / pre >
2024-07-10 00:18:00 +00:00
< / div >
< / div >
< / div >
2024-12-19 12:48:57 +00:00
"""
2024-07-10 00:18:00 +00:00
ind + = 1
2024-12-19 12:48:57 +00:00
sources_text + = " </div> "
self . personality . ui ( sources_text )
2024-02-11 01:09:31 +00:00
# Send final message
2024-12-19 12:48:57 +00:00
if (
self . config . activate_internet_search
or force_using_internet
or generation_type == " full_context_with_internet "
) :
2024-02-11 01:09:31 +00:00
from lollms . internet import get_favicon_url , get_root_url
2024-12-19 12:48:57 +00:00
sources_text + = """
2024-07-10 00:18:00 +00:00
< div class = " mt-4 text-sm " >
< div class = " text-gray-500 font-semibold mb-2 " > Sources : < / div >
2024-07-21 20:50:39 +00:00
< div class = " grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 h-64 overflow-y-auto scrollbar-thin scrollbar-thumb-gray-300 scrollbar-track-gray-100 " >
2024-12-19 12:48:57 +00:00
"""
2024-07-10 00:18:00 +00:00
2024-11-30 23:46:28 +00:00
for source in context_details [ " internet_search_infos " ] :
2024-02-11 01:09:31 +00:00
url = source [ " url " ]
title = source [ " title " ]
brief = source [ " brief " ]
2024-12-19 12:48:57 +00:00
favicon_url = (
get_favicon_url ( url )
or " /personalities/generic/lollms/assets/logo.png "
)
2024-07-10 00:18:00 +00:00
root_url = get_root_url ( url )
2024-12-19 12:48:57 +00:00
sources_text + = f """
2024-07-21 20:50:39 +00:00
< div class = " relative flex flex-col items-start gap-2 rounded-lg border border-gray-200 bg-white p-3 shadow-sm transition duration-200 ease-in-out transform hover:scale-105 hover:border-gray-300 hover:shadow-lg dark:border-gray-700 dark:bg-gray-800 dark:hover:border-gray-600 dark:hover:shadow-lg animate-fade-in " >
< a class = " flex items-center w-full " target = " _blank " href = " {url} " title = " {brief} " >
< img class = " h-8 w-8 rounded-full " src = " {favicon_url} " alt = " {title} " onerror = " this.onerror=null;this.src= ' /personalities/generic/lollms/assets/logo.png ' ; " >
2024-07-21 20:47:36 +00:00
< div class = " ml-2 " >
2024-07-21 20:50:39 +00:00
< div class = " text-gray-700 dark:text-gray-300 font-semibold text-sm " > { title } < / div >
< div class = " text-gray-500 dark:text-gray-400 text-xs " > { root_url } < / div >
< div class = " text-gray-400 dark:text-gray-500 text-xs " > { brief } < / div >
2024-07-21 20:47:36 +00:00
< / div >
< / a >
< / div >
2024-12-19 12:48:57 +00:00
"""
2024-07-10 00:18:00 +00:00
2024-12-19 12:48:57 +00:00
sources_text + = """
2024-07-10 00:18:00 +00:00
< / div >
< / div >
2024-12-19 12:48:57 +00:00
"""
2024-07-10 00:18:00 +00:00
2024-07-21 20:50:39 +00:00
# Add CSS for animations and scrollbar styles
2024-12-19 12:48:57 +00:00
sources_text + = """
2024-07-10 00:18:00 +00:00
< style >
@keyframes fadeIn {
from { opacity : 0 ; transform : translateY ( 10 px ) ; }
to { opacity : 1 ; transform : translateY ( 0 ) ; }
}
. animate - fade - in {
animation : fadeIn 0.5 s ease - in - out ;
}
2024-07-21 20:50:39 +00:00
. scrollbar - thin : : - webkit - scrollbar {
width : 8 px ;
}
. scrollbar - thin : : - webkit - scrollbar - thumb {
background - color : #cbd5e1; /* Tailwind gray-300 */
border - radius : 10 px ;
}
. scrollbar - thin : : - webkit - scrollbar - track {
background : #f9fafb; /* Tailwind gray-100 */
}
2024-07-10 00:18:00 +00:00
< / style >
2024-12-19 12:48:57 +00:00
"""
2024-06-25 23:27:29 +00:00
self . personality . ui ( sources_text )
2024-07-21 20:50:39 +00:00
2024-02-26 00:55:44 +00:00
except Exception as ex :
trace_exception ( ex )
2024-06-25 23:27:29 +00:00
try :
2024-12-19 12:48:57 +00:00
self . update_message_step (
client_id ,
" 🔥 warming up ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
self . update_message_step (
client_id ,
" ✍ generating ... " ,
MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_STEP_END_SUCCESS ,
)
2024-06-25 23:27:29 +00:00
except Exception as ex :
ASCIIColors . warning ( " Couldn ' t send status update to client " )
2024-05-12 23:24:50 +00:00
self . close_message ( client_id )
2024-02-10 10:33:09 +00:00
2024-12-19 12:48:57 +00:00
client . processing = False
2024-05-30 17:31:32 +00:00
# Clients are now kept forever
2024-12-19 12:48:57 +00:00
# if client.schedule_for_deletion:
2024-05-30 17:31:32 +00:00
# self.session.remove_client(client.client_id, client.client_id)
2024-01-07 02:18:13 +00:00
2024-12-19 12:48:57 +00:00
ASCIIColors . success (
f " ╔══════════════════════════════════════════════════╗ "
)
ASCIIColors . success (
f " ║ Done ║ "
)
ASCIIColors . success (
f " ╚══════════════════════════════════════════════════╝ "
)
2024-01-07 02:18:13 +00:00
if self . config . auto_title :
2024-02-26 00:55:44 +00:00
d = client . discussion
2024-01-07 02:18:13 +00:00
ttl = d . title ( )
2024-12-19 12:48:57 +00:00
if ttl is None or ttl == " " or ttl == " untitled " :
2024-01-07 02:18:13 +00:00
title = self . make_discussion_title ( d , client_id = client_id )
d . rename ( title )
2024-01-08 00:38:42 +00:00
asyncio . run (
2024-12-19 12:48:57 +00:00
self . sio . emit (
" disucssion_renamed " ,
{
" status " : True ,
" discussion_id " : d . discussion_id ,
" title " : title ,
} ,
to = client_id ,
)
2024-01-07 14:27:14 +00:00
)
2024-12-19 12:48:57 +00:00
self . busy = False
2024-01-07 02:18:13 +00:00
else :
self . cancel_gen = False
2024-12-19 12:48:57 +00:00
# No discussion available
2024-01-07 02:18:13 +00:00
ASCIIColors . warning ( " No discussion selected!!! " )
self . error ( " No discussion selected!!! " , client_id = client_id )
2024-12-19 12:48:57 +00:00
2024-01-07 02:18:13 +00:00
print ( )
2024-12-19 12:48:57 +00:00
self . busy = False
2024-01-07 21:34:33 +00:00
return " "
2024-05-05 15:28:45 +00:00
2024-12-19 12:48:57 +00:00
def receive_and_generate ( self , text , client : Client , callback = None ) :
2024-05-05 15:28:45 +00:00
prompt = text
try :
nb_tokens = len ( self . model . tokenize ( prompt ) )
except :
nb_tokens = None
2024-12-19 12:48:57 +00:00
2024-05-05 15:28:45 +00:00
message = client . discussion . add_message (
2024-12-19 12:48:57 +00:00
operation_type = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT . value ,
sender_type = SENDER_TYPES . SENDER_TYPES_USER . value ,
sender = (
self . config . user_name . strip ( )
if self . config . use_user_name_in_discussions
else self . personality . user_message_prefix
) ,
content = prompt ,
metadata = None ,
2024-05-05 15:28:45 +00:00
parent_message_id = self . message_id ,
2024-12-19 12:48:57 +00:00
nb_tokens = nb_tokens ,
)
context_details = self . prepare_query (
client . client_id ,
client . discussion . current_message . id ,
False ,
n_tokens = self . config . min_n_predict ,
force_using_internet = False ,
2024-05-05 15:28:45 +00:00
)
2024-05-05 18:57:11 +00:00
self . new_message (
2024-12-19 12:48:57 +00:00
client . client_id ,
self . personality . name ,
operation_type = MSG_OPERATION_TYPE . MSG_OPERATION_TYPE_SET_CONTENT ,
content = " " ,
2024-05-05 18:57:11 +00:00
)
client . generated_text = " "
2024-12-19 12:48:57 +00:00
ASCIIColors . info (
f " prompt has { self . config . ctx_size - context_details [ ' available_space ' ] } tokens "
)
ASCIIColors . info (
f " warmup for generating up to { min ( context_details [ ' available_space ' ] , self . config . max_n_predict if self . config . max_n_predict else self . config . ctx_size ) } tokens "
)
self . generate (
context_details ,
client . client_id ,
False ,
(
callback
if callback
else partial ( self . process_data , client_id = client . client_id )
) ,
)
self . close_message ( client . client_id )
2024-05-05 18:57:11 +00:00
return client . generated_text