@@ -52,8 +52,8 @@ Lists all the available built-in tools.
5252const gptscript = require (' @gptscript-ai/gptscript' );
5353
5454async function listTools () {
55- const tools = await gptscript .listTools ();
56- console .log (tools);
55+ const tools = await gptscript .listTools ();
56+ console .log (tools);
5757}
5858```
5959
@@ -67,12 +67,12 @@ Lists all the available models, returns a list.
6767const gptscript = require (' @gptscript-ai/gptscript' );
6868
6969async function listModels () {
70- let models = [];
71- try {
72- models = await gptscript .listModels ();
73- } catch (error) {
74- console .error (error);
75- }
70+ let models = [];
71+ try {
72+ models = await gptscript .listModels ();
73+ } catch (error) {
74+ console .error (error);
75+ }
7676}
7777```
7878
@@ -86,11 +86,11 @@ Get the first of the current `gptscript` binary being used for the calls.
8686const gptscript = require (' @gptscript-ai/gptscript' );
8787
8888async function version () {
89- try {
90- console .log (await gptscript .version ());
91- } catch (error) {
92- console .error (error);
93- }
89+ try {
90+ console .log (await gptscript .version ());
91+ } catch (error) {
92+ console .error (error);
93+ }
9494}
9595```
9696
@@ -103,14 +103,14 @@ representing the contents of a gptscript file.
103103const gptscript = require (' @gptscript-ai/gptscript' );
104104
105105const t = {
106- instructions: " Who was the president of the united states in 1928?"
106+ instructions: " Who was the president of the united states in 1928?"
107107};
108108
109109try {
110- const run = gptscript .evaluate (t);
111- console .log (await run .text ());
110+ const run = gptscript .evaluate (t);
111+ console .log (await run .text ());
112112} catch (error) {
113- console .error (error);
113+ console .error (error);
114114}
115115```
116116
@@ -122,17 +122,17 @@ Executes a GPT script file with optional input and arguments. The script is rela
122122const gptscript = require (' @gptscript-ai/gptscript' );
123123
124124const opts = {
125- disableCache: true ,
126- input: " --input World"
125+ disableCache: true ,
126+ input: " --input World"
127127};
128128
129129async function execFile () {
130- try {
131- const run = gptscript .run (' ./hello.gpt' , opts);
132- console .log (await run .text ());
133- } catch (e) {
134- console .error (e);
135- }
130+ try {
131+ const run = gptscript .run (' ./hello.gpt' , opts);
132+ console .log (await run .text ());
133+ } catch (e) {
134+ console .error (e);
135+ }
136136}
137137```
138138
@@ -159,22 +159,72 @@ Subscribing to `RunEventType.Event` gets you all events.
159159const gptscript = require (' @gptscript-ai/gptscript' );
160160
161161const opts = {
162- disableCache: true ,
163- input: " --testin how high is that there mouse?"
162+ disableCache: true ,
163+ input: " --testin how high is that there mouse?"
164164};
165165
166166async function streamExecFileWithEvents () {
167- try {
168- const run = gptscript .run (' ./test.gpt' , opts);
167+ try {
168+ const run = gptscript .run (' ./test.gpt' , opts);
169169
170- run .on (gptscript .RunEventType .Event , data => {
171- console .log (` event: ${ data} ` );
172- });
170+ run .on (gptscript .RunEventType .Event , data => {
171+ console .log (` event: ${ data} ` );
172+ });
173173
174- await run .text ();
175- } catch (e) {
176- console .error (e);
177- }
174+ await run .text ();
175+ } catch (e) {
176+ console .error (e);
177+ }
178+ }
179+ ```
180+
181+ ### Chat support
182+
183+ For tools that support chat, you can use the ` nextChat ` method on the run object to continue the chat. This method takes
184+ a string representing the next chat message from the user.
185+
186+ If the chat can/should continue, then the ` Run ` 's state will be ` RunState.Continue ` . Note that calling ` nextChat ` on
187+ a ` Run ` object is an error. Each call to ` nextChat ` will return a new ` Run ` instance, so, the call can keep track of the
188+ chat ` Run ` s, if desired.
189+
190+ Here is an example flow for chat.
191+
192+ ``` javascript
193+ const gptscript = require (' @gptscript-ai/gptscript' );
194+
195+ const opts = {
196+ disableCache: true
197+ };
198+
199+ const t = {
200+ chat: true ,
201+ tools: [" sys.chat.finish" ],
202+ instructions: " You are a chat bot. Don't finish the conversation until I say 'bye'."
203+ };
204+
205+ async function streamExecFileWithEvents () {
206+ let run = gptscript .evaluate (t, opts);
207+ try {
208+ // Wait for the initial run to complete.
209+ await run .text ();
210+
211+ while (run .RunState === gptscript .RunState .Continue ) {
212+ // ...Get the next input from the user somehow...
213+
214+ run = run .nextChat (inputFromUser)
215+
216+ // Get the output from gptscript
217+ const output = await run .text ()
218+
219+ // Display the output to the user...
220+ }
221+ } catch (e) {
222+ console .error (e);
223+ }
224+
225+
226+ // The state here should either be RunState.Finished (on success) or RunState.Error (on error).
227+ console .log (run .state )
178228}
179229```
180230
0 commit comments