Skip to content

Commit

Permalink
fix: set global options as environment variables for security
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Jun 13, 2024
1 parent 39e4e76 commit caca17a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
ctx, cancel := context.WithCancel(context.Background())

in, _ := io.Pipe()
serverProcess = exec.CommandContext(ctx, getCommand(), append(opts.toArgs(), "--listen-address", serverURL, "sdkserver")...)
serverProcess = exec.CommandContext(ctx, getCommand(), "--listen-address", serverURL, "sdkserver")
serverProcess.Env = append(os.Environ(), opts.toEnv()...)
serverProcess.Stdin = in

serverProcessCancel = func() {
Expand Down
2 changes: 1 addition & 1 deletion gptscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
}

var err error
g, err = NewGPTScript(GlobalOptions{})
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY")})
if err != nil {
panic(fmt.Sprintf("error creating gptscript: %s", err))
}
Expand Down
8 changes: 4 additions & 4 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ type GlobalOptions struct {
DefaultModel string `json:"DefaultModel"`
}

func (g GlobalOptions) toArgs() []string {
func (g GlobalOptions) toEnv() []string {
var args []string
if g.OpenAIAPIKey != "" {
args = append(args, "--openai-api-key", g.OpenAIAPIKey)
args = append(args, "OPENAI_API_KEY="+g.OpenAIAPIKey)
}
if g.OpenAIBaseURL != "" {
args = append(args, "--openai-base-url", g.OpenAIBaseURL)
args = append(args, "OPENAI_BASE_URL="+g.OpenAIBaseURL)
}
if g.DefaultModel != "" {
args = append(args, "--default-model", g.DefaultModel)
args = append(args, "GPTSCRIPT_DEFAULT_MODEL="+g.DefaultModel)
}

return args
Expand Down

0 comments on commit caca17a

Please sign in to comment.