enhanced core

This commit is contained in:
Saifeddine ALOUI 2024-04-05 00:53:36 +02:00
parent f4424cfc3d
commit a2da257b1c
2 changed files with 52 additions and 2 deletions

View File

@ -2566,8 +2566,19 @@ class APScript(StateMachine):
if not PackageManager.check_package_installed("autopep8"): if not PackageManager.check_package_installed("autopep8"):
PackageManager.install_package("autopep8") PackageManager.install_package("autopep8")
import autopep8 import autopep8
global_prompt = f"{prompt}\n!@>Code Builder:```python\n" global_prompt = "\n".join([
f"{prompt}",
"!@>Extra conditions:",
"- The code must be complete, not just snippets, and should be put inside a single python markdown code.",
"-Preceive each python codeblock with a line using this syntax:",
"$$file_name|the file path relative to the root folder of the project$$",
"```python",
"# Placeholder. Here you need to put the code for the file",
"```",
"!@>Code Builder:"
])
code = self.fast_gen(global_prompt, max_title_length) code = self.fast_gen(global_prompt, max_title_length)
code_blocks = self.extract_code_blocks(code)
try: try:
back_quote_index = code.index("```") # Remove trailing backticks back_quote_index = code.index("```") # Remove trailing backticks
if back_quote_index>=0: if back_quote_index>=0:
@ -2723,6 +2734,33 @@ The AI should respond in this format using data from actions_list:
gen = fix_json(gen) gen = fix_json(gen)
return generate_actions(actions_list, gen) return generate_actions(actions_list, gen)
def parse_directory_structure(self, structure):
paths = []
lines = structure.strip().split('\n')
stack = []
for line in lines:
line = line.rstrip()
level = (len(line) - len(line.lstrip())) // 4
if '/' in line or line.endswith(':'):
directory = line.strip(' ├─└│').rstrip(':').rstrip('/')
while stack and level < stack[-1][0]:
stack.pop()
stack.append((level, directory))
path = '/'.join([dir for _, dir in stack]) + '/'
paths.append(path)
else:
file = line.strip(' ├─└│')
if stack:
path = '/'.join([dir for _, dir in stack]) + '/' + file
paths.append(path)
return paths
def extract_code_blocks(self, text: str) -> List[dict]: def extract_code_blocks(self, text: str) -> List[dict]:
remaining = text remaining = text
bloc_index = 0 bloc_index = 0
@ -2746,10 +2784,12 @@ The AI should respond in this format using data from actions_list:
for index, code_delimiter_position in enumerate(indices): for index, code_delimiter_position in enumerate(indices):
block_infos = { block_infos = {
'index':index, 'index':index,
'file_name': "",
'content': "", 'content': "",
'type':"" 'type':""
} }
if is_start: if is_start:
sub_text = text[code_delimiter_position+3:] sub_text = text[code_delimiter_position+3:]
if len(sub_text)>0: if len(sub_text)>0:
try: try:
@ -2876,6 +2916,7 @@ The AI should respond in this format using data from actions_list:
return -1 return -1
else: else:
return -1 return -1
def multichoice_ranking(self, question: str, possible_answers:list, context:str = "", max_answer_length: int = 50, conditionning="") -> int: def multichoice_ranking(self, question: str, possible_answers:list, context:str = "", max_answer_length: int = 50, conditionning="") -> int:
""" """
Ranks answers for a question from best to worst. returns a list of integers Ranks answers for a question from best to worst. returns a list of integers
@ -2922,6 +2963,8 @@ The AI should respond in this format using data from actions_list:
ASCIIColors.red("Model failed to rank inputs") ASCIIColors.red("Model failed to rank inputs")
return None return None
def build_html5_integration(self, html, ifram_name="unnamed"): def build_html5_integration(self, html, ifram_name="unnamed"):
""" """
This function creates an HTML5 iframe with the given HTML content and iframe name. This function creates an HTML5 iframe with the given HTML content and iframe name.

View File

@ -19,12 +19,14 @@ from pathlib import Path
from typing import List from typing import List
import psutil import psutil
import yaml import yaml
from lollms.security import sanitize_path from lollms.security import sanitize_path, check_access
# --------------------- Parameter Classes ------------------------------- # --------------------- Parameter Classes -------------------------------
class ExtensionInstallInfos(BaseModel): class ExtensionInstallInfos(BaseModel):
client_id:str
name:str name:str
class ExtensionMountingInfos(BaseModel): class ExtensionMountingInfos(BaseModel):
client_id:str
category:str category:str
folder:str folder:str
language:str language:str
@ -123,6 +125,7 @@ def get_all_extensions():
# --------------------- Installing ------------------------------- # --------------------- Installing -------------------------------
@router.post("/install_extension") @router.post("/install_extension")
def install_extension(data: ExtensionInstallInfos): def install_extension(data: ExtensionInstallInfos):
check_access(lollmsElfServer, data.client_id)
if not data.name: if not data.name:
try: try:
data.name=lollmsElfServer.config.extensions[-1] data.name=lollmsElfServer.config.extensions[-1]
@ -148,6 +151,7 @@ def install_extension(data: ExtensionInstallInfos):
@router.post("/reinstall_extension") @router.post("/reinstall_extension")
def reinstall_extension(data: ExtensionInstallInfos): def reinstall_extension(data: ExtensionInstallInfos):
check_access(lollmsElfServer, data.client_id)
if not data.name: if not data.name:
try: try:
data.name=sanitize_path(lollmsElfServer.config.extensions[-1]) data.name=sanitize_path(lollmsElfServer.config.extensions[-1])
@ -184,6 +188,7 @@ def reinstall_extension(data: ExtensionInstallInfos):
@router.post("/mount_extension") @router.post("/mount_extension")
def mount_extension(data:ExtensionMountingInfos): def mount_extension(data:ExtensionMountingInfos):
check_access(lollmsElfServer, data.client_id)
print("- Mounting extension") print("- Mounting extension")
category = sanitize_path(data.category) category = sanitize_path(data.category)
name = sanitize_path(data.folder) name = sanitize_path(data.folder)
@ -210,6 +215,7 @@ def mount_extension(data:ExtensionMountingInfos):
@router.post("/remount_extension") @router.post("/remount_extension")
def remount_extension(data:ExtensionMountingInfos): def remount_extension(data:ExtensionMountingInfos):
check_access(lollmsElfServer, data.client_id)
print("- Remounting extension") print("- Remounting extension")
category = sanitize_path(data.category) category = sanitize_path(data.category)
name = sanitize_path(data.folder) name = sanitize_path(data.folder)
@ -250,6 +256,7 @@ def remount_extension(data:ExtensionMountingInfos):
@router.post("/unmount_extension") @router.post("/unmount_extension")
def unmount_extension(data:ExtensionMountingInfos): def unmount_extension(data:ExtensionMountingInfos):
check_access(lollmsElfServer, data.client_id)
print("- Unmounting extension ...") print("- Unmounting extension ...")
category = sanitize_path(data.category) category = sanitize_path(data.category)
name = sanitize_path(data.folder) name = sanitize_path(data.folder)