-
Notifications
You must be signed in to change notification settings - Fork 58
/
util.go
38 lines (29 loc) · 946 Bytes
/
util.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
package route
import (
"fmt"
"github.com/bricks-cloud/bricksllm/internal/hasher"
"github.com/bricks-cloud/bricksllm/internal/util"
goopenai "github.com/sashabaranov/go-openai"
)
func ComputeCacheKeyForEmbeddingsRequest(path string, req *goopenai.EmbeddingRequest) string {
if req == nil {
return ""
}
input, _ := util.ConvertAnyToStr(req.Input)
return hasher.Hash(fmt.Sprintf("%s-%s-%s-%s", path, input, req.EncodingFormat, req.User))
}
func ComputeCacheKeyForChatCompletionRequest(path string, req *goopenai.ChatCompletionRequest) string {
if req == nil {
return ""
}
input := ""
for _, m := range req.Messages {
input += m.Name
input += m.Role
input += m.Content
}
if req.ResponseFormat != nil && len(req.ResponseFormat.Type) != 0 {
return hasher.Hash(fmt.Sprintf("%s-%s-%s-%s", path, input, req.User, string(req.ResponseFormat.Type)))
}
return hasher.Hash(fmt.Sprintf("%s-%s-%s", path, input, req.User))
}