This commit is contained in:
yumoqing 2024-04-08 09:57:50 +08:00
parent 9e687ee3c1
commit 7d45d9b71f
5 changed files with 127 additions and 12 deletions

View File

@ -5,8 +5,8 @@ SOURCES=" page_data_loader.js factory.js uitypesdef.js utils.js \
input.js registerfunction.js button.js accordion.js \ input.js registerfunction.js button.js accordion.js \
tree.js multiple_state_image.js dynamiccolumn.js form.js message.js conform.js \ tree.js multiple_state_image.js dynamiccolumn.js form.js message.js conform.js \
paging.js datagrid.js dataviewer.js iframe.js \ paging.js datagrid.js dataviewer.js iframe.js \
floaticonbar.js \ floaticonbar.js miniform.js wterm.js dynamicaccordion.js \
miniform.js wterm.js dynamicaccordion.js " llm_dialog.js "
echo ${SOURCES} echo ${SOURCES}
cat ${SOURCES} > ../dist/bricks.js cat ${SOURCES} > ../dist/bricks.js
# uglifyjs --compress --mangle -- ../dist/bricks.js > ../dist/bricks.min.js # uglifyjs --compress --mangle -- ../dist/bricks.js > ../dist/bricks.min.js

View File

@ -261,3 +261,19 @@ body {
padding: 10px; padding: 10px;
background-color: #ffffff; background-color: #ffffff;
} }
.llm_msg {
width: 90%;
margin-left: 5px;
margin-right: auto;
margin-bottom: 10px;
background-color:#fefe44;
}
.user_msg {
width: 90%;
margin-left: auto;
margin-right: 5px;
margin-bottom: 10px;
background-color:#44fefe;
}

View File

@ -4,15 +4,26 @@ function url_params(data) {
} }
/* /*
async function doit() { async function doit() {
const response = await fetch("http://localhost:3000") const response = await fetch("http://localhost:3000")
const reader = response.body.getReader() const reader = response.body.getReader();
const decoder = new TextDecoder() const decoder = new TextDecoder('utf-8');
let result = await reader.read() let result = await reader.read();
while (!result.done) { var buffer = '';
const text = decoder.decode(result.value) while (!result.done) {
console.log("chunk is", text) const text = decoder.decode(result.value);
result = await reader.read() buffer += text;
} const newline = /\r?\n/gm;
var rez = newline.exec(buffer);
while (rez){
yield buffer.substring(0, rez.index);
buffer = buffer.substring(newline.lastIndex);
rez = newline.exec(buffer);
}
result = await reader.read()
}
if (buffer != ''){
yield bufffer;
}
} }
// chunked response handle // chunked response handle
*/ */
@ -115,7 +126,6 @@ bricks.HttpText = class {
"data":result.data || '', "data":result.data || '',
"code":result.code || '' "code":result.code || ''
}; };
const error = new Error();
error.info = resp_error; error.info = resp_error;
return error; return error;
} }
@ -191,6 +201,30 @@ bricks.HttpResponse = class extends bricks.HttpText {
} }
} }
bricks.HttpResponseStream = class extends bricks.HttpText {
async get_result_data(resp){
const reader = resp.body.getReader();
const decoder = new TextDecoder('utf-8');
let result = await reader.read();
var buffer = '';
while (!result.done) {
const text = decoder.decode(result.value);
buffer += text;
const newline = /\r?\n/gm;
var rez = newline.exec(buffer);
while (rez){
yield buffer.substring(0, rez.index);
buffer = buffer.substring(newline.lastIndex);
rez = newline.exec(buffer);
}
result = await reader.read()
}
if (buffer != ''){
yield bufffer;
}
}
}
bricks.HttpRaw = class extends bricks.HttpText { bricks.HttpRaw = class extends bricks.HttpText {
async get_result_data(resp){ async get_result_data(resp){
return await resp.blob(); return await resp.blob();

61
bricks/llm_dialog.js Normal file
View File

@ -0,0 +1,61 @@
var bricks = window.bricks || {}
bricks.LlmDialog = class extends bricks.VScrollPanel {
/*
{
"url":
"model",
"user_msg_css"
"llm_msg_css"
}
method:
set_prompt(prompt)
event:
llm_answer event.params = assistant answer content
*/
constructor(opts){
super(opts);
this.messages = [];
}
set_prompt(prompt){
var w = bricks.MdWidget({mdtext:prompt});
w.set_css(this.user_msg_css||'user_msg');
this.add_widget(w);
this.llm_request(prompt);
}
llm_request(prompt){
var d = {
messages:this.messages,
model:this.model,
prompt:prompt
}
var hr = bricks.HttpResponseStream();
var w = bricks.MdWidget({mdtext:''});
w.set_css(this.llm_msg_css||'llm_msg');
this.add_widget(w);
for await (const line of hr.get(this.url, {params=d})) {
this.processLine(line, w);
}
this.messages.push({
role:'user',
content:prompt
});
this.messages.push({
role:'assistant',
content:w.md_content
});
var d = {
model:this.model,
messages:this.messages
}
this.dispatch('llm_answered', d);
}
processLine(l, w){
var d = JSON.parse();
var txt = w.md_content + d.content;
w.set_content(txt);
}
}
bricks.Factory.register('LlmDialog', bricks.LlmDialog);

View File

@ -28,6 +28,10 @@ bricks.MdWidget = class extends bricks.JsWidget {
schedule_once(f, 0.01); schedule_once(f, 0.01);
} }
} }
set_content(content){
this.md_content = content;
this._build1();
}
show_scroll(event){ show_scroll(event){
bricks.debug('scrollY=', window.scrollY); bricks.debug('scrollY=', window.scrollY);
} }