/
utils.go
167 lines (141 loc) · 4.66 KB
/
utils.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
167
package internal
import (
"encoding/json"
"fmt"
"io"
"net/http"
log "github.com/sirupsen/logrus"
)
// Define static errors.
var (
ErrJSONUnmarshalFailed = fmt.Errorf("json.Unmarshal failed")
ErrJSONMarshalFailed = fmt.Errorf("json.MarshalIndent failed")
ErrLoggerNotFound = fmt.Errorf("logger not found in context")
ErrInvalidRequestFormat = fmt.Errorf("invalid request format")
)
// Reads Data from the Client.
func ReadAndUnmarshalBody(cfg *Config, logger *log.Logger, resp http.ResponseWriter, req *http.Request) (RequestData, error) {
var requestData RequestData
// Read the request body
body, err := io.ReadAll(req.Body)
if err != nil {
errorResponse := map[string]interface{}{
"status": "error",
"message": "error reading request body",
}
err, marshalErr := json.MarshalIndent(errorResponse, "", " ")
if err != nil {
logger.WithFields(log.Fields{"error": marshalErr}).Error("failed to marshal JSON")
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return requestData, fmt.Errorf("json.MarshalIndent failed: %w", marshalErr)
}
http.Error(resp, string(err), http.StatusInternalServerError)
return requestData, fmt.Errorf("io.ReadAll failed: %w", err)
}
defer req.Body.Close()
// Check Content-Type and error if it's not a JSON payload
contentType := req.Header.Get("Content-Type")
if contentType != "application/json" {
errorResponse := map[string]interface{}{
"status": "error",
"message": "Invalid request format. Please send a JSON payload.",
}
errorData, err := json.MarshalIndent(errorResponse, "", " ")
if err != nil {
logger.WithFields(log.Fields{"error": err}).Error("Failed to marshal JSON")
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return requestData, fmt.Errorf("json.MarshalIndent failed: %w", err)
}
http.Error(resp, string(errorData), http.StatusBadRequest)
return requestData, ErrInvalidRequestFormat
}
// Try to unmarshal the body into the requestData struct
err = json.Unmarshal(body, &requestData)
if err != nil {
errorResponse := map[string]interface{}{
"status": "error",
"message": "Error parsing JSON payload",
}
errorData, marshalErr := json.MarshalIndent(errorResponse, "", " ")
if marshalErr != nil {
logger.WithFields(log.Fields{"error": marshalErr}).Error("Failed to marshal JSON")
http.Error(resp, "Internal Server Error", http.StatusInternalServerError)
return requestData, fmt.Errorf("%w: %v", ErrJSONMarshalFailed, marshalErr)
}
http.Error(resp, string(errorData), http.StatusBadRequest)
logger.WithFields(log.Fields{"error": err, "body": string(body)}).Error("Error parsing JSON payload")
return requestData, fmt.Errorf("%w: %v", ErrJSONUnmarshalFailed, err)
}
return requestData, nil
}
func createJSONResponse(content string, responseType string, isClosing bool) JSONResponse {
commonFields := JSONResponse{
ID: "chatcmpl-1692118020279965440",
Object: responseType, // This can be "chat.completion" or "text_completion"
Created: 1692118020,
Model: "Nous-Hermes-Llama2-GPTQ",
Usage: map[string]interface{}{
"prompt_tokens": 58,
"completion_tokens": 100,
"total_tokens": 1000,
},
}
if responseType == "chat.completion" {
if isClosing {
commonFields.Choices = []Choice{
{
Index: 0,
FinishReason: "stop",
Message: map[string]string{
"role": "assistant",
"content": "", // Empty Content sent to the Client
},
Delta: map[string]string{
"role": "assistant",
"content": "", // Empty Content sent to the Client
},
},
}
} else {
commonFields.Choices = []Choice{
{
Index: 0,
FinishReason: "stop",
Message: map[string]string{
"role": "assistant",
"content": content,
},
Delta: map[string]string{
"role": "assistant",
"content": content,
},
},
}
}
} else if responseType == "text_completion" {
commonFields.Choices = []Choice{
{
Index: 0,
FinishReason: "length", // This can be modified if needed
Text: content,
},
}
}
return commonFields
}
// Create a function to send JSONResponse.
func sendJSONResponse(writer http.ResponseWriter, resp JSONResponse, flusher http.Flusher, mode string) {
data, err := json.Marshal(resp)
if err != nil {
http.Error(writer, "Error creating JSON response", http.StatusInternalServerError)
return
}
// Conditionally prepend "data:" based on the mode
if mode == "chat" {
fmt.Fprintf(writer, "data: %s\r\n\r\n", data)
} else {
// For completion mode, send the JSON without "data:"
fmt.Fprintf(writer, "%s\r\n\r\n", data)
}
flusher.Flush()
}