@@ -4,6 +4,8 @@ package ollamaclient
4
4
import (
5
5
"bytes"
6
6
"context"
7
+ "crypto/sha256"
8
+ "encoding/hex"
7
9
"encoding/json"
8
10
"errors"
9
11
"fmt"
@@ -267,16 +269,36 @@ func (oc *Config) GetResponse(promptAndOptionalImages ...string) (OutputResponse
267
269
if seed < 0 {
268
270
temperature = oc .TemperatureIfNegativeSeed
269
271
} else {
272
+ temperature = 0 // Since temperature is set to 0 when seed >=0
270
273
// 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 [:])
272
291
if Cache == nil {
273
292
if err := InitCache (); err != nil {
274
293
return OutputResponse {}, err
275
294
}
276
295
}
277
296
if entry , err := Cache .Get (cacheKey ); err == nil {
278
297
var res OutputResponse
279
- json .Unmarshal (entry , & res )
298
+ err = json .Unmarshal (entry , & res )
299
+ if err != nil {
300
+ return OutputResponse {}, err
301
+ }
280
302
return res , nil
281
303
}
282
304
}
@@ -342,9 +364,11 @@ func (oc *Config) GetResponse(promptAndOptionalImages ...string) (OutputResponse
342
364
}
343
365
response .Response = outputString
344
366
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 )
348
372
}
349
373
return response , nil
350
374
}
0 commit comments