bugfix
This commit is contained in:
parent
7d45d9b71f
commit
d6c2363da2
BIN
bricks/.DS_Store
vendored
BIN
bricks/.DS_Store
vendored
Binary file not shown.
@ -83,7 +83,7 @@ bricks.str2data = function(s, d){
|
||||
return d[key];
|
||||
}
|
||||
return s.replace(regex, (k, key, typ) => {
|
||||
if (d.hasOwnProperty(key){
|
||||
if (d.hasOwnProperty(key)){
|
||||
var f = funcs[typ];
|
||||
if (f){
|
||||
return f(d[key]);
|
||||
|
@ -277,3 +277,6 @@ body {
|
||||
margin-bottom: 10px;
|
||||
background-color:#44fefe;
|
||||
}
|
||||
.llm_title {
|
||||
background-color:#fafafa;
|
||||
}
|
||||
|
@ -201,26 +201,26 @@ bricks.HttpResponse = class extends bricks.HttpText {
|
||||
}
|
||||
}
|
||||
|
||||
bricks.HttpResponseStream = class extends bricks.HttpText {
|
||||
async get_result_data(resp){
|
||||
bricks.HttpResponseStream = class extends bricks.HttpResponse {
|
||||
async handle_chunk(resp, handler){
|
||||
const reader = resp.body.getReader();
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
let result = await reader.read();
|
||||
var buffer = '';
|
||||
var buff_ = '';
|
||||
while (!result.done) {
|
||||
const text = decoder.decode(result.value);
|
||||
buffer += text;
|
||||
buff_ += text;
|
||||
const newline = /\r?\n/gm;
|
||||
var rez = newline.exec(buffer);
|
||||
var rez = newline.exec(buff_);
|
||||
while (rez){
|
||||
yield buffer.substring(0, rez.index);
|
||||
buffer = buffer.substring(newline.lastIndex);
|
||||
rez = newline.exec(buffer);
|
||||
handler(buff_.substring(0, rez.index));
|
||||
buff_ = buff_.substr(newline.lastIndex);
|
||||
rez = newline.exec(buff_);
|
||||
}
|
||||
result = await reader.read()
|
||||
}
|
||||
if (buffer != ''){
|
||||
yield bufffer;
|
||||
if (buff_ != ''){
|
||||
handler(buff_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
var bricks = window.bricks || {}
|
||||
|
||||
bricks.LlmDialog = class extends bricks.VScrollPanel {
|
||||
bricks.LlmDialog = class extends bricks.VBox {
|
||||
/*
|
||||
{
|
||||
"url":
|
||||
"model",
|
||||
"mapi":
|
||||
"user_msg_css"
|
||||
"llm_icon"
|
||||
"title_ccs"
|
||||
"llm_msg_css"
|
||||
}
|
||||
method:
|
||||
@ -14,46 +17,64 @@ bricks.LlmDialog = class extends bricks.VScrollPanel {
|
||||
llm_answer event.params = assistant answer content
|
||||
*/
|
||||
constructor(opts){
|
||||
opts.height = opts.height || '100%';
|
||||
super(opts);
|
||||
this.title_w = new bricks.HBox({cheight:2});
|
||||
this.title_w.add_widget(new bricks.Icon({
|
||||
url:this.llm_icon||bricks_resource('imgs/llm.png')
|
||||
}));
|
||||
this.title_w.set_css(this.title_ccs||'llm_title');
|
||||
var t_w = new bricks.Text({text:this.model});
|
||||
this.title_w.add_widget(t_w);
|
||||
this.add_widget(this.title_w);
|
||||
var filler = new bricks.Filler({});
|
||||
this.body = new bricks.VScrollPanel({height:'100%'});
|
||||
filler.add_widget(this.body);
|
||||
this.add_widget(filler);
|
||||
this.messages = [];
|
||||
}
|
||||
set_prompt(prompt){
|
||||
var w = bricks.MdWidget({mdtext:prompt});
|
||||
async set_prompt(prompt){
|
||||
var w = new 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.body.add_widget(w);
|
||||
await this.llm_request(prompt);
|
||||
}
|
||||
async llm_request(prompt){
|
||||
this.messages.push({
|
||||
role:'user',
|
||||
content:prompt
|
||||
});
|
||||
var d = {
|
||||
messages:this.messages,
|
||||
mapi:this.mapi,
|
||||
prompt:prompt,
|
||||
model:this.model,
|
||||
}
|
||||
var hr = new bricks.HttpResponseStream();
|
||||
var resp = await hr.post(this.url, {params:d});
|
||||
await hr.handle_chunk(resp, this.processLine.bind(this));
|
||||
this.messages.push({
|
||||
role:'assistant',
|
||||
content:w.md_content
|
||||
content:this.llm_msg_w.md_content
|
||||
});
|
||||
this.llm_msg_w = null;
|
||||
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);
|
||||
processLine(l){
|
||||
var d = JSON.parse(l);
|
||||
if (!this.llm_msg_w) {
|
||||
this.llm_msg_w = new bricks.MdWidget({mdtext:d.content});
|
||||
this.llm_msg_w.set_css(this.llm_msg_css||'llm_msg');
|
||||
this.body.add_widget(this.llm_msg_w);
|
||||
return;
|
||||
}
|
||||
var txt = this.llm_msg_w.md_content + d.content;
|
||||
this.llm_msg_w.set_content(txt);
|
||||
this.body.dom_element.scrollTop = this.body.dom_element.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user