Skip to content

Commit

Permalink
Merge pull request #649 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
chore: already cached performance improvements
  • Loading branch information
ibuildthecloud authored Jul 22, 2024
2 parents e519494 + 3c5d290 commit 7c5da26
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379
github.com/gptscript-ai/cmd v0.0.0-20240625175447-4250b42feb7d
github.com/gptscript-ai/tui v0.0.0-20240716053605-ecddbcf60eac
github.com/gptscript-ai/tui v0.0.0-20240722014329-d50b5ac5db74
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/valast v1.4.4
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
Expand Down Expand Up @@ -63,7 +63,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240715172623-8176fb20c5cb // indirect
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240722014125-d757d09f606b // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hexops/autogold v1.3.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf037
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
github.com/gptscript-ai/cmd v0.0.0-20240625175447-4250b42feb7d h1:sKf7T7twhGXs6AVbvD9pKDVewykkwSAPwEpmIEQIR/4=
github.com/gptscript-ai/cmd v0.0.0-20240625175447-4250b42feb7d/go.mod h1:DJAo1xTht1LDkNYFNydVjTHd576TC7MlpsVRl3oloVw=
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240715172623-8176fb20c5cb h1:xeSbO4mLYnoTg7diNW0tpxY/0yDSSdgjohMzwE4Za6k=
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240715172623-8176fb20c5cb/go.mod h1:Dh6vYRAiVcyC3ElZIGzTvNF1FxtYwA07BHfSiFKQY7s=
github.com/gptscript-ai/tui v0.0.0-20240716053605-ecddbcf60eac h1:zZ993dp2mx/63JD4THwMeBcn3C8SogcLeQRJUZsMSM4=
github.com/gptscript-ai/tui v0.0.0-20240716053605-ecddbcf60eac/go.mod h1:Ex2xQMzTMfb5UgLz9rctATPps8DnfPeJQh8o/AiQCoE=
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240722014125-d757d09f606b h1:Hxu8oPE43uQ2sZ7P+9yGSX9bXh0RoJfOgvY/SlCwFlM=
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240722014125-d757d09f606b/go.mod h1:Dh6vYRAiVcyC3ElZIGzTvNF1FxtYwA07BHfSiFKQY7s=
github.com/gptscript-ai/tui v0.0.0-20240722014329-d50b5ac5db74 h1:69BENZCN2y4BCxmPjMRp+ZQ47ay4i5gRgREKZatu5oE=
github.com/gptscript-ai/tui v0.0.0-20240722014329-d50b5ac5db74/go.mod h1:sP/9g7+nLq65aGef5F30AEG+Cuu4BwlglUYv1Pzps4Y=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCate
instructions = append(instructions, inputContext.Content)
}
var extraEnv = []string{
strings.TrimSpace(fmt.Sprintf("GPTSCRIPT_CONTEXT=%s", strings.Join(instructions, "\n"))),
strings.TrimSpace("GPTSCRIPT_CONTEXT=" + strings.Join(instructions, "\n")),
}

cmd, stop, err := e.newCommand(ctx.Ctx, extraEnv, tool, input)
Expand Down
4 changes: 4 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func AppendPath(env []string, binPath string) []string {
// Lookup will try to find bin in the PATH in env. It will refer to PATHEXT for Windows support.
// If bin can not be resolved to anything the original bin string is returned.
func Lookup(env []string, bin string) string {
if strings.Contains(bin, string(filepath.Separator)) {
return bin
}

for _, env := range env {
for _, prefix := range []string{"PATH=", "Path="} {
suffix, ok := strings.CutPrefix(env, prefix)
Expand Down
29 changes: 29 additions & 0 deletions pkg/llm/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/gptscript-ai/gptscript/pkg/openai"
"github.com/gptscript-ai/gptscript/pkg/remote"
"github.com/gptscript-ai/gptscript/pkg/types"
)

Expand Down Expand Up @@ -41,11 +42,39 @@ func (r *Registry) ListModels(ctx context.Context, providers ...string) (result
return result, nil
}

func (r *Registry) fastPath(modelName string) Client {
// This is optimization hack to avoid doing List Models
if len(r.clients) != 2 {
return nil
}

_, modelFromProvider := types.SplitToolRef(modelName)
if modelFromProvider != "" {
return nil
}

_, ok := r.clients[0].(*openai.Client)
if !ok {
return nil
}

_, ok = r.clients[1].(*remote.Client)
if !ok {
return nil
}

return r.clients[0]
}

func (r *Registry) Call(ctx context.Context, messageRequest types.CompletionRequest, status chan<- types.CompletionStatus) (*types.CompletionMessage, error) {
if messageRequest.Model == "" {
return nil, fmt.Errorf("model is required")
}

if c := r.fastPath(messageRequest.Model); c != nil {
return c.Call(ctx, messageRequest, status)
}

var errs []error
var oaiClient *openai.Client
for _, client := range r.clients {
Expand Down
19 changes: 18 additions & 1 deletion pkg/loader/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
url2 "net/url"
"path"
"regexp"
"strings"
"time"

Expand All @@ -33,6 +34,14 @@ type cacheValue struct {
Time time.Time
}

func (c *cacheKey) isStatic() bool {
return c.Repo != nil &&
c.Repo.Revision != "" &&
stableRef.MatchString(c.Repo.Revision)
}

var stableRef = regexp.MustCompile("^([a-f0-9]{7,40}$|v[0-9]|[0-9])")

func loadURL(ctx context.Context, cache *cache.Client, base *source, name string) (*source, bool, error) {
var (
repo *types.Repo
Expand All @@ -47,9 +56,17 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string
cachedValue cacheValue
)

if cachedKey.Repo == nil {
if _, rev, ok := strings.Cut(name, "@"); ok && stableRef.MatchString(rev) {
cachedKey.Repo = &types.Repo{
Revision: rev,
}
}
}

if ok, err := cache.Get(ctx, cachedKey, &cachedValue); err != nil {
return nil, false, err
} else if ok && time.Since(cachedValue.Time) < CacheTimeout {
} else if ok && (cachedKey.isStatic() || time.Since(cachedValue.Time) < CacheTimeout) {
return cachedValue.Source, true, nil
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/types/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,12 @@ func (t Tool) addContextExportedTools(prg Program, result *toolRefSet) error {
func (t Tool) getCompletionToolRefs(prg Program, agentGroup []ToolReference) ([]ToolReference, error) {
result := toolRefSet{}

for _, agent := range agentGroup {
// don't add yourself
if agent.ToolID != t.ID {
result.Add(agent)
if t.Chat {
for _, agent := range agentGroup {
// don't add yourself
if agent.ToolID != t.ID {
result.Add(agent)
}
}
}

Expand Down

0 comments on commit 7c5da26

Please sign in to comment.