Skip to content

Commit

Permalink
feat: add support for the SDK server
Browse files Browse the repository at this point in the history
With this change, the gptscript SDK server will be fork/exec-ed instead
of fork/exec-ing for every gptscript command. This produces enhancements
with daemons in gptscript.

This change also intentionally removes support for browser-based
applications.
  • Loading branch information
thedadams committed May 25, 2024
1 parent 525ae1d commit 332d517
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 1,099 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ running `npm install`.

## Usage

To use the module and run gptscripts, you need to first set the OPENAI_API_KEY environment variable to your OpenAI API
key.
To use the module and run gptscripts, you need to first set the `OPENAI_API_KEY` environment variable to your OpenAI API
key. You can also set the `GPTSCRIPT_BIN` environment variable to change the execution of the gptscripts.

To ensure it is working properly, you can run the following command:

Expand All @@ -31,11 +31,10 @@ You will see "Hello, World!" in the output of the command.

## Client

There are currently a couple "global" options, and the client helps to manage those. A client without any options is
likely what you want. However, here are the current global options:

- `gptscriptURL`: The URL (including `http(s)://) of an "SDK server" to use instead of the fork/exec model.
- `gptscriptBin`: The path to a `gptscript` binary to use instead of the bundled one.
The client allows the caller to run gptscript files, tools, and other operations (see below). There are currently no
options for this singleton client, so `await gptscript.Client.init()` is all you need. Although, the intention is that a
single client is all you need for the life of your application, you should call `close()` on the client when you are
done.

## Options

Expand All @@ -45,7 +44,6 @@ None of the options is required, and the defaults will reduce the number of call
- `disableCache`: Enable or disable caching, default (true)
- `cacheDir`: Specify the cache directory
- `quiet`: No output logging
- `chdir`: Change current working directory
- `subTool`: Use tool of this name, not the first tool
- `workspace`: Directory to use for the workspace, if specified it will not be deleted on exit

Expand All @@ -61,9 +59,10 @@ Lists all the available built-in tools.
const gptscript = require('@gptscript-ai/gptscript');

async function listTools() {
const client = new gptscript.Client();
const client = await gptscript.Client.init();
const tools = await client.listTools();
console.log(tools);
client.close()
}
```

Expand All @@ -78,12 +77,13 @@ const gptscript = require('@gptscript-ai/gptscript');

async function listModels() {
let models = [];
const client = await gptscript.Client.init();
try {
const client = new gptscript.Client();
models = await client.listModels();
} catch (error) {
console.error(error);
}
client.close()
}
```

Expand All @@ -97,12 +97,13 @@ Get the first of the current `gptscript` binary being used for the calls.
const gptscript = require('@gptscript-ai/gptscript');

async function version() {
const client = await gptscript.Client.init();
try {
const client = new gptscript.Client();
console.log(await client.version());
} catch (error) {
console.error(error);
}
client.close()
}
```

Expand All @@ -118,13 +119,14 @@ const t = {
instructions: "Who was the president of the united states in 1928?"
};

const client = await gptscript.Client.init();
try {
const client = new gptscript.Client();
const run = client.evaluate(t);
console.log(await run.text());
} catch (error) {
console.error(error);
}
client.close();
```

### run
Expand All @@ -140,13 +142,14 @@ const opts = {
};

async function execFile() {
const client = await gptscript.Client.init();
try {
const client = new gptscript.Client();
const run = client.run('./hello.gpt', opts);
console.log(await run.text());
} catch (e) {
console.error(e);
}
client.close();
}
```

Expand Down Expand Up @@ -178,8 +181,8 @@ const opts = {
};

async function streamExecFileWithEvents() {
const client = await gptscript.Client.init();
try {
const client = new gptscript.Client();
const run = client.run('./test.gpt', opts);

run.on(gptscript.RunEventType.Event, data => {
Expand All @@ -190,6 +193,7 @@ async function streamExecFileWithEvents() {
} catch (e) {
console.error(e);
}
client.close();
}
```

Expand Down Expand Up @@ -218,7 +222,7 @@ const t = {
};

async function streamExecFileWithEvents() {
const client = new gptscript.Client();
const client = await gptscript.Client.init();
let run = client.evaluate(t, opts);
try {
// Wait for the initial run to complete.
Expand All @@ -238,6 +242,7 @@ async function streamExecFileWithEvents() {
console.error(e);
}

client.close();

// The state here should either be RunState.Finished (on success) or RunState.Error (on error).
console.log(run.state)
Expand Down
18 changes: 0 additions & 18 deletions babel.test.cjs

This file was deleted.

Loading

0 comments on commit 332d517

Please sign in to comment.