-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample-tool-chat-stream.js
49 lines (42 loc) · 1.43 KB
/
example-tool-chat-stream.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 * as gptscript from "../dist/gptscript.js";
import * as readline from "readline";
import {stdin as input, stdout as output} from 'node:process';
const client = new gptscript.Client(process.env.GPTSCRIPT_URL)
const rl = readline.createInterface({input, output});
const t = {
chat: true,
tools: ["sys.chat.finish"],
instructions: "Say hello and start a chat session.",
};
let r = client.evaluate(t, {
disableCache: true,
});
(async () => {
r.on(gptscript.RunEventType.Event, (data) => {
console.log(JSON.stringify(data))
})
console.log(await r.text());
const recursiveAsyncReadLine = function () {
rl.question(`>> `, async function (answer) {
if (answer === "") //we need some base case, for recursion
return rl.close(); //closing RL and returning from function.
try {
r = r.nextChat(answer);
} catch (e) {
console.error(e);
}
console.log(await r.text());
console.log(r.state);
if (r.state === gptscript.RunState.Finished) {
console.log("The conversation is finished. Goodbye!");
return rl.close();
}
recursiveAsyncReadLine(); //Calling this function again to ask new question
});
};
recursiveAsyncReadLine();
})()
rl.on("close", function () {
console.log("\nBYE BYE !!!");
process.exit(0);
});