Skip to content

Commit 4264040

Browse files
authored
Merge pull request #29 from thedadams/global-replace-env
feat: add the option to set the environment at the global level
2 parents dea2702 + 29cd40d commit 4264040

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ When creating a `GTPScript` instance, you can pass the following global options.
2727
- `APIKey`: Specify an OpenAI API key for authenticating requests
2828
- `BaseURL`: A base URL for an OpenAI compatible API (the default is `https://api.openai.com/v1`)
2929
- `DefaultModel`: The default model to use for OpenAI requests
30+
- `Env`: Supply the environment variables. Supplying anything here means that nothing from the environment is used. The default is `os.Environ()`. Supplying `Env` at the run/evaluate level will be treated as "additional."
3031

3132
## Run Options
3233

@@ -43,7 +44,6 @@ As noted above, the Global Options are also available to specify here. These opt
4344
- `chatState`: The chat state to continue, or null to start a new chat and return the state
4445
- `confirm`: Prompt before running potentially dangerous commands
4546
- `prompt`: Allow prompting of the user
46-
- `env`: Extra environment variables to pass to the script in the form `KEY=VAL`
4747

4848
## Functions
4949

gptscript.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
7474

7575
in, _ := io.Pipe()
7676
serverProcess = exec.CommandContext(ctx, getCommand(), "--listen-address", serverURL, "sdkserver")
77-
serverProcess.Env = append(os.Environ(), opts.toEnv()...)
77+
if opts.Env == nil {
78+
opts.Env = os.Environ()
79+
}
80+
serverProcess.Env = append(opts.Env[:], opts.toEnv()...)
7881
serverProcess.Stdin = in
7982

8083
serverProcessCancel = func() {

opts.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package gptscript
33
// GlobalOptions allows specification of settings that are used for every call made.
44
// These options can be overridden by the corresponding Options.
55
type GlobalOptions struct {
6-
OpenAIAPIKey string `json:"APIKey"`
7-
OpenAIBaseURL string `json:"BaseURL"`
8-
DefaultModel string `json:"DefaultModel"`
6+
OpenAIAPIKey string `json:"APIKey"`
7+
OpenAIBaseURL string `json:"BaseURL"`
8+
DefaultModel string `json:"DefaultModel"`
9+
Env []string `json:"env"`
910
}
1011

1112
func (g GlobalOptions) toEnv() []string {
@@ -27,14 +28,13 @@ func (g GlobalOptions) toEnv() []string {
2728
type Options struct {
2829
GlobalOptions `json:",inline"`
2930

30-
Confirm bool `json:"confirm"`
31-
Input string `json:"input"`
32-
DisableCache bool `json:"disableCache"`
33-
CacheDir string `json:"cacheDir"`
34-
SubTool string `json:"subTool"`
35-
Workspace string `json:"workspace"`
36-
ChatState string `json:"chatState"`
37-
IncludeEvents bool `json:"includeEvents"`
38-
Prompt bool `json:"prompt"`
39-
Env []string `json:"env"`
31+
Confirm bool `json:"confirm"`
32+
Input string `json:"input"`
33+
DisableCache bool `json:"disableCache"`
34+
CacheDir string `json:"cacheDir"`
35+
SubTool string `json:"subTool"`
36+
Workspace string `json:"workspace"`
37+
ChatState string `json:"chatState"`
38+
IncludeEvents bool `json:"includeEvents"`
39+
Prompt bool `json:"prompt"`
4040
}

run_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestRestartingErrorRun(t *testing.T) {
2020
Instructions: instructions,
2121
}
2222

23-
run, err := g.Evaluate(context.Background(), Options{Env: []string{"EXIT_CODE=1"}, IncludeEvents: true}, tool, contextTool)
23+
run, err := g.Evaluate(context.Background(), Options{GlobalOptions: GlobalOptions{Env: []string{"EXIT_CODE=1"}}, IncludeEvents: true}, tool, contextTool)
2424
if err != nil {
2525
t.Errorf("Error executing tool: %v", err)
2626
}

0 commit comments

Comments
 (0)