Skip to content

Commit 7c5da26

Browse files
Merge pull request #649 from ibuildthecloud/main
chore: already cached performance improvements
2 parents e519494 + 3c5d290 commit 7c5da26

File tree

7 files changed

+64
-12
lines changed

7 files changed

+64
-12
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86
1818
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379
1919
github.com/gptscript-ai/cmd v0.0.0-20240625175447-4250b42feb7d
20-
github.com/gptscript-ai/tui v0.0.0-20240716053605-ecddbcf60eac
20+
github.com/gptscript-ai/tui v0.0.0-20240722014329-d50b5ac5db74
2121
github.com/hexops/autogold/v2 v2.2.1
2222
github.com/hexops/valast v1.4.4
2323
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
@@ -63,7 +63,7 @@ require (
6363
github.com/google/go-cmp v0.6.0 // indirect
6464
github.com/gookit/color v1.5.4 // indirect
6565
github.com/gorilla/css v1.0.0 // indirect
66-
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240715172623-8176fb20c5cb // indirect
66+
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240722014125-d757d09f606b // indirect
6767
github.com/hashicorp/errwrap v1.0.0 // indirect
6868
github.com/hashicorp/go-multierror v1.1.1 // indirect
6969
github.com/hexops/autogold v1.3.1 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf037
171171
github.com/gptscript-ai/chat-completion-client v0.0.0-20240531200700-af8e7ecf0379/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
172172
github.com/gptscript-ai/cmd v0.0.0-20240625175447-4250b42feb7d h1:sKf7T7twhGXs6AVbvD9pKDVewykkwSAPwEpmIEQIR/4=
173173
github.com/gptscript-ai/cmd v0.0.0-20240625175447-4250b42feb7d/go.mod h1:DJAo1xTht1LDkNYFNydVjTHd576TC7MlpsVRl3oloVw=
174-
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240715172623-8176fb20c5cb h1:xeSbO4mLYnoTg7diNW0tpxY/0yDSSdgjohMzwE4Za6k=
175-
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240715172623-8176fb20c5cb/go.mod h1:Dh6vYRAiVcyC3ElZIGzTvNF1FxtYwA07BHfSiFKQY7s=
176-
github.com/gptscript-ai/tui v0.0.0-20240716053605-ecddbcf60eac h1:zZ993dp2mx/63JD4THwMeBcn3C8SogcLeQRJUZsMSM4=
177-
github.com/gptscript-ai/tui v0.0.0-20240716053605-ecddbcf60eac/go.mod h1:Ex2xQMzTMfb5UgLz9rctATPps8DnfPeJQh8o/AiQCoE=
174+
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240722014125-d757d09f606b h1:Hxu8oPE43uQ2sZ7P+9yGSX9bXh0RoJfOgvY/SlCwFlM=
175+
github.com/gptscript-ai/go-gptscript v0.9.3-0.20240722014125-d757d09f606b/go.mod h1:Dh6vYRAiVcyC3ElZIGzTvNF1FxtYwA07BHfSiFKQY7s=
176+
github.com/gptscript-ai/tui v0.0.0-20240722014329-d50b5ac5db74 h1:69BENZCN2y4BCxmPjMRp+ZQ47ay4i5gRgREKZatu5oE=
177+
github.com/gptscript-ai/tui v0.0.0-20240722014329-d50b5ac5db74/go.mod h1:sP/9g7+nLq65aGef5F30AEG+Cuu4BwlglUYv1Pzps4Y=
178178
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
179179
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
180180
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=

pkg/engine/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCate
9696
instructions = append(instructions, inputContext.Content)
9797
}
9898
var extraEnv = []string{
99-
strings.TrimSpace(fmt.Sprintf("GPTSCRIPT_CONTEXT=%s", strings.Join(instructions, "\n"))),
99+
strings.TrimSpace("GPTSCRIPT_CONTEXT=" + strings.Join(instructions, "\n")),
100100
}
101101

102102
cmd, stop, err := e.newCommand(ctx.Ctx, extraEnv, tool, input)

pkg/env/env.go

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func AppendPath(env []string, binPath string) []string {
6060
// Lookup will try to find bin in the PATH in env. It will refer to PATHEXT for Windows support.
6161
// If bin can not be resolved to anything the original bin string is returned.
6262
func Lookup(env []string, bin string) string {
63+
if strings.Contains(bin, string(filepath.Separator)) {
64+
return bin
65+
}
66+
6367
for _, env := range env {
6468
for _, prefix := range []string{"PATH=", "Path="} {
6569
suffix, ok := strings.CutPrefix(env, prefix)

pkg/llm/registry.go

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88

99
"github.com/gptscript-ai/gptscript/pkg/openai"
10+
"github.com/gptscript-ai/gptscript/pkg/remote"
1011
"github.com/gptscript-ai/gptscript/pkg/types"
1112
)
1213

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

45+
func (r *Registry) fastPath(modelName string) Client {
46+
// This is optimization hack to avoid doing List Models
47+
if len(r.clients) != 2 {
48+
return nil
49+
}
50+
51+
_, modelFromProvider := types.SplitToolRef(modelName)
52+
if modelFromProvider != "" {
53+
return nil
54+
}
55+
56+
_, ok := r.clients[0].(*openai.Client)
57+
if !ok {
58+
return nil
59+
}
60+
61+
_, ok = r.clients[1].(*remote.Client)
62+
if !ok {
63+
return nil
64+
}
65+
66+
return r.clients[0]
67+
}
68+
4469
func (r *Registry) Call(ctx context.Context, messageRequest types.CompletionRequest, status chan<- types.CompletionStatus) (*types.CompletionMessage, error) {
4570
if messageRequest.Model == "" {
4671
return nil, fmt.Errorf("model is required")
4772
}
4873

74+
if c := r.fastPath(messageRequest.Model); c != nil {
75+
return c.Call(ctx, messageRequest, status)
76+
}
77+
4978
var errs []error
5079
var oaiClient *openai.Client
5180
for _, client := range r.clients {

pkg/loader/url.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
url2 "net/url"
99
"path"
10+
"regexp"
1011
"strings"
1112
"time"
1213

@@ -33,6 +34,14 @@ type cacheValue struct {
3334
Time time.Time
3435
}
3536

37+
func (c *cacheKey) isStatic() bool {
38+
return c.Repo != nil &&
39+
c.Repo.Revision != "" &&
40+
stableRef.MatchString(c.Repo.Revision)
41+
}
42+
43+
var stableRef = regexp.MustCompile("^([a-f0-9]{7,40}$|v[0-9]|[0-9])")
44+
3645
func loadURL(ctx context.Context, cache *cache.Client, base *source, name string) (*source, bool, error) {
3746
var (
3847
repo *types.Repo
@@ -47,9 +56,17 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string
4756
cachedValue cacheValue
4857
)
4958

59+
if cachedKey.Repo == nil {
60+
if _, rev, ok := strings.Cut(name, "@"); ok && stableRef.MatchString(rev) {
61+
cachedKey.Repo = &types.Repo{
62+
Revision: rev,
63+
}
64+
}
65+
}
66+
5067
if ok, err := cache.Get(ctx, cachedKey, &cachedValue); err != nil {
5168
return nil, false, err
52-
} else if ok && time.Since(cachedValue.Time) < CacheTimeout {
69+
} else if ok && (cachedKey.isStatic() || time.Since(cachedValue.Time) < CacheTimeout) {
5370
return cachedValue.Source, true, nil
5471
}
5572

pkg/types/tool.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,12 @@ func (t Tool) addContextExportedTools(prg Program, result *toolRefSet) error {
660660
func (t Tool) getCompletionToolRefs(prg Program, agentGroup []ToolReference) ([]ToolReference, error) {
661661
result := toolRefSet{}
662662

663-
for _, agent := range agentGroup {
664-
// don't add yourself
665-
if agent.ToolID != t.ID {
666-
result.Add(agent)
663+
if t.Chat {
664+
for _, agent := range agentGroup {
665+
// don't add yourself
666+
if agent.ToolID != t.ID {
667+
result.Add(agent)
668+
}
667669
}
668670
}
669671

0 commit comments

Comments
 (0)