-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
281 lines (243 loc) · 8.06 KB
/
server.go
File metadata and controls
281 lines (243 loc) · 8.06 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"time"
"github.com/adaptive-scale/superclass/pkg/classifier"
"github.com/adaptive-scale/superclass/pkg/extractor"
log "github.com/sirupsen/logrus"
)
type Server struct {
uploadDir string
provider classifier.Provider
config classifier.ModelConfig
}
type ClassificationRequest struct {
Categories []string `json:"categories,omitempty"`
}
type ClassificationResponse struct {
Category string `json:"category"`
Confidence float64 `json:"confidence"`
Summary string `json:"summary"`
Keywords []string `json:"keywords"`
RawText string `json:"raw_text,omitempty"`
Error string `json:"error,omitempty"`
}
func NewServer(uploadDir string, provider classifier.Provider, config classifier.ModelConfig) *Server {
return &Server{
uploadDir: uploadDir,
provider: provider,
config: config,
}
}
// getEnvWithDefault gets an environment variable with a default value
func getEnvWithDefault(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// getEnvFloat64WithDefault gets a float64 environment variable with a default value
func getEnvFloat64WithDefault(key string, defaultValue float64) float64 {
if value := os.Getenv(key); value != "" {
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
return floatValue
}
}
return defaultValue
}
// getEnvIntWithDefault gets an int environment variable with a default value
func getEnvIntWithDefault(key string, defaultValue int) int {
if value := os.Getenv(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
func NewServerFromEnv() *Server {
log.Debug("Starting server initialization from environment")
// Get configuration from environment variables
uploadDir := getEnvWithDefault("UPLOAD_DIR", "/tmp/superclass-uploads")
modelType := getEnvWithDefault("MODEL_TYPE", "gpt-4")
provider := classifier.ProviderFromString(getEnvWithDefault("MODEL_PROVIDER", "openai"))
maxCost := getEnvFloat64WithDefault("MAX_COST", 0.1)
maxLatency := getEnvIntWithDefault("MAX_LATENCY", 30)
log.WithFields(log.Fields{
"uploadDir": uploadDir,
"modelType": modelType,
"provider": provider,
"maxCost": maxCost,
"maxLatency": maxLatency,
}).Info("Server configuration loaded")
log.Debug("Creating model configuration")
// Create model config
config := classifier.ModelConfig{
Model: modelType,
APIKey: os.Getenv("OPENAI_API_KEY"), // Will be overridden by provider-specific key
Parameters: map[string]interface{}{
"max_tokens": 2000,
"temperature": 0.3,
"max_cost": maxCost,
"max_latency": maxLatency,
},
}
log.Debug("Setting provider-specific API key")
// Set the appropriate API key based on the provider
switch provider {
case classifier.OpenAI:
config.APIKey = os.Getenv("OPENAI_API_KEY")
log.Debug("Using OpenAI provider")
case classifier.Anthropic:
config.APIKey = os.Getenv("ANTHROPIC_API_KEY")
log.Debug("Using Anthropic provider")
case classifier.Azure:
config.APIKey = os.Getenv("AZURE_OPENAI_API_KEY")
log.Debug("Using Azure OpenAI provider")
}
log.Debug("Server initialization completed")
return NewServer(uploadDir, provider, config)
}
func (s *Server) handleClassify(w http.ResponseWriter, r *http.Request) {
logger := log.WithFields(log.Fields{
"handler": "classify",
"method": r.Method,
"remote": r.RemoteAddr,
})
if r.Method != http.MethodPost {
logger.Warn("Method not allowed")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse multipart form with 32MB max memory
if err := r.ParseMultipartForm(32 << 20); err != nil {
logger.WithError(err).Error("Failed to parse form")
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
// Parse optional categories from form
var classificationReq ClassificationRequest
if categoriesJSON := r.FormValue("categories"); categoriesJSON != "" {
if err := json.Unmarshal([]byte(categoriesJSON), &classificationReq.Categories); err != nil {
logger.WithError(err).Error("Failed to parse categories")
http.Error(w, "Invalid categories format", http.StatusBadRequest)
return
}
}
file, header, err := r.FormFile("file")
if err != nil {
logger.WithError(err).Error("Failed to get file from form")
http.Error(w, "Failed to get file from form", http.StatusBadRequest)
return
}
defer file.Close()
logger = logger.WithFields(log.Fields{
"filename": header.Filename,
"size": header.Size,
"content_type": header.Header.Get("Content-Type"),
"has_categories": len(classificationReq.Categories) > 0,
})
logger.Info("Processing uploaded file")
// Create temporary file
tempFile := filepath.Join(s.uploadDir, header.Filename)
out, err := os.Create(tempFile)
if err != nil {
logger.WithError(err).Error("Failed to create temporary file")
http.Error(w, "Failed to create temporary file", http.StatusInternalServerError)
return
}
defer func() {
out.Close()
if err := os.Remove(tempFile); err != nil {
logger.WithError(err).Warn("Failed to remove temporary file")
}
}()
// Copy uploaded file to temporary file
if _, err := io.Copy(out, file); err != nil {
logger.WithError(err).Error("Failed to save file")
http.Error(w, "Failed to save file", http.StatusInternalServerError)
return
}
logger.Debug("Starting classification")
// Extract and classify
result, err := extractor.ExtractAndClassifyWithOptions(tempFile, s.provider, s.config, classifier.ClassificationOptions{
Categories: classificationReq.Categories,
})
if err != nil {
logger.WithError(err).Error("Classification failed")
json.NewEncoder(w).Encode(ClassificationResponse{
Error: err.Error(),
})
return
}
// Prepare response
response := ClassificationResponse{
Category: result.Classification.Category,
Confidence: result.Classification.Confidence,
Summary: result.Classification.Summary,
Keywords: result.Classification.Keywords,
RawText: result.Text,
}
logger.WithFields(log.Fields{
"category": response.Category,
"confidence": response.Confidence,
"keywords": response.Keywords,
"used_categories": classificationReq.Categories,
}).Info("Classification completed successfully")
// Send response
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
logger.WithError(err).Error("Failed to encode response")
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
return
}
}
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
logger := log.WithFields(log.Fields{
"handler": "health",
"method": r.Method,
"remote": r.RemoteAddr,
"request_id": r.Header.Get("X-Request-ID"),
})
logger.Debug("Health check requested")
// Add basic system metrics in debug mode
logger.WithFields(log.Fields{
"uptime": time.Since(startTime).String(),
"goroutines": runtime.NumGoroutine(),
"cpu_cores": runtime.NumCPU(),
}).Debug("System metrics")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
logger.Debug("Health check completed")
}
var startTime time.Time
func (s *Server) Start(port int) error {
startTime = time.Now()
log.Debug("Ensuring upload directory exists")
// Create upload directory if it doesn't exist
if err := os.MkdirAll(s.uploadDir, 0755); err != nil {
log.WithError(err).Error("Failed to create upload directory")
return fmt.Errorf("failed to create upload directory: %w", err)
}
log.Debug("Registering HTTP handlers")
// Register routes
http.HandleFunc("/classify", s.handleClassify)
http.HandleFunc("/health", s.handleHealth)
// Start server
addr := fmt.Sprintf(":%d", port)
log.WithFields(log.Fields{
"address": addr,
"port": port,
"upload_dir": s.uploadDir,
"provider": s.provider,
"model": s.config.Model,
}).Info("Server starting on port %d", port)
log.Debug("Starting HTTP server")
return http.ListenAndServe(addr, nil)
}