Skip to content

Commit 1c7120e

Browse files
authored
Merge pull request #53 from thedadams/global-replace-env
feat: add ability to set global environment variables
2 parents abe1a57 + 1ecc7d1 commit 1c7120e

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ instance when you are done.
3838
## Global Options
3939

4040
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.
41+
run `Options`. Except `Env`, anything specified as a run option will take precedence over the global
42+
option. Any `env` provided in the run options are appended.
4243

4344
- `APIKey`: Specify an OpenAI API key for authenticating requests
4445
- `BaseURL`: A base URL for an OpenAI compatible API (the default is `https://api.openai.com/v1`)
4546
- `DefaultModel`: The default model to use for OpenAI requests
47+
- `Env`: Replace the system's environment variables with these in the for `KEY=VAL`
4648

4749
## Run Options
4850

src/gptscript.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@ export interface GlobalOpts {
88
APIKey?: string
99
BaseURL?: string
1010
DefaultModel?: string
11+
Env?: string[]
1112
}
1213

13-
function globalOptsToArgs(opts?: GlobalOpts): string[] {
14-
const args: string[] = []
14+
function globalOptsToEnv(env: NodeJS.ProcessEnv, opts?: GlobalOpts) {
1515
if (!opts) {
16-
return args
16+
return
1717
}
1818

1919
if (opts.APIKey) {
20-
args.push("--openai-api-key", opts.APIKey)
20+
env["OPENAI_API_KEY"] = opts.APIKey
2121
}
2222
if (opts.BaseURL) {
23-
args.push("--openai-base-url", opts.BaseURL)
23+
env["OPENAI_BASE_URL"] = opts.BaseURL
2424
}
2525
if (opts.DefaultModel) {
26-
args.push("--default-model", opts.DefaultModel)
26+
env["GPTSCRIPT_DEFAULT_MODEL"] = opts.DefaultModel
2727
}
28-
29-
return args
3028
}
3129

3230
export interface RunOpts {
@@ -83,10 +81,23 @@ export class GPTScript {
8381
GPTScript.serverURL = "http://" + u.hostname + ":" + String((s.address() as net.AddressInfo).port)
8482
srv.close()
8583

86-
const args = globalOptsToArgs(opts)
87-
args.push("--listen-address", GPTScript.serverURL.replace("http://", ""), "sdkserver")
88-
GPTScript.serverProcess = child_process.spawn(getCmdPath(), args, {
89-
env: process.env,
84+
let env = process.env
85+
if (opts && opts.Env) {
86+
env = {}
87+
for (const v of opts.Env) {
88+
const equalIndex = v.indexOf("=")
89+
if (equalIndex === -1) {
90+
env[v] = ""
91+
} else {
92+
env[v.substring(0, equalIndex)] = v.substring(equalIndex + 1)
93+
}
94+
}
95+
}
96+
97+
globalOptsToEnv(env, opts)
98+
99+
GPTScript.serverProcess = child_process.spawn(getCmdPath(), ["--listen-address", GPTScript.serverURL.replace("http://", ""), "sdkserver"], {
100+
env: env,
90101
stdio: ["pipe"]
91102
})
92103

tests/gptscript.test.ts

+1-1
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({APIKey: process.env.GPTSCRIPT_API_KEY!})
15+
g = new gptscript.GPTScript({APIKey: process.env.OPENAI_API_KEY})
1616
})
1717
afterAll(() => {
1818
g.close()

0 commit comments

Comments
 (0)