-
Notifications
You must be signed in to change notification settings - Fork 57
/
file.go
166 lines (138 loc) · 3.97 KB
/
file.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package proxy
import (
"encoding/json"
goopenai "github.com/sashabaranov/go-openai"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func logListFilesRequest(log *zap.Logger, prod bool, cid string, params map[string]string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
}
if v, ok := params["purpose"]; ok {
fields = append(fields, zap.String("purpose", v))
}
log.Info("openai list files request", fields...)
}
}
func logListFilesResponse(log *zap.Logger, data []byte, prod bool, cid string) {
files := &goopenai.FilesList{}
err := json.Unmarshal(data, files)
if err != nil {
logError(log, "error when unmarshalling list files response", prod, cid, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.Any("files", files.Files),
}
log.Info("openai list files response", fields...)
}
}
func logRetrieveFileRequest(log *zap.Logger, prod bool, cid, fid string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("file_id", fid),
}
log.Info("openai retrieve file request", fields...)
}
}
func logRetrieveFileResponse(log *zap.Logger, data []byte, prod bool, cid string) {
file := &goopenai.File{}
err := json.Unmarshal(data, file)
if err != nil {
logError(log, "error when unmarshalling retrieve file response", prod, cid, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("id", file.ID),
zap.Int("bytes", file.Bytes),
zap.Int64("createdAt", file.CreatedAt),
zap.String("filename", file.FileName),
zap.String("object", file.Object),
zap.String("purpose", file.Purpose),
zap.String("status", file.Status),
zap.String("status_details", file.StatusDetails),
}
log.Info("openai retrieve file request", fields...)
}
}
func logDeleteFileRequest(log *zap.Logger, prod bool, cid, fid string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("file_id", fid),
}
log.Info("openai delete file request", fields...)
}
}
func logDeleteFileResponse(log *zap.Logger, data []byte, prod bool, cid string) {
dr := &DeletionResponse{}
err := json.Unmarshal(data, dr)
if err != nil {
logError(log, "error when unmarshalling delete file response", prod, cid, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("id", dr.Id),
zap.String("object", dr.Object),
zap.Bool("deleted", dr.Deleted),
}
log.Info("openai delete file response", fields...)
}
}
func logRetrieveFileContentRequest(log *zap.Logger, data []byte, prod bool, cid, fid string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("file_id", fid),
}
log.Info("openai retrieve file content request", fields...)
}
}
func logRetrieveFileContentResponse(log *zap.Logger, data []byte, prod bool, cid string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
}
log.Info("openai retrieve file content response", fields...)
}
}
func logUploadFileRequest(log *zap.Logger, prod bool, cid, purpose string) {
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("purpose", purpose),
}
log.Info("openai upload file request", fields...)
}
}
func logUploadFileResponse(log *zap.Logger, data []byte, prod bool, cid string) {
file := &goopenai.File{}
err := json.Unmarshal(data, file)
if err != nil {
logError(log, "error when unmarshalling retrieve file response", prod, cid, err)
return
}
if prod {
fields := []zapcore.Field{
zap.String(correlationId, cid),
zap.String("id", file.ID),
zap.Int("bytes", file.Bytes),
zap.Int64("createdAt", file.CreatedAt),
zap.String("filename", file.FileName),
zap.String("object", file.Object),
zap.String("purpose", file.Purpose),
zap.String("status", file.Status),
zap.String("status_details", file.StatusDetails),
}
log.Info("openai retrieve file request", fields...)
}
}