├── README.md ├── example.js ├── gpt.d.ts ├── gpt.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # gpt4all-wrapper-js 2 | 3 | This just provides a simple js wrapper around [gpt4all](https://github.com/nomic-ai/gpt4all/). 4 | 5 | ## Installation and Usage 6 | 7 | You need to get gpt4all and put the gpt4.js and example.js in the chat folder. 8 | 9 | ``` 10 | import GPT4 from './gpt.js'; 11 | 12 | let GPT = new GPT4('./gpt4all-lora-quantized-OSX-m1'); 13 | GPT.on('ready',async ()=>{ 14 | console.log(await GPT.ask("What is the capital of Korea?")); 15 | console.log(await GPT.ask("Are you an AI?")); 16 | }); 17 | ``` 18 | 19 | ## License 20 | 21 | MIT 22 | 23 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | import GPT4 from './gpt.js'; 2 | 3 | let GPT = new GPT4('./gpt4all-lora-quantized-OSX-m1'); 4 | GPT.on('ready',async ()=>{ 5 | console.log(await GPT.ask("What is the capital of Korea?")); 6 | console.log(await GPT.ask("Are you an AI?")); 7 | }); 8 | -------------------------------------------------------------------------------- /gpt.d.ts: -------------------------------------------------------------------------------- 1 | type possibleEvents = 'ready' | 'data' | 'exit' 2 | 3 | declare class GPT4ALL { 4 | constructor(app: string) 5 | ask(msg: string): Promise 6 | } 7 | 8 | export default GPT4ALL; 9 | -------------------------------------------------------------------------------- /gpt.js: -------------------------------------------------------------------------------- 1 | const { spawn } = require('child_process'); 2 | const { EventEmitter } = require("events"); 3 | 4 | class GPT4ALL extends EventEmitter { 5 | status = 0; 6 | buffer = ""; 7 | constructor(app) { 8 | super(); 9 | this.chat = spawn(app); 10 | this.chat.stdout.on('data', (data) => { 11 | let str = data.toString(); 12 | if (str.includes("\x1b[33m")) { 13 | this.status = 1; 14 | this.emit("ready"); 15 | this.buffer = ""; 16 | } else if (str.includes(">")) { 17 | this.emit("data", this.buffer); 18 | this.buffer = ""; 19 | } else { 20 | this.buffer += str; 21 | } 22 | }); 23 | } 24 | async ask(msg) { 25 | if (!this.status) return null; 26 | this.chat.stdin.write(`${msg}\n`); 27 | return new Promise(resolve => this.once("data", resolve)); 28 | } 29 | } 30 | module.exports = GPT4ALL; 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gpt4all-wrapper-js", 3 | "version": "1.0.0", 4 | "type":"module", 5 | "description": "", 6 | "main": "gpt.js" 7 | } 8 | --------------------------------------------------------------------------------