|
| 1 | +import * as gptscript from "../dist/gptscript.js"; |
| 2 | +import * as readline from "readline"; |
| 3 | +import {stdin as input, stdout as output} from 'node:process'; |
| 4 | + |
| 5 | +const client = new gptscript.Client(process.env.GPTSCRIPT_URL) |
| 6 | + |
| 7 | +const rl = readline.createInterface({input, output}); |
| 8 | + |
| 9 | +const t = { |
| 10 | + chat: true, |
| 11 | + tools: ["sys.chat.finish"], |
| 12 | + instructions: "Say hello and start a chat session.", |
| 13 | +}; |
| 14 | + |
| 15 | +let r = client.evaluate(t, { |
| 16 | + disableCache: true, |
| 17 | +}); |
| 18 | + |
| 19 | +(async () => { |
| 20 | + r.on(gptscript.RunEventType.Event, (data) => { |
| 21 | + console.log(JSON.stringify(data)) |
| 22 | + }) |
| 23 | + console.log(await r.text()); |
| 24 | + const recursiveAsyncReadLine = function () { |
| 25 | + rl.question(`>> `, async function (answer) { |
| 26 | + if (answer === "") //we need some base case, for recursion |
| 27 | + return rl.close(); //closing RL and returning from function. |
| 28 | + try { |
| 29 | + r = r.nextChat(answer); |
| 30 | + } catch (e) { |
| 31 | + console.error(e); |
| 32 | + } |
| 33 | + console.log(await r.text()); |
| 34 | + console.log(r.state); |
| 35 | + if (r.state === gptscript.RunState.Finished) { |
| 36 | + console.log("The conversation is finished. Goodbye!"); |
| 37 | + return rl.close(); |
| 38 | + } |
| 39 | + recursiveAsyncReadLine(); //Calling this function again to ask new question |
| 40 | + }); |
| 41 | + }; |
| 42 | + |
| 43 | + recursiveAsyncReadLine(); |
| 44 | +})() |
| 45 | + |
| 46 | +rl.on("close", function () { |
| 47 | + console.log("\nBYE BYE !!!"); |
| 48 | + process.exit(0); |
| 49 | +}); |
0 commit comments