Skip to content

Commit 1e50320

Browse files
Merge pull request #328 from ibuildthecloud/main
bug: fix broken caching
2 parents bf41c3b + 2c09a2c commit 1e50320

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

pkg/cache/cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha256"
66
"encoding/gob"
77
"encoding/hex"
8+
"encoding/json"
89
"errors"
910
"io/fs"
1011
"os"
@@ -71,7 +72,7 @@ func (c *Client) CacheDir() string {
7172

7273
func (c *Client) cacheKey(key any) (string, error) {
7374
hash := sha256.New()
74-
if err := gob.NewEncoder(hash).Encode(key); err != nil {
75+
if err := json.NewEncoder(hash).Encode(key); err != nil {
7576
return "", err
7677
}
7778
digest := hash.Sum(nil)

pkg/hash/seed.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package hash
22

33
import (
4-
"encoding/gob"
4+
"encoding/json"
55
"hash/fnv"
66
)
77

88
func Seed(input any) int {
99
h := fnv.New32a()
10-
if err := gob.NewEncoder(h).Encode(input); err != nil {
10+
if err := json.NewEncoder(h).Encode(input); err != nil {
1111
panic(err)
1212
}
1313
return int(h.Sum32())

pkg/openai/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func override(left, right string) string {
447447
}
448448

449449
func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest, transactionID string, partial chan<- types.CompletionStatus) (responses []openai.ChatCompletionStreamResponse, _ error) {
450-
request.Stream = os.Getenv("GPTSCRIPT_INTERNAL_OPENAI_STREAMING") != "false"
450+
streamResponse := os.Getenv("GPTSCRIPT_INTERNAL_OPENAI_STREAMING") != "false"
451451

452452
partial <- types.CompletionStatus{
453453
CompletionID: transactionID,
@@ -459,7 +459,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
459459

460460
slog.Debug("calling openai", "message", request.Messages)
461461

462-
if !request.Stream {
462+
if !streamResponse {
463463
resp, err := c.c.CreateChatCompletion(ctx, request)
464464
if err != nil {
465465
return nil, err

pkg/types/completion.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ type CompletionRequest struct {
2121
}
2222

2323
func (r *CompletionRequest) GetCache() bool {
24-
return r.Cache != nil && !*r.Cache
24+
if r.Cache == nil {
25+
return true
26+
}
27+
return *r.Cache
2528
}
2629

2730
type CompletionTool struct {

0 commit comments

Comments
 (0)