Spaces:
Runtime error
Runtime error
fix: Statics and history
Browse files- static/css/app.css +1 -1
- static/js/chatHandler.js +30 -17
- static/js/windowHandler.js +12 -3
static/css/app.css
CHANGED
|
@@ -152,7 +152,7 @@ button{
|
|
| 152 |
|
| 153 |
.chat .message pre {
|
| 154 |
overflow-x: scroll;
|
| 155 |
-
|
| 156 |
padding: 10px;
|
| 157 |
}
|
| 158 |
|
|
|
|
| 152 |
|
| 153 |
.chat .message pre {
|
| 154 |
overflow-x: scroll;
|
| 155 |
+
border: solid 1px #e5e4e4;
|
| 156 |
padding: 10px;
|
| 157 |
}
|
| 158 |
|
static/js/chatHandler.js
CHANGED
|
@@ -3,17 +3,18 @@ class ChatGPT{
|
|
| 3 |
|
| 4 |
$("#input-delete").click(()=> $(document).trigger("chat:limpiar"))
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
if (localStorage.getItem("convesacion") !== null) {
|
| 7 |
this.convesacion = JSON.parse(localStorage.getItem("convesacion"));
|
| 8 |
$(document).trigger("chat:cargar", this.convesacion);
|
| 9 |
}else{
|
| 10 |
-
let fecha = new Date().toJSON().slice(0, 10);
|
| 11 |
-
this.definicion = "Te llamas Chatsito, eres un asistente de apoyo a los amigos de MIA, ";
|
| 12 |
-
this.definicion += "tu objetivo principal es responder preguntas y hacer sentir bien ";
|
| 13 |
-
this.definicion += "a tu interlocutor.\n";
|
| 14 |
-
this.definicion += "Responde de manera amistosa con un poco de comedia.\n";
|
| 15 |
-
this.definicion += "Knowledge cutoff: 2021-09-01\nCurrent date: "+fecha;
|
| 16 |
-
this.definicion = {role: "system", content: this.definicion, tokens: 100};
|
| 17 |
this.convesacion = [this.definicion];
|
| 18 |
}
|
| 19 |
|
|
@@ -84,10 +85,13 @@ class ChatGPT{
|
|
| 84 |
tempMensajes.push({role: actMsg.role, content: actMsg.content})
|
| 85 |
tokens += actMsg.tokens
|
| 86 |
}
|
| 87 |
-
|
|
|
|
| 88 |
tempMensajes.push({role: "user", content: mensaje});
|
|
|
|
|
|
|
| 89 |
$(document).trigger("precarga:inicio", mensaje);
|
| 90 |
-
|
| 91 |
fetch(this.endpointChat, {
|
| 92 |
method: "POST",
|
| 93 |
body: JSON.stringify({
|
|
@@ -95,8 +99,10 @@ class ChatGPT{
|
|
| 95 |
token: this.token,
|
| 96 |
config: this.config
|
| 97 |
}),
|
| 98 |
-
}).then(response => ({
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
if(!rb){
|
| 101 |
return false;
|
| 102 |
}
|
|
@@ -111,9 +117,10 @@ class ChatGPT{
|
|
| 111 |
controller.close();
|
| 112 |
if(rstat==200){
|
| 113 |
$(document).trigger("precarga:fin");
|
| 114 |
-
|
|
|
|
| 115 |
}else{
|
| 116 |
-
|
| 117 |
console.log("terminado con errores")
|
| 118 |
}
|
| 119 |
return
|
|
@@ -131,10 +138,16 @@ class ChatGPT{
|
|
| 131 |
|
| 132 |
if(rstat==200){
|
| 133 |
if(data.object == "chat.token"){
|
| 134 |
-
|
| 135 |
-
}else if(data.choices[0].
|
| 136 |
-
let temp = data.choices[0].delta
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
}
|
| 139 |
}else{
|
| 140 |
$(document).trigger("precarga:error", {status: rstat, mensaje: data.detail});
|
|
|
|
| 3 |
|
| 4 |
$("#input-delete").click(()=> $(document).trigger("chat:limpiar"))
|
| 5 |
|
| 6 |
+
let fecha = new Date().toJSON().slice(0, 10);
|
| 7 |
+
this.definicion = "Te llamas Chatsito, eres un asistente de apoyo a los amigos de MIA, ";
|
| 8 |
+
this.definicion += "tu objetivo principal es responder preguntas y hacer sentir bien ";
|
| 9 |
+
this.definicion += "a tu interlocutor.\n";
|
| 10 |
+
this.definicion += "Responde de manera amistosa con un poco de comedia.\n";
|
| 11 |
+
this.definicion += "Knowledge cutoff: 2021-09-01\nCurrent date: "+fecha;
|
| 12 |
+
this.definicion = {role: "system", content: this.definicion, tokens: 100};
|
| 13 |
+
|
| 14 |
if (localStorage.getItem("convesacion") !== null) {
|
| 15 |
this.convesacion = JSON.parse(localStorage.getItem("convesacion"));
|
| 16 |
$(document).trigger("chat:cargar", this.convesacion);
|
| 17 |
}else{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
this.convesacion = [this.definicion];
|
| 19 |
}
|
| 20 |
|
|
|
|
| 85 |
tempMensajes.push({role: actMsg.role, content: actMsg.content})
|
| 86 |
tokens += actMsg.tokens
|
| 87 |
}
|
| 88 |
+
|
| 89 |
+
console.log("Enviando: ", mensaje);
|
| 90 |
tempMensajes.push({role: "user", content: mensaje});
|
| 91 |
+
this.convesacion.push({role: "user", content: mensaje});
|
| 92 |
+
this.convesacion.push({});
|
| 93 |
$(document).trigger("precarga:inicio", mensaje);
|
| 94 |
+
let self = this;
|
| 95 |
fetch(this.endpointChat, {
|
| 96 |
method: "POST",
|
| 97 |
body: JSON.stringify({
|
|
|
|
| 99 |
token: this.token,
|
| 100 |
config: this.config
|
| 101 |
}),
|
| 102 |
+
}).then(response => ({
|
| 103 |
+
rb: response.body,
|
| 104 |
+
rstat: response.status
|
| 105 |
+
})).then(({rb, rstat}) => {
|
| 106 |
if(!rb){
|
| 107 |
return false;
|
| 108 |
}
|
|
|
|
| 117 |
controller.close();
|
| 118 |
if(rstat==200){
|
| 119 |
$(document).trigger("precarga:fin");
|
| 120 |
+
localStorage.setItem("convesacion", JSON.stringify(self.convesacion))
|
| 121 |
+
console.log("terminado", self.convesacion[self.convesacion.length-1].content)
|
| 122 |
}else{
|
| 123 |
+
this.convesacion.pop()
|
| 124 |
console.log("terminado con errores")
|
| 125 |
}
|
| 126 |
return
|
|
|
|
| 138 |
|
| 139 |
if(rstat==200){
|
| 140 |
if(data.object == "chat.token"){
|
| 141 |
+
self.token = data.token;
|
| 142 |
+
}else if(data.choices[0].hasOwnProperty("delta")){
|
| 143 |
+
let temp = data.choices[0].delta;
|
| 144 |
+
let key = Object.keys(temp)[0];
|
| 145 |
+
let elActual = self.convesacion[self.convesacion.length-1]
|
| 146 |
+
if(!elActual.hasOwnProperty(key)){elActual[key]="";}
|
| 147 |
+
elActual[key] += temp[key]
|
| 148 |
+
if(elActual.hasOwnProperty("content") && temp.content){
|
| 149 |
+
$(document).trigger("precarga:mensaje", temp.content);
|
| 150 |
+
}
|
| 151 |
}
|
| 152 |
}else{
|
| 153 |
$(document).trigger("precarga:error", {status: rstat, mensaje: data.detail});
|
static/js/windowHandler.js
CHANGED
|
@@ -80,7 +80,15 @@ class WindowHandler{
|
|
| 80 |
cargarChat(mensajes){
|
| 81 |
mensajes.forEach((mensaje) => {
|
| 82 |
if(mensaje.role!="system"){
|
| 83 |
-
this.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
});
|
| 86 |
}
|
|
@@ -113,11 +121,12 @@ class WindowHandler{
|
|
| 113 |
|
| 114 |
respuestaFin(){
|
| 115 |
let msgProcesado = this.procesarTexto(this.mensaje);
|
|
|
|
| 116 |
$("button").prop("disabled", false);
|
| 117 |
$("textarea").prop("disabled", false);
|
| 118 |
$("textarea").focus();
|
| 119 |
-
this.active.find("div p").html(msgProcesado)
|
| 120 |
-
Prism.highlightAllUnder(this.active[0])
|
| 121 |
this.active = false;
|
| 122 |
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 123 |
}
|
|
|
|
| 80 |
cargarChat(mensajes){
|
| 81 |
mensajes.forEach((mensaje) => {
|
| 82 |
if(mensaje.role!="system"){
|
| 83 |
+
let clone = this.template.clone();
|
| 84 |
+
if(mensaje.role=="user") {clone.addClass("me");}
|
| 85 |
+
let texto = this.procesarTexto(mensaje.content);
|
| 86 |
+
clone.find("div p").html(texto);
|
| 87 |
+
this.chatbox.append(clone);
|
| 88 |
+
this.active = clone;
|
| 89 |
+
Prism.highlightAllUnder(this.active[0])
|
| 90 |
+
this.active = false;
|
| 91 |
+
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 92 |
}
|
| 93 |
});
|
| 94 |
}
|
|
|
|
| 121 |
|
| 122 |
respuestaFin(){
|
| 123 |
let msgProcesado = this.procesarTexto(this.mensaje);
|
| 124 |
+
this.mensaje = "";
|
| 125 |
$("button").prop("disabled", false);
|
| 126 |
$("textarea").prop("disabled", false);
|
| 127 |
$("textarea").focus();
|
| 128 |
+
this.active.find("div p").html(msgProcesado);
|
| 129 |
+
Prism.highlightAllUnder(this.active[0]);
|
| 130 |
this.active = false;
|
| 131 |
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 132 |
}
|