forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
151 lines (128 loc) · 3.15 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cache
import (
"context"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"encoding/json"
"errors"
"io/fs"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/adrg/xdg"
"github.com/getkin/kin-openapi/openapi3"
openai "github.com/gptscript-ai/chat-completion-client"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/gptscript-ai/gptscript/pkg/version"
)
type Client struct {
dir string
noop bool
}
type Options struct {
DisableCache bool `usage:"Disable caching of LLM API responses"`
CacheDir string `usage:"Directory to store cache (default: $XDG_CACHE_HOME/gptscript)"`
}
func init() {
gob.Register(openai.ChatCompletionRequest{})
gob.Register(openapi3.Schema{})
}
func Complete(opts ...Options) (result Options) {
for _, opt := range opts {
result.CacheDir = types.FirstSet(opt.CacheDir, result.CacheDir)
result.DisableCache = types.FirstSet(opt.DisableCache, result.DisableCache)
}
if result.CacheDir == "" {
result.CacheDir = filepath.Join(xdg.CacheHome, version.ProgramName)
} else if !filepath.IsAbs(result.CacheDir) {
var err error
result.CacheDir, err = makeAbsolute(result.CacheDir)
if err != nil {
result.CacheDir = filepath.Join(xdg.CacheHome, version.ProgramName)
}
}
return
}
func makeAbsolute(path string) (string, error) {
if strings.HasPrefix(path, "~"+string(filepath.Separator)) {
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[2:]), nil
}
return filepath.Abs(path)
}
type noCacheKey struct{}
func IsNoCache(ctx context.Context) bool {
v, _ := ctx.Value(noCacheKey{}).(bool)
return v
}
func WithNoCache(ctx context.Context) context.Context {
return context.WithValue(ctx, noCacheKey{}, true)
}
func New(opts ...Options) (*Client, error) {
opt := Complete(opts...)
if err := os.MkdirAll(opt.CacheDir, 0755); err != nil {
return nil, err
}
return &Client{
dir: opt.CacheDir,
noop: opt.DisableCache,
}, nil
}
func (c *Client) CacheDir() string {
return c.dir
}
func (c *Client) cacheKey(key any) (string, error) {
hash := sha256.New()
if err := json.NewEncoder(hash).Encode(key); err != nil {
return "", err
}
digest := hash.Sum(nil)
return hex.EncodeToString(digest), nil
}
func (c *Client) Store(ctx context.Context, key, value any) error {
if c == nil {
return nil
}
if c.noop || IsNoCache(ctx) {
keyValue, err := c.cacheKey(key)
if err == nil {
p := filepath.Join(c.dir, keyValue)
if _, err := os.Stat(p); err == nil {
_ = os.Remove(p)
}
}
return nil
}
keyValue, err := c.cacheKey(key)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(c.dir, keyValue))
if err != nil {
return err
}
defer f.Close()
return gob.NewEncoder(f).Encode(value)
}
func (c *Client) Get(ctx context.Context, key, out any) (bool, error) {
if c == nil || c.noop || IsNoCache(ctx) {
return false, nil
}
keyValue, err := c.cacheKey(key)
if err != nil {
return false, err
}
f, err := os.Open(filepath.Join(c.dir, keyValue))
if errors.Is(err, fs.ErrNotExist) {
return false, nil
} else if err != nil {
return false, err
}
defer f.Close()
return gob.NewDecoder(f).Decode(out) == nil, nil
}