Spaces:
Runtime error
Runtime error
File size: 7,143 Bytes
1110b36 6eb753d 1110b36 3a33259 1110b36 6eb753d 1110b36 98fda2f 1110b36 229e6a9 1110b36 4b40455 1110b36 6eb753d 4b40455 6eb753d 2b4d82f 6eb753d 4b40455 d2c52b6 4b40455 6eb753d 97752e5 ad1cb72 30a29fd 838ed10 6eb753d 30a29fd 4b40455 1110b36 4b40455 98fda2f 1110b36 97752e5 2d7d2cc 7e0743f 97752e5 6eb753d 97752e5 2d7d2cc 97752e5 42d365f 744be20 42d365f 744be20 3a33259 744be20 3a33259 6eb753d 3a33259 b44092b 744be20 3a33259 0568fb7 845c944 744be20 845c944 eb99830 3a33259 eb99830 2d7d2cc eb99830 744be20 845c944 eb99830 845c944 eb99830 3a33259 eb99830 744be20 eb99830 845c944 eb99830 845c944 744be20 6a3eb62 d2c52b6 744be20 1110b36 744be20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | class ChatGPT{
constructor(token){
$("#input-delete").click(()=> $(document).trigger("chat:limpiar"))
let fecha = new Date().toJSON().slice(0, 10);
this.definicion = "Te llamas Chatsito, eres un asistente de apoyo a los amigos de MIA, ";
this.definicion += "tu objetivo principal es responder preguntas y hacer sentir bien ";
this.definicion += "a tu interlocutor.\n";
this.definicion += "Responde de manera amistosa con un poco de comedia.\n";
this.definicion += "Knowledge cutoff: 2021-09-01\nCurrent date: "+fecha;
this.definicion = {role: "system", content: this.definicion, tokens: 100};
if (localStorage.getItem("convesacion") !== null) {
this.convesacion = JSON.parse(localStorage.getItem("convesacion"));
$(document).trigger("chat:cargar", this.convesacion);
}else{
this.convesacion = [this.definicion];
}
if (localStorage.getItem("config") !== null) {
this.config = JSON.parse(localStorage.getItem("config"));
}else{
this.config = {
temperature: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
googleSearch: false
};
}
this.execStart = 0;
this.endpointChat = "/chat_stream";
this.token = token
// this.evCtx = document
this.reintentos = 0
$(document).on("chat:limpiar", () => {
this.limpiarConfig()
});
$(document).on("chat:enviar", (event, params) => {
this.reintentos = 0;
this.enviar(params);
});
$(document).on("enviar:reintentar", (event, params) => {
this.reintentos++;
this.enviar(params.mensaje)
});
// Signaling
// los on se escuchan cuando suena lo que se està disparando.
$(document).on("accion:switch_change", (event, params) => {
this.config.googleSearch = params;
console.log(this.config);
})
$(document).on("enviar:error", (event, params) => this.reenviar(params));
}
limpiarConfig(){
localStorage.removeItem('convesacion');
localStorage.removeItem('config');
this.config = {
temperature: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0
};
this.convesacion = [this.definicion];
}
reenviar(params){
if(params.status==404){
$(document).trigger("precarga:error",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
return
}
if(this.reintentos < 3 ){
$(document).trigger("enviar:enviar",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
}else{
$(document).trigger("precarga:error",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
}
}
enviar(mensaje){
// Configuración de variables base
let tempMensajes = [];
let tokens = 0;
// Calculo de tokens y reconfiguración de mensajes para este caso
for(let i = 0; i < this.convesacion.length; i++){
let actMsg = this.convesacion[i];
tempMensajes.push({role: actMsg.role, content: actMsg.content})
tokens += actMsg.tokens
}
console.log("Enviando: ", mensaje);
tempMensajes.push({role: "user", content: mensaje});
this.convesacion.push({role: "user", content: mensaje});
this.convesacion.push({});
$(document).trigger("precarga:inicio", mensaje);
let self = this;
fetch(this.endpointChat, {
method: "POST",
body: JSON.stringify({
messages: tempMensajes,
token: this.token,
config: this.config
}),
}).then(response => ({
rb: response.body,
rstat: response.status
})).then(({rb, rstat}) => {
if(!rb){
return false;
}
const reader = rb.getReader();
return new ReadableStream({
start(controller) {
function push() {
reader.read().then(({done, value}) => {
if(done){
controller.close();
if(rstat==200){
$(document).trigger("precarga:fin");
localStorage.setItem("convesacion", JSON.stringify(self.convesacion))
console.log("terminado", self.convesacion[self.convesacion.length-1].content)
}else{
self.convesacion.pop()
$(document).trigger("enviar:error", {mensaje: mensaje})
}
return
}
let elements = new TextDecoder("utf-8").decode(value).split("}{");
let elLen = elements.length;
for(let i in elements){
let data = elements[i];
if(!data){
continue;
}
data =JSON.parse((i>0?"{":"") + data + (i<elLen-1?"}":""));
if(rstat==200){
if(data.object == "chat.token"){
self.token = data.token;
}else if(data.choices[0].hasOwnProperty("delta")){
let temp = data.choices[0].delta;
let key = Object.keys(temp)[0];
let elActual = self.convesacion[self.convesacion.length-1]
if(!elActual.hasOwnProperty(key)){elActual[key]="";}
elActual[key] += temp[key]
if(elActual.hasOwnProperty("content") && temp.content){
$(document).trigger("precarga:mensaje", temp.content);
}
}
}else{
$(document).trigger("precarga:error", {status: rstat, mensaje: data.detail});
}
}
push();
})
}
push();
},
});
}).then(data => {
}).catch(err =>{
console.log('Solicitud fallida', err)
});
}
}
|