Skip to content

Commit 689611d

Browse files
committed
feat: allow setting global gptscript variables
1 parent 0355a49 commit 689611d

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,24 @@ You will see "Hello, World!" in the output of the command.
3131

3232
## GPTScript
3333

34-
The GPTScript instance allows the caller to run gptscript files, tools, and other operations (see below). There are
35-
currently no options for this class, so `new gptscript.GPTScript()` is all you need. Although, the intention is that a
36-
single instance is all you need for the life of your application, you should call `close()` on the instance when you
37-
are done.
34+
The GPTScript instance allows the caller to run gptscript files, tools, and other operations (see below). Note that the
35+
intention is that a single instance is all you need for the life of your application, you should call `close()` on the
36+
instance when you are done.
3837

39-
## Options
38+
## Global Options
39+
40+
When creating a `GTPScript` instance, you can pass the following global options. These options are also available as
41+
run `Options`. Anything specified as a run option will take precedence over the global option.
42+
43+
- `APIKey`: Specify an OpenAI API key for authenticating requests
44+
- `BaseURL`: A base URL for an OpenAI compatible API (the default is `https://api.openai.com/v1`)
45+
- `DefaultModel`: The default model to use for OpenAI requests
46+
47+
## Run Options
4048

4149
These are optional options that can be passed to the various `exec` functions.
4250
None of the options is required, and the defaults will reduce the number of calls made to the Model API.
51+
As noted above, the Global Options are also available to specify here. These options would take precedence.
4352

4453
- `cache`: Enable or disable caching. Default (true).
4554
- `cacheDir`: Specify the cache directory.

src/gptscript.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import child_process from "child_process"
44
import {fileURLToPath} from "url"
55
import net from "net"
66

7+
export interface GlobalOpts {
8+
APIKey?: string
9+
BaseURL?: string
10+
DefaultModel?: string
11+
}
12+
13+
function globalOptsToArgs(opts?: GlobalOpts): string[] {
14+
const args: string[] = []
15+
if (!opts) {
16+
return args
17+
}
18+
19+
if (opts.APIKey) {
20+
args.push("--openai-api-key", opts.APIKey)
21+
}
22+
if (opts.BaseURL) {
23+
args.push("--openai-base-url", opts.BaseURL)
24+
}
25+
if (opts.DefaultModel) {
26+
args.push("--default-model", opts.DefaultModel)
27+
}
28+
29+
return args
30+
}
31+
732
export interface RunOpts {
833
input?: string
934
disableCache?: boolean
@@ -15,6 +40,10 @@ export interface RunOpts {
1540
confirm?: boolean
1641
prompt?: boolean
1742
env?: string[]
43+
44+
APIKey?: string
45+
BaseURL?: string
46+
DefaultModel?: string
1847
}
1948

2049
export enum RunEventType {
@@ -40,7 +69,7 @@ export class GPTScript {
4069

4170
private ready: boolean
4271

43-
constructor() {
72+
constructor(opts?: GlobalOpts) {
4473
this.ready = false
4574
GPTScript.instanceCount++
4675
if (!GPTScript.serverURL) {
@@ -54,7 +83,9 @@ export class GPTScript {
5483
GPTScript.serverURL = "http://" + u.hostname + ":" + String((s.address() as net.AddressInfo).port)
5584
srv.close()
5685

57-
GPTScript.serverProcess = child_process.spawn(getCmdPath(), ["--listen-address", GPTScript.serverURL.replace("http://", ""), "sdkserver"], {
86+
const args = globalOptsToArgs(opts)
87+
args.push("--listen-address", GPTScript.serverURL.replace("http://", ""), "sdkserver")
88+
GPTScript.serverProcess = child_process.spawn(getCmdPath(), args, {
5889
env: process.env,
5990
stdio: ["pipe"]
6091
})

tests/gptscript.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("gptscript module", () => {
1212
throw new Error("neither OPENAI_API_KEY nor GPTSCRIPT_URL is set")
1313
}
1414

15-
g = new gptscript.GPTScript()
15+
g = new gptscript.GPTScript({APIKey: process.env.GPTSCRIPT_API_KEY!})
1616
})
1717
afterAll(() => {
1818
g.close()

0 commit comments

Comments
 (0)