Skip to content

Commit

Permalink
feat: allow bypassing of built-in proxy
Browse files Browse the repository at this point in the history
If specific proxy environment variables are set, the built-in proxy will
be bypassed.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Nov 6, 2024
1 parent 53a6ae0 commit 266bc58
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,9 @@ func invalidArgument(input string, err error) string {
return fmt.Sprintf("Failed to parse arguments %s: %v", input, err)
}

func SysModelProviderCredential(ctx context.Context, _ []string, _ string, _ chan<- string) (string, error) {
func SysModelProviderCredential(ctx context.Context, env []string, _ string, _ chan<- string) (string, error) {
engineContext, _ := engine.FromContext(ctx)
auth, url, err := engineContext.Engine.Model.ProxyInfo()
auth, url, err := engineContext.Engine.Model.ProxyInfo(env)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type Model interface {
Call(ctx context.Context, messageRequest types.CompletionRequest, env []string, status chan<- types.CompletionStatus) (*types.CompletionMessage, error)
ProxyInfo() (string, string, error)
ProxyInfo([]string) (string, string, error)
}

type RuntimeManager interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gptscript/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (n noopModel) Call(_ context.Context, _ types.CompletionRequest, _ []string
return nil, errors.New("unsupported")
}

func (n noopModel) ProxyInfo() (string, string, error) {
func (n noopModel) ProxyInfo([]string) (string, string, error) {
return "", "", errors.New("unsupported")
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/llm/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ import (
"github.com/gptscript-ai/gptscript/pkg/openai"
)

func (r *Registry) ProxyInfo() (string, string, error) {
func (r *Registry) ProxyInfo(env []string) (string, string, error) {
var proxyURL, proxyToken string
for _, e := range env {
if url, ok := strings.CutPrefix(e, "GPTSCRIPT_MODEL_PROVIDER_PROXY_URL="); ok {
proxyURL = url
} else if token, ok := strings.CutPrefix(e, "GPTSCRIPT_MODEL_PROVIDER_PROXY_TOKEN="); ok {
proxyToken = token
}
}

if proxyToken != "" && proxyURL != "" {
return proxyToken, proxyURL, nil
}

r.proxyLock.Lock()
defer r.proxyLock.Unlock()

Expand Down Expand Up @@ -77,7 +90,7 @@ func (r *Registry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

auth, targetURL := oai.ProxyInfo()
auth, targetURL := oai.ProxyInfo(nil)
if targetURL == "" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func NewClient(ctx context.Context, credStore credentials.CredentialStore, opts
}, nil
}

func (c *Client) ProxyInfo() (token, urlBase string) {
func (c *Client) ProxyInfo([]string) (token, urlBase string) {
if c.invalidAuth {
return "", ""
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/tester/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Result struct {
Err error
}

func (c *Client) ProxyInfo() (string, string, error) {
func (c *Client) ProxyInfo([]string) (string, string, error) {
return "test-auth", "test-url", nil
}

Expand Down

0 comments on commit 266bc58

Please sign in to comment.