-
Notifications
You must be signed in to change notification settings - Fork 64
/
thread.go
112 lines (92 loc) · 2.41 KB
/
thread.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
package proxy
import (
"encoding/json"
"github.com/bricks-cloud/bricksllm/internal/provider/openai"
goopenai "github.com/sashabaranov/go-openai"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func logCreateThreadRequest(log *zap.Logger, data []byte, prod, private bool) {
tr := &openai.ThreadRequest{}
err := json.Unmarshal(data, tr)
if err != nil {
logError(log, "error when unmarshalling create thread request", prod, err)
return
}
if private {
for _, m := range tr.Messages {
m.Content = ""
}
}
if prod {
fields := []zapcore.Field{
zap.Any("metadata", tr.Metadata),
zap.Any("messages", tr.Messages),
}
log.Info("openai create thread request", fields...)
}
}
func logThreadResponse(log *zap.Logger, data []byte, prod bool) {
t := &goopenai.Thread{}
err := json.Unmarshal(data, t)
if err != nil {
logError(log, "error when unmarshalling thread response", prod, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String("id", t.ID),
zap.String("object", t.Object),
zap.Int64("created_at", t.CreatedAt),
zap.Any("metadata", t.Metadata),
}
log.Info("openai create thread response", fields...)
}
}
func logRetrieveThreadRequest(log *zap.Logger, prod bool, tid string) {
if prod {
fields := []zapcore.Field{
zap.String("id", tid),
}
log.Info("openai retrieve thread request", fields...)
}
}
func logModifyThreadRequest(log *zap.Logger, data []byte, prod bool, tid string) {
tr := &goopenai.ThreadRequest{}
err := json.Unmarshal(data, tr)
if err != nil {
logError(log, "error when unmarshalling modify thread request", prod, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String("id", tid),
zap.Any("metadata", tr.Metadata),
}
log.Info("openai modify thread request", fields...)
}
}
func logDeleteThreadRequest(log *zap.Logger, prod bool, tid string) {
if prod {
fields := []zapcore.Field{
zap.String("id", tid),
}
log.Info("openai delete thread request", fields...)
}
}
func logDeleteThreadResponse(log *zap.Logger, data []byte, prod bool) {
tdr := &goopenai.ThreadDeleteResponse{}
err := json.Unmarshal(data, tdr)
if err != nil {
logError(log, "error when unmarshalling thread deletion response", prod, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String("id", tdr.ID),
zap.String("object", tdr.Object),
zap.Bool("deleted", tdr.Deleted),
}
log.Info("openai thread deletion response", fields...)
}
}