The core is updated

This commit is contained in:
Saifeddine ALOUI 2024-01-07 22:34:09 +01:00
parent 310eafa0ab
commit fb402a5d3d
3 changed files with 114 additions and 104 deletions

View File

@ -7,8 +7,9 @@ description:
application. These routes are specific to configurations
"""
from fastapi import APIRouter, Request
from fastapi import APIRouter, Request, HTTPException
from pydantic import BaseModel
from json import JSONDecodeError
import pkg_resources
from lollms.server.elf_server import LOLLMSElfServer
from lollms.binding import BindingBuilder, InstallOption
@ -17,6 +18,7 @@ from lollms.utilities import load_config, trace_exception, gc
from pathlib import Path
from typing import List
import json
from typing import List, Any
class SettingsInfos(BaseModel):
setting_name:str
@ -28,108 +30,113 @@ lollmsElfServer = LOLLMSElfServer.get_instance()
# ----------------------------------- Settings -----------------------------------------
@router.post("/update_setting")
def update_setting(data:SettingsInfos):
setting_name = data.setting_name
async def update_setting(request: Request):
"""
Endpoint to apply configuration settings.
ASCIIColors.info(f"Requested updating of setting {data.setting_name} to {data.setting_value}")
if setting_name== "temperature":
lollmsElfServer.config["temperature"]=float(data.setting_value)
elif setting_name== "n_predict":
lollmsElfServer.config["n_predict"]=int(data.setting_value)
elif setting_name== "top_k":
lollmsElfServer.config["top_k"]=int(data.setting_value)
elif setting_name== "top_p":
lollmsElfServer.config["top_p"]=float(data.setting_value)
elif setting_name== "repeat_penalty":
lollmsElfServer.config["repeat_penalty"]=float(data.setting_value)
elif setting_name== "repeat_last_n":
lollmsElfServer.config["repeat_last_n"]=int(data.setting_value)
:param request: The HTTP request object.
:return: A JSON response with the status of the operation.
"""
elif setting_name== "n_threads":
lollmsElfServer.config["n_threads"]=int(data.setting_value)
elif setting_name== "ctx_size":
lollmsElfServer.config["ctx_size"]=int(data.setting_value)
try:
config_data = (await request.json())
if "config" in config_data.keys():
config_data = config_data["config"]
setting_name = config_data["setting_name"]
setting_value = config_data["setting_value"]
elif setting_name== "personality_folder":
lollmsElfServer.personality_name=data.setting_value
if len(lollmsElfServer.config["personalities"])>0:
if lollmsElfServer.config["active_personality_id"]<len(lollmsElfServer.config["personalities"]):
lollmsElfServer.config["personalities"][lollmsElfServer.config["active_personality_id"]] = f"{lollmsElfServer.personality_category}/{lollmsElfServer.personality_name}"
ASCIIColors.info(f"Requested updating of setting {setting_name} to {setting_value}")
if setting_name== "binding_name":
if lollmsElfServer.config['binding_name']!= setting_value:
print(f"New binding selected : {setting_value}")
lollmsElfServer.config["binding_name"]=setting_value
try:
if lollmsElfServer.binding:
lollmsElfServer.binding.destroy_model()
lollmsElfServer.binding = None
lollmsElfServer.model = None
for per in lollmsElfServer.mounted_personalities:
if per is not None:
per.model = None
gc.collect()
lollmsElfServer.binding = BindingBuilder().build_binding(lollmsElfServer.config, lollmsElfServer.lollms_paths, InstallOption.INSTALL_IF_NECESSARY, lollmsCom=lollmsElfServer)
lollmsElfServer.model = None
lollmsElfServer.config.save_config()
ASCIIColors.green("Binding loaded successfully")
except Exception as ex:
ASCIIColors.error(f"Couldn't build binding: [{ex}]")
trace_exception(ex)
return {"status":False, 'error':str(ex)}
else:
lollmsElfServer.config["active_personality_id"] = 0
lollmsElfServer.config["personalities"][lollmsElfServer.config["active_personality_id"]] = f"{lollmsElfServer.personality_category}/{lollmsElfServer.personality_name}"
if lollmsElfServer.personality_category!="custom_personalities":
personality_fn = lollmsElfServer.lollms_paths.personalities_zoo_path/lollmsElfServer.config["personalities"][lollmsElfServer.config["active_personality_id"]]
else:
personality_fn = lollmsElfServer.lollms_paths.personal_personalities_path/lollmsElfServer.config["personalities"][lollmsElfServer.config["active_personality_id"]].split("/")[-1]
lollmsElfServer.personality.load_personality(personality_fn)
else:
lollmsElfServer.config["personalities"].append(f"{lollmsElfServer.personality_category}/{lollmsElfServer.personality_name}")
elif setting_name== "override_personality_model_parameters":
lollmsElfServer.config["override_personality_model_parameters"]=bool(data.setting_value)
if lollmsElfServer.config["debug"]:
print(f"Configuration {setting_name} set to {setting_value}")
return {'setting_name': setting_name, "status":True}
elif setting_name== "binding_name":
if lollmsElfServer.config['binding_name']!= data.setting_value:
print(f"New binding selected : {data.setting_value}")
lollmsElfServer.config["binding_name"]=data.setting_value
elif setting_name == "model_name":
ASCIIColors.yellow(f"Changing model to: {setting_value}")
lollmsElfServer.config["model_name"]=setting_value
lollmsElfServer.config.save_config()
try:
if lollmsElfServer.binding:
lollmsElfServer.binding.destroy_model()
lollmsElfServer.binding = None
lollmsElfServer.model = None
for per in lollmsElfServer.mounted_personalities:
if per is not None:
per.model = None
gc.collect()
lollmsElfServer.binding = BindingBuilder().build_binding(lollmsElfServer.config, lollmsElfServer.lollms_paths, InstallOption.INSTALL_IF_NECESSARY, lollmsCom=lollmsElfServer)
lollmsElfServer.model = None
lollmsElfServer.config.save_config()
ASCIIColors.green("Binding loaded successfully")
lollmsElfServer.model = lollmsElfServer.binding.build_model()
if lollmsElfServer.model is not None:
ASCIIColors.yellow("New model OK")
for per in lollmsElfServer.mounted_personalities:
if per is not None:
per.model = lollmsElfServer.model
except Exception as ex:
ASCIIColors.error(f"Couldn't build binding: [{ex}]")
trace_exception(ex)
return {"status":False, 'error':str(ex)}
lollmsElfServer.InfoMessage(f"It looks like you we couldn't load the model.\nHere is the error message:\n{ex}")
else:
if lollmsElfServer.config["debug"]:
print(f"Configuration {data.setting_name} set to {data.setting_value}")
return {'setting_name': data.setting_name, "status":True}
if setting_name in lollmsElfServer.config.config.keys():
lollmsElfServer.config[setting_name] = setting_value
else:
if lollmsElfServer.config["debug"]:
print(f"Configuration {setting_name} couldn't be set to {setting_value}")
return {'setting_name': setting_name, "status":False}
if lollmsElfServer.config["debug"]:
print(f"Configuration {setting_name} set to {setting_value}")
ASCIIColors.success(f"Configuration {setting_name} updated")
if lollmsElfServer.config.auto_save:
lollmsElfServer.config.save_config()
# Tell that the setting was changed
return {'setting_name': setting_name, "status":True}
except Exception as ex:
trace_exception(ex)
lollmsElfServer.error(ex)
return {"status":False,"error":str(ex)}
elif setting_name == "model_name":
ASCIIColors.yellow(f"Changing model to: {data.setting_value}")
lollmsElfServer.config["model_name"]=data.setting_value
lollmsElfServer.config.save_config()
@router.post("/apply_settings")
async def apply_settings(request: Request):
"""
Endpoint to apply configuration settings.
:param request: The HTTP request object.
:return: A JSON response with the status of the operation.
"""
try:
config_data = await request.json()
config = config_data["config"]
try:
lollmsElfServer.model = None
for per in lollmsElfServer.mounted_personalities:
if per is not None:
per.model = None
lollmsElfServer.model = lollmsElfServer.binding.build_model()
if lollmsElfServer.model is not None:
ASCIIColors.yellow("New model OK")
for per in lollmsElfServer.mounted_personalities:
if per is not None:
per.model = lollmsElfServer.model
for key in lollmsElfServer.config.config.keys():
lollmsElfServer.config.config[key] = config.get(key, lollmsElfServer.config.config[key])
ASCIIColors.success("OK")
lollmsElfServer.rebuild_personalities()
if lollmsElfServer.config.auto_save:
lollmsElfServer.config.save_config()
return {"status":True}
except Exception as ex:
trace_exception(ex)
lollmsElfServer.InfoMessage(f"It looks like you we couldn't load the model.\nHere is the error message:\n{ex}")
else:
if data.setting_name in lollmsElfServer.config.config.keys():
lollmsElfServer.config[data.setting_name] = data.setting_value
else:
if lollmsElfServer.config["debug"]:
print(f"Configuration {data.setting_name} couldn't be set to {data.setting_value}")
return {'setting_name': data.setting_name, "status":False}
if lollmsElfServer.config["debug"]:
print(f"Configuration {data.setting_name} set to {data.setting_value}")
ASCIIColors.success(f"Configuration {data.setting_name} updated")
if lollmsElfServer.config.auto_save:
lollmsElfServer.config.save_config()
# Tell that the setting was changed
return {'setting_name': data.setting_name, "status":True}
return {"status":False,"error":str(ex)}
except Exception as ex:
trace_exception(ex)
lollmsElfServer.error(ex)
return {"status":False,"error":str(ex)}

View File

@ -24,11 +24,6 @@ import yaml
class PersonalityListingInfos(BaseModel):
category:str
class PersonalityInstallInfos(BaseModel):
name:str
class PersonalityMountingInfos(BaseModel):
category:str
folder:str
@ -51,7 +46,7 @@ def list_personalities_categories():
return personalities_categories
@router.get("/list_personalities")
def list_personalities(category:PersonalityListingInfos):
def list_personalities(category:str):
if not category:
return []
try:
@ -159,7 +154,7 @@ def get_all_personalities():
@router.get("/list_mounted_personalities")
def list_mounted_personalities(lollmsElfServer):
def list_mounted_personalities():
ASCIIColors.yellow("- Listing mounted personalities")
return {"status": True,
"personalities":lollmsElfServer.config["personalities"],
@ -186,10 +181,18 @@ def get_current_personality_path_infos():
@router.post("/reinstall_personality")
def reinstall_personality(data: PersonalityInstallInfos):
if not 'name' in data:
data['name']=lollmsElfServer.config.personalities[lollmsElfServer.config["active_personality_id"]]
async def reinstall_personality(request: Request):
"""
Endpoint to apply configuration settings.
:param request: The HTTP request object.
:return: A JSON response with the status of the operation.
"""
try:
data = (await request.json())
if not 'name' in data:
data['name']=lollmsElfServer.config.personalities[lollmsElfServer.config["active_personality_id"]]
personality_path = lollmsElfServer.lollms_paths.personalities_zoo_path / data['name']
ASCIIColors.info(f"- Reinstalling personality {data['name']}...")
ASCIIColors.info("Unmounting personality")

View File

@ -28,6 +28,8 @@ import yaml
import asyncio
import ctypes
def run_async(func):
"""
run_async(func) -> None
@ -40,12 +42,10 @@ def run_async(func):
Returns:
None: Nothing is returned since the function is meant to perform side effects.
"""
async def wrapper(*args, **kwargs):
return asyncio.run(func(*args, **kwargs))
return wrapper()
try:
return asyncio.run(func)
except:
return func
def terminate_thread(thread):
if thread:
if not thread.is_alive():