From 29cd40d53263c8bd87c52d29e20d20c44224e417 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Thu, 13 Jun 2024 13:50:09 -0400 Subject: [PATCH] feat: add the option to set the environment at the global level Setting the environment at the global level will replace the environment variables from the system. Not specifying any environment will use os.Environ() Signed-off-by: Donnie Adams --- README.md | 2 +- gptscript.go | 5 ++++- opts.go | 26 +++++++++++++------------- run_test.go | 2 +- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8be6a89..2d9485e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ When creating a `GTPScript` instance, you can pass the following global options. - `APIKey`: Specify an OpenAI API key for authenticating requests - `BaseURL`: A base URL for an OpenAI compatible API (the default is `https://api.openai.com/v1`) - `DefaultModel`: The default model to use for OpenAI requests +- `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." ## Run Options @@ -43,7 +44,6 @@ As noted above, the Global Options are also available to specify here. These opt - `chatState`: The chat state to continue, or null to start a new chat and return the state - `confirm`: Prompt before running potentially dangerous commands - `prompt`: Allow prompting of the user -- `env`: Extra environment variables to pass to the script in the form `KEY=VAL` ## Functions diff --git a/gptscript.go b/gptscript.go index 5722ade..2aea811 100644 --- a/gptscript.go +++ b/gptscript.go @@ -74,7 +74,10 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) { in, _ := io.Pipe() serverProcess = exec.CommandContext(ctx, getCommand(), "--listen-address", serverURL, "sdkserver") - serverProcess.Env = append(os.Environ(), opts.toEnv()...) + if opts.Env == nil { + opts.Env = os.Environ() + } + serverProcess.Env = append(opts.Env[:], opts.toEnv()...) serverProcess.Stdin = in serverProcessCancel = func() { diff --git a/opts.go b/opts.go index 1ce8b3b..03f2cd4 100644 --- a/opts.go +++ b/opts.go @@ -3,9 +3,10 @@ package gptscript // GlobalOptions allows specification of settings that are used for every call made. // These options can be overridden by the corresponding Options. type GlobalOptions struct { - OpenAIAPIKey string `json:"APIKey"` - OpenAIBaseURL string `json:"BaseURL"` - DefaultModel string `json:"DefaultModel"` + OpenAIAPIKey string `json:"APIKey"` + OpenAIBaseURL string `json:"BaseURL"` + DefaultModel string `json:"DefaultModel"` + Env []string `json:"env"` } func (g GlobalOptions) toEnv() []string { @@ -27,14 +28,13 @@ func (g GlobalOptions) toEnv() []string { type Options struct { GlobalOptions `json:",inline"` - Confirm bool `json:"confirm"` - Input string `json:"input"` - DisableCache bool `json:"disableCache"` - CacheDir string `json:"cacheDir"` - SubTool string `json:"subTool"` - Workspace string `json:"workspace"` - ChatState string `json:"chatState"` - IncludeEvents bool `json:"includeEvents"` - Prompt bool `json:"prompt"` - Env []string `json:"env"` + Confirm bool `json:"confirm"` + Input string `json:"input"` + DisableCache bool `json:"disableCache"` + CacheDir string `json:"cacheDir"` + SubTool string `json:"subTool"` + Workspace string `json:"workspace"` + ChatState string `json:"chatState"` + IncludeEvents bool `json:"includeEvents"` + Prompt bool `json:"prompt"` } diff --git a/run_test.go b/run_test.go index 041cc4e..f9014a1 100644 --- a/run_test.go +++ b/run_test.go @@ -20,7 +20,7 @@ func TestRestartingErrorRun(t *testing.T) { Instructions: instructions, } - run, err := g.Evaluate(context.Background(), Options{Env: []string{"EXIT_CODE=1"}, IncludeEvents: true}, tool, contextTool) + run, err := g.Evaluate(context.Background(), Options{GlobalOptions: GlobalOptions{Env: []string{"EXIT_CODE=1"}}, IncludeEvents: true}, tool, contextTool) if err != nil { t.Errorf("Error executing tool: %v", err) }