/
message.go
138 lines (116 loc) 路 3.19 KB
/
message.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package proxy
import (
"encoding/json"
goopenai "github.com/sashabaranov/go-openai"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func logCreateMessageRequest(log *zap.Logger, data []byte, prod, private bool, cid string) {
mr := &goopenai.MessageRequest{}
err := json.Unmarshal(data, mr)
if err != nil {
logError(log, "error when unmarshalling create message request", prod, cid, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("role", mr.Role),
zap.Any("file_ids", mr.FileIds),
zap.Any("metadata", mr.Metadata),
}
if !private && len(mr.Content) != 0 {
fields = append(fields, zap.String("content", mr.Content))
}
log.Info("openai create message request", fields...)
}
}
func logMessageResponse(log *zap.Logger, data []byte, prod, private bool, cid string) {
m := &goopenai.Message{}
err := json.Unmarshal(data, m)
if err != nil {
logError(log, "error when unmarshalling message response", prod, cid, err)
return
}
if private {
for _, mc := range m.Content {
mc.Text = nil
}
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("id", m.ID),
zap.String("object", m.Object),
zap.Int("created_at", m.CreatedAt),
zap.String("role", m.Role),
zap.Any("content", m.Content),
zap.Any("file_ids", m.FileIds),
zap.Any("assistant_id", m.AssistantID),
zap.Any("run_id", m.RunID),
zap.Any("metadata", m.Metadata),
}
log.Info("openai message response", fields...)
}
}
func logRetrieveMessageRequest(log *zap.Logger, prod bool, cid, mid, tid string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("thread_id", tid),
zap.String("message_id", mid),
}
log.Info("openai retrieve message request", fields...)
}
}
func logModifyMessageRequest(log *zap.Logger, data []byte, prod, private bool, cid, tid, mid string) {
mr := &goopenai.MessageRequest{}
err := json.Unmarshal(data, mr)
if err != nil {
logError(log, "error when unmarshalling modify message request", prod, cid, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("thread_id", tid),
zap.String("message_id", mid),
zap.Any("metadata", mr.Metadata),
}
if !private && len(mr.Content) != 0 {
fields = append(fields, zap.String("content", mr.Content))
}
log.Info("openai modify message request", fields...)
}
}
func logListMessagesRequest(log *zap.Logger, prod bool, cid, tid string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("thread_id", tid),
}
log.Info("openai list messages request", fields...)
}
}
func logListMessagesResponse(log *zap.Logger, data []byte, prod, private bool, cid string) {
ms := &goopenai.MessagesList{}
err := json.Unmarshal(data, ms)
if err != nil {
logError(log, "error when unmarshalling list messages response", prod, cid, err)
return
}
for _, m := range ms.Messages {
if private {
for _, mc := range m.Content {
mc.Text = nil
}
}
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.Any("messages", ms.Messages),
}
log.Info("openai list messages response", fields...)
}
}