Skip to content

Commit 39e4e76

Browse files
committed
feat: allow setting global gptscript variables
Signed-off-by: Donnie Adams <[email protected]>
1 parent 4341fa1 commit 39e4e76

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ Additionally, you need the `gptscript` binary. You can install it on your system
1818

1919
## GPTScript
2020

21-
The GPTScript instance allows the caller to run gptscript files, tools, and other operations (see below). There are currently no options for this instance, so calling `NewGPTScript()` is all you need. Although, the intention is that a single GPTScript instance is all you need for the life of your application, you should call `Close()` on the instance when you are done.
21+
The GPTScript instance allows the caller to run gptscript files, tools, and other operations (see below). Note that the intention is that a single GPTScript instance is all you need for the life of your application, you should call `Close()` on the instance when you are done.
2222

23-
## Options
23+
## Global Options
24+
25+
When creating a `GTPScript` instance, you can pass the following global options. These options are also available as run `Options`. Anything specified as a run option will take precedence over the global option.
26+
27+
- `APIKey`: Specify an OpenAI API key for authenticating requests
28+
- `BaseURL`: A base URL for an OpenAI compatible API (the default is `https://api.openai.com/v1`)
29+
- `DefaultModel`: The default model to use for OpenAI requests
30+
31+
## Run Options
2432

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

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

gptscript.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ type gptscript struct {
4444
url string
4545
}
4646

47-
func NewGPTScript() (GPTScript, error) {
47+
func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
4848
lock.Lock()
4949
defer lock.Unlock()
5050
gptscriptCount++
5151

52-
if serverProcessCancel == nil && os.Getenv("GPTSCRIPT_DISABLE_SERVER") != "true" {
52+
if serverURL == "" {
5353
serverURL = os.Getenv("GPTSCRIPT_URL")
54+
}
55+
56+
if serverProcessCancel == nil && os.Getenv("GPTSCRIPT_DISABLE_SERVER") != "true" {
5457
if serverURL == "" {
5558
l, err := net.Listen("tcp", "127.0.0.1:0")
5659
if err != nil {
@@ -68,7 +71,7 @@ func NewGPTScript() (GPTScript, error) {
6871
ctx, cancel := context.WithCancel(context.Background())
6972

7073
in, _ := io.Pipe()
71-
serverProcess = exec.CommandContext(ctx, getCommand(), "--listen-address", serverURL, "sdkserver")
74+
serverProcess = exec.CommandContext(ctx, getCommand(), append(opts.toArgs(), "--listen-address", serverURL, "sdkserver")...)
7275
serverProcess.Stdin = in
7376

7477
serverProcessCancel = func() {

gptscript_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
2020
}
2121

2222
var err error
23-
g, err = NewGPTScript()
23+
g, err = NewGPTScript(GlobalOptions{})
2424
if err != nil {
2525
panic(fmt.Sprintf("error creating gptscript: %s", err))
2626
}
@@ -31,7 +31,7 @@ func TestMain(m *testing.M) {
3131
}
3232

3333
func TestCreateAnotherGPTScript(t *testing.T) {
34-
g, err := NewGPTScript()
34+
g, err := NewGPTScript(GlobalOptions{})
3535
if err != nil {
3636
t.Errorf("error creating gptscript: %s", err)
3737
}
@@ -107,7 +107,9 @@ func TestAbortRun(t *testing.T) {
107107
func TestSimpleEvaluate(t *testing.T) {
108108
tool := ToolDef{Instructions: "What is the capital of the united states?"}
109109

110-
run, err := g.Evaluate(context.Background(), Options{}, tool)
110+
run, err := g.Evaluate(context.Background(), Options{
111+
GlobalOptions: GlobalOptions{},
112+
}, tool)
111113
if err != nil {
112114
t.Errorf("Error executing tool: %v", err)
113115
}

opts.go

+25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
package gptscript
22

3+
// GlobalOptions allows specification of settings that are used for every call made.
4+
// These options can be overridden by the corresponding Options.
5+
type GlobalOptions struct {
6+
OpenAIAPIKey string `json:"APIKey"`
7+
OpenAIBaseURL string `json:"BaseURL"`
8+
DefaultModel string `json:"DefaultModel"`
9+
}
10+
11+
func (g GlobalOptions) toArgs() []string {
12+
var args []string
13+
if g.OpenAIAPIKey != "" {
14+
args = append(args, "--openai-api-key", g.OpenAIAPIKey)
15+
}
16+
if g.OpenAIBaseURL != "" {
17+
args = append(args, "--openai-base-url", g.OpenAIBaseURL)
18+
}
19+
if g.DefaultModel != "" {
20+
args = append(args, "--default-model", g.DefaultModel)
21+
}
22+
23+
return args
24+
}
25+
326
// Options represents options for the gptscript tool or file.
427
type Options struct {
28+
GlobalOptions `json:",inline"`
29+
530
Confirm bool `json:"confirm"`
631
Input string `json:"input"`
732
DisableCache bool `json:"disableCache"`

0 commit comments

Comments
 (0)