upgraded fc

This commit is contained in:
Saifeddine ALOUI 2025-03-07 21:39:32 +01:00
parent 64f4cded62
commit f9157d86f1
2 changed files with 43 additions and 32 deletions

View File

@ -178,6 +178,36 @@ class LollmsApplication(LoLLMsCom):
self.tasks_library = TasksLibrary(self)
def load_function_call(self, fc, client):
dr = Path(fc["dir"])
try:
with open(dr/"config.yaml", "r") as f:
fc_dict = yaml.safe_load(f.read())
# let us check static settings from fc_dict
# Step 1: Construct the full path to the function.py module
module_path = dr / "function.py"
module_name = "function" # Name for the loaded module
# Step 2: Use importlib.util to load the module from the file path
spec = importlib.util.spec_from_file_location(module_name, module_path)
if spec is None:
raise ImportError(f"Could not load module from {module_path}")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module # Add the module to sys.modules
spec.loader.exec_module(module) # Execute the module
# Step 3: Retrieve the class from the module using the class name
class_name = fc_dict["class_name"]
class_ = getattr(module, class_name)
# Step 4: Create an instance of the class and store it in fc_dict["class"]
fc_dict["class"] = class_(self, client)
return fc_dict
except Exception as ex:
self.error("Couldn't add function call to context")
trace_exception(ex)
return None
def embed_function_call_in_prompt(self, original_prompt):
"""Embeds function call descriptions in the system prompt"""
function_descriptions = [
@ -1371,34 +1401,9 @@ Answer directly with the reformulation of the last prompt.
if len(self.config.mounted_function_calls)>0:
for fc in self.config.mounted_function_calls:
if fc["selected"]:
dr = Path(fc["dir"])
try:
with open(dr/"config.yaml", "r") as f:
fc_dict = yaml.safe_load(f.read())
# let us check static settings from fc_dict
# Step 1: Construct the full path to the function.py module
module_path = dr / "function.py"
module_name = "function" # Name for the loaded module
# Step 2: Use importlib.util to load the module from the file path
spec = importlib.util.spec_from_file_location(module_name, module_path)
if spec is None:
raise ImportError(f"Could not load module from {module_path}")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module # Add the module to sys.modules
spec.loader.exec_module(module) # Execute the module
# Step 3: Retrieve the class from the module using the class name
class_name = fc_dict["class_name"]
class_ = getattr(module, class_name)
# Step 4: Create an instance of the class and store it in fc_dict["class"]
fc_dict["class"] = class_(self, client)
function_calls.append(fc_dict)
except Exception as ex:
self.error("Couldn't add function call to context")
trace_exception(ex)
fci = self.load_function_call(fc, client)
if fci:
function_calls.append(fci)
# Calculate the total number of tokens between conditionning, documentation, and knowledge
total_tokens = n_cond_tk + n_isearch_tk + n_doc_tk + n_user_description_tk + n_positive_boost + n_negative_boost + n_fun_mode + n_think_first_mode

View File

@ -218,17 +218,23 @@ async def toggle_function_call(request: Request):
@router.post("/get_function_call_settings")
async def get_function_call_settings(request: Request):
data = await request.json()
check_access(lollmsElfServer,data["client_id"])
client = check_access(lollmsElfServer,data["client_id"])
fn_dir = data.get("dir")
function_name = data.get("name")
# Add new entry
for entry in lollmsElfServer.config.mounted_function_calls:
if entry["name"] == function_name and Path(entry["dir"]).parent.name == str(fn_dir):
if hasattr(entry,"static_params"):
return entry.static_params.config_template.template
else:
try:
fci = lollmsElfServer.load_function_call(entry, client)
if hasattr(fci,"static_params"):
return fci.static_params.config_template.template
else:
return {}
except Exception as ex:
trace_exception(ex)
return {}
return {}
@router.post("/set_function_call_settings")