This commit is contained in:
Saifeddine ALOUI 2023-10-15 03:12:36 +02:00
parent 875bcc2bb4
commit f0507f9e5c
8 changed files with 187 additions and 64 deletions

View File

@ -1215,13 +1215,13 @@ class LoLLMsAPPI(LollmsApplication):
"doc":str_docs,
"conditionning":conditionning,
"content":discussion_messages
}, self.model.tokenize, self.model.detokenize, self.config.ctx_size, place_holders_to_sacrifice=["content"])
}, self.model.tokenize, self.model.detokenize, self.config.ctx_size-self.config.min_n_predict, place_holders_to_sacrifice=["content"])
else:
pr = PromptReshaper("{{conditionning}}\n{{content}}")
discussion_messages = pr.build({
"conditionning":conditionning,
"content":discussion_messages
}, self.model.tokenize, self.model.detokenize, self.config.ctx_size, place_holders_to_sacrifice=["content"])
}, self.model.tokenize, self.model.detokenize, self.config.ctx_size-self.config.min_n_predict, place_holders_to_sacrifice=["content"])
# remove extra returns
discussion_messages = self.clean_string(discussion_messages)
tokens = self.model.tokenize(discussion_messages)

78
app.py
View File

@ -473,24 +473,11 @@ class LoLLMsWebUI(LoLLMsAPPI):
)
self.add_endpoint(
"/execute_python_code", "execute_python_code", self.execute_python_code, methods=["POST"]
"/execute_code", "execute_code", self.execute_code, methods=["POST"]
)
def execute_python_code(self):
"""Executes Python code and returns the output."""
data = request.get_json()
code = data["code"]
discussion_id = data.get("discussion_id","unknown_discussion")
message_id = data.get("message_id","unknown_message")
ASCIIColors.info("Executing python code:")
ASCIIColors.yellow(code)
def execute_python(self, code, discussion_id, message_id):
def spawn_process(code):
"""Executes Python code and returns the output as JSON."""
@ -536,6 +523,67 @@ class LoLLMsWebUI(LoLLMsAPPI):
return json.dumps(output_json)
return spawn_process(code)
def execute_bash(self, code, discussion_id, message_id):
def spawn_process(code):
"""Executes Python code and returns the output as JSON."""
# Start the timer.
start_time = time.time()
# Create a temporary file.
root_folder = self.lollms_paths.personal_outputs_path/"discussions"/f"d_{discussion_id}"
root_folder.mkdir(parents=True,exist_ok=True)
try:
# Execute the Python code in a temporary file.
process = subprocess.Popen(
code,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Get the output and error from the process.
output, error = process.communicate()
except Exception as ex:
# Stop the timer.
execution_time = time.time() - start_time
error_message = f"Error executing Python code: {ex}"
error_json = {"output": "<div class='text-red-500'>"+str(ex)+"\n"+get_trace_exception(ex)+"</div>", "execution_time": execution_time}
return json.dumps(error_json)
# Stop the timer.
execution_time = time.time() - start_time
# Check if the process was successful.
if process.returncode != 0:
# The child process threw an exception.
error_message = f"Error executing Python code: {error.decode('utf8')}"
error_json = {"output": "<div class='text-red-500'>"+error_message+"</div>", "execution_time": execution_time}
return json.dumps(error_json)
# The child process was successful.
output_json = {"output": output.decode("utf8"), "execution_time": execution_time}
return json.dumps(output_json)
return spawn_process(code)
def execute_code(self):
"""Executes Python code and returns the output."""
data = request.get_json()
code = data["code"]
discussion_id = data.get("discussion_id","unknown_discussion")
message_id = data.get("message_id","unknown_message")
language = data.get("language","python")
ASCIIColors.info("Executing python code:")
ASCIIColors.yellow(code)
if language=="python":
return self.execute_python(code, discussion_id, message_id)
elif language=="bash":
return self.execute_bash(code, discussion_id, message_id)
def copy_files(self, src, dest):
for item in os.listdir(src):
src_file = os.path.join(src, item)

View File

@ -1,5 +1,5 @@
# =================== Lord Of Large Language Models Configuration file ===========================
version: 25
version: 26
binding_name: null
model_name: null
@ -14,6 +14,7 @@ discussion_prompt_separator: "!@>"
seed: -1
n_predict: 1024
ctx_size: 2048
min_n_predict: 256
temperature: 0.9
top_k: 50
top_p: 0.95

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

4
web/dist/index.html vendored
View File

@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LoLLMS WebUI - Welcome</title>
<script type="module" crossorigin src="/assets/index-7169784d.js"></script>
<link rel="stylesheet" href="/assets/index-766ad978.css">
<script type="module" crossorigin src="/assets/index-a63dc690.js"></script>
<link rel="stylesheet" href="/assets/index-50e8a572.css">
</head>
<body>
<div id="app"></div>

View File

@ -374,7 +374,7 @@ export default {
this.filesList = []
this.isFileSentList = []
},
send_file(file){
send_file(file, next){
const formData = new FormData();
formData.append('file', file);
console.log("Uploading file")
@ -388,15 +388,8 @@ export default {
socket.on('file_received',(resp)=>{
if(resp.status){
console.log(resp.filename)
let index = this.filesList.findIndex((file) => file.name === resp.filename);
if(index>=0){
this.isFileSentList[index]=true;
console.log(this.isFileSentList)
}
else{
console.log("Not found")
}
this.isFileSentList[this.filesList.length-1]=true;
console.log(this.isFileSentList)
this.onShowToastMessage("File uploaded successfully",4,true);
}
else{
@ -410,6 +403,8 @@ export default {
}
socket.off('file_received')
next()
})
socket.emit('send_file', data);
};
@ -522,11 +517,24 @@ export default {
this.$emit('stopGenerating')
},
addFiles(event) {
console.log("Adding file")
this.filesList = this.filesList.concat([...event.target.files])
console.log(`Files_list : ${this.filesList}`)
this.isFileSentList = this.isFileSentList.concat([false] * this.filesList.length)
this.send_file(this.filesList[this.filesList.length-1])
console.log("Adding files");
const newFiles = [...event.target.files];
let index = 0;
const sendNextFile = () => {
if (index >= newFiles.length) {
console.log(`Files_list: ${this.filesList}`);
return;
}
const file = newFiles[index];
this.filesList.push(file);
this.isFileSentList.push(false);
this.send_file(file, () => {
index++;
sendNextFile();
}
);
};
sendNextFile();
}
},
watch: {

View File

@ -35,9 +35,30 @@ const markdownIt = new MarkdownIt('commonmark', {
typographer: true,
highlight: (str, lang) => {
let id = generateUniqueId();
let discussion_id = 0
let message_id = 0
let btn_exec_txt = (lang=='python' || lang=='bash') ?'<button class="px-2 py-1 ml-10 mb-2 text-left p-2 text-sm font-medium bg-bg-dark-tone-panel dark:bg-bg-dark-tone rounded-lg hover:bg-primary dark:hover:bg-primary text-white text-xs transition-colors duration-200">' +
'<span class="mr-1" id="exec-btn_' +
id +
'" onclick="executeCode(' +
id + ',' + discussion_id + ',' + message_id + ',`' + lang +
'`)">Execute</span>'+
'</button>':''
let btn_open_in_vs_code_txt = (lang=='python' || lang=='bash') ?'<button class="px-2 py-1 ml-10 mb-2 text-left p-2 text-sm font-medium bg-bg-dark-tone-panel dark:bg-bg-dark-tone rounded-lg hover:bg-primary dark:hover:bg-primary text-white text-xs transition-colors duration-200">' +
'<span class="mr-1" id="exec-btn_' +
id + '5' +
'" onclick="openInVsCode(' +
id + '5,' + discussion_id + ',' + message_id + ',`' + lang +
'`)">Show in vs code</span>'+
'</button>':''
if (lang && hljs.getLanguage(lang)) {
try {
const highlightedCode = hljs.highlight(lang, str).value;
return (
'<div class="bg-bg-light-tone-panel dark:bg-bg-dark-tone-panel p-2 rounded-lg shadow-sm">' +
lang +
@ -53,16 +74,9 @@ const markdownIt = new MarkdownIt('commonmark', {
id +
')">Copied!</span>' +
'</button>' +
'<button class="px-2 py-1 ml-10 mb-2 text-left p-2 text-sm font-medium bg-bg-dark-tone-panel dark:bg-bg-dark-tone rounded-lg hover:bg-primary dark:hover:bg-primary text-white text-xs transition-colors duration-200">' +
'<span class="mr-1" id="exec-btn_' +
id +
'" onclick="executeCode(' +
id +
')">Execute</span>'+
'</button>' +
btn_exec_txt +
btn_open_in_vs_code_txt +
'<pre class="hljs p-1 rounded-md break-all grid grid-cols-1">' +
'<code id="code_' +
id +
@ -85,13 +99,7 @@ const markdownIt = new MarkdownIt('commonmark', {
console.error(`Syntax highlighting failed for language '${lang}':`, error);
}
}
let btn_exec_txt = lang=='python'?'<button class="px-2 py-1 ml-10 mb-2 text-left p-2 text-sm font-medium bg-bg-dark-tone-panel dark:bg-bg-dark-tone rounded-lg hover:bg-primary dark:hover:bg-primary text-white text-xs transition-colors duration-200">' +
'<span class="mr-1" id="exec-btn_' +
id +
'" onclick="executeCode(' +
id +
')">Execute</span>'+
'</button>':''
let codeString =
'<div class="bg-bg-light-tone-panel dark:bg-bg-dark-tone-panel p-2 rounded-lg shadow-sm">' +
@ -112,6 +120,8 @@ const markdownIt = new MarkdownIt('commonmark', {
btn_exec_txt +
btn_open_in_vs_code_txt +
'<pre class="hljs p-1 rounded-md break-all grid grid-cols-1">' +
'<code id="code_' +
id +
@ -134,7 +144,7 @@ const markdownIt = new MarkdownIt('commonmark', {
hljs.configure({ languages: [] }); // Reset languages
hljs.configure({ languages: ['javascript'] }); // Set JavaScript as the default language
hljs.configure({ languages: ['bash'] }); // Set bash as the default language
markdownIt.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const token = tokens[idx];
@ -183,15 +193,43 @@ export default {
document.execCommand('copy');
window.getSelection().removeAllRanges();
}
function executeCode(id) {
function executeCode(id, discussion_id, message_id, lang) {
const codeElement = document.getElementById('code_' + id);
const codeExecElement = document.getElementById('code_exec_' + id);
const preExecElement = document.getElementById('pre_exec_' + id);
const code = codeElement.innerText
const json = JSON.stringify({ 'code': code })
const json = JSON.stringify({ 'code': code, 'discussion_id': discussion_id, 'message_id':message_id, 'language': lang})
console.log(json)
fetch('http://localhost:9600/execute_python_code', {
fetch('http://localhost:9600/execute_code', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: json
}).then(response=>{
// Parse the JSON data from the response body
return response.json();
})
.then(jsonData => {
// Now you can work with the JSON data
console.log(jsonData);
preExecElement.classList.remove('hidden');
codeExecElement.innerHTML = jsonData.output
})
.catch(error => {
// Handle any errors that occurred during the fetch process
console.error('Fetch error:', error);
});
}
function openInVsCode(id, discussion_id, message_id, lang) {
const codeElement = document.getElementById('code_' + id);
const codeExecElement = document.getElementById('code_exec_' + id);
const preExecElement = document.getElementById('pre_exec_' + id);
const code = codeElement.innerText
const json = JSON.stringify({ 'code': code, 'discussion_id': discussion_id, 'message_id':message_id, 'language': lang})
console.log(json)
fetch('http://localhost:9600/open_in_vs_code', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: json