-
Notifications
You must be signed in to change notification settings - Fork 4
/
chat.js
49 lines (46 loc) · 2.08 KB
/
chat.js
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
import {Ollama} from "ollama";
import * as p from "peer";
import fs from "fs";
import { config, validateConfig } from './config/env.js';
validateConfig();
const host=config.host;
const baseDir=config.baseDir;
const modelV=config.modelV;
const chatDir=baseDir+'/chat';
const limiter=-4;
const ollama=new Ollama({host:'http://'+host+':11434'});
const peerServer=p.PeerServer({port:9000,path:"/chat"});
peerServer.on('connection',(client) => {
const tokens=JSON.parse(fs.readFileSync(baseDir+'/t.json'));
if(client.id && tokens.findIndex(x=>x.t===client.id)>-1) {
let chats=JSON.parse(fs.readFileSync(chatDir+'/chats.json'));
let chatSession=chats[chats.findIndex(x=>x.active===1)];
//
fs.readFile(chatDir+'/session_'+chatSession.id+'.json', (err, content) => {
if(!err) {
let history=JSON.parse(content);
let context=[];
context=history.slice(limiter);
let reply='';
ollama.chat({model:modelV,messages:context,keep_alive:-1,stream:true,options:{verbose:true}}).then(
async(stream)=>{
for await(const chunk of stream) {
reply=reply+chunk.message.content;
client.send({content:chunk.message.content,done:chunk.done});
// save history
if(chunk.done) {
history.push({role:'assistant',content:reply});
fs.writeFileSync(chatDir+'/session_'+chatSession.id+'.json',JSON.stringify(history));
//
if(chatSession.name.split(' ').indexOf('Untitled')>-1) {
chats[chats.findIndex(x=>x.id===chatSession.id)].name=history[0].content;
fs.writeFileSync(chatDir+'/chats.json',JSON.stringify(chats));
}
}
}
}
)
}
});
}
});