Update main.go
Browse files
main.go
CHANGED
|
@@ -26,7 +26,6 @@ var modelAliases = map[string]string{
|
|
| 26 |
"Kimi-K2": "moonshotai/kimi-k2-instruct",
|
| 27 |
}
|
| 28 |
|
| 29 |
-
|
| 30 |
var thinkingModels = map[string]bool{
|
| 31 |
"z-ai/glm4.7": true,
|
| 32 |
}
|
|
@@ -81,19 +80,16 @@ func resolveModel(requested string) string {
|
|
| 81 |
}
|
| 82 |
|
| 83 |
func injectSystemPrompt(messages []Message, modelID string) []Message {
|
| 84 |
-
// Strip all system messages from client — gateway prompt is the only one
|
| 85 |
filtered := make([]Message, 0, len(messages))
|
| 86 |
for _, m := range messages {
|
| 87 |
if m.Role != "system" {
|
| 88 |
filtered = append(filtered, m)
|
| 89 |
}
|
| 90 |
}
|
| 91 |
-
|
| 92 |
prompt, ok := systemPrompts[modelID]
|
| 93 |
if !ok || prompt == "" {
|
| 94 |
return filtered
|
| 95 |
}
|
| 96 |
-
|
| 97 |
return append([]Message{{Role: "system", Content: prompt}}, filtered...)
|
| 98 |
}
|
| 99 |
|
|
@@ -115,7 +111,6 @@ func handleModels(w http.ResponseWriter, r *http.Request) {
|
|
| 115 |
http.Error(w, `{"error":{"message":"Unauthorized","type":"auth_error"}}`, http.StatusUnauthorized)
|
| 116 |
return
|
| 117 |
}
|
| 118 |
-
|
| 119 |
type ModelObj struct {
|
| 120 |
ID string `json:"id"`
|
| 121 |
Object string `json:"object"`
|
|
@@ -126,18 +121,13 @@ func handleModels(w http.ResponseWriter, r *http.Request) {
|
|
| 126 |
Object string `json:"object"`
|
| 127 |
Data []ModelObj `json:"data"`
|
| 128 |
}
|
| 129 |
-
|
| 130 |
models := ModelsResponse{Object: "list"}
|
| 131 |
now := time.Now().Unix()
|
| 132 |
for alias := range modelAliases {
|
| 133 |
models.Data = append(models.Data, ModelObj{
|
| 134 |
-
ID:
|
| 135 |
-
Object: "model",
|
| 136 |
-
Created: now,
|
| 137 |
-
OwnedBy: "nvidia",
|
| 138 |
})
|
| 139 |
}
|
| 140 |
-
|
| 141 |
w.Header().Set("Content-Type", "application/json")
|
| 142 |
json.NewEncoder(w).Encode(models)
|
| 143 |
}
|
|
@@ -147,12 +137,10 @@ func handleChat(w http.ResponseWriter, r *http.Request) {
|
|
| 147 |
http.Error(w, `{"error":{"message":"Unauthorized","type":"auth_error"}}`, http.StatusUnauthorized)
|
| 148 |
return
|
| 149 |
}
|
| 150 |
-
|
| 151 |
if r.Method != http.MethodPost {
|
| 152 |
http.Error(w, `{"error":{"message":"Method not allowed"}}`, http.StatusMethodNotAllowed)
|
| 153 |
return
|
| 154 |
}
|
| 155 |
-
|
| 156 |
var req ChatRequest
|
| 157 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 158 |
http.Error(w, `{"error":{"message":"Invalid request body"}}`, http.StatusBadRequest)
|
|
@@ -173,7 +161,6 @@ func handleChat(w http.ResponseWriter, r *http.Request) {
|
|
| 173 |
TopP: req.TopP,
|
| 174 |
Stop: req.Stop,
|
| 175 |
}
|
| 176 |
-
|
| 177 |
if thinkingModels[modelID] {
|
| 178 |
upstream.ExtraBody = &ExtraBody{
|
| 179 |
ChatTemplateKwargs: map[string]interface{}{
|
|
@@ -188,7 +175,6 @@ func handleChat(w http.ResponseWriter, r *http.Request) {
|
|
| 188 |
http.Error(w, `{"error":{"message":"Failed to marshal request"}}`, http.StatusInternalServerError)
|
| 189 |
return
|
| 190 |
}
|
| 191 |
-
|
| 192 |
upstreamReq, err := http.NewRequest(http.MethodPost, NvidiaBaseURL+"/chat/completions", bytes.NewReader(body))
|
| 193 |
if err != nil {
|
| 194 |
http.Error(w, `{"error":{"message":"Failed to create upstream request"}}`, http.StatusInternalServerError)
|
|
@@ -223,7 +209,6 @@ func handleChat(w http.ResponseWriter, r *http.Request) {
|
|
| 223 |
flusher, canFlush := w.(http.Flusher)
|
| 224 |
scanner := bufio.NewScanner(resp.Body)
|
| 225 |
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
|
| 226 |
-
|
| 227 |
for scanner.Scan() {
|
| 228 |
fmt.Fprintf(w, "%s\n", scanner.Text())
|
| 229 |
if canFlush {
|
|
@@ -259,7 +244,6 @@ func main() {
|
|
| 259 |
if port == "" {
|
| 260 |
port = "7860"
|
| 261 |
}
|
| 262 |
-
|
| 263 |
mux := http.NewServeMux()
|
| 264 |
mux.HandleFunc("/v1/chat/completions", corsMiddleware(loggingMiddleware(handleChat)))
|
| 265 |
mux.HandleFunc("/v1/models", corsMiddleware(loggingMiddleware(handleModels)))
|
|
@@ -267,292 +251,7 @@ func main() {
|
|
| 267 |
w.WriteHeader(http.StatusOK)
|
| 268 |
w.Write([]byte(`{"status":"ok"}`))
|
| 269 |
})
|
| 270 |
-
|
| 271 |
log.Printf("Gateway starting on :%s", port)
|
| 272 |
-
if err := http.ListenAndServe(":"+port, mux); err != nil {
|
| 273 |
-
log.Fatal(err)
|
| 274 |
-
}
|
| 275 |
-
}
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
var modelAliases = map[string]string{
|
| 279 |
-
"Bielik-11b": "speakleash/bielik-11b-v2.6-instruct",
|
| 280 |
-
"GLM-4.7": "z-ai/glm4.7",
|
| 281 |
-
"Mistral-Small-4": "mistralai/mistral-small-4-119b-2603",
|
| 282 |
-
"DeepSeek-V3.1": "deepseek-ai/deepseek-v3.1",
|
| 283 |
-
"Kimi-K2": "moonshotai/kimi-k2-instruct",
|
| 284 |
-
}
|
| 285 |
-
|
| 286 |
-
var thinkingModels = map[string]bool{
|
| 287 |
-
"z-ai/glm4.7": true,
|
| 288 |
-
}
|
| 289 |
-
|
| 290 |
-
type Message struct {
|
| 291 |
-
Role string `json:"role"`
|
| 292 |
-
Content interface{} `json:"content"`
|
| 293 |
-
ToolCallID string `json:"tool_call_id,omitempty"`
|
| 294 |
-
ToolCalls interface{} `json:"tool_calls,omitempty"`
|
| 295 |
-
Name string `json:"name,omitempty"`
|
| 296 |
-
}
|
| 297 |
-
|
| 298 |
-
type ChatRequest struct {
|
| 299 |
-
Model string `json:"model"`
|
| 300 |
-
Messages []Message `json:"messages"`
|
| 301 |
-
Stream *bool `json:"stream,omitempty"`
|
| 302 |
-
Tools []interface{} `json:"tools,omitempty"`
|
| 303 |
-
ToolChoice interface{} `json:"tool_choice,omitempty"`
|
| 304 |
-
Temperature *float64 `json:"temperature,omitempty"`
|
| 305 |
-
MaxTokens *int `json:"max_tokens,omitempty"`
|
| 306 |
-
TopP *float64 `json:"top_p,omitempty"`
|
| 307 |
-
Stop interface{} `json:"stop,omitempty"`
|
| 308 |
-
}
|
| 309 |
-
|
| 310 |
-
type UpstreamRequest struct {
|
| 311 |
-
Model string `json:"model"`
|
| 312 |
-
Messages []Message `json:"messages"`
|
| 313 |
-
Stream bool `json:"stream"`
|
| 314 |
-
Tools []interface{} `json:"tools,omitempty"`
|
| 315 |
-
ToolChoice interface{} `json:"tool_choice,omitempty"`
|
| 316 |
-
Temperature *float64 `json:"temperature,omitempty"`
|
| 317 |
-
MaxTokens *int `json:"max_tokens,omitempty"`
|
| 318 |
-
TopP *float64 `json:"top_p,omitempty"`
|
| 319 |
-
Stop interface{} `json:"stop,omitempty"`
|
| 320 |
-
ExtraBody *ExtraBody `json:"extra_body,omitempty"`
|
| 321 |
-
}
|
| 322 |
-
|
| 323 |
-
type ExtraBody struct {
|
| 324 |
-
ChatTemplateKwargs map[string]interface{} `json:"chat_template_kwargs,omitempty"`
|
| 325 |
-
}
|
| 326 |
-
|
| 327 |
-
func loadSystemPrompt(modelID string) string {
|
| 328 |
-
// Try alias name first, then full model ID
|
| 329 |
-
for alias, fullID := range modelAliases {
|
| 330 |
-
if fullID == modelID {
|
| 331 |
-
path := fmt.Sprintf("system_prompts/%s.txt", strings.ToLower(strings.ReplaceAll(alias, "-", "_")))
|
| 332 |
-
if data, err := os.ReadFile(path); err == nil {
|
| 333 |
-
return strings.TrimSpace(string(data))
|
| 334 |
-
}
|
| 335 |
-
}
|
| 336 |
-
}
|
| 337 |
-
return ""
|
| 338 |
-
}
|
| 339 |
-
|
| 340 |
-
func resolveModel(requested string) string {
|
| 341 |
-
if full, ok := modelAliases[requested]; ok {
|
| 342 |
-
return full
|
| 343 |
-
}
|
| 344 |
-
// Check if it's already a full model ID
|
| 345 |
-
for _, full := range modelAliases {
|
| 346 |
-
if full == requested {
|
| 347 |
-
return requested
|
| 348 |
-
}
|
| 349 |
-
}
|
| 350 |
-
return requested
|
| 351 |
-
}
|
| 352 |
-
|
| 353 |
-
func injectSystemPrompt(messages []Message, modelID string) []Message {
|
| 354 |
-
// Strip any system messages from client - gateway prompt always wins
|
| 355 |
-
filtered := make([]Message, 0, len(messages))
|
| 356 |
-
for _, m := range messages {
|
| 357 |
-
if m.Role != "system" {
|
| 358 |
-
filtered = append(filtered, m)
|
| 359 |
-
}
|
| 360 |
-
}
|
| 361 |
-
|
| 362 |
-
prompt := loadSystemPrompt(modelID)
|
| 363 |
-
if prompt == "" {
|
| 364 |
-
return filtered
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
return append([]Message{{Role: "system", Content: prompt}}, filtered...)
|
| 368 |
-
}
|
| 369 |
-
|
| 370 |
-
func authenticate(r *http.Request) bool {
|
| 371 |
-
auth := r.Header.Get("Authorization")
|
| 372 |
-
if strings.HasPrefix(auth, "Bearer ") {
|
| 373 |
-
token := strings.TrimPrefix(auth, "Bearer ")
|
| 374 |
-
if token == GatewayAPIKey {
|
| 375 |
-
return true
|
| 376 |
-
}
|
| 377 |
-
}
|
| 378 |
-
// Also check x-api-key header
|
| 379 |
-
if r.Header.Get("x-api-key") == GatewayAPIKey {
|
| 380 |
-
return true
|
| 381 |
-
}
|
| 382 |
-
return false
|
| 383 |
-
}
|
| 384 |
-
|
| 385 |
-
func handleModels(w http.ResponseWriter, r *http.Request) {
|
| 386 |
-
if !authenticate(r) {
|
| 387 |
-
http.Error(w, `{"error":{"message":"Unauthorized","type":"auth_error"}}`, http.StatusUnauthorized)
|
| 388 |
-
return
|
| 389 |
-
}
|
| 390 |
-
|
| 391 |
-
type ModelObj struct {
|
| 392 |
-
ID string `json:"id"`
|
| 393 |
-
Object string `json:"object"`
|
| 394 |
-
Created int64 `json:"created"`
|
| 395 |
-
OwnedBy string `json:"owned_by"`
|
| 396 |
-
}
|
| 397 |
-
type ModelsResponse struct {
|
| 398 |
-
Object string `json:"object"`
|
| 399 |
-
Data []ModelObj `json:"data"`
|
| 400 |
-
}
|
| 401 |
-
|
| 402 |
-
models := ModelsResponse{Object: "list"}
|
| 403 |
-
now := time.Now().Unix()
|
| 404 |
-
for alias := range modelAliases {
|
| 405 |
-
models.Data = append(models.Data, ModelObj{
|
| 406 |
-
ID: alias,
|
| 407 |
-
Object: "model",
|
| 408 |
-
Created: now,
|
| 409 |
-
OwnedBy: "nvidia",
|
| 410 |
-
})
|
| 411 |
-
}
|
| 412 |
-
|
| 413 |
-
w.Header().Set("Content-Type", "application/json")
|
| 414 |
-
json.NewEncoder(w).Encode(models)
|
| 415 |
-
}
|
| 416 |
-
|
| 417 |
-
func handleChat(w http.ResponseWriter, r *http.Request) {
|
| 418 |
-
if !authenticate(r) {
|
| 419 |
-
http.Error(w, `{"error":{"message":"Unauthorized","type":"auth_error"}}`, http.StatusUnauthorized)
|
| 420 |
-
return
|
| 421 |
-
}
|
| 422 |
-
|
| 423 |
-
if r.Method != http.MethodPost {
|
| 424 |
-
http.Error(w, `{"error":{"message":"Method not allowed"}}`, http.StatusMethodNotAllowed)
|
| 425 |
-
return
|
| 426 |
-
}
|
| 427 |
-
|
| 428 |
-
var req ChatRequest
|
| 429 |
-
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 430 |
-
http.Error(w, `{"error":{"message":"Invalid request body"}}`, http.StatusBadRequest)
|
| 431 |
-
return
|
| 432 |
-
}
|
| 433 |
-
|
| 434 |
-
modelID := resolveModel(req.Model)
|
| 435 |
-
req.Messages = injectSystemPrompt(req.Messages, modelID)
|
| 436 |
-
|
| 437 |
-
upstream := UpstreamRequest{
|
| 438 |
-
Model: modelID,
|
| 439 |
-
Messages: req.Messages,
|
| 440 |
-
Stream: true, // always true
|
| 441 |
-
Tools: req.Tools,
|
| 442 |
-
ToolChoice: req.ToolChoice,
|
| 443 |
-
Temperature: req.Temperature,
|
| 444 |
-
MaxTokens: req.MaxTokens,
|
| 445 |
-
TopP: req.TopP,
|
| 446 |
-
Stop: req.Stop,
|
| 447 |
-
}
|
| 448 |
-
|
| 449 |
-
// Add extra_body for thinking models
|
| 450 |
-
if thinkingModels[modelID] {
|
| 451 |
-
upstream.ExtraBody = &ExtraBody{
|
| 452 |
-
ChatTemplateKwargs: map[string]interface{}{
|
| 453 |
-
"enable_thinking": false,
|
| 454 |
-
"clear_thinking": true,
|
| 455 |
-
},
|
| 456 |
-
}
|
| 457 |
-
}
|
| 458 |
-
|
| 459 |
-
body, err := json.Marshal(upstream)
|
| 460 |
-
if err != nil {
|
| 461 |
-
http.Error(w, `{"error":{"message":"Failed to marshal request"}}`, http.StatusInternalServerError)
|
| 462 |
-
return
|
| 463 |
-
}
|
| 464 |
-
|
| 465 |
-
upstreamReq, err := http.NewRequest(http.MethodPost, NvidiaBaseURL+"/chat/completions", bytes.NewReader(body))
|
| 466 |
-
if err != nil {
|
| 467 |
-
http.Error(w, `{"error":{"message":"Failed to create upstream request"}}`, http.StatusInternalServerError)
|
| 468 |
-
return
|
| 469 |
-
}
|
| 470 |
-
upstreamReq.Header.Set("Content-Type", "application/json")
|
| 471 |
-
upstreamReq.Header.Set("Authorization", "Bearer "+NvidiaAPIKey)
|
| 472 |
-
upstreamReq.Header.Set("Accept", "text/event-stream")
|
| 473 |
-
|
| 474 |
-
client := &http.Client{Timeout: 300 * time.Second}
|
| 475 |
-
resp, err := client.Do(upstreamReq)
|
| 476 |
-
if err != nil {
|
| 477 |
-
http.Error(w, fmt.Sprintf(`{"error":{"message":"Upstream error: %s"}}`, err.Error()), http.StatusBadGateway)
|
| 478 |
-
return
|
| 479 |
-
}
|
| 480 |
-
defer resp.Body.Close()
|
| 481 |
-
|
| 482 |
-
if resp.StatusCode != http.StatusOK {
|
| 483 |
-
upstreamBody, _ := io.ReadAll(resp.Body)
|
| 484 |
-
w.Header().Set("Content-Type", "application/json")
|
| 485 |
-
w.WriteHeader(resp.StatusCode)
|
| 486 |
-
w.Write(upstreamBody)
|
| 487 |
-
return
|
| 488 |
-
}
|
| 489 |
-
|
| 490 |
-
// Stream response back
|
| 491 |
-
w.Header().Set("Content-Type", "text/event-stream")
|
| 492 |
-
w.Header().Set("Cache-Control", "no-cache")
|
| 493 |
-
w.Header().Set("Connection", "keep-alive")
|
| 494 |
-
w.Header().Set("X-Accel-Buffering", "no")
|
| 495 |
-
w.WriteHeader(http.StatusOK)
|
| 496 |
-
|
| 497 |
-
flusher, canFlush := w.(http.Flusher)
|
| 498 |
-
|
| 499 |
-
scanner := bufio.NewScanner(resp.Body)
|
| 500 |
-
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
|
| 501 |
-
|
| 502 |
-
for scanner.Scan() {
|
| 503 |
-
line := scanner.Text()
|
| 504 |
-
fmt.Fprintf(w, "%s\n", line)
|
| 505 |
-
if canFlush {
|
| 506 |
-
flusher.Flush()
|
| 507 |
-
}
|
| 508 |
-
}
|
| 509 |
-
}
|
| 510 |
-
|
| 511 |
-
func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
| 512 |
-
return func(w http.ResponseWriter, r *http.Request) {
|
| 513 |
-
start := time.Now()
|
| 514 |
-
log.Printf("[%s] %s %s", r.Method, r.URL.Path, r.RemoteAddr)
|
| 515 |
-
next(w, r)
|
| 516 |
-
log.Printf("[%s] %s done in %s", r.Method, r.URL.Path, time.Since(start))
|
| 517 |
-
}
|
| 518 |
-
}
|
| 519 |
-
|
| 520 |
-
func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
| 521 |
-
return func(w http.ResponseWriter, r *http.Request) {
|
| 522 |
-
w.Header().Set("Access-Control-Allow-Origin", "*")
|
| 523 |
-
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
| 524 |
-
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, x-api-key")
|
| 525 |
-
if r.Method == http.MethodOptions {
|
| 526 |
-
w.WriteHeader(http.StatusNoContent)
|
| 527 |
-
return
|
| 528 |
-
}
|
| 529 |
-
next(w, r)
|
| 530 |
-
}
|
| 531 |
-
}
|
| 532 |
-
|
| 533 |
-
func main() {
|
| 534 |
-
port := os.Getenv("PORT")
|
| 535 |
-
if port == "" {
|
| 536 |
-
port = "7860"
|
| 537 |
-
}
|
| 538 |
-
|
| 539 |
-
mux := http.NewServeMux()
|
| 540 |
-
mux.HandleFunc("/v1/chat/completions", corsMiddleware(loggingMiddleware(handleChat)))
|
| 541 |
-
mux.HandleFunc("/v1/models", corsMiddleware(loggingMiddleware(handleModels)))
|
| 542 |
-
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
| 543 |
-
w.WriteHeader(http.StatusOK)
|
| 544 |
-
w.Write([]byte(`{"status":"ok"}`))
|
| 545 |
-
})
|
| 546 |
-
|
| 547 |
-
log.Printf("Gateway starting on :%s", port)
|
| 548 |
-
log.Printf("Models: %v", func() []string {
|
| 549 |
-
keys := make([]string, 0, len(modelAliases))
|
| 550 |
-
for k := range modelAliases {
|
| 551 |
-
keys = append(keys, k)
|
| 552 |
-
}
|
| 553 |
-
return keys
|
| 554 |
-
}())
|
| 555 |
-
|
| 556 |
if err := http.ListenAndServe(":"+port, mux); err != nil {
|
| 557 |
log.Fatal(err)
|
| 558 |
}
|
|
|
|
| 26 |
"Kimi-K2": "moonshotai/kimi-k2-instruct",
|
| 27 |
}
|
| 28 |
|
|
|
|
| 29 |
var thinkingModels = map[string]bool{
|
| 30 |
"z-ai/glm4.7": true,
|
| 31 |
}
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
func injectSystemPrompt(messages []Message, modelID string) []Message {
|
|
|
|
| 83 |
filtered := make([]Message, 0, len(messages))
|
| 84 |
for _, m := range messages {
|
| 85 |
if m.Role != "system" {
|
| 86 |
filtered = append(filtered, m)
|
| 87 |
}
|
| 88 |
}
|
|
|
|
| 89 |
prompt, ok := systemPrompts[modelID]
|
| 90 |
if !ok || prompt == "" {
|
| 91 |
return filtered
|
| 92 |
}
|
|
|
|
| 93 |
return append([]Message{{Role: "system", Content: prompt}}, filtered...)
|
| 94 |
}
|
| 95 |
|
|
|
|
| 111 |
http.Error(w, `{"error":{"message":"Unauthorized","type":"auth_error"}}`, http.StatusUnauthorized)
|
| 112 |
return
|
| 113 |
}
|
|
|
|
| 114 |
type ModelObj struct {
|
| 115 |
ID string `json:"id"`
|
| 116 |
Object string `json:"object"`
|
|
|
|
| 121 |
Object string `json:"object"`
|
| 122 |
Data []ModelObj `json:"data"`
|
| 123 |
}
|
|
|
|
| 124 |
models := ModelsResponse{Object: "list"}
|
| 125 |
now := time.Now().Unix()
|
| 126 |
for alias := range modelAliases {
|
| 127 |
models.Data = append(models.Data, ModelObj{
|
| 128 |
+
ID: alias, Object: "model", Created: now, OwnedBy: "nvidia",
|
|
|
|
|
|
|
|
|
|
| 129 |
})
|
| 130 |
}
|
|
|
|
| 131 |
w.Header().Set("Content-Type", "application/json")
|
| 132 |
json.NewEncoder(w).Encode(models)
|
| 133 |
}
|
|
|
|
| 137 |
http.Error(w, `{"error":{"message":"Unauthorized","type":"auth_error"}}`, http.StatusUnauthorized)
|
| 138 |
return
|
| 139 |
}
|
|
|
|
| 140 |
if r.Method != http.MethodPost {
|
| 141 |
http.Error(w, `{"error":{"message":"Method not allowed"}}`, http.StatusMethodNotAllowed)
|
| 142 |
return
|
| 143 |
}
|
|
|
|
| 144 |
var req ChatRequest
|
| 145 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 146 |
http.Error(w, `{"error":{"message":"Invalid request body"}}`, http.StatusBadRequest)
|
|
|
|
| 161 |
TopP: req.TopP,
|
| 162 |
Stop: req.Stop,
|
| 163 |
}
|
|
|
|
| 164 |
if thinkingModels[modelID] {
|
| 165 |
upstream.ExtraBody = &ExtraBody{
|
| 166 |
ChatTemplateKwargs: map[string]interface{}{
|
|
|
|
| 175 |
http.Error(w, `{"error":{"message":"Failed to marshal request"}}`, http.StatusInternalServerError)
|
| 176 |
return
|
| 177 |
}
|
|
|
|
| 178 |
upstreamReq, err := http.NewRequest(http.MethodPost, NvidiaBaseURL+"/chat/completions", bytes.NewReader(body))
|
| 179 |
if err != nil {
|
| 180 |
http.Error(w, `{"error":{"message":"Failed to create upstream request"}}`, http.StatusInternalServerError)
|
|
|
|
| 209 |
flusher, canFlush := w.(http.Flusher)
|
| 210 |
scanner := bufio.NewScanner(resp.Body)
|
| 211 |
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
|
|
|
|
| 212 |
for scanner.Scan() {
|
| 213 |
fmt.Fprintf(w, "%s\n", scanner.Text())
|
| 214 |
if canFlush {
|
|
|
|
| 244 |
if port == "" {
|
| 245 |
port = "7860"
|
| 246 |
}
|
|
|
|
| 247 |
mux := http.NewServeMux()
|
| 248 |
mux.HandleFunc("/v1/chat/completions", corsMiddleware(loggingMiddleware(handleChat)))
|
| 249 |
mux.HandleFunc("/v1/models", corsMiddleware(loggingMiddleware(handleModels)))
|
|
|
|
| 251 |
w.WriteHeader(http.StatusOK)
|
| 252 |
w.Write([]byte(`{"status":"ok"}`))
|
| 253 |
})
|
|
|
|
| 254 |
log.Printf("Gateway starting on :%s", port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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