lollms-webui/api/__init__.py

3643 lines
190 KiB
Python
Raw Normal View History

######
2023-06-08 06:58:02 +00:00
# Project : lollms-webui
2023-11-15 14:06:06 +00:00
# File : api/__init__.py
# Author : ParisNeo with the help of the community
2023-05-21 20:46:02 +00:00
# license : Apache 2.0
# Description :
2023-06-08 06:58:02 +00:00
# A simple api to communicate with lollms-webui and its models.
######
2023-06-10 21:09:56 +00:00
from flask import request
from datetime import datetime
2023-08-02 23:07:29 +00:00
from api.db import DiscussionsDB, Discussion
2023-04-23 18:28:24 +00:00
from pathlib import Path
2023-06-21 22:43:59 +00:00
from lollms.config import InstallOption
2023-08-02 23:07:29 +00:00
from lollms.types import MSG_TYPE, SENDER_TYPES
2023-10-15 23:07:03 +00:00
from lollms.extension import LOLLMSExtension, ExtensionBuilder
2023-06-22 21:33:57 +00:00
from lollms.personality import AIPersonality, PersonalityBuilder
from lollms.binding import LOLLMSConfig, BindingBuilder, LLMBinding, ModelBuilder, BindingType
2023-06-10 13:16:28 +00:00
from lollms.paths import LollmsPaths
2023-07-12 15:48:39 +00:00
from lollms.helpers import ASCIIColors, trace_exception
2023-07-04 00:19:20 +00:00
from lollms.app import LollmsApplication
2023-10-08 23:18:21 +00:00
from lollms.utilities import File64BitsManager, PromptReshaper
2023-10-09 23:47:49 +00:00
from safe_store import TextVectorizer, VectorizationMethod, VisualizationMethod
2023-05-13 22:24:26 +00:00
import threading
2023-05-14 11:33:45 +00:00
from tqdm import tqdm
2023-05-21 23:29:20 +00:00
import traceback
2023-05-29 20:03:12 +00:00
import sys
2023-07-04 07:24:44 +00:00
import gc
2023-07-13 14:49:54 +00:00
import ctypes
from functools import partial
2023-08-02 23:07:29 +00:00
import json
2023-08-30 12:38:24 +00:00
import shutil
2023-09-14 08:38:33 +00:00
import re
import string
2023-09-14 11:30:15 +00:00
import requests
from datetime import datetime
2023-11-02 20:20:29 +00:00
from typing import List, Tuple
2023-07-13 14:49:54 +00:00
def terminate_thread(thread):
2023-07-15 18:02:03 +00:00
if thread:
if not thread.is_alive():
2023-07-18 14:39:05 +00:00
ASCIIColors.yellow("Thread not alive")
2023-07-15 18:02:03 +00:00
return
2023-07-13 14:49:54 +00:00
2023-07-15 18:02:03 +00:00
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)
2023-09-07 12:53:11 +00:00
del thread
gc.collect()
2023-07-15 18:02:03 +00:00
raise SystemError("Failed to terminate the thread.")
2023-07-18 14:39:05 +00:00
else:
ASCIIColors.yellow("Canceled successfully")
2023-04-23 18:28:24 +00:00
2023-04-20 17:30:03 +00:00
__author__ = "parisneo"
2023-06-08 06:58:02 +00:00
__github__ = "https://github.com/ParisNeo/lollms-webui"
2023-04-20 17:30:03 +00:00
__copyright__ = "Copyright 2023, "
__license__ = "Apache 2.0"
2023-05-13 22:24:26 +00:00
2023-05-17 15:38:40 +00:00
import subprocess
import pkg_resources
# ===========================================================
# Manage automatic install scripts
def is_package_installed(package_name):
try:
dist = pkg_resources.get_distribution(package_name)
return True
except pkg_resources.DistributionNotFound:
return False
def install_package(package_name):
try:
# Check if the package is already installed
__import__(package_name)
print(f"{package_name} is already installed.")
except ImportError:
print(f"{package_name} is not installed. Installing...")
# Install the package using pip
subprocess.check_call(["pip", "install", package_name])
print(f"{package_name} has been successfully installed.")
def parse_requirements_file(requirements_path):
with open(requirements_path, 'r') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'):
# Skip empty and commented lines
continue
package_name, _, version_specifier = line.partition('==')
package_name, _, version_specifier = line.partition('>=')
if is_package_installed(package_name):
# The package is already installed
print(f"{package_name} is already installed.")
else:
# The package is not installed, install it
if version_specifier:
install_package(f"{package_name}{version_specifier}")
else:
install_package(package_name)
# ===========================================================
2023-11-26 01:33:25 +00:00
class LoLLMsAPI(LollmsApplication):
2023-06-10 13:16:28 +00:00
def __init__(self, config:LOLLMSConfig, socketio, config_file_path:str, lollms_paths: LollmsPaths) -> None:
2023-06-19 22:53:53 +00:00
2023-11-08 14:21:05 +00:00
super().__init__("Lollms_webui",config, lollms_paths, callback=self.process_chunk, notification_callback=self.notify)
2023-10-09 23:47:49 +00:00
2023-08-19 12:06:24 +00:00
self.busy = False
2023-10-05 00:16:18 +00:00
self.nb_received_tokens = 0
2023-07-13 14:49:54 +00:00
2023-05-13 22:24:26 +00:00
self.socketio = socketio
self.config_file_path = config_file_path
2023-04-23 22:19:15 +00:00
self.cancel_gen = False
2023-10-07 21:16:27 +00:00
# Keeping track of current discussion and message
2023-05-09 05:06:01 +00:00
self._current_user_message_id = 0
self._current_ai_message_id = 0
self._message_id = 0
self.db_path = config["db_path"]
2023-06-10 13:49:41 +00:00
if Path(self.db_path).is_absolute():
# Create database object
self.db = DiscussionsDB(self.db_path)
else:
# Create database object
2023-10-08 23:18:21 +00:00
self.db = DiscussionsDB(self.lollms_paths.personal_databases_path/self.db_path)
# If the database is empty, populate it with tables
2023-06-15 16:51:04 +00:00
ASCIIColors.info("Checking discussions database... ",end="")
2023-06-15 16:49:47 +00:00
self.db.create_tables()
self.db.add_missing_columns()
2023-06-15 16:51:04 +00:00
ASCIIColors.success("ok")
2023-10-09 23:47:49 +00:00
# prepare vectorization
if self.config.data_vectorization_activate and self.config.use_discussions_history:
try:
ASCIIColors.yellow("Loading vectorized discussions")
folder = self.lollms_paths.personal_databases_path/"vectorized_dbs"
folder.mkdir(parents=True, exist_ok=True)
self.discussions_store = TextVectorizer(
vectorization_method=VectorizationMethod.TFIDF_VECTORIZER,#=VectorizationMethod.BM25_VECTORIZER,
database_path=folder/self.config.db_path,
data_visualization_method=VisualizationMethod.PCA,#VisualizationMethod.PCA,
save_db=True
)
ASCIIColors.yellow("1- Exporting discussions")
discussions = self.db.export_all_as_markdown_list_for_vectorization()
ASCIIColors.yellow("2- Adding discussions to vectorizer")
for (title,discussion) in discussions:
2023-10-10 15:54:01 +00:00
if discussion!='' and title!='None':
2023-10-09 23:47:49 +00:00
self.discussions_store.add_document(title, discussion, chunk_size=self.config.data_vectorization_chunk_size, overlap_size=self.config.data_vectorization_overlap_size, force_vectorize=False, add_as_a_bloc=False)
ASCIIColors.yellow("3- Indexing database")
self.discussions_store.index()
2023-10-28 02:41:04 +00:00
ASCIIColors.yellow("4- Saving database")
2023-10-09 23:47:49 +00:00
self.discussions_store.save_to_json()
ASCIIColors.yellow("Ready")
except Exception as ex:
trace_exception(ex)
self.discussions_store = None
else:
self.discussions_store = None
# This is used to keep track of messages
2023-06-23 12:18:55 +00:00
self.download_infos={}
2023-07-13 14:49:54 +00:00
2023-07-31 01:14:05 +00:00
self.connections = {0:{
"current_discussion":None,
"generated_text":"",
"cancel_generation": False,
"generation_thread": None,
"processing":False,
"schedule_for_deletion":False
}
}
2023-07-13 14:49:54 +00:00
2023-05-13 22:24:26 +00:00
# =========================================================================================
# Socket IO stuff
# =========================================================================================
@socketio.on('connect')
def connect():
2023-07-13 14:49:54 +00:00
#Create a new connection information
self.connections[request.sid] = {
2023-09-14 08:38:33 +00:00
"current_discussion":self.db.load_last_discussion(),
2023-07-13 14:49:54 +00:00
"generated_text":"",
2023-09-29 17:31:20 +00:00
"continuing": False,
"first_chunk": True,
2023-07-13 14:49:54 +00:00
"cancel_generation": False,
2023-07-15 18:02:03 +00:00
"generation_thread": None,
2023-07-26 22:03:28 +00:00
"processing":False,
"schedule_for_deletion":False
2023-08-03 09:48:13 +00:00
}
self.socketio.emit('connected', room=request.sid)
2023-06-11 19:02:06 +00:00
ASCIIColors.success(f'Client {request.sid} connected')
2023-05-13 22:24:26 +00:00
@socketio.on('disconnect')
def disconnect():
2023-07-13 14:49:54 +00:00
try:
2023-08-03 09:48:13 +00:00
self.socketio.emit('disconnected', room=request.sid)
2023-07-26 22:03:28 +00:00
if self.connections[request.sid]["processing"]:
self.connections[request.sid]["schedule_for_deletion"]=True
else:
del self.connections[request.sid]
2023-07-13 14:49:54 +00:00
except Exception as ex:
pass
2023-06-11 19:02:06 +00:00
ASCIIColors.error(f'Client {request.sid} disconnected')
2023-05-13 22:24:26 +00:00
2023-06-23 00:02:37 +00:00
2023-06-23 12:18:55 +00:00
@socketio.on('cancel_install')
def cancel_install(data):
2023-07-20 00:12:54 +00:00
try:
model_name = data["model_name"]
binding_folder = data["binding_folder"]
model_url = data["model_url"]
signature = f"{model_name}_{binding_folder}_{model_url}"
self.download_infos[signature]["cancel"]=True
self.socketio.emit('canceled', {
'status': True
},
room=request.sid
)
except Exception as ex:
trace_exception(ex)
self.socketio.emit('canceled', {
'status': False,
'error':str(ex)
},
room=request.sid
)
2023-05-13 22:24:26 +00:00
@socketio.on('install_model')
def install_model(data):
2023-07-13 14:49:54 +00:00
room_id = request.sid
2023-06-24 15:17:15 +00:00
2023-05-13 22:24:26 +00:00
def install_model_():
print("Install model triggered")
2023-10-19 00:30:18 +00:00
model_path = data["path"].replace("\\","/")
2023-09-12 23:23:06 +00:00
model_type:str=data["type"]
2023-05-13 22:24:26 +00:00
progress = 0
2023-10-20 19:18:29 +00:00
installation_dir = self.binding.searchModelParentFolder(model_path.split('/')[-1], model_type)
2023-11-10 16:03:57 +00:00
if model_type=="gptq" or model_type=="awq":
2023-09-13 00:00:41 +00:00
parts = model_path.split("/")
if len(parts)==2:
2023-09-13 21:45:51 +00:00
filename = parts[1]
2023-09-13 00:00:41 +00:00
else:
filename = parts[4]
2023-09-12 23:23:06 +00:00
installation_path = installation_dir / filename
else:
filename = Path(model_path).name
installation_path = installation_dir / filename
2023-05-13 22:24:26 +00:00
print("Model install requested")
print(f"Model path : {model_path}")
2023-06-23 10:23:18 +00:00
model_name = filename
binding_folder = self.config["binding_name"]
model_url = model_path
2023-06-23 12:18:55 +00:00
signature = f"{model_name}_{binding_folder}_{model_url}"
try:
self.download_infos[signature]={
"start_time":datetime.now(),
"total_size":self.binding.get_file_size(model_path),
"downloaded_size":0,
"progress":0,
"speed":0,
"cancel":False
}
if installation_path.exists():
print("Error: Model already exists. please remove it first")
socketio.emit('install_progress',{
'status': False,
'error': f'model already exists. Please remove it first.\nThe model can be found here:{installation_path}',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
2023-11-10 16:03:57 +00:00
return
2023-06-23 10:23:18 +00:00
socketio.emit('install_progress',{
'status': True,
'progress': progress,
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
2023-06-23 12:44:25 +00:00
}, room=room_id)
2023-05-13 22:24:26 +00:00
def callback(downloaded_size, total_size):
progress = (downloaded_size / total_size) * 100
now = datetime.now()
dt = (now - self.download_infos[signature]['start_time']).total_seconds()
speed = downloaded_size/dt
self.download_infos[signature]['downloaded_size'] = downloaded_size
self.download_infos[signature]['speed'] = speed
if progress - self.download_infos[signature]['progress']>2:
self.download_infos[signature]['progress'] = progress
socketio.emit('install_progress',{
'status': True,
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
2023-06-28 20:08:18 +00:00
if self.download_infos[signature]["cancel"]:
raise Exception("canceled")
if hasattr(self.binding, "download_model"):
2023-07-07 16:25:18 +00:00
try:
self.binding.download_model(model_path, installation_path, callback)
2023-07-07 16:25:18 +00:00
except Exception as ex:
ASCIIColors.warning(str(ex))
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': 'canceled',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
del self.download_infos[signature]
try:
2023-08-30 12:38:24 +00:00
if installation_path.is_dir():
shutil.rmtree(installation_path)
else:
installation_path.unlink()
except Exception as ex:
2023-08-30 12:38:24 +00:00
trace_exception(ex)
ASCIIColors.error(f"Couldn't delete file. Please try to remove it manually.\n{installation_path}")
return
2023-06-23 12:18:55 +00:00
else:
try:
self.download_file(model_path, installation_path, callback)
except Exception as ex:
ASCIIColors.warning(str(ex))
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': 'canceled',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
del self.download_infos[signature]
installation_path.unlink()
return
socketio.emit('install_progress',{
'status': True,
'error': '',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': 100,
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
del self.download_infos[signature]
except Exception as ex:
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': str(ex),
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': '',
'total_size': 0,
'downloaded_size': 0,
'progress': 0,
'speed': 0,
}, room=room_id
)
2023-05-13 22:24:26 +00:00
tpe = threading.Thread(target=install_model_, args=())
tpe.start()
2023-06-22 21:23:56 +00:00
2023-05-13 22:24:26 +00:00
@socketio.on('uninstall_model')
def uninstall_model(data):
model_path = data['path']
model_type:str=data.get("type","ggml")
2023-10-30 00:32:24 +00:00
installation_dir = self.binding.searchModelParentFolder(model_path)
2023-06-23 10:30:05 +00:00
binding_folder = self.config["binding_name"]
2023-11-15 14:06:06 +00:00
if model_type=="gptq" or model_type=="awq":
2023-09-12 23:23:06 +00:00
filename = model_path.split("/")[4]
installation_path = installation_dir / filename
else:
filename = Path(model_path).name
installation_path = installation_dir / filename
model_name = filename
2023-05-13 22:24:26 +00:00
if not installation_path.exists():
2023-09-12 23:23:06 +00:00
socketio.emit('uninstall_progress',{
2023-06-23 10:30:05 +00:00
'status': False,
'error': 'The model does not exist',
'model_name' : model_name,
'binding_folder' : binding_folder
}, room=request.sid)
2023-07-07 17:34:30 +00:00
try:
2023-09-15 09:25:57 +00:00
if not installation_path.exists():
# Try to find a version
model_path = installation_path.name.lower().replace("-ggml","").replace("-gguf","")
2023-10-19 22:23:16 +00:00
candidates = [m for m in installation_dir.iterdir() if model_path in m.name]
2023-09-15 09:25:57 +00:00
if len(candidates)>0:
model_path = candidates[0]
installation_path = model_path
2023-08-30 12:38:24 +00:00
if installation_path.is_dir():
shutil.rmtree(installation_path)
else:
installation_path.unlink()
2023-09-12 23:23:06 +00:00
socketio.emit('uninstall_progress',{
2023-07-26 16:12:24 +00:00
'status': True,
'error': '',
'model_name' : model_name,
'binding_folder' : binding_folder
}, room=request.sid)
2023-07-07 17:34:30 +00:00
except Exception as ex:
2023-08-30 12:38:24 +00:00
trace_exception(ex)
2023-07-07 17:34:30 +00:00
ASCIIColors.error(f"Couldn't delete {installation_path}, please delete it manually and restart the app")
2023-09-12 23:23:06 +00:00
socketio.emit('uninstall_progress',{
2023-07-26 16:12:24 +00:00
'status': False,
'error': f"Couldn't delete {installation_path}, please delete it manually and restart the app",
'model_name' : model_name,
'binding_folder' : binding_folder
}, room=request.sid)
2023-06-22 21:33:57 +00:00
2023-08-02 23:07:29 +00:00
@socketio.on('new_discussion')
def new_discussion(data):
client_id = request.sid
title = data["title"]
self.connections[client_id]["current_discussion"] = self.db.create_discussion(title)
# Get the current timestamp
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Return a success response
if self.connections[client_id]["current_discussion"] is None:
self.connections[client_id]["current_discussion"] = self.db.load_last_discussion()
if self.personality.welcome_message!="":
message = self.connections[client_id]["current_discussion"].add_message(
message_type = MSG_TYPE.MSG_TYPE_FULL.value if self.personality.include_welcome_message_in_disucssion else MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value,
sender_type = SENDER_TYPES.SENDER_TYPES_AI.value,
sender = self.personality.name,
content = self.personality.welcome_message,
metadata = None,
rank = 0,
parent_message_id = -1,
binding = self.config.binding_name,
model = self.config.model_name,
personality = self.config.personalities[self.config.active_personality_id],
created_at=None,
finished_generating_at=None
)
self.socketio.emit('discussion_created',
{'id':self.connections[client_id]["current_discussion"].discussion_id},
room=client_id
)
else:
self.socketio.emit('discussion_created',
{'id':0},
room=client_id
)
@socketio.on('load_discussion')
def load_discussion(data):
client_id = request.sid
2023-11-19 00:21:29 +00:00
ASCIIColors.yellow(f"Loading discussion for client {client_id} ... ", end="")
2023-08-02 23:07:29 +00:00
if "id" in data:
discussion_id = data["id"]
self.connections[client_id]["current_discussion"] = Discussion(discussion_id, self.db)
else:
if self.connections[client_id]["current_discussion"] is not None:
discussion_id = self.connections[client_id]["current_discussion"].discussion_id
self.connections[client_id]["current_discussion"] = Discussion(discussion_id, self.db)
else:
self.connections[client_id]["current_discussion"] = self.db.create_discussion()
messages = self.connections[client_id]["current_discussion"].get_messages()
2023-08-17 23:29:53 +00:00
jsons = [m.to_json() for m in messages]
2023-08-02 23:07:29 +00:00
self.socketio.emit('discussion',
2023-08-17 23:29:53 +00:00
jsons,
2023-08-02 23:07:29 +00:00
room=client_id
)
2023-11-19 00:21:29 +00:00
ASCIIColors.green(f"ok")
2023-08-25 00:12:33 +00:00
2023-06-22 21:33:57 +00:00
@socketio.on('upload_file')
def upload_file(data):
2023-10-05 00:16:18 +00:00
ASCIIColors.yellow("Uploading file")
2023-06-22 21:33:57 +00:00
file = data['file']
filename = file.filename
save_path = self.lollms_paths.personal_uploads_path/filename # Specify the desired folder path
try:
2023-06-22 21:39:05 +00:00
if not self.personality.processor is None:
file.save(save_path)
2023-08-22 01:43:17 +00:00
self.personality.processor.add_file(save_path, partial(self.process_chunk, client_id = request.sid))
2023-06-22 21:39:05 +00:00
# File saved successfully
socketio.emit('progress', {'status':True, 'progress': 100})
2023-06-23 00:02:37 +00:00
2023-06-22 21:39:05 +00:00
else:
2023-08-22 01:43:17 +00:00
file.save(save_path)
self.personality.add_file(save_path, partial(self.process_chunk, client_id = request.sid))
# File saved successfully
socketio.emit('progress', {'status':True, 'progress': 100})
2023-06-22 21:33:57 +00:00
except Exception as e:
# Error occurred while saving the file
2023-06-22 21:39:05 +00:00
socketio.emit('progress', {'status':False, 'error': str(e)})
2023-05-13 22:24:26 +00:00
2023-06-25 20:56:06 +00:00
@socketio.on('cancel_generation')
def cancel_generation():
2023-07-13 14:49:54 +00:00
client_id = request.sid
2023-06-25 20:56:06 +00:00
self.cancel_gen = True
2023-07-13 14:49:54 +00:00
#kill thread
ASCIIColors.error(f'Client {request.sid} requested cancelling generation')
terminate_thread(self.connections[client_id]['generation_thread'])
2023-06-25 20:56:06 +00:00
ASCIIColors.error(f'Client {request.sid} canceled generation')
2023-08-19 12:06:24 +00:00
self.busy=False
2023-10-02 00:18:57 +00:00
2023-08-25 00:12:33 +00:00
@socketio.on('get_personality_files')
def get_personality_files(data):
client_id = request.sid
self.connections[client_id]["generated_text"] = ""
self.connections[client_id]["cancel_generation"] = False
try:
self.personality.setCallback(partial(self.process_chunk,client_id = client_id))
except Exception as ex:
trace_exception(ex)
2023-10-27 23:36:53 +00:00
@socketio.on('send_file_chunk')
def send_file_chunk(data):
2023-07-31 22:51:06 +00:00
client_id = request.sid
2023-10-27 23:36:53 +00:00
filename = data['filename']
chunk = data['chunk']
offset = data['offset']
is_last_chunk = data['isLastChunk']
chunk_index = data['chunkIndex']
path:Path = self.lollms_paths.personal_uploads_path / self.personality.personality_folder_name
path.mkdir(parents=True, exist_ok=True)
file_path = path / data["filename"]
# Save the chunk to the server or process it as needed
# For example:
if chunk_index==0:
with open(file_path, 'wb') as file:
file.write(chunk)
else:
with open(file_path, 'ab') as file:
file.write(chunk)
2023-07-31 22:51:06 +00:00
2023-10-27 23:36:53 +00:00
if is_last_chunk:
print('File received and saved successfully')
2023-07-31 22:51:06 +00:00
if self.personality.processor:
2023-08-25 00:12:33 +00:00
result = self.personality.processor.add_file(file_path, partial(self.process_chunk, client_id=client_id))
2023-08-23 02:21:58 +00:00
else:
2023-08-25 00:12:33 +00:00
result = self.personality.add_file(file_path, partial(self.process_chunk, client_id=client_id))
2023-10-27 23:36:53 +00:00
self.socketio.emit('file_received', {'status': True, 'filename': filename})
else:
# Request the next chunk from the client
self.socketio.emit('request_next_chunk', {'offset': offset + len(chunk)})
2023-08-19 01:25:36 +00:00
2023-08-19 12:06:24 +00:00
@self.socketio.on('cancel_text_generation')
def cancel_text_generation(data):
client_id = request.sid
2023-08-19 16:48:08 +00:00
self.connections[client_id]["requested_stop"]=True
2023-08-19 12:06:24 +00:00
print(f"Client {client_id} requested canceling generation")
self.socketio.emit("generation_canceled", {"message":"Generation is canceled."}, room=client_id)
self.socketio.sleep(0)
self.busy = False
2023-08-19 01:25:36 +00:00
2023-08-19 22:12:08 +00:00
@self.socketio.on('execute_python_code')
def execute_python_code(data):
"""Executes Python code and returns the output."""
client_id = request.sid
code = data["code"]
# Import the necessary modules.
import io
import sys
import time
# Create a Python interpreter.
interpreter = io.StringIO()
sys.stdout = interpreter
# Execute the code.
start_time = time.time()
exec(code)
end_time = time.time()
# Get the output.
output = interpreter.getvalue()
self.socketio.emit("execution_output", {"output":output,"execution_time":end_time - start_time}, room=client_id)
2023-08-19 01:25:36 +00:00
2023-10-16 20:28:03 +00:00
@self.socketio.on('create_empty_message')
def create_empty_message(data):
client_id = request.sid
2023-10-30 00:32:24 +00:00
type = data.get("type",0)
if type==0:
ASCIIColors.info(f"Building empty User message requested by : {client_id}")
# send the message to the bot
print(f"Creating an empty message for AI answer orientation")
if self.connections[client_id]["current_discussion"]:
if not self.model:
self.notify("No model selected. Please make sure you select a model before starting generation", False, client_id)
return
2023-11-19 00:21:29 +00:00
self.new_message(client_id, self.config.user_name, "", sender_type=SENDER_TYPES.SENDER_TYPES_USER, open=True)
2023-10-30 00:32:24 +00:00
self.socketio.sleep(0.01)
else:
if self.personality is None:
self.notify("Select a personality",False,None)
return
ASCIIColors.info(f"Building empty AI message requested by : {client_id}")
# send the message to the bot
print(f"Creating an empty message for AI answer orientation")
if self.connections[client_id]["current_discussion"]:
if not self.model:
self.notify("No model selected. Please make sure you select a model before starting generation", False, client_id)
return
2023-11-19 00:21:29 +00:00
self.new_message(client_id, self.personality.name, "[edit this to put your ai answer start]", open=True)
2023-10-30 00:32:24 +00:00
self.socketio.sleep(0.01)
2023-10-16 20:28:03 +00:00
2023-08-19 01:25:36 +00:00
# A copy of the original lollms-server generation code needed for playground
@self.socketio.on('generate_text')
def handle_generate_text(data):
client_id = request.sid
2023-10-02 00:18:57 +00:00
self.cancel_gen = False
2023-08-19 01:25:36 +00:00
ASCIIColors.info(f"Text generation requested by client: {client_id}")
2023-08-19 12:06:24 +00:00
if self.busy:
self.socketio.emit("busy", {"message":"I am busy. Come back later."}, room=client_id)
2023-08-19 01:25:36 +00:00
self.socketio.sleep(0)
2023-08-19 12:06:24 +00:00
ASCIIColors.warning(f"OOps request {client_id} refused!! Server busy")
2023-08-19 01:25:36 +00:00
return
def generate_text():
2023-08-19 12:06:24 +00:00
self.busy = True
2023-08-19 01:25:36 +00:00
try:
model = self.model
self.connections[client_id]["is_generating"]=True
self.connections[client_id]["requested_stop"]=False
prompt = data['prompt']
2023-08-19 12:06:24 +00:00
tokenized = model.tokenize(prompt)
personality_id = data.get('personality', -1)
n_crop = data.get('n_crop', len(tokenized))
if n_crop!=-1:
prompt = model.detokenize(tokenized[-n_crop:])
2023-08-19 01:25:36 +00:00
n_predicts = data["n_predicts"]
parameters = data.get("parameters",{
"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"]
})
if personality_id==-1:
# Raw text generation
self.answer = {"full_text":""}
def callback(text, message_type: MSG_TYPE, metadata:dict={}):
if message_type == MSG_TYPE.MSG_TYPE_CHUNK:
ASCIIColors.success(f"generated:{len(self.answer['full_text'].split())} words", end='\r')
self.answer["full_text"] = self.answer["full_text"] + text
self.socketio.emit('text_chunk', {'chunk': text, 'type':MSG_TYPE.MSG_TYPE_CHUNK.value}, room=client_id)
self.socketio.sleep(0)
if client_id in self.connections:# Client disconnected
if self.connections[client_id]["requested_stop"]:
return False
else:
return True
else:
return False
tk = model.tokenize(prompt)
n_tokens = len(tk)
2023-08-31 00:20:00 +00:00
fd = model.detokenize(tk[-min(self.config.ctx_size-n_predicts,n_tokens):])
2023-08-19 01:25:36 +00:00
try:
ASCIIColors.print("warming up", ASCIIColors.color_bright_cyan)
2023-11-09 01:00:49 +00:00
2023-08-19 01:25:36 +00:00
generated_text = model.generate(fd,
n_predict=n_predicts,
callback=callback,
temperature = parameters["temperature"],
top_k = parameters["top_k"],
top_p = parameters["top_p"],
repeat_penalty = parameters["repeat_penalty"],
repeat_last_n = parameters["repeat_last_n"],
2023-08-31 00:20:00 +00:00
seed = parameters["seed"],
2023-08-19 01:25:36 +00:00
)
ASCIIColors.success(f"\ndone")
if client_id in self.connections:
if not self.connections[client_id]["requested_stop"]:
# Emit the generated text to the client
self.socketio.emit('text_generated', {'text': generated_text}, room=client_id)
self.socketio.sleep(0)
except Exception as ex:
self.socketio.emit('generation_error', {'error': str(ex)}, room=client_id)
ASCIIColors.error(f"\ndone")
2023-08-19 12:06:24 +00:00
self.busy = False
2023-08-19 01:25:36 +00:00
else:
try:
personality: AIPersonality = self.personalities[personality_id]
2023-09-15 08:18:49 +00:00
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
2023-08-19 01:25:36 +00:00
personality.model = model
cond_tk = personality.model.tokenize(personality.personality_conditioning)
n_cond_tk = len(cond_tk)
# Placeholder code for text generation
# Replace this with your actual text generation logic
print(f"Text generation requested by client: {client_id}")
self.answer["full_text"] = ''
full_discussion_blocks = self.connections[client_id]["full_discussion_blocks"]
if prompt != '':
if personality.processor is not None and personality.processor_cfg["process_model_input"]:
preprocessed_prompt = personality.processor.process_model_input(prompt)
else:
preprocessed_prompt = prompt
if personality.processor is not None and personality.processor_cfg["custom_workflow"]:
full_discussion_blocks.append(ump)
full_discussion_blocks.append(preprocessed_prompt)
else:
full_discussion_blocks.append(ump)
full_discussion_blocks.append(preprocessed_prompt)
full_discussion_blocks.append(personality.link_text)
full_discussion_blocks.append(personality.ai_message_prefix)
full_discussion = personality.personality_conditioning + ''.join(full_discussion_blocks)
def callback(text, message_type: MSG_TYPE, metadata:dict={}):
if message_type == MSG_TYPE.MSG_TYPE_CHUNK:
self.answer["full_text"] = self.answer["full_text"] + text
self.socketio.emit('text_chunk', {'chunk': text}, room=client_id)
self.socketio.sleep(0)
try:
if self.connections[client_id]["requested_stop"]:
return False
else:
return True
except: # If the client is disconnected then we stop talking to it
return False
tk = personality.model.tokenize(full_discussion)
n_tokens = len(tk)
fd = personality.model.detokenize(tk[-min(self.config.ctx_size-n_cond_tk-personality.model_n_predicts,n_tokens):])
if personality.processor is not None and personality.processor_cfg["custom_workflow"]:
ASCIIColors.info("processing...")
generated_text = personality.processor.run_workflow(prompt, previous_discussion_text=personality.personality_conditioning+fd, callback=callback)
else:
ASCIIColors.info("generating...")
generated_text = personality.model.generate(
personality.personality_conditioning+fd,
n_predict=personality.model_n_predicts,
callback=callback)
if personality.processor is not None and personality.processor_cfg["process_model_output"]:
generated_text = personality.processor.process_model_output(generated_text)
full_discussion_blocks.append(generated_text.strip())
ASCIIColors.success("\ndone")
# Emit the generated text to the client
self.socketio.emit('text_generated', {'text': generated_text}, room=client_id)
self.socketio.sleep(0)
except Exception as ex:
self.socketio.emit('generation_error', {'error': str(ex)}, room=client_id)
ASCIIColors.error(f"\ndone")
2023-08-19 12:06:24 +00:00
self.busy = False
2023-08-19 01:25:36 +00:00
except Exception as ex:
trace_exception(ex)
self.socketio.emit('generation_error', {'error': str(ex)}, room=client_id)
2023-08-19 12:06:24 +00:00
self.busy = False
2023-08-19 01:25:36 +00:00
# Start the text generation task in a separate thread
task = self.socketio.start_background_task(target=generate_text)
@socketio.on('execute_command')
def execute_command(data):
client_id = request.sid
command = data["command"]
parameters = data["parameters"]
if self.personality.processor is not None:
self.start_time = datetime.now()
self.personality.processor.callback = partial(self.process_chunk, client_id=client_id)
self.personality.processor.execute_command(command, parameters)
else:
self.notify("Non scripted personalities do not support commands",False,client_id)
2023-05-13 22:24:26 +00:00
@socketio.on('generate_msg')
def generate_msg(data):
2023-07-13 14:49:54 +00:00
client_id = request.sid
2023-10-02 00:18:57 +00:00
self.cancel_gen = False
2023-07-13 14:49:54 +00:00
self.connections[client_id]["generated_text"]=""
self.connections[client_id]["cancel_generation"]=False
2023-09-29 17:31:20 +00:00
self.connections[client_id]["continuing"]=False
self.connections[client_id]["first_chunk"]=True
2023-07-15 18:02:03 +00:00
2023-07-20 00:12:54 +00:00
if not self.model:
ASCIIColors.error("Model not selected. Please select a model")
2023-08-02 23:07:29 +00:00
self.notify("Model not selected. Please select a model", False, client_id)
2023-07-20 00:12:54 +00:00
return
2023-07-16 18:22:25 +00:00
2023-08-19 12:06:24 +00:00
if not self.busy:
2023-08-02 23:07:29 +00:00
if self.connections[client_id]["current_discussion"] is None:
2023-05-14 09:10:49 +00:00
if self.db.does_last_discussion_have_messages():
2023-08-02 23:07:29 +00:00
self.connections[client_id]["current_discussion"] = self.db.create_discussion()
2023-05-14 09:10:49 +00:00
else:
2023-08-02 23:07:29 +00:00
self.connections[client_id]["current_discussion"] = self.db.load_last_discussion()
2023-05-13 22:24:26 +00:00
2023-08-02 23:07:29 +00:00
prompt = data["prompt"]
2023-09-15 08:18:49 +00:00
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
2023-08-02 23:07:29 +00:00
message = self.connections[client_id]["current_discussion"].add_message(
message_type = MSG_TYPE.MSG_TYPE_FULL.value,
sender_type = SENDER_TYPES.SENDER_TYPES_USER.value,
sender = ump.replace(self.config.discussion_prompt_separator,"").replace(":",""),
content=prompt,
metadata=None,
parent_message_id=self.message_id
2023-05-14 09:10:49 +00:00
)
2023-04-20 17:30:03 +00:00
2023-08-04 22:28:36 +00:00
ASCIIColors.green("Starting message generation by "+self.personality.name)
2023-08-02 23:07:29 +00:00
self.connections[client_id]['generation_thread'] = threading.Thread(target=self.start_message_generation, args=(message, message.id, client_id))
2023-07-13 14:49:54 +00:00
self.connections[client_id]['generation_thread'].start()
2023-07-06 09:37:12 +00:00
self.socketio.sleep(0.01)
2023-06-23 00:02:37 +00:00
ASCIIColors.info("Started generation task")
2023-08-19 12:06:24 +00:00
self.busy=True
2023-07-13 14:49:54 +00:00
#tpe = threading.Thread(target=self.start_message_generation, args=(message, message_id, client_id))
2023-06-21 22:43:59 +00:00
#tpe.start()
2023-05-14 09:10:49 +00:00
else:
2023-08-19 12:06:24 +00:00
self.notify("I am busy. Come back later.", False, client_id)
2023-05-13 22:24:26 +00:00
@socketio.on('generate_msg_from')
2023-08-02 23:07:29 +00:00
def generate_msg_from(data):
2023-07-13 14:49:54 +00:00
client_id = request.sid
2023-10-02 00:18:57 +00:00
self.cancel_gen = False
2023-09-29 17:31:20 +00:00
self.connections[client_id]["continuing"]=False
self.connections[client_id]["first_chunk"]=True
2023-08-02 23:07:29 +00:00
if self.connections[client_id]["current_discussion"] is None:
2023-08-04 22:28:36 +00:00
ASCIIColors.warning("Please select a discussion")
self.notify("Please select a discussion first", False, client_id)
2023-08-02 23:07:29 +00:00
return
2023-07-15 21:06:28 +00:00
id_ = data['id']
2023-08-04 22:28:36 +00:00
if id_==-1:
message = self.connections[client_id]["current_discussion"].current_message
else:
2023-09-29 17:31:20 +00:00
message = self.connections[client_id]["current_discussion"].load_message(id_)
2023-08-02 23:07:29 +00:00
if message is None:
return
self.connections[client_id]['generation_thread'] = threading.Thread(target=self.start_message_generation, args=(message, message.id, client_id))
2023-07-13 14:49:54 +00:00
self.connections[client_id]['generation_thread'].start()
2023-06-30 22:14:51 +00:00
@socketio.on('continue_generate_msg_from')
def handle_connection(data):
2023-07-13 14:49:54 +00:00
client_id = request.sid
2023-10-02 00:18:57 +00:00
self.cancel_gen = False
2023-09-29 17:31:20 +00:00
self.connections[client_id]["continuing"]=True
self.connections[client_id]["first_chunk"]=True
2023-08-02 23:07:29 +00:00
if self.connections[client_id]["current_discussion"] is None:
2023-08-04 22:28:36 +00:00
ASCIIColors.yellow("Please select a discussion")
2023-08-02 23:07:29 +00:00
self.notify("Please select a discussion", False, client_id)
return
id_ = data['id']
2023-08-04 22:28:36 +00:00
if id_==-1:
message = self.connections[client_id]["current_discussion"].current_message
else:
2023-09-29 17:31:20 +00:00
message = self.connections[client_id]["current_discussion"].load_message(id_)
2023-08-02 23:07:29 +00:00
2023-09-15 08:18:49 +00:00
self.connections[client_id]["generated_text"]=message.content
2023-08-02 23:07:29 +00:00
self.connections[client_id]['generation_thread'] = threading.Thread(target=self.start_message_generation, args=(message, message.id, client_id, True))
2023-07-13 14:49:54 +00:00
self.connections[client_id]['generation_thread'].start()
2023-06-30 22:14:51 +00:00
# generation status
self.generating=False
ASCIIColors.blue(f"Your personal data is stored here :",end="")
ASCIIColors.green(f"{self.lollms_paths.personal_path}")
2023-08-27 00:31:22 +00:00
2023-11-26 01:33:25 +00:00
def clean_string(self, input_string):
# Remove extra spaces by replacing multiple spaces with a single space
#cleaned_string = re.sub(r'\s+', ' ', input_string)
# Remove extra line breaks by replacing multiple consecutive line breaks with a single line break
cleaned_string = re.sub(r'\n\s*\n', '\n', input_string)
# Create a string containing all punctuation characters
punctuation_chars = string.punctuation
# Define a regular expression pattern to match and remove non-alphanumeric characters
#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)}]'
# Use re.sub to replace the matched characters with an empty string
cleaned_string = re.sub(pattern, '', cleaned_string)
return cleaned_string
def make_discussion_title(self, discussion, client_id=None):
"""
Builds a title for a discussion
"""
# Get the list of messages
messages = discussion.get_messages()
discussion_messages = "!@>instruction: Create a short title to this discussion\n"
discussion_title = "\n!@>Discussion title:"
available_space = self.config.ctx_size - 150 - len(self.model.tokenize(discussion_messages))- len(self.model.tokenize(discussion_title))
# Initialize a list to store the full messages
full_message_list = []
# 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
if message.content != '' and (
message.message_type <= MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER.value and message.message_type != MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value):
# Tokenize the message content
message_tokenized = self.model.tokenize(
"\n" + self.config.discussion_prompt_separator + message.sender + ": " + message.content.strip())
# 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
for message_tokens in full_message_list:
discussion_messages += self.model.detokenize(message_tokens)
discussion_messages += discussion_title
title = [""]
def receive(
chunk:str,
message_type:MSG_TYPE
):
if chunk:
title[0] += chunk
antiprompt = self.personality.detect_antiprompt(title[0])
if antiprompt:
ASCIIColors.warning(f"\nDetected hallucination with antiprompt: {antiprompt}")
title[0] = self.remove_text_from_string(title[0],antiprompt)
return False
else:
return True
self._generate(discussion_messages, 150, client_id, receive)
ASCIIColors.info(title[0])
return title[0]
2023-07-17 23:04:48 +00:00
def rebuild_personalities(self, reload_all=False):
2023-07-29 00:41:09 +00:00
if reload_all:
self.mounted_personalities=[]
2023-06-21 22:43:59 +00:00
loaded = self.mounted_personalities
loaded_names = [f"{p.category}/{p.personality_folder_name}:{p.selected_language}" if p.selected_language else f"{p.category}/{p.personality_folder_name}" for p in loaded]
2023-06-21 22:43:59 +00:00
mounted_personalities=[]
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Building mounted Personalities ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
2023-06-23 07:36:52 +00:00
to_remove=[]
2023-06-21 22:43:59 +00:00
for i,personality in enumerate(self.config['personalities']):
2023-07-04 09:26:57 +00:00
if i==self.config["active_personality_id"]:
ASCIIColors.red("*", end="")
ASCIIColors.green(f" {personality}")
else:
ASCIIColors.yellow(f" {personality}")
2023-07-29 00:41:09 +00:00
if personality in loaded_names:
2023-06-21 22:43:59 +00:00
mounted_personalities.append(loaded[loaded_names.index(personality)])
else:
personality_path = f"{personality}" if not ":" in personality else f"{personality.split(':')[0]}"
2023-06-21 22:43:59 +00:00
try:
personality = AIPersonality(personality_path,
self.lollms_paths,
self.config,
2023-06-21 23:40:15 +00:00
model=self.model,
2023-08-29 00:25:50 +00:00
app=self,
selected_language=personality.split(":")[1] if ":" in personality else None,
2023-06-21 23:23:34 +00:00
run_scripts=True)
2023-06-21 22:43:59 +00:00
mounted_personalities.append(personality)
except Exception as ex:
2023-06-21 23:23:34 +00:00
ASCIIColors.error(f"Personality file not found or is corrupted ({personality_path}).\nReturned the following exception:{ex}\nPlease 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.")
2023-06-23 07:36:52 +00:00
ASCIIColors.info("Trying to force reinstall")
2023-06-21 22:43:59 +00:00
if self.config["debug"]:
print(ex)
2023-06-23 06:49:05 +00:00
try:
personality = AIPersonality(
personality_path,
self.lollms_paths,
self.config,
self.model,
2023-08-29 00:25:50 +00:00
app = self,
run_scripts=True,
selected_language=personality.split(":")[1] if ":" in personality else None,
2023-06-23 06:49:05 +00:00
installation_option=InstallOption.FORCE_INSTALL)
2023-06-23 07:36:52 +00:00
mounted_personalities.append(personality)
2023-07-12 15:48:39 +00:00
except Exception as ex:
2023-06-23 06:49:05 +00:00
ASCIIColors.error(f"Couldn't load personality at {personality_path}")
2023-07-12 15:48:39 +00:00
trace_exception(ex)
2023-06-23 07:36:52 +00:00
ASCIIColors.info(f"Unmounting personality")
to_remove.append(i)
personality = AIPersonality(None,
self.lollms_paths,
2023-06-23 06:49:05 +00:00
self.config,
2023-11-12 22:03:35 +00:00
self.model,
app=self,
2023-06-23 06:49:05 +00:00
run_scripts=True,
installation_option=InstallOption.FORCE_INSTALL)
2023-06-23 07:36:52 +00:00
mounted_personalities.append(personality)
2023-06-23 06:49:05 +00:00
ASCIIColors.info("Reverted to default personality")
2023-09-29 17:31:20 +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"]]}')
else:
ASCIIColors.warning('An error was encountered while trying to mount personality')
2023-06-21 22:43:59 +00:00
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Done ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
2023-06-23 07:36:52 +00:00
# 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}")
if self.config["active_personality_id"]>=len(self.config["personalities"]):
self.config["active_personality_id"]=0
2023-06-21 22:43:59 +00:00
return mounted_personalities
2023-10-05 23:23:47 +00:00
def rebuild_extensions(self, reload_all=False):
if reload_all:
self.mounted_extensions=[]
loaded = self.mounted_extensions
loaded_names = [f"{p.category}/{p.extension_folder_name}" for p in loaded]
mounted_extensions=[]
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Building mounted Extensions ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
to_remove=[]
for i,extension in enumerate(self.config['extensions']):
ASCIIColors.yellow(f" {extension}")
if extension in loaded_names:
mounted_extensions.append(loaded[loaded_names.index(extension)])
else:
2023-10-15 23:07:03 +00:00
extension_path = self.lollms_paths.extensions_zoo_path/f"{extension}"
2023-10-05 23:23:47 +00:00
try:
2023-10-15 23:07:03 +00:00
extension = ExtensionBuilder().build_extension(extension_path,self.lollms_paths, self)
2023-10-05 23:23:47 +00:00
mounted_extensions.append(extension)
except Exception as ex:
2023-10-15 23:07:03 +00:00
ASCIIColors.error(f"Personality file not found or is corrupted ({extension_path}).\nReturned the following exception:{ex}\nPlease 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.")
2023-10-05 23:23:47 +00:00
ASCIIColors.info("Trying to force reinstall")
if self.config["debug"]:
print(ex)
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"]]}')
else:
ASCIIColors.warning('An error was encountered while trying to mount personality')
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_extensions):
mounted_extensions.pop(index)
self.config["personalities"].pop(index)
2023-11-08 19:20:24 +00:00
ASCIIColors.info(f"removed personality {extension_path}")
2023-10-05 23:23:47 +00:00
if self.config["active_personality_id"]>=len(self.config["personalities"]):
self.config["active_personality_id"]=0
return mounted_extensions
2023-06-22 21:33:57 +00:00
# ================================== LOLLMSApp
2023-05-09 05:06:01 +00:00
#properties
@property
def message_id(self):
return self._message_id
2023-07-15 21:06:28 +00:00
@message_id.setter
def message_id(self, id):
self._message_id=id
2023-05-09 05:06:01 +00:00
@property
def current_user_message_id(self):
return self._current_user_message_id
@current_user_message_id.setter
def current_user_message_id(self, id):
self._current_user_message_id=id
self._message_id = id
@property
def current_ai_message_id(self):
return self._current_ai_message_id
@current_ai_message_id.setter
def current_ai_message_id(self, id):
self._current_ai_message_id=id
self._message_id = id
2023-05-13 22:24:26 +00:00
def download_file(self, url, installation_path, callback=None):
"""
2023-05-14 11:33:45 +00:00
Downloads a file from a URL, reports the download progress using a callback function, and displays a progress bar.
2023-05-13 22:24:26 +00:00
Args:
url (str): The URL of the file to download.
2023-05-14 11:33:45 +00:00
installation_path (str): The path where the file should be saved.
2023-05-13 22:24:26 +00:00
callback (function, optional): A callback function to be called during the download
with the progress percentage as an argument. Defaults to None.
"""
2023-05-14 09:10:49 +00:00
try:
2023-05-14 11:33:45 +00:00
response = requests.get(url, stream=True)
# Get the file size from the response headers
total_size = int(response.headers.get('content-length', 0))
with open(installation_path, 'wb') as file:
downloaded_size = 0
with tqdm(total=total_size, unit='B', unit_scale=True, ncols=80) as progress_bar:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
downloaded_size += len(chunk)
if callback is not None:
2023-06-23 12:44:25 +00:00
callback(downloaded_size, total_size)
2023-05-14 11:33:45 +00:00
progress_bar.update(len(chunk))
2023-05-09 05:06:01 +00:00
2023-05-14 09:10:49 +00:00
if callback is not None:
callback(total_size, total_size)
2023-05-14 11:33:45 +00:00
print("File downloaded successfully")
except Exception as e:
print("Couldn't download file:", str(e))
2023-07-13 23:42:29 +00:00
def prepare_reception(self, client_id):
2023-09-29 17:31:20 +00:00
if not self.connections[client_id]["continuing"]:
self.connections[client_id]["generated_text"] = ""
self.connections[client_id]["first_chunk"]=True
2023-07-05 00:19:11 +00:00
self.nb_received_tokens = 0
self.start_time = datetime.now()
2023-10-05 18:48:16 +00:00
2023-11-02 20:20:29 +00:00
def prepare_query(self, client_id: str, message_id: int = -1, is_continue: bool = False, n_tokens: int = 0) -> Tuple[str, str, List[str]]:
"""
Prepares the query for the model.
Args:
client_id (str): The client ID.
message_id (int): The message ID. Default is -1.
is_continue (bool): Whether the query is a continuation. Default is False.
n_tokens (int): The number of tokens. Default is 0.
Returns:
Tuple[str, str, List[str]]: The prepared query, original message content, and tokenized query.
"""
# Get the list of messages
2023-08-02 23:07:29 +00:00
messages = self.connections[client_id]["current_discussion"].get_messages()
2023-11-02 20:20:29 +00:00
# Find the index of the message with the specified message_id
message_index = -1
2023-07-15 21:06:28 +00:00
for i, message in enumerate(messages):
2023-11-02 20:20:29 +00:00
if message.id == message_id:
message_index = i
2023-05-16 23:48:35 +00:00
break
2023-08-23 23:34:18 +00:00
2023-11-02 20:20:29 +00:00
# Define current message
current_message = messages[message_index]
# Build the conditionning text block
2023-09-12 23:43:23 +00:00
conditionning = self.personality.personality_conditioning
2023-08-23 02:21:58 +00:00
2023-11-02 20:20:29 +00:00
# Check if there are document files to add to the prompt
documentation = ""
2023-11-11 23:42:58 +00:00
if self.personality.persona_data_vectorizer:
if documentation=="":
documentation="!@>Documentation:\n"
2023-11-12 01:20:53 +00:00
if self.config.data_vectorization_build_keys_words:
2023-11-21 15:47:13 +00:00
query = self.personality.fast_gen("!@>prompt:"+current_message.content+"\n!@>instruction: Convert the prompt to a web search query."+"\nDo not answer the prompt. Do not add explanations. Use comma separated syntax to make a list of keywords in the same line.\nThe keywords should reflect the ideas written in the prompt so that a seach engine can process them efficiently.\n!@>query: ", max_generation_size=256, show_progress=True)
2023-11-12 16:55:42 +00:00
ASCIIColors.cyan(f"Query:{query}")
2023-11-12 01:20:53 +00:00
else:
query = current_message.content
docs, sorted_similarities = self.personality.persona_data_vectorizer.recover_text(query, top_k=self.config.data_vectorization_nb_chunks)
2023-11-11 23:42:58 +00:00
for doc, infos in zip(docs, sorted_similarities):
documentation += f"document chunk:\n{doc}"
2023-11-09 01:00:49 +00:00
if len(self.personality.text_files) > 0 and self.personality.vectorizer:
2023-11-06 11:50:32 +00:00
if documentation=="":
2023-11-10 22:30:45 +00:00
documentation="!@>Documentation:\n"
2023-11-12 01:20:53 +00:00
if self.config.data_vectorization_build_keys_words:
2023-11-21 15:47:13 +00:00
query = self.personality.fast_gen("!@>prompt:"+current_message.content+"\n!@>instruction: Convert the prompt to a web search query."+"\nDo not answer the prompt. Do not add explanations. Use comma separated syntax to make a list of keywords in the same line.\nThe keywords should reflect the ideas written in the prompt so that a seach engine can process them efficiently.\n!@>query: ", max_generation_size=256, show_progress=True)
2023-11-12 16:55:42 +00:00
ASCIIColors.cyan(f"Query:{query}")
2023-11-12 01:20:53 +00:00
else:
query = current_message.content
docs, sorted_similarities = self.personality.vectorizer.recover_text(query, top_k=self.config.data_vectorization_nb_chunks)
2023-11-02 20:20:29 +00:00
for doc, infos in zip(docs, sorted_similarities):
documentation += f"document chunk:\nchunk path: {infos[0]}\nchunk content:{doc}"
2023-10-07 21:16:27 +00:00
2023-11-02 20:20:29 +00:00
# Check if there is discussion history to add to the prompt
history = ""
if self.config.use_discussions_history and self.discussions_store is not None:
2023-11-06 11:50:32 +00:00
if history=="":
2023-11-10 22:30:45 +00:00
documentation="!@>History:\n"
2023-11-02 20:20:29 +00:00
docs, sorted_similarities = self.discussions_store.recover_text(current_message.content, top_k=self.config.data_vectorization_nb_chunks)
for doc, infos in zip(docs, sorted_similarities):
2023-11-21 15:47:13 +00:00
history += f"!@>discussion chunk:\n!@>discussion title: {infos[0]}\nchunk content:{doc}"
# Add information about the user
user_description=""
if self.config.use_user_name_in_discussions:
user_description="!@>User description:\n"+self.config.user_description
# Tokenize the conditionning text and calculate its number of tokens
tokens_conditionning = self.model.tokenize(conditionning)
n_cond_tk = len(tokens_conditionning)
# Tokenize the documentation text and calculate its number of tokens
if len(documentation)>0:
tokens_documentation = self.model.tokenize(documentation)
n_doc_tk = len(tokens_documentation)
else:
tokens_documentation = []
n_doc_tk = 0
# Tokenize the history text and calculate its number of tokens
if len(history)>0:
tokens_history = self.model.tokenize(history)
n_history_tk = len(tokens_history)
else:
tokens_history = []
n_history_tk = 0
# Tokenize user description
if len(user_description)>0:
tokens_user_description = self.model.tokenize(user_description)
n_user_description_tk = len(tokens_user_description)
else:
tokens_user_description = []
n_user_description_tk = 0
# Calculate the total number of tokens between conditionning, documentation, and history
total_tokens = n_cond_tk + n_doc_tk + n_history_tk + n_user_description_tk
# Calculate the available space for the messages
available_space = self.config.ctx_size - n_tokens - total_tokens
# Raise an error if the available space is 0 or less
if available_space<1:
raise Exception("Not enough space in context!!")
# Accumulate messages until the cumulative number of tokens exceeds available_space
tokens_accumulated = 0
# Initialize a list to store the full messages
full_message_list = []
# If this is not a continue request, we add the AI prompt
if not is_continue:
message_tokenized = self.model.tokenize(
"\n" +self.personality.ai_message_prefix.strip()
)
full_message_list.append(message_tokenized)
# Update the cumulative number of tokens
tokens_accumulated += len(message_tokenized)
# Accumulate messages starting from message_index
for i in range(message_index, -1, -1):
message = messages[i]
# Check if the message content is not empty and visible to the AI
if message.content != '' and (
message.message_type <= MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER.value and message.message_type != MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value):
# Tokenize the message content
message_tokenized = self.model.tokenize(
"\n" + self.config.discussion_prompt_separator + message.sender + ": " + message.content.strip())
# 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
discussion_messages = ""
for message_tokens in full_message_list:
discussion_messages += self.model.detokenize(message_tokens)
# Build the final prompt by concatenating the conditionning and discussion messages
prompt_data = conditionning + documentation + history + user_description + discussion_messages
# Tokenize the prompt data
tokens = self.model.tokenize(prompt_data)
# if this is a debug then show prompt construction details
if self.config["debug"]:
ASCIIColors.bold("CONDITIONNING")
ASCIIColors.yellow(conditionning)
ASCIIColors.bold("DOC")
ASCIIColors.yellow(documentation)
ASCIIColors.bold("HISTORY")
ASCIIColors.yellow(history)
ASCIIColors.bold("DISCUSSION")
ASCIIColors.hilight(discussion_messages,"!@>",ASCIIColors.color_yellow,ASCIIColors.color_bright_red,False)
ASCIIColors.bold("Final prompt")
ASCIIColors.hilight(prompt_data,"!@>",ASCIIColors.color_yellow,ASCIIColors.color_bright_red,False)
ASCIIColors.info(f"prompt size:{len(tokens)} tokens")
ASCIIColors.info(f"available space after doc and history:{available_space} tokens")
# Return the prepared query, original message content, and tokenized query
return prompt_data, current_message.content, tokens
def get_discussion_to(self, client_id, message_id=-1):
messages = self.connections[client_id]["current_discussion"].get_messages()
full_message_list = []
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
for message in messages:
if message["id"]<= message_id or message_id==-1:
if message["type"]!=MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER:
if message["sender"]==self.personality.name:
full_message_list.append(self.personality.ai_message_prefix+message["content"])
else:
full_message_list.append(ump + message["content"])
link_text = "\n"# self.personality.link_text
if len(full_message_list) > self.config["nb_messages_to_remember"]:
discussion_messages = self.personality.personality_conditioning+ link_text.join(full_message_list[-self.config["nb_messages_to_remember"]:])
else:
discussion_messages = self.personality.personality_conditioning+ link_text.join(full_message_list)
return discussion_messages # Removes the last return
def notify(self, content, status, client_id=None):
self.socketio.emit('notification', {
'content': content,# self.connections[client_id]["generated_text"],
'status': status
}, room=client_id
)
def new_message(self,
client_id,
sender=None,
content="",
parameters=None,
metadata=None,
ui=None,
message_type:MSG_TYPE=MSG_TYPE.MSG_TYPE_FULL,
sender_type:SENDER_TYPES=SENDER_TYPES.SENDER_TYPES_AI,
open=False
):
mtdt = metadata if metadata is None or type(metadata) == str else json.dumps(metadata, indent=4)
if sender==None:
sender= self.personality.name
msg = self.connections[client_id]["current_discussion"].add_message(
message_type = message_type.value,
sender_type = sender_type.value,
sender = sender,
content = content,
metadata = mtdt,
ui = ui,
rank = 0,
parent_message_id = self.connections[client_id]["current_discussion"].current_message.id,
binding = self.config["binding_name"],
model = self.config["model_name"],
personality = self.config["personalities"][self.config["active_personality_id"]],
) # first the content is empty, but we'll fill it at the end
self.socketio.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": msg.id,
2023-11-22 19:35:48 +00:00
"discussion_id": self.connections[client_id]["current_discussion"].id,
2023-11-21 15:47:13 +00:00
"parent_message_id": msg.parent_message_id,
'binding': self.config["binding_name"],
'model' : self.config["model_name"],
'personality': self.config["personalities"][self.config["active_personality_id"]],
'created_at': self.connections[client_id]["current_discussion"].current_message.created_at,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
'open': open
}, room=client_id
)
def update_message(self, client_id, chunk,
parameters=None,
metadata=[],
ui=None,
msg_type:MSG_TYPE=None
):
self.connections[client_id]["current_discussion"].current_message.finished_generating_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
mtdt = json.dumps(metadata, indent=4) if metadata is not None and type(metadata)== list else metadata
if self.nb_received_tokens==1:
self.socketio.emit('update_message', {
"sender": self.personality.name,
'id':self.connections[client_id]["current_discussion"].current_message.id,
'content': "✍ warming up ...",# self.connections[client_id]["generated_text"],
'ui': ui,
'discussion_id':self.connections[client_id]["current_discussion"].discussion_id,
'message_type': MSG_TYPE.MSG_TYPE_STEP_END.value,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
'parameters':parameters,
'metadata':metadata
}, room=client_id
)
self.socketio.emit('update_message', {
"sender": self.personality.name,
'id':self.connections[client_id]["current_discussion"].current_message.id,
'content': chunk,# self.connections[client_id]["generated_text"],
'ui': ui,
'discussion_id':self.connections[client_id]["current_discussion"].discussion_id,
'message_type': msg_type.value if msg_type is not None else MSG_TYPE.MSG_TYPE_CHUNK.value if self.nb_received_tokens>1 else MSG_TYPE.MSG_TYPE_FULL.value,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
'parameters':parameters,
'metadata':metadata
}, room=client_id
)
self.socketio.sleep(0.01)
if msg_type != MSG_TYPE.MSG_TYPE_INFO:
self.connections[client_id]["current_discussion"].update_message(self.connections[client_id]["generated_text"], new_metadata=mtdt, new_ui=ui)
def close_message(self, client_id):
if not self.connections[client_id]["current_discussion"]:
return
#fix halucination
self.connections[client_id]["generated_text"]=self.connections[client_id]["generated_text"].split("!@>")[0]
# Send final message
self.connections[client_id]["current_discussion"].current_message.finished_generating_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.socketio.emit('close_message', {
"sender": self.personality.name,
"id": self.connections[client_id]["current_discussion"].current_message.id,
"content":self.connections[client_id]["generated_text"],
'binding': self.config["binding_name"],
'model' : self.config["model_name"],
'personality':self.config["personalities"][self.config["active_personality_id"]],
'created_at': self.connections[client_id]["current_discussion"].current_message.created_at,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
}, room=client_id
)
def process_chunk(
self,
chunk:str,
message_type:MSG_TYPE,
parameters:dict=None,
metadata:list=None,
client_id:int=0
):
"""
Processes a chunk of generated text
"""
if chunk is None:
return True
if not client_id in list(self.connections.keys()):
self.notify("Connection lost",False, client_id)
return
if message_type == MSG_TYPE.MSG_TYPE_STEP:
ASCIIColors.info("--> Step:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_STEP_START:
ASCIIColors.info("--> Step started:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_STEP_END:
if parameters['status']:
ASCIIColors.success("--> Step ended:"+chunk)
else:
ASCIIColors.error("--> Step ended:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_EXCEPTION:
self.notify(chunk,False, client_id)
ASCIIColors.error("--> Exception from personality:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_WARNING:
self.notify(chunk,True, client_id)
ASCIIColors.error("--> Exception from personality:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_INFO:
self.notify(chunk,True, client_id)
ASCIIColors.info("--> Info:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_UI:
self.update_message(client_id, "", parameters, metadata, chunk, MSG_TYPE.MSG_TYPE_UI)
if message_type == MSG_TYPE.MSG_TYPE_NEW_MESSAGE:
self.nb_received_tokens = 0
self.start_time = datetime.now()
self.new_message(
client_id,
self.personality.name,
chunk if parameters["type"]!=MSG_TYPE.MSG_TYPE_UI.value else '',
metadata = [{
"title":chunk,
"content":parameters["metadata"]
}
] if parameters["type"]==MSG_TYPE.MSG_TYPE_JSON_INFOS.value else None,
ui= chunk if parameters["type"]==MSG_TYPE.MSG_TYPE_UI.value else None,
message_type= MSG_TYPE(parameters["type"]))
elif message_type == MSG_TYPE.MSG_TYPE_FINISHED_MESSAGE:
self.close_message(client_id)
elif message_type == MSG_TYPE.MSG_TYPE_CHUNK:
if self.nb_received_tokens==0:
self.start_time = datetime.now()
dt =(datetime.now() - self.start_time).seconds
if dt==0:
dt=1
spd = self.nb_received_tokens/dt
ASCIIColors.green(f"Received {self.nb_received_tokens} tokens (speed: {spd:.2f}t/s) ",end="\r",flush=True)
sys.stdout = sys.__stdout__
sys.stdout.flush()
if chunk:
self.connections[client_id]["generated_text"] += chunk
antiprompt = self.personality.detect_antiprompt(self.connections[client_id]["generated_text"])
if antiprompt:
ASCIIColors.warning(f"\nDetected hallucination with antiprompt: {antiprompt}")
self.connections[client_id]["generated_text"] = self.remove_text_from_string(self.connections[client_id]["generated_text"],antiprompt)
self.update_message(client_id, self.connections[client_id]["generated_text"], parameters, metadata, None, MSG_TYPE.MSG_TYPE_FULL)
return False
else:
self.nb_received_tokens += 1
if self.connections[client_id]["continuing"] and self.connections[client_id]["first_chunk"]:
self.update_message(client_id, self.connections[client_id]["generated_text"], parameters, metadata)
else:
self.update_message(client_id, chunk, parameters, metadata, msg_type=MSG_TYPE.MSG_TYPE_CHUNK)
self.connections[client_id]["first_chunk"]=False
# 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
elif message_type == MSG_TYPE.MSG_TYPE_FULL:
self.connections[client_id]["generated_text"] = chunk
self.nb_received_tokens += 1
dt =(datetime.now() - self.start_time).seconds
if dt==0:
dt=1
spd = self.nb_received_tokens/dt
ASCIIColors.green(f"Received {self.nb_received_tokens} tokens (speed: {spd:.2f}t/s) ",end="\r",flush=True)
self.update_message(client_id, chunk, parameters, metadata, ui=None, msg_type=message_type)
return True
# Stream the generated text to the frontend
else:
self.update_message(client_id, chunk, parameters, metadata, ui=None, msg_type=message_type)
return True
def generate(self, full_prompt, prompt, n_predict, client_id, callback=None):
if self.personality.processor is not None:
ASCIIColors.info("Running workflow")
try:
self.personality.processor.run_workflow( prompt, full_prompt, callback)
except Exception as ex:
trace_exception(ex)
# Catch the exception and get the traceback as a list of strings
traceback_lines = traceback.format_exception(type(ex), ex, ex.__traceback__)
# Join the traceback lines into a single string
traceback_text = ''.join(traceback_lines)
ASCIIColors.error(f"Workflow run failed.\nError:{ex}")
ASCIIColors.error(traceback_text)
if callback:
callback(f"Workflow run failed\nError:{ex}", MSG_TYPE.MSG_TYPE_EXCEPTION)
print("Finished executing the workflow")
return
self._generate(full_prompt, n_predict, client_id, callback)
ASCIIColors.success("\nFinished executing the generation")
def _generate(self, prompt, n_predict, client_id, callback=None):
self.nb_received_tokens = 0
self.start_time = datetime.now()
if self.model is not None:
if self.model.binding_type==BindingType.TEXT_IMAGE and len(self.personality.image_files)>0:
ASCIIColors.info(f"warmup for generating up to {n_predict} tokens")
if self.config["override_personality_model_parameters"]:
output = self.model.generate_with_images(
prompt,
self.personality.image_files,
callback=callback,
n_predict=n_predict,
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']
)
else:
output = self.model.generate_with_images(
prompt,
self.personality.image_files,
callback=callback,
n_predict=min(n_predict,self.personality.model_n_predicts),
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,
repeat_last_n = self.personality.model_repeat_last_n,
seed=self.config['seed'],
n_threads=self.config['n_threads']
)
else:
ASCIIColors.info(f"warmup for generating up to {n_predict} tokens")
if self.config["override_personality_model_parameters"]:
output = self.model.generate(
prompt,
callback=callback,
n_predict=n_predict,
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']
)
else:
output = self.model.generate(
prompt,
callback=callback,
n_predict=min(n_predict,self.personality.model_n_predicts),
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,
repeat_last_n = self.personality.model_repeat_last_n,
seed=self.config['seed'],
n_threads=self.config['n_threads']
)
else:
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.")
print("To do this: Install the model to your models/<binding name> folder.")
print("Then set your model information in your local configuration file that you can find in configs/local_config.yaml")
print("You can also use the ui to set your model in the settings page.")
output = ""
return output
def start_message_generation(self, message, message_id, client_id, is_continue=False):
if self.personality is None:
self.notify("Select a personality",False,None)
return
ASCIIColors.info(f"Text generation requested by client: {client_id}")
# send the message to the bot
print(f"Received message : {message.content}")
if self.connections[client_id]["current_discussion"]:
if not self.model:
self.notify("No model selected. Please make sure you select a model before starting generation", False, client_id)
return
# First we need to send the new message ID to the client
if is_continue:
self.connections[client_id]["current_discussion"].load_message(message_id)
self.connections[client_id]["generated_text"] = message.content
else:
self.new_message(client_id, self.personality.name, "")
self.update_message(client_id, "✍ warming up ...", msg_type=MSG_TYPE.MSG_TYPE_STEP_START)
self.socketio.sleep(0.01)
# prepare query and reception
self.discussion_messages, self.current_message, tokens = self.prepare_query(client_id, message_id, is_continue)
self.prepare_reception(client_id)
self.generating = True
self.connections[client_id]["processing"]=True
self.generate(
self.discussion_messages,
self.current_message,
n_predict = self.config.ctx_size-len(tokens)-1,
client_id=client_id,
callback=partial(self.process_chunk,client_id = client_id)
)
print()
print("## Done Generation ##")
print()
self.cancel_gen = False
# Send final message
self.close_message(client_id)
self.socketio.sleep(0.01)
self.connections[client_id]["processing"]=False
if self.connections[client_id]["schedule_for_deletion"]:
del self.connections[client_id]
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Done ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
self.busy=False
else:
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
self.cancel_gen = False
#No discussion available
ASCIIColors.warning("No discussion selected!!!")
self.notify("No discussion selected!!!",False, client_id)
print()
self.busy=False
return ""
######
# Project : lollms-webui
# File : api/__init__.py
# Author : ParisNeo with the help of the community
# license : Apache 2.0
# Description :
# A simple api to communicate with lollms-webui and its models.
######
from flask import request
from datetime import datetime
from api.db import DiscussionsDB, Discussion
from pathlib import Path
from lollms.config import InstallOption
from lollms.types import MSG_TYPE, SENDER_TYPES
from lollms.extension import LOLLMSExtension, ExtensionBuilder
from lollms.personality import AIPersonality, PersonalityBuilder
from lollms.binding import LOLLMSConfig, BindingBuilder, LLMBinding, ModelBuilder, BindingType
from lollms.paths import LollmsPaths
from lollms.helpers import ASCIIColors, trace_exception
from lollms.app import LollmsApplication
from lollms.utilities import File64BitsManager, PromptReshaper
from safe_store import TextVectorizer, VectorizationMethod, VisualizationMethod
import threading
from tqdm import tqdm
import traceback
import sys
import gc
import ctypes
from functools import partial
import json
import shutil
import re
import string
import requests
from datetime import datetime
from typing import List, Tuple
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:
ASCIIColors.yellow("Canceled successfully")
__author__ = "parisneo"
__github__ = "https://github.com/ParisNeo/lollms-webui"
__copyright__ = "Copyright 2023, "
__license__ = "Apache 2.0"
import subprocess
import pkg_resources
# ===========================================================
# Manage automatic install scripts
def is_package_installed(package_name):
try:
dist = pkg_resources.get_distribution(package_name)
return True
except pkg_resources.DistributionNotFound:
return False
def install_package(package_name):
try:
# Check if the package is already installed
__import__(package_name)
print(f"{package_name} is already installed.")
except ImportError:
print(f"{package_name} is not installed. Installing...")
# Install the package using pip
subprocess.check_call(["pip", "install", package_name])
print(f"{package_name} has been successfully installed.")
def parse_requirements_file(requirements_path):
with open(requirements_path, 'r') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'):
# Skip empty and commented lines
continue
package_name, _, version_specifier = line.partition('==')
package_name, _, version_specifier = line.partition('>=')
if is_package_installed(package_name):
# The package is already installed
print(f"{package_name} is already installed.")
else:
# The package is not installed, install it
if version_specifier:
install_package(f"{package_name}{version_specifier}")
else:
install_package(package_name)
# ===========================================================
2023-11-26 01:33:25 +00:00
class LoLLMsAPI(LollmsApplication):
2023-11-21 15:47:13 +00:00
def __init__(self, config:LOLLMSConfig, socketio, config_file_path:str, lollms_paths: LollmsPaths) -> None:
super().__init__("Lollms_webui",config, lollms_paths, callback=self.process_chunk, notification_callback=self.notify)
self.busy = False
self.nb_received_tokens = 0
self.socketio = socketio
self.config_file_path = config_file_path
self.cancel_gen = False
# Keeping track of current discussion and message
self._current_user_message_id = 0
self._current_ai_message_id = 0
self._message_id = 0
self.db_path = config["db_path"]
if Path(self.db_path).is_absolute():
# Create database object
self.db = DiscussionsDB(self.db_path)
else:
# Create database object
self.db = DiscussionsDB(self.lollms_paths.personal_databases_path/self.db_path)
# If the database is empty, populate it with tables
ASCIIColors.info("Checking discussions database... ",end="")
self.db.create_tables()
self.db.add_missing_columns()
ASCIIColors.success("ok")
# prepare vectorization
if self.config.data_vectorization_activate and self.config.use_discussions_history:
try:
ASCIIColors.yellow("Loading vectorized discussions")
folder = self.lollms_paths.personal_databases_path/"vectorized_dbs"
folder.mkdir(parents=True, exist_ok=True)
self.discussions_store = TextVectorizer(
vectorization_method=VectorizationMethod.TFIDF_VECTORIZER,#=VectorizationMethod.BM25_VECTORIZER,
database_path=folder/self.config.db_path,
data_visualization_method=VisualizationMethod.PCA,#VisualizationMethod.PCA,
save_db=True
)
ASCIIColors.yellow("1- Exporting discussions")
discussions = self.db.export_all_as_markdown_list_for_vectorization()
ASCIIColors.yellow("2- Adding discussions to vectorizer")
for (title,discussion) in discussions:
if discussion!='' and title!='None':
self.discussions_store.add_document(title, discussion, chunk_size=self.config.data_vectorization_chunk_size, overlap_size=self.config.data_vectorization_overlap_size, force_vectorize=False, add_as_a_bloc=False)
ASCIIColors.yellow("3- Indexing database")
self.discussions_store.index()
ASCIIColors.yellow("4- Saving database")
self.discussions_store.save_to_json()
ASCIIColors.yellow("Ready")
except Exception as ex:
trace_exception(ex)
self.discussions_store = None
else:
self.discussions_store = None
# This is used to keep track of messages
self.download_infos={}
self.connections = {0:{
"current_discussion":None,
"generated_text":"",
"cancel_generation": False,
"generation_thread": None,
"processing":False,
"schedule_for_deletion":False
}
}
# =========================================================================================
# Socket IO stuff
# =========================================================================================
@socketio.on('connect')
def connect():
#Create a new connection information
self.connections[request.sid] = {
"current_discussion":self.db.load_last_discussion(),
"generated_text":"",
"continuing": False,
"first_chunk": True,
"cancel_generation": False,
"generation_thread": None,
"processing":False,
"schedule_for_deletion":False
}
self.socketio.emit('connected', room=request.sid)
ASCIIColors.success(f'Client {request.sid} connected')
@socketio.on('disconnect')
def disconnect():
try:
self.socketio.emit('disconnected', room=request.sid)
if self.connections[request.sid]["processing"]:
self.connections[request.sid]["schedule_for_deletion"]=True
else:
del self.connections[request.sid]
except Exception as ex:
pass
ASCIIColors.error(f'Client {request.sid} disconnected')
@socketio.on('cancel_install')
def cancel_install(data):
try:
model_name = data["model_name"]
binding_folder = data["binding_folder"]
model_url = data["model_url"]
signature = f"{model_name}_{binding_folder}_{model_url}"
self.download_infos[signature]["cancel"]=True
self.socketio.emit('canceled', {
'status': True
},
room=request.sid
)
except Exception as ex:
trace_exception(ex)
self.socketio.emit('canceled', {
'status': False,
'error':str(ex)
},
room=request.sid
)
@socketio.on('install_model')
def install_model(data):
room_id = request.sid
def install_model_():
print("Install model triggered")
model_path = data["path"].replace("\\","/")
2023-11-25 22:32:32 +00:00
if data["type"].lower() in model_path.lower():
model_type:str=data["type"]
else:
mtt = None
for mt in self.binding.models_dir_names:
if mt.lower() in model_path.lower():
mtt = mt
break
if mtt:
model_type = mtt
else:
model_type:str=self.binding.models_dir_names[0]
2023-11-21 15:47:13 +00:00
progress = 0
installation_dir = self.binding.searchModelParentFolder(model_path.split('/')[-1], model_type)
if model_type=="gptq" or model_type=="awq":
parts = model_path.split("/")
if len(parts)==2:
filename = parts[1]
else:
filename = parts[4]
installation_path = installation_dir / filename
else:
filename = Path(model_path).name
installation_path = installation_dir / filename
print("Model install requested")
print(f"Model path : {model_path}")
model_name = filename
binding_folder = self.config["binding_name"]
model_url = model_path
signature = f"{model_name}_{binding_folder}_{model_url}"
try:
self.download_infos[signature]={
"start_time":datetime.now(),
"total_size":self.binding.get_file_size(model_path),
"downloaded_size":0,
"progress":0,
"speed":0,
"cancel":False
}
if installation_path.exists():
print("Error: Model already exists. please remove it first")
socketio.emit('install_progress',{
'status': False,
'error': f'model already exists. Please remove it first.\nThe model can be found here:{installation_path}',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
return
socketio.emit('install_progress',{
'status': True,
'progress': progress,
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
def callback(downloaded_size, total_size):
progress = (downloaded_size / total_size) * 100
now = datetime.now()
dt = (now - self.download_infos[signature]['start_time']).total_seconds()
speed = downloaded_size/dt
self.download_infos[signature]['downloaded_size'] = downloaded_size
self.download_infos[signature]['speed'] = speed
if progress - self.download_infos[signature]['progress']>2:
self.download_infos[signature]['progress'] = progress
socketio.emit('install_progress',{
'status': True,
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
if self.download_infos[signature]["cancel"]:
raise Exception("canceled")
if hasattr(self.binding, "download_model"):
try:
self.binding.download_model(model_path, installation_path, callback)
except Exception as ex:
ASCIIColors.warning(str(ex))
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': 'canceled',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
del self.download_infos[signature]
try:
if installation_path.is_dir():
shutil.rmtree(installation_path)
else:
installation_path.unlink()
except Exception as ex:
trace_exception(ex)
ASCIIColors.error(f"Couldn't delete file. Please try to remove it manually.\n{installation_path}")
return
else:
try:
self.download_file(model_path, installation_path, callback)
except Exception as ex:
ASCIIColors.warning(str(ex))
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': 'canceled',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': self.download_infos[signature]['progress'],
'speed': self.download_infos[signature]['speed'],
}, room=room_id
)
del self.download_infos[signature]
installation_path.unlink()
return
socketio.emit('install_progress',{
'status': True,
'error': '',
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': self.download_infos[signature]['start_time'].strftime("%Y-%m-%d %H:%M:%S"),
'total_size': self.download_infos[signature]['total_size'],
'downloaded_size': self.download_infos[signature]['downloaded_size'],
'progress': 100,
'speed': self.download_infos[signature]['speed'],
}, room=room_id)
del self.download_infos[signature]
except Exception as ex:
trace_exception(ex)
socketio.emit('install_progress',{
'status': False,
'error': str(ex),
'model_name' : model_name,
'binding_folder' : binding_folder,
'model_url' : model_url,
'start_time': '',
'total_size': 0,
'downloaded_size': 0,
'progress': 0,
'speed': 0,
}, room=room_id
)
tpe = threading.Thread(target=install_model_, args=())
tpe.start()
@socketio.on('uninstall_model')
def uninstall_model(data):
model_path = data['path']
model_type:str=data.get("type","ggml")
installation_dir = self.binding.searchModelParentFolder(model_path)
binding_folder = self.config["binding_name"]
if model_type=="gptq" or model_type=="awq":
filename = model_path.split("/")[4]
installation_path = installation_dir / filename
else:
filename = Path(model_path).name
installation_path = installation_dir / filename
model_name = filename
if not installation_path.exists():
socketio.emit('uninstall_progress',{
'status': False,
'error': 'The model does not exist',
'model_name' : model_name,
'binding_folder' : binding_folder
}, room=request.sid)
try:
if not installation_path.exists():
# Try to find a version
model_path = installation_path.name.lower().replace("-ggml","").replace("-gguf","")
candidates = [m for m in installation_dir.iterdir() if model_path in m.name]
if len(candidates)>0:
model_path = candidates[0]
installation_path = model_path
if installation_path.is_dir():
shutil.rmtree(installation_path)
else:
installation_path.unlink()
socketio.emit('uninstall_progress',{
'status': True,
'error': '',
'model_name' : model_name,
'binding_folder' : binding_folder
}, room=request.sid)
except Exception as ex:
trace_exception(ex)
ASCIIColors.error(f"Couldn't delete {installation_path}, please delete it manually and restart the app")
socketio.emit('uninstall_progress',{
'status': False,
'error': f"Couldn't delete {installation_path}, please delete it manually and restart the app",
'model_name' : model_name,
'binding_folder' : binding_folder
}, room=request.sid)
@socketio.on('new_discussion')
def new_discussion(data):
client_id = request.sid
title = data["title"]
self.connections[client_id]["current_discussion"] = self.db.create_discussion(title)
# Get the current timestamp
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Return a success response
if self.connections[client_id]["current_discussion"] is None:
self.connections[client_id]["current_discussion"] = self.db.load_last_discussion()
if self.personality.welcome_message!="":
message = self.connections[client_id]["current_discussion"].add_message(
message_type = MSG_TYPE.MSG_TYPE_FULL.value if self.personality.include_welcome_message_in_disucssion else MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value,
sender_type = SENDER_TYPES.SENDER_TYPES_AI.value,
sender = self.personality.name,
content = self.personality.welcome_message,
metadata = None,
rank = 0,
parent_message_id = -1,
binding = self.config.binding_name,
model = self.config.model_name,
personality = self.config.personalities[self.config.active_personality_id],
created_at=None,
finished_generating_at=None
)
self.socketio.emit('discussion_created',
{'id':self.connections[client_id]["current_discussion"].discussion_id},
room=client_id
)
else:
self.socketio.emit('discussion_created',
{'id':0},
room=client_id
)
@socketio.on('load_discussion')
def load_discussion(data):
client_id = request.sid
ASCIIColors.yellow(f"Loading discussion for client {client_id} ... ", end="")
if "id" in data:
discussion_id = data["id"]
self.connections[client_id]["current_discussion"] = Discussion(discussion_id, self.db)
else:
if self.connections[client_id]["current_discussion"] is not None:
discussion_id = self.connections[client_id]["current_discussion"].discussion_id
self.connections[client_id]["current_discussion"] = Discussion(discussion_id, self.db)
else:
self.connections[client_id]["current_discussion"] = self.db.create_discussion()
messages = self.connections[client_id]["current_discussion"].get_messages()
jsons = [m.to_json() for m in messages]
self.socketio.emit('discussion',
jsons,
room=client_id
)
ASCIIColors.green(f"ok")
@socketio.on('upload_file')
def upload_file(data):
ASCIIColors.yellow("Uploading file")
file = data['file']
filename = file.filename
save_path = self.lollms_paths.personal_uploads_path/filename # Specify the desired folder path
try:
if not self.personality.processor is None:
file.save(save_path)
self.personality.processor.add_file(save_path, partial(self.process_chunk, client_id = request.sid))
# File saved successfully
socketio.emit('progress', {'status':True, 'progress': 100})
else:
file.save(save_path)
self.personality.add_file(save_path, partial(self.process_chunk, client_id = request.sid))
# File saved successfully
socketio.emit('progress', {'status':True, 'progress': 100})
except Exception as e:
# Error occurred while saving the file
socketio.emit('progress', {'status':False, 'error': str(e)})
@socketio.on('cancel_generation')
def cancel_generation():
client_id = request.sid
self.cancel_gen = True
#kill thread
ASCIIColors.error(f'Client {request.sid} requested cancelling generation')
terminate_thread(self.connections[client_id]['generation_thread'])
ASCIIColors.error(f'Client {request.sid} canceled generation')
self.busy=False
@socketio.on('get_personality_files')
def get_personality_files(data):
client_id = request.sid
self.connections[client_id]["generated_text"] = ""
self.connections[client_id]["cancel_generation"] = False
try:
self.personality.setCallback(partial(self.process_chunk,client_id = client_id))
except Exception as ex:
trace_exception(ex)
@socketio.on('send_file_chunk')
def send_file_chunk(data):
client_id = request.sid
filename = data['filename']
chunk = data['chunk']
offset = data['offset']
is_last_chunk = data['isLastChunk']
chunk_index = data['chunkIndex']
path:Path = self.lollms_paths.personal_uploads_path / self.personality.personality_folder_name
path.mkdir(parents=True, exist_ok=True)
file_path = path / data["filename"]
# Save the chunk to the server or process it as needed
# For example:
if chunk_index==0:
with open(file_path, 'wb') as file:
file.write(chunk)
else:
with open(file_path, 'ab') as file:
file.write(chunk)
if is_last_chunk:
print('File received and saved successfully')
if self.personality.processor:
result = self.personality.processor.add_file(file_path, partial(self.process_chunk, client_id=client_id))
else:
result = self.personality.add_file(file_path, partial(self.process_chunk, client_id=client_id))
self.socketio.emit('file_received', {'status': True, 'filename': filename})
else:
# Request the next chunk from the client
self.socketio.emit('request_next_chunk', {'offset': offset + len(chunk)})
@self.socketio.on('cancel_text_generation')
def cancel_text_generation(data):
client_id = request.sid
self.connections[client_id]["requested_stop"]=True
print(f"Client {client_id} requested canceling generation")
self.socketio.emit("generation_canceled", {"message":"Generation is canceled."}, room=client_id)
self.socketio.sleep(0)
self.busy = False
@self.socketio.on('execute_python_code')
def execute_python_code(data):
"""Executes Python code and returns the output."""
client_id = request.sid
code = data["code"]
# Import the necessary modules.
import io
import sys
import time
# Create a Python interpreter.
interpreter = io.StringIO()
sys.stdout = interpreter
# Execute the code.
start_time = time.time()
exec(code)
end_time = time.time()
# Get the output.
output = interpreter.getvalue()
self.socketio.emit("execution_output", {"output":output,"execution_time":end_time - start_time}, room=client_id)
@self.socketio.on('create_empty_message')
def create_empty_message(data):
client_id = request.sid
type = data.get("type",0)
if type==0:
ASCIIColors.info(f"Building empty User message requested by : {client_id}")
# send the message to the bot
print(f"Creating an empty message for AI answer orientation")
if self.connections[client_id]["current_discussion"]:
if not self.model:
self.notify("No model selected. Please make sure you select a model before starting generation", False, client_id)
return
self.new_message(client_id, self.config.user_name, "", sender_type=SENDER_TYPES.SENDER_TYPES_USER, open=True)
self.socketio.sleep(0.01)
else:
if self.personality is None:
self.notify("Select a personality",False,None)
return
ASCIIColors.info(f"Building empty AI message requested by : {client_id}")
# send the message to the bot
print(f"Creating an empty message for AI answer orientation")
if self.connections[client_id]["current_discussion"]:
if not self.model:
self.notify("No model selected. Please make sure you select a model before starting generation", False, client_id)
return
self.new_message(client_id, self.personality.name, "[edit this to put your ai answer start]", open=True)
self.socketio.sleep(0.01)
# A copy of the original lollms-server generation code needed for playground
@self.socketio.on('generate_text')
def handle_generate_text(data):
client_id = request.sid
self.cancel_gen = False
ASCIIColors.info(f"Text generation requested by client: {client_id}")
if self.busy:
self.socketio.emit("busy", {"message":"I am busy. Come back later."}, room=client_id)
self.socketio.sleep(0)
ASCIIColors.warning(f"OOps request {client_id} refused!! Server busy")
return
def generate_text():
self.busy = True
try:
model = self.model
self.connections[client_id]["is_generating"]=True
self.connections[client_id]["requested_stop"]=False
prompt = data['prompt']
tokenized = model.tokenize(prompt)
personality_id = data.get('personality', -1)
n_crop = data.get('n_crop', len(tokenized))
if n_crop!=-1:
prompt = model.detokenize(tokenized[-n_crop:])
n_predicts = data["n_predicts"]
parameters = data.get("parameters",{
"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"]
})
if personality_id==-1:
# Raw text generation
self.answer = {"full_text":""}
def callback(text, message_type: MSG_TYPE, metadata:dict={}):
if message_type == MSG_TYPE.MSG_TYPE_CHUNK:
ASCIIColors.success(f"generated:{len(self.answer['full_text'].split())} words", end='\r')
self.answer["full_text"] = self.answer["full_text"] + text
self.socketio.emit('text_chunk', {'chunk': text, 'type':MSG_TYPE.MSG_TYPE_CHUNK.value}, room=client_id)
self.socketio.sleep(0)
if client_id in self.connections:# Client disconnected
if self.connections[client_id]["requested_stop"]:
return False
else:
return True
else:
return False
tk = model.tokenize(prompt)
n_tokens = len(tk)
fd = model.detokenize(tk[-min(self.config.ctx_size-n_predicts,n_tokens):])
try:
ASCIIColors.print("warming up", ASCIIColors.color_bright_cyan)
generated_text = model.generate(fd,
n_predict=n_predicts,
callback=callback,
temperature = parameters["temperature"],
top_k = parameters["top_k"],
top_p = parameters["top_p"],
repeat_penalty = parameters["repeat_penalty"],
repeat_last_n = parameters["repeat_last_n"],
seed = parameters["seed"],
)
ASCIIColors.success(f"\ndone")
if client_id in self.connections:
if not self.connections[client_id]["requested_stop"]:
# Emit the generated text to the client
self.socketio.emit('text_generated', {'text': generated_text}, room=client_id)
self.socketio.sleep(0)
except Exception as ex:
self.socketio.emit('generation_error', {'error': str(ex)}, room=client_id)
ASCIIColors.error(f"\ndone")
self.busy = False
else:
try:
personality: AIPersonality = self.personalities[personality_id]
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
personality.model = model
cond_tk = personality.model.tokenize(personality.personality_conditioning)
n_cond_tk = len(cond_tk)
# Placeholder code for text generation
# Replace this with your actual text generation logic
print(f"Text generation requested by client: {client_id}")
self.answer["full_text"] = ''
full_discussion_blocks = self.connections[client_id]["full_discussion_blocks"]
if prompt != '':
if personality.processor is not None and personality.processor_cfg["process_model_input"]:
preprocessed_prompt = personality.processor.process_model_input(prompt)
else:
preprocessed_prompt = prompt
if personality.processor is not None and personality.processor_cfg["custom_workflow"]:
full_discussion_blocks.append(ump)
full_discussion_blocks.append(preprocessed_prompt)
else:
full_discussion_blocks.append(ump)
full_discussion_blocks.append(preprocessed_prompt)
full_discussion_blocks.append(personality.link_text)
full_discussion_blocks.append(personality.ai_message_prefix)
full_discussion = personality.personality_conditioning + ''.join(full_discussion_blocks)
def callback(text, message_type: MSG_TYPE, metadata:dict={}):
if message_type == MSG_TYPE.MSG_TYPE_CHUNK:
self.answer["full_text"] = self.answer["full_text"] + text
self.socketio.emit('text_chunk', {'chunk': text}, room=client_id)
self.socketio.sleep(0)
try:
if self.connections[client_id]["requested_stop"]:
return False
else:
return True
except: # If the client is disconnected then we stop talking to it
return False
tk = personality.model.tokenize(full_discussion)
n_tokens = len(tk)
fd = personality.model.detokenize(tk[-min(self.config.ctx_size-n_cond_tk-personality.model_n_predicts,n_tokens):])
if personality.processor is not None and personality.processor_cfg["custom_workflow"]:
ASCIIColors.info("processing...")
generated_text = personality.processor.run_workflow(prompt, previous_discussion_text=personality.personality_conditioning+fd, callback=callback)
else:
ASCIIColors.info("generating...")
generated_text = personality.model.generate(
personality.personality_conditioning+fd,
n_predict=personality.model_n_predicts,
callback=callback)
if personality.processor is not None and personality.processor_cfg["process_model_output"]:
generated_text = personality.processor.process_model_output(generated_text)
full_discussion_blocks.append(generated_text.strip())
ASCIIColors.success("\ndone")
# Emit the generated text to the client
self.socketio.emit('text_generated', {'text': generated_text}, room=client_id)
self.socketio.sleep(0)
except Exception as ex:
self.socketio.emit('generation_error', {'error': str(ex)}, room=client_id)
ASCIIColors.error(f"\ndone")
self.busy = False
except Exception as ex:
trace_exception(ex)
self.socketio.emit('generation_error', {'error': str(ex)}, room=client_id)
self.busy = False
# Start the text generation task in a separate thread
task = self.socketio.start_background_task(target=generate_text)
@socketio.on('execute_command')
def execute_command(data):
client_id = request.sid
command = data["command"]
parameters = data["parameters"]
if self.personality.processor is not None:
self.start_time = datetime.now()
self.personality.processor.callback = partial(self.process_chunk, client_id=client_id)
self.personality.processor.execute_command(command, parameters)
2023-11-24 23:03:43 +00:00
self.close_message(client_id)
2023-11-21 15:47:13 +00:00
else:
self.notify("Non scripted personalities do not support commands",False,client_id)
@socketio.on('generate_msg')
def generate_msg(data):
client_id = request.sid
self.cancel_gen = False
self.connections[client_id]["generated_text"]=""
self.connections[client_id]["cancel_generation"]=False
self.connections[client_id]["continuing"]=False
self.connections[client_id]["first_chunk"]=True
if not self.model:
ASCIIColors.error("Model not selected. Please select a model")
self.notify("Model not selected. Please select a model", False, client_id)
return
if not self.busy:
if self.connections[client_id]["current_discussion"] is None:
if self.db.does_last_discussion_have_messages():
self.connections[client_id]["current_discussion"] = self.db.create_discussion()
else:
self.connections[client_id]["current_discussion"] = self.db.load_last_discussion()
prompt = data["prompt"]
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
message = self.connections[client_id]["current_discussion"].add_message(
message_type = MSG_TYPE.MSG_TYPE_FULL.value,
sender_type = SENDER_TYPES.SENDER_TYPES_USER.value,
sender = ump.replace(self.config.discussion_prompt_separator,"").replace(":",""),
content=prompt,
metadata=None,
parent_message_id=self.message_id
)
ASCIIColors.green("Starting message generation by "+self.personality.name)
self.connections[client_id]['generation_thread'] = threading.Thread(target=self.start_message_generation, args=(message, message.id, client_id))
self.connections[client_id]['generation_thread'].start()
self.socketio.sleep(0.01)
ASCIIColors.info("Started generation task")
self.busy=True
#tpe = threading.Thread(target=self.start_message_generation, args=(message, message_id, client_id))
#tpe.start()
else:
self.notify("I am busy. Come back later.", False, client_id)
@socketio.on('generate_msg_from')
def generate_msg_from(data):
client_id = request.sid
self.cancel_gen = False
self.connections[client_id]["continuing"]=False
self.connections[client_id]["first_chunk"]=True
if self.connections[client_id]["current_discussion"] is None:
ASCIIColors.warning("Please select a discussion")
self.notify("Please select a discussion first", False, client_id)
return
id_ = data['id']
2023-11-24 23:03:43 +00:00
generation_type = data.get('msg_type',None)
2023-11-21 15:47:13 +00:00
if id_==-1:
message = self.connections[client_id]["current_discussion"].current_message
else:
message = self.connections[client_id]["current_discussion"].load_message(id_)
if message is None:
return
2023-11-24 23:03:43 +00:00
self.connections[client_id]['generation_thread'] = threading.Thread(target=self.start_message_generation, args=(message, message.id, client_id, False, generation_type))
2023-11-21 15:47:13 +00:00
self.connections[client_id]['generation_thread'].start()
@socketio.on('continue_generate_msg_from')
def handle_connection(data):
client_id = request.sid
self.cancel_gen = False
self.connections[client_id]["continuing"]=True
self.connections[client_id]["first_chunk"]=True
if self.connections[client_id]["current_discussion"] is None:
ASCIIColors.yellow("Please select a discussion")
self.notify("Please select a discussion", False, client_id)
return
id_ = data['id']
if id_==-1:
message = self.connections[client_id]["current_discussion"].current_message
else:
message = self.connections[client_id]["current_discussion"].load_message(id_)
self.connections[client_id]["generated_text"]=message.content
self.connections[client_id]['generation_thread'] = threading.Thread(target=self.start_message_generation, args=(message, message.id, client_id, True))
self.connections[client_id]['generation_thread'].start()
# generation status
self.generating=False
ASCIIColors.blue(f"Your personal data is stored here :",end="")
ASCIIColors.green(f"{self.lollms_paths.personal_path}")
def rebuild_personalities(self, reload_all=False):
if reload_all:
self.mounted_personalities=[]
loaded = self.mounted_personalities
loaded_names = [f"{p.category}/{p.personality_folder_name}:{p.selected_language}" if p.selected_language else f"{p.category}/{p.personality_folder_name}" for p in loaded]
mounted_personalities=[]
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Building mounted Personalities ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
to_remove=[]
for i,personality in enumerate(self.config['personalities']):
if i==self.config["active_personality_id"]:
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:
personality_path = f"{personality}" if not ":" in personality else f"{personality.split(':')[0]}"
try:
personality = AIPersonality(personality_path,
self.lollms_paths,
self.config,
model=self.model,
app=self,
selected_language=personality.split(":")[1] if ":" in personality else None,
run_scripts=True)
mounted_personalities.append(personality)
except Exception as ex:
ASCIIColors.error(f"Personality file not found or is corrupted ({personality_path}).\nReturned the following exception:{ex}\nPlease 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.")
ASCIIColors.info("Trying to force reinstall")
if self.config["debug"]:
print(ex)
try:
personality = AIPersonality(
personality_path,
self.lollms_paths,
self.config,
self.model,
app = self,
run_scripts=True,
selected_language=personality.split(":")[1] if ":" in personality else None,
installation_option=InstallOption.FORCE_INSTALL)
mounted_personalities.append(personality)
except Exception as ex:
ASCIIColors.error(f"Couldn't load personality at {personality_path}")
trace_exception(ex)
ASCIIColors.info(f"Unmounting personality")
to_remove.append(i)
personality = AIPersonality(None,
self.lollms_paths,
self.config,
self.model,
app=self,
run_scripts=True,
installation_option=InstallOption.FORCE_INSTALL)
mounted_personalities.append(personality)
ASCIIColors.info("Reverted to default personality")
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"]]}')
else:
ASCIIColors.warning('An error was encountered while trying to mount personality')
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}")
if self.config["active_personality_id"]>=len(self.config["personalities"]):
self.config["active_personality_id"]=0
return mounted_personalities
def rebuild_extensions(self, reload_all=False):
if reload_all:
self.mounted_extensions=[]
loaded = self.mounted_extensions
loaded_names = [f"{p.category}/{p.extension_folder_name}" for p in loaded]
mounted_extensions=[]
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Building mounted Extensions ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
to_remove=[]
for i,extension in enumerate(self.config['extensions']):
ASCIIColors.yellow(f" {extension}")
if extension in loaded_names:
mounted_extensions.append(loaded[loaded_names.index(extension)])
else:
extension_path = self.lollms_paths.extensions_zoo_path/f"{extension}"
try:
extension = ExtensionBuilder().build_extension(extension_path,self.lollms_paths, self)
mounted_extensions.append(extension)
except Exception as ex:
2023-11-22 19:35:48 +00:00
ASCIIColors.error(f"Extension file not found or is corrupted ({extension_path}).\nReturned the following exception:{ex}\nPlease 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.")
trace_exception(ex)
2023-11-21 15:47:13 +00:00
ASCIIColors.info("Trying to force reinstall")
if self.config["debug"]:
print(ex)
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_extensions):
mounted_extensions.pop(index)
2023-11-22 19:35:48 +00:00
self.config["extensions"].pop(index)
2023-11-21 15:47:13 +00:00
ASCIIColors.info(f"removed personality {extension_path}")
return mounted_extensions
# ================================== LOLLMSApp
#properties
@property
def message_id(self):
return self._message_id
@message_id.setter
def message_id(self, id):
self._message_id=id
@property
def current_user_message_id(self):
return self._current_user_message_id
@current_user_message_id.setter
def current_user_message_id(self, id):
self._current_user_message_id=id
self._message_id = id
@property
def current_ai_message_id(self):
return self._current_ai_message_id
@current_ai_message_id.setter
def current_ai_message_id(self, id):
self._current_ai_message_id=id
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
total_size = int(response.headers.get('content-length', 0))
with open(installation_path, 'wb') as file:
downloaded_size = 0
with tqdm(total=total_size, unit='B', unit_scale=True, ncols=80) as progress_bar:
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
#cleaned_string = re.sub(r'\s+', ' ', input_string)
# Remove extra line breaks by replacing multiple consecutive line breaks with a single line break
cleaned_string = re.sub(r'\n\s*\n', '\n', input_string)
# Create a string containing all punctuation characters
punctuation_chars = string.punctuation
# Define a regular expression pattern to match and remove non-alphanumeric characters
#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)}]'
# Use re.sub to replace the matched characters with an empty string
cleaned_string = re.sub(pattern, '', cleaned_string)
return cleaned_string
def make_discussion_title(self, discussion, client_id=None):
"""
Builds a title for a discussion
"""
# Get the list of messages
messages = discussion.get_messages()
discussion_messages = "!@>instruction: Create a short title to this discussion\n"
discussion_title = "\n!@>Discussion title:"
available_space = self.config.ctx_size - 150 - len(self.model.tokenize(discussion_messages))- len(self.model.tokenize(discussion_title))
# Initialize a list to store the full messages
full_message_list = []
# 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
if message.content != '' and (
message.message_type <= MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER.value and message.message_type != MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value):
# Tokenize the message content
message_tokenized = self.model.tokenize(
"\n" + self.config.discussion_prompt_separator + message.sender + ": " + message.content.strip())
# 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
for message_tokens in full_message_list:
discussion_messages += self.model.detokenize(message_tokens)
discussion_messages += discussion_title
title = [""]
def receive(
chunk:str,
message_type:MSG_TYPE
):
if chunk:
title[0] += chunk
antiprompt = self.personality.detect_antiprompt(title[0])
if antiprompt:
ASCIIColors.warning(f"\nDetected hallucination with antiprompt: {antiprompt}")
title[0] = self.remove_text_from_string(title[0],antiprompt)
return False
else:
return True
self._generate(discussion_messages, 150, client_id, receive)
ASCIIColors.info(title[0])
return title[0]
2023-11-26 01:33:25 +00:00
2023-11-21 15:47:13 +00:00
2023-11-26 01:33:25 +00:00
def prepare_reception(self, client_id):
if not self.connections[client_id]["continuing"]:
self.connections[client_id]["generated_text"] = ""
self.connections[client_id]["first_chunk"]=True
self.nb_received_tokens = 0
self.start_time = datetime.now()
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
def prepare_query(self, client_id: str, message_id: int = -1, is_continue: bool = False, n_tokens: int = 0, generation_type = None) -> Tuple[str, str, List[str]]:
2023-11-21 15:47:13 +00:00
"""
Prepares the query for the model.
Args:
client_id (str): The client ID.
message_id (int): The message ID. Default is -1.
is_continue (bool): Whether the query is a continuation. Default is False.
n_tokens (int): The number of tokens. Default is 0.
Returns:
Tuple[str, str, List[str]]: The prepared query, original message content, and tokenized query.
"""
# Get the list of messages
messages = self.connections[client_id]["current_discussion"].get_messages()
# Find the index of the message with the specified message_id
message_index = -1
for i, message in enumerate(messages):
if message.id == message_id:
message_index = i
break
# Define current message
current_message = messages[message_index]
# Build the conditionning text block
conditionning = self.personality.personality_conditioning
# Check if there are document files to add to the prompt
documentation = ""
2023-11-24 23:03:43 +00:00
history = ""
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
if generation_type != "simple_question":
if self.personality.persona_data_vectorizer:
if documentation=="":
documentation="!@>Documentation:\n"
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
if self.config.data_vectorization_build_keys_words:
query = self.personality.fast_gen("!@>prompt:"+current_message.content+"\n!@>instruction: Convert the prompt to a web search query."+"\nDo not answer the prompt. Do not add explanations. Use comma separated syntax to make a list of keywords in the same line.\nThe keywords should reflect the ideas written in the prompt so that a seach engine can process them efficiently.\n!@>query: ", max_generation_size=256, show_progress=True)
ASCIIColors.cyan(f"Query:{query}")
else:
query = current_message.content
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
docs, sorted_similarities = self.personality.persona_data_vectorizer.recover_text(query, top_k=self.config.data_vectorization_nb_chunks)
for doc, infos in zip(docs, sorted_similarities):
documentation += f"document chunk:\n{doc}"
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
if len(self.personality.text_files) > 0 and self.personality.vectorizer:
if documentation=="":
documentation="!@>Documentation:\n"
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
if self.config.data_vectorization_build_keys_words:
query = self.personality.fast_gen("!@>prompt:"+current_message.content+"\n!@>instruction: Convert the prompt to a web search query."+"\nDo not answer the prompt. Do not add explanations. Use comma separated syntax to make a list of keywords in the same line.\nThe keywords should reflect the ideas written in the prompt so that a seach engine can process them efficiently.\n!@>query: ", max_generation_size=256, show_progress=True)
ASCIIColors.cyan(f"Query:{query}")
else:
query = current_message.content
2023-11-21 15:47:13 +00:00
2023-11-24 23:03:43 +00:00
docs, sorted_similarities = self.personality.vectorizer.recover_text(query, top_k=self.config.data_vectorization_nb_chunks)
for doc, infos in zip(docs, sorted_similarities):
documentation += f"document chunk:\nchunk path: {infos[0]}\nchunk content:{doc}"
# Check if there is discussion history to add to the prompt
if self.config.use_discussions_history and self.discussions_store is not None:
if history=="":
documentation="!@>History:\n"
docs, sorted_similarities = self.discussions_store.recover_text(current_message.content, top_k=self.config.data_vectorization_nb_chunks)
for doc, infos in zip(docs, sorted_similarities):
history += f"!@>discussion chunk:\n!@>discussion title: {infos[0]}\nchunk content:{doc}"
2023-10-07 21:16:27 +00:00
2023-11-10 22:30:45 +00:00
# Add information about the user
user_description=""
if self.config.use_user_name_in_discussions:
user_description="!@>User description:\n"+self.config.user_description
2023-11-02 20:20:29 +00:00
# Tokenize the conditionning text and calculate its number of tokens
tokens_conditionning = self.model.tokenize(conditionning)
n_cond_tk = len(tokens_conditionning)
2023-10-07 21:16:27 +00:00
2023-11-02 20:20:29 +00:00
# Tokenize the documentation text and calculate its number of tokens
if len(documentation)>0:
tokens_documentation = self.model.tokenize(documentation)
n_doc_tk = len(tokens_documentation)
2023-08-23 02:21:58 +00:00
else:
2023-11-02 20:20:29 +00:00
tokens_documentation = []
n_doc_tk = 0
# Tokenize the history text and calculate its number of tokens
if len(history)>0:
tokens_history = self.model.tokenize(history)
n_history_tk = len(tokens_history)
else:
tokens_history = []
n_history_tk = 0
2023-11-10 22:30:45 +00:00
# Tokenize user description
if len(user_description)>0:
tokens_user_description = self.model.tokenize(user_description)
n_user_description_tk = len(tokens_user_description)
else:
tokens_user_description = []
n_user_description_tk = 0
2023-11-02 20:20:29 +00:00
# Calculate the total number of tokens between conditionning, documentation, and history
2023-11-10 22:30:45 +00:00
total_tokens = n_cond_tk + n_doc_tk + n_history_tk + n_user_description_tk
2023-11-02 20:20:29 +00:00
# Calculate the available space for the messages
available_space = self.config.ctx_size - n_tokens - total_tokens
# Raise an error if the available space is 0 or less
if available_space<1:
raise Exception("Not enough space in context!!")
# Accumulate messages until the cumulative number of tokens exceeds available_space
tokens_accumulated = 0
# Initialize a list to store the full messages
full_message_list = []
# If this is not a continue request, we add the AI prompt
if not is_continue:
message_tokenized = self.model.tokenize(
"\n" +self.personality.ai_message_prefix.strip()
)
full_message_list.append(message_tokenized)
# Update the cumulative number of tokens
tokens_accumulated += len(message_tokenized)
2023-11-24 23:03:43 +00:00
if generation_type != "simple_question":
# Accumulate messages starting from message_index
for i in range(message_index, -1, -1):
message = messages[i]
# Check if the message content is not empty and visible to the AI
if message.content != '' and (
message.message_type <= MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER.value and message.message_type != MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value):
# Tokenize the message content
message_tokenized = self.model.tokenize(
"\n" + self.config.discussion_prompt_separator + message.sender + ": " + message.content.strip())
# 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)
else:
message = messages[message_index]
2023-11-02 20:20:29 +00:00
# Check if the message content is not empty and visible to the AI
if message.content != '' and (
message.message_type <= MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER.value and message.message_type != MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_AI.value):
# Tokenize the message content
message_tokenized = self.model.tokenize(
"\n" + self.config.discussion_prompt_separator + message.sender + ": " + message.content.strip())
# 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
discussion_messages = ""
for message_tokens in full_message_list:
discussion_messages += self.model.detokenize(message_tokens)
# Build the final prompt by concatenating the conditionning and discussion messages
2023-11-10 22:30:45 +00:00
prompt_data = conditionning + documentation + history + user_description + discussion_messages
2023-11-02 20:20:29 +00:00
# Tokenize the prompt data
tokens = self.model.tokenize(prompt_data)
# if this is a debug then show prompt construction details
2023-08-23 23:34:18 +00:00
if self.config["debug"]:
2023-11-02 20:20:29 +00:00
ASCIIColors.bold("CONDITIONNING")
ASCIIColors.yellow(conditionning)
ASCIIColors.bold("DOC")
ASCIIColors.yellow(documentation)
ASCIIColors.bold("HISTORY")
ASCIIColors.yellow(history)
ASCIIColors.bold("DISCUSSION")
2023-11-09 01:00:49 +00:00
ASCIIColors.hilight(discussion_messages,"!@>",ASCIIColors.color_yellow,ASCIIColors.color_bright_red,False)
2023-11-02 20:20:29 +00:00
ASCIIColors.bold("Final prompt")
2023-11-09 01:00:49 +00:00
ASCIIColors.hilight(prompt_data,"!@>",ASCIIColors.color_yellow,ASCIIColors.color_bright_red,False)
2023-11-02 20:20:29 +00:00
ASCIIColors.info(f"prompt size:{len(tokens)} tokens")
ASCIIColors.info(f"available space after doc and history:{available_space} tokens")
# Return the prepared query, original message content, and tokenized query
return prompt_data, current_message.content, tokens
2023-08-02 23:07:29 +00:00
def get_discussion_to(self, client_id, message_id=-1):
messages = self.connections[client_id]["current_discussion"].get_messages()
full_message_list = []
2023-09-15 08:18:49 +00:00
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
2023-07-16 16:57:30 +00:00
2023-04-17 22:23:31 +00:00
for message in messages:
if message["id"]<= message_id or message_id==-1:
2023-07-06 18:56:21 +00:00
if message["type"]!=MSG_TYPE.MSG_TYPE_FULL_INVISIBLE_TO_USER:
2023-04-30 20:40:19 +00:00
if message["sender"]==self.personality.name:
2023-08-02 23:07:29 +00:00
full_message_list.append(self.personality.ai_message_prefix+message["content"])
2023-04-17 22:23:31 +00:00
else:
2023-08-02 23:07:29 +00:00
full_message_list.append(ump + message["content"])
2023-04-17 22:23:31 +00:00
2023-07-27 23:16:26 +00:00
link_text = "\n"# self.personality.link_text
2023-04-17 22:23:31 +00:00
2023-08-02 23:07:29 +00:00
if len(full_message_list) > self.config["nb_messages_to_remember"]:
discussion_messages = self.personality.personality_conditioning+ link_text.join(full_message_list[-self.config["nb_messages_to_remember"]:])
2023-04-17 22:23:31 +00:00
else:
2023-08-02 23:07:29 +00:00
discussion_messages = self.personality.personality_conditioning+ link_text.join(full_message_list)
2023-04-17 22:23:31 +00:00
return discussion_messages # Removes the last return
2023-06-21 22:43:59 +00:00
2023-11-24 23:03:43 +00:00
def notify(self, content, status=True, client_id=None):
2023-08-02 23:07:29 +00:00
self.socketio.emit('notification', {
'content': content,# self.connections[client_id]["generated_text"],
'status': status
}, room=client_id
)
def new_message(self,
client_id,
2023-10-30 00:32:24 +00:00
sender=None,
content="",
2023-08-17 23:29:53 +00:00
parameters=None,
2023-08-30 00:35:26 +00:00
metadata=None,
ui=None,
2023-08-02 23:07:29 +00:00
message_type:MSG_TYPE=MSG_TYPE.MSG_TYPE_FULL,
2023-11-19 00:21:29 +00:00
sender_type:SENDER_TYPES=SENDER_TYPES.SENDER_TYPES_AI,
open=False
2023-08-02 23:07:29 +00:00
):
2023-09-04 00:05:46 +00:00
mtdt = metadata if metadata is None or type(metadata) == str else json.dumps(metadata, indent=4)
2023-10-30 00:32:24 +00:00
if sender==None:
sender= self.personality.name
2023-08-02 23:07:29 +00:00
msg = self.connections[client_id]["current_discussion"].add_message(
message_type = message_type.value,
sender_type = sender_type.value,
sender = sender,
content = content,
2023-09-04 00:05:46 +00:00
metadata = mtdt,
ui = ui,
2023-08-02 23:07:29 +00:00
rank = 0,
parent_message_id = self.connections[client_id]["current_discussion"].current_message.id,
binding = self.config["binding_name"],
model = self.config["model_name"],
personality = self.config["personalities"][self.config["active_personality_id"]],
) # first the content is empty, but we'll fill it at the end
self.socketio.emit('new_message',
{
2023-10-30 00:32:24 +00:00
"sender": sender,
2023-08-02 23:07:29 +00:00
"message_type": message_type.value,
"sender_type": SENDER_TYPES.SENDER_TYPES_AI.value,
"content": content,
2023-08-17 23:29:53 +00:00
"parameters": parameters,
2023-09-04 00:05:46 +00:00
"metadata": metadata,
2023-08-30 00:35:26 +00:00
"ui": ui,
2023-08-02 23:07:29 +00:00
"id": msg.id,
"parent_message_id": msg.parent_message_id,
'binding': self.config["binding_name"],
'model' : self.config["model_name"],
'personality': self.config["personalities"][self.config["active_personality_id"]],
'created_at': self.connections[client_id]["current_discussion"].current_message.created_at,
2023-11-19 00:21:29 +00:00
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
'open': open
2023-08-02 23:07:29 +00:00
}, room=client_id
)
2023-08-17 23:29:53 +00:00
def update_message(self, client_id, chunk,
parameters=None,
metadata=[],
2023-08-30 00:35:26 +00:00
ui=None,
2023-08-17 23:29:53 +00:00
msg_type:MSG_TYPE=None
):
2023-08-02 23:07:29 +00:00
self.connections[client_id]["current_discussion"].current_message.finished_generating_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
2023-08-17 23:29:53 +00:00
mtdt = json.dumps(metadata, indent=4) if metadata is not None and type(metadata)== list else metadata
2023-10-23 23:40:39 +00:00
if self.nb_received_tokens==1:
2023-10-22 14:24:09 +00:00
self.socketio.emit('update_message', {
"sender": self.personality.name,
'id':self.connections[client_id]["current_discussion"].current_message.id,
'content': "✍ warming up ...",# self.connections[client_id]["generated_text"],
'ui': ui,
'discussion_id':self.connections[client_id]["current_discussion"].discussion_id,
'message_type': MSG_TYPE.MSG_TYPE_STEP_END.value,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
'parameters':parameters,
'metadata':metadata
}, room=client_id
)
2023-08-02 23:07:29 +00:00
self.socketio.emit('update_message', {
"sender": self.personality.name,
'id':self.connections[client_id]["current_discussion"].current_message.id,
2023-08-30 00:35:26 +00:00
'content': chunk,# self.connections[client_id]["generated_text"],
'ui': ui,
2023-08-02 23:07:29 +00:00
'discussion_id':self.connections[client_id]["current_discussion"].discussion_id,
'message_type': msg_type.value if msg_type is not None else MSG_TYPE.MSG_TYPE_CHUNK.value if self.nb_received_tokens>1 else MSG_TYPE.MSG_TYPE_FULL.value,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
2023-08-17 23:29:53 +00:00
'parameters':parameters,
2023-09-04 00:05:46 +00:00
'metadata':metadata
2023-08-02 23:07:29 +00:00
}, room=client_id
)
self.socketio.sleep(0.01)
2023-11-15 23:27:08 +00:00
if msg_type != MSG_TYPE.MSG_TYPE_INFO:
self.connections[client_id]["current_discussion"].update_message(self.connections[client_id]["generated_text"], new_metadata=mtdt, new_ui=ui)
2023-08-02 23:07:29 +00:00
def close_message(self, client_id):
2023-09-13 21:17:45 +00:00
if not self.connections[client_id]["current_discussion"]:
return
2023-09-07 07:03:56 +00:00
#fix halucination
self.connections[client_id]["generated_text"]=self.connections[client_id]["generated_text"].split("!@>")[0]
2023-08-02 23:07:29 +00:00
# Send final message
self.connections[client_id]["current_discussion"].current_message.finished_generating_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.socketio.emit('close_message', {
"sender": self.personality.name,
"id": self.connections[client_id]["current_discussion"].current_message.id,
"content":self.connections[client_id]["generated_text"],
'binding': self.config["binding_name"],
'model' : self.config["model_name"],
'personality':self.config["personalities"][self.config["active_personality_id"]],
'created_at': self.connections[client_id]["current_discussion"].current_message.created_at,
'finished_generating_at': self.connections[client_id]["current_discussion"].current_message.finished_generating_at,
}, room=client_id
)
2023-08-17 23:29:53 +00:00
def process_chunk(
self,
2023-09-04 00:05:46 +00:00
chunk:str,
2023-08-17 23:29:53 +00:00
message_type:MSG_TYPE,
2023-09-04 00:05:46 +00:00
parameters:dict=None,
2023-08-30 00:35:26 +00:00
metadata:list=None,
2023-08-17 23:29:53 +00:00
client_id:int=0
):
2023-05-28 23:25:25 +00:00
"""
2023-08-16 01:47:54 +00:00
Processes a chunk of generated text
2023-05-28 23:25:25 +00:00
"""
2023-11-15 21:12:12 +00:00
if chunk is None:
return True
2023-11-04 01:50:56 +00:00
if not client_id in list(self.connections.keys()):
self.notify("Connection lost",False, client_id)
return
2023-06-30 20:09:47 +00:00
if message_type == MSG_TYPE.MSG_TYPE_STEP:
ASCIIColors.info("--> Step:"+chunk)
2023-06-27 06:57:32 +00:00
if message_type == MSG_TYPE.MSG_TYPE_STEP_START:
ASCIIColors.info("--> Step started:"+chunk)
if message_type == MSG_TYPE.MSG_TYPE_STEP_END:
2023-08-17 23:29:53 +00:00
if parameters['status']:
2023-07-29 00:41:09 +00:00
ASCIIColors.success("--> Step ended:"+chunk)
else:
ASCIIColors.error("--> Step ended:"+chunk)
2023-06-27 06:57:32 +00:00
if message_type == MSG_TYPE.MSG_TYPE_EXCEPTION:
2023-08-02 23:07:29 +00:00
self.notify(chunk,False, client_id)
2023-06-27 06:57:32 +00:00
ASCIIColors.error("--> Exception from personality:"+chunk)
2023-08-03 09:48:13 +00:00
if message_type == MSG_TYPE.MSG_TYPE_WARNING:
self.notify(chunk,True, client_id)
ASCIIColors.error("--> Exception from personality:"+chunk)
2023-08-03 15:16:17 +00:00
if message_type == MSG_TYPE.MSG_TYPE_INFO:
self.notify(chunk,True, client_id)
ASCIIColors.info("--> Info:"+chunk)
2023-08-30 00:35:26 +00:00
if message_type == MSG_TYPE.MSG_TYPE_UI:
2023-09-05 22:57:23 +00:00
self.update_message(client_id, "", parameters, metadata, chunk, MSG_TYPE.MSG_TYPE_UI)
2023-07-31 01:14:05 +00:00
2023-07-29 00:41:09 +00:00
if message_type == MSG_TYPE.MSG_TYPE_NEW_MESSAGE:
2023-08-02 23:07:29 +00:00
self.nb_received_tokens = 0
self.start_time = datetime.now()
2023-08-17 23:29:53 +00:00
self.new_message(
client_id,
self.personality.name,
2023-09-04 00:05:46 +00:00
chunk if parameters["type"]!=MSG_TYPE.MSG_TYPE_UI.value else '',
metadata = [{
"title":chunk,
"content":parameters["metadata"]
}
] if parameters["type"]==MSG_TYPE.MSG_TYPE_JSON_INFOS.value else None,
ui= chunk if parameters["type"]==MSG_TYPE.MSG_TYPE_UI.value else None,
2023-08-17 23:29:53 +00:00
message_type= MSG_TYPE(parameters["type"]))
2023-08-02 23:07:29 +00:00
2023-07-31 01:14:05 +00:00
elif message_type == MSG_TYPE.MSG_TYPE_FINISHED_MESSAGE:
2023-08-02 23:07:29 +00:00
self.close_message(client_id)
2023-07-29 00:41:09 +00:00
elif message_type == MSG_TYPE.MSG_TYPE_CHUNK:
2023-11-09 01:00:49 +00:00
if self.nb_received_tokens==0:
2023-11-08 19:14:22 +00:00
self.start_time = datetime.now()
dt =(datetime.now() - self.start_time).seconds
if dt==0:
dt=1
spd = self.nb_received_tokens/dt
ASCIIColors.green(f"Received {self.nb_received_tokens} tokens (speed: {spd:.2f}t/s) ",end="\r",flush=True)
2023-07-07 22:15:34 +00:00
sys.stdout = sys.__stdout__
sys.stdout.flush()
2023-11-09 01:00:49 +00:00
if chunk:
self.connections[client_id]["generated_text"] += chunk
2023-09-08 20:39:18 +00:00
antiprompt = self.personality.detect_antiprompt(self.connections[client_id]["generated_text"])
2023-07-21 15:01:08 +00:00
if antiprompt:
2023-07-23 23:25:05 +00:00
ASCIIColors.warning(f"\nDetected hallucination with antiprompt: {antiprompt}")
2023-07-21 15:01:08 +00:00
self.connections[client_id]["generated_text"] = self.remove_text_from_string(self.connections[client_id]["generated_text"],antiprompt)
2023-08-30 12:04:59 +00:00
self.update_message(client_id, self.connections[client_id]["generated_text"], parameters, metadata, None, MSG_TYPE.MSG_TYPE_FULL)
2023-06-21 22:43:59 +00:00
return False
2023-07-21 15:01:08 +00:00
else:
2023-09-07 07:03:56 +00:00
self.nb_received_tokens += 1
2023-09-29 17:31:20 +00:00
if self.connections[client_id]["continuing"] and self.connections[client_id]["first_chunk"]:
self.update_message(client_id, self.connections[client_id]["generated_text"], parameters, metadata)
else:
2023-10-22 14:24:09 +00:00
self.update_message(client_id, chunk, parameters, metadata, msg_type=MSG_TYPE.MSG_TYPE_CHUNK)
2023-09-29 17:31:20 +00:00
self.connections[client_id]["first_chunk"]=False
2023-07-21 15:01:08 +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
2023-06-21 22:43:59 +00:00
# Stream the generated text to the main process
elif message_type == MSG_TYPE.MSG_TYPE_FULL:
2023-07-13 23:42:29 +00:00
self.connections[client_id]["generated_text"] = chunk
2023-07-05 00:19:11 +00:00
self.nb_received_tokens += 1
dt =(datetime.now() - self.start_time).seconds
if dt==0:
dt=1
spd = self.nb_received_tokens/dt
ASCIIColors.green(f"Received {self.nb_received_tokens} tokens (speed: {spd:.2f}t/s) ",end="\r",flush=True)
2023-08-30 12:04:59 +00:00
self.update_message(client_id, chunk, parameters, metadata, ui=None, msg_type=message_type)
2023-06-21 22:43:59 +00:00
return True
2023-06-27 06:57:32 +00:00
# Stream the generated text to the frontend
2023-06-21 22:43:59 +00:00
else:
2023-08-30 00:35:26 +00:00
self.update_message(client_id, chunk, parameters, metadata, ui=None, msg_type=message_type)
2023-06-21 22:43:59 +00:00
return True
2023-07-13 23:42:29 +00:00
def generate(self, full_prompt, prompt, n_predict, client_id, callback=None):
2023-06-21 22:43:59 +00:00
if self.personality.processor is not None:
2023-08-23 21:29:42 +00:00
ASCIIColors.info("Running workflow")
2023-07-05 15:51:42 +00:00
try:
2023-07-15 18:02:03 +00:00
self.personality.processor.run_workflow( prompt, full_prompt, callback)
2023-07-05 15:51:42 +00:00
except Exception as ex:
2023-08-30 00:35:26 +00:00
trace_exception(ex)
2023-07-05 15:51:42 +00:00
# Catch the exception and get the traceback as a list of strings
traceback_lines = traceback.format_exception(type(ex), ex, ex.__traceback__)
# Join the traceback lines into a single string
traceback_text = ''.join(traceback_lines)
ASCIIColors.error(f"Workflow run failed.\nError:{ex}")
ASCIIColors.error(traceback_text)
2023-07-13 14:49:54 +00:00
if callback:
callback(f"Workflow run failed\nError:{ex}", MSG_TYPE.MSG_TYPE_EXCEPTION)
2023-07-05 15:51:42 +00:00
print("Finished executing the workflow")
return
2023-06-21 22:43:59 +00:00
2023-08-22 01:43:17 +00:00
2023-07-13 23:42:29 +00:00
self._generate(full_prompt, n_predict, client_id, callback)
2023-07-11 22:35:59 +00:00
ASCIIColors.success("\nFinished executing the generation")
2023-05-13 22:24:26 +00:00
2023-07-13 23:42:29 +00:00
def _generate(self, prompt, n_predict, client_id, callback=None):
2023-07-05 00:19:11 +00:00
self.nb_received_tokens = 0
self.start_time = datetime.now()
2023-06-21 22:43:59 +00:00
if self.model is not None:
if self.model.binding_type==BindingType.TEXT_IMAGE and len(self.personality.image_files)>0:
ASCIIColors.info(f"warmup for generating up to {n_predict} tokens")
if self.config["override_personality_model_parameters"]:
output = self.model.generate_with_images(
prompt,
self.personality.image_files,
callback=callback,
n_predict=n_predict,
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']
)
else:
output = self.model.generate_with_images(
prompt,
self.personality.image_files,
callback=callback,
n_predict=min(n_predict,self.personality.model_n_predicts),
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,
repeat_last_n = self.personality.model_repeat_last_n,
seed=self.config['seed'],
n_threads=self.config['n_threads']
)
2023-06-21 22:43:59 +00:00
else:
ASCIIColors.info(f"warmup for generating up to {n_predict} tokens")
if self.config["override_personality_model_parameters"]:
output = self.model.generate(
prompt,
callback=callback,
n_predict=n_predict,
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']
)
else:
output = self.model.generate(
prompt,
callback=callback,
n_predict=min(n_predict,self.personality.model_n_predicts),
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,
repeat_last_n = self.personality.model_repeat_last_n,
seed=self.config['seed'],
n_threads=self.config['n_threads']
)
2023-06-21 22:43:59 +00:00
else:
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.")
print("To do this: Install the model to your models/<binding name> folder.")
print("Then set your model information in your local configuration file that you can find in configs/local_config.yaml")
print("You can also use the ui to set your model in the settings page.")
output = ""
return output
2023-11-24 23:03:43 +00:00
def start_message_generation(self, message, message_id, client_id, is_continue=False, generation_type=None):
2023-10-10 21:42:01 +00:00
if self.personality is None:
self.notify("Select a personality",False,None)
return
2023-07-13 14:49:54 +00:00
ASCIIColors.info(f"Text generation requested by client: {client_id}")
2023-05-13 22:24:26 +00:00
# send the message to the bot
2023-08-02 23:07:29 +00:00
print(f"Received message : {message.content}")
if self.connections[client_id]["current_discussion"]:
2023-07-29 00:41:09 +00:00
if not self.model:
2023-08-02 23:07:29 +00:00
self.notify("No model selected. Please make sure you select a model before starting generation", False, client_id)
2023-07-29 00:41:09 +00:00
return
2023-05-13 22:24:26 +00:00
# First we need to send the new message ID to the client
2023-06-30 22:14:51 +00:00
if is_continue:
2023-08-02 23:07:29 +00:00
self.connections[client_id]["current_discussion"].load_message(message_id)
2023-08-04 22:28:36 +00:00
self.connections[client_id]["generated_text"] = message.content
2023-06-30 22:14:51 +00:00
else:
2023-10-22 14:24:09 +00:00
self.new_message(client_id, self.personality.name, "")
self.update_message(client_id, "✍ warming up ...", msg_type=MSG_TYPE.MSG_TYPE_STEP_START)
2023-07-06 09:37:12 +00:00
self.socketio.sleep(0.01)
2023-05-13 22:24:26 +00:00
# prepare query and reception
2023-11-24 23:03:43 +00:00
self.discussion_messages, self.current_message, tokens = self.prepare_query(client_id, message_id, is_continue, generation_type=generation_type)
2023-07-13 23:42:29 +00:00
self.prepare_reception(client_id)
2023-05-28 18:36:00 +00:00
self.generating = True
2023-07-26 22:03:28 +00:00
self.connections[client_id]["processing"]=True
2023-07-13 14:49:54 +00:00
self.generate(
self.discussion_messages,
self.current_message,
2023-07-13 23:42:29 +00:00
n_predict = self.config.ctx_size-len(tokens)-1,
client_id=client_id,
2023-07-13 14:49:54 +00:00
callback=partial(self.process_chunk,client_id = client_id)
)
2023-05-13 22:24:26 +00:00
print()
2023-05-28 23:25:25 +00:00
print("## Done Generation ##")
2023-05-13 22:24:26 +00:00
print()
2023-06-18 07:06:52 +00:00
self.cancel_gen = False
2023-05-13 22:24:26 +00:00
# Send final message
2023-08-02 23:07:29 +00:00
self.close_message(client_id)
2023-07-06 09:37:12 +00:00
self.socketio.sleep(0.01)
2023-07-26 22:03:28 +00:00
self.connections[client_id]["processing"]=False
if self.connections[client_id]["schedule_for_deletion"]:
del self.connections[client_id]
2023-05-13 22:24:26 +00:00
2023-06-30 22:14:51 +00:00
ASCIIColors.success(f" ╔══════════════════════════════════════════════════╗ ")
ASCIIColors.success(f" ║ Done ║ ")
ASCIIColors.success(f" ╚══════════════════════════════════════════════════╝ ")
2023-11-26 01:33:25 +00:00
if self.config.auto_title:
d = self.connections[client_id]["current_discussion"]
ttl = d.title()
if ttl is None or ttl=="" or ttl=="untitled":
title = self.make_discussion_title(d, client_id=client_id)
d.rename(title)
self.socketio.emit('disucssion_renamed',{
'status': True,
2023-11-26 01:51:51 +00:00
'discussion_id':d.discussion_id,
2023-11-26 01:33:25 +00:00
'title':title
2023-11-26 01:51:51 +00:00
}, room=client_id)
2023-11-26 01:33:25 +00:00
2023-08-19 12:06:24 +00:00
self.busy=False
2023-08-16 01:47:54 +00:00
2023-05-05 12:23:07 +00:00
else:
2023-09-15 08:18:49 +00:00
ump = self.config.discussion_prompt_separator +self.config.user_name.strip() if self.config.use_user_name_in_discussions else self.personality.user_message_prefix
2023-07-19 15:41:23 +00:00
2023-06-30 22:14:51 +00:00
self.cancel_gen = False
2023-05-13 22:24:26 +00:00
#No discussion available
2023-06-30 22:14:51 +00:00
ASCIIColors.warning("No discussion selected!!!")
2023-08-02 23:07:29 +00:00
self.notify("No discussion selected!!!",False, client_id)
2023-05-13 22:24:26 +00:00
print()
2023-08-19 12:06:24 +00:00
self.busy=False
2023-05-13 22:24:26 +00:00
return ""
2023-11-06 08:14:21 +00:00