From f9157d86f144be5f14b8fa09161986bf6ba60643 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Fri, 7 Mar 2025 21:39:32 +0100 Subject: [PATCH] upgraded fc --- lollms/app.py | 61 ++++++++++--------- .../server/endpoints/lollms_function_calls.py | 14 +++-- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/lollms/app.py b/lollms/app.py index 62adef9..2ac0773 100644 --- a/lollms/app.py +++ b/lollms/app.py @@ -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 diff --git a/lollms/server/endpoints/lollms_function_calls.py b/lollms/server/endpoints/lollms_function_calls.py index 0e65bdb..8db9f25 100644 --- a/lollms/server/endpoints/lollms_function_calls.py +++ b/lollms/server/endpoints/lollms_function_calls.py @@ -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")