Skip to content

Commit 0a23bd3

Browse files
committed
Fix an issue with the cache if the prompt contained images
1 parent 1a7104b commit 0a23bd3

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

v2/ollamaclient.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package ollamaclient
44
import (
55
"bytes"
66
"context"
7+
"crypto/sha256"
8+
"encoding/hex"
79
"encoding/json"
810
"errors"
911
"fmt"
@@ -267,16 +269,36 @@ func (oc *Config) GetResponse(promptAndOptionalImages ...string) (OutputResponse
267269
if seed < 0 {
268270
temperature = oc.TemperatureIfNegativeSeed
269271
} else {
272+
temperature = 0 // Since temperature is set to 0 when seed >=0
270273
// The cache is only used for fixed seeds and a temperature of 0
271-
cacheKey = prompt + "-" + oc.ModelName
274+
keyData := struct {
275+
Prompts []string
276+
ModelName string
277+
Seed int
278+
Temperature float64
279+
}{
280+
Prompts: promptAndOptionalImages,
281+
ModelName: oc.ModelName,
282+
Seed: seed,
283+
Temperature: temperature,
284+
}
285+
keyDataBytes, err := json.Marshal(keyData)
286+
if err != nil {
287+
return OutputResponse{}, err
288+
}
289+
hash := sha256.Sum256(keyDataBytes)
290+
cacheKey = hex.EncodeToString(hash[:])
272291
if Cache == nil {
273292
if err := InitCache(); err != nil {
274293
return OutputResponse{}, err
275294
}
276295
}
277296
if entry, err := Cache.Get(cacheKey); err == nil {
278297
var res OutputResponse
279-
json.Unmarshal(entry, &res)
298+
err = json.Unmarshal(entry, &res)
299+
if err != nil {
300+
return OutputResponse{}, err
301+
}
280302
return res, nil
281303
}
282304
}
@@ -342,9 +364,11 @@ func (oc *Config) GetResponse(promptAndOptionalImages ...string) (OutputResponse
342364
}
343365
response.Response = outputString
344366
if cacheKey != "" {
345-
var data []byte
346-
json.Unmarshal([]byte(data), &response)
347-
Cache.Set(cacheKey, []byte(data))
367+
data, err := json.Marshal(response)
368+
if err != nil {
369+
return OutputResponse{}, err
370+
}
371+
Cache.Set(cacheKey, data)
348372
}
349373
return response, nil
350374
}

0 commit comments

Comments
 (0)