Skip to content

Day 1:搭骨架——反向代理+多模型路由

这个项目做什么

做一个 AI Agent 网关——它站在用户和多个 LLM 之间,负责:智能路由、限流保护、负载均衡、可观测性。这是 Go 在 AI 基础设施里最擅长的事。

代码

bash
mkdir agent_gateway && cd agent_gateway
go mod init agent_gateway
go
package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
    "strings"
)

// ===== 多模型路由 =====
type ModelRouter struct {
    models map[string]*ModelEndpoint
}

type ModelEndpoint struct {
    Name    string
    URL     string
    APIKey  string
    Cost    float64 // 每1K token的费用
}

func NewRouter() *ModelRouter {
    return &ModelRouter{
        models: map[string]*ModelEndpoint{
            "gpt-3.5": {
                Name: "gpt-3.5-turbo",
                URL:  "https://api.openai.com/v1/chat/completions",
                Cost: 0.0015,
            },
            "gpt-4o": {
                Name: "gpt-4o",
                URL:  "https://api.openai.com/v1/chat/completions",
                Cost: 0.03,
            },
            "doubao": {
                Name: "doubao-pro-32k",
                URL:  "https://ark.cn-beijing.volces.com/api/v3/chat/completions",
                Cost: 0.0008,
            },
        },
    }
}

// 智能路由:根据请求内容选择合适的模型
func (r *ModelRouter) Route(ctx context.Context, 
    req ChatRequest) *ModelEndpoint {

    question := req.Messages[len(req.Messages)-1].Content

    // 路由策略
    switch {
    case isComplex(question):
        return r.models["gpt-4o"]    // 复杂推理 → 最强模型
    case isChinese(question):
        return r.models["doubao"]     // 中文 → 便宜又合规
    default:
        return r.models["gpt-3.5"]   // 默认 → 便宜通用
    }
}

func isComplex(q string) bool {
    return len([]rune(q)) > 200 ||
        strings.Contains(q, "分析") ||
        strings.Contains(q, "代码")
}

func isChinese(q string) bool {
    for _, r := range q {
        if r >= 0x4e00 && r <= 0x9fff {
            return true
        }
    }
    return false
}

// ===== 反向代理 =====
func (r *ModelRouter) ProxyHandler(w http.ResponseWriter, 
    req *http.Request) {

    var chatReq ChatRequest
    json.NewDecoder(req.Body).Decode(&chatReq)

    // 选择模型
    endpoint := r.Route(req.Context(), chatReq)
    log.Printf("路由到 %s (%s)", endpoint.Name, endpoint.URL)

    // 创建反向代理
    target, _ := url.Parse(endpoint.URL)
    proxy := httputil.NewSingleHostReverseProxy(target)

    // 注入 API Key
    req.Header.Set("Authorization", "Bearer "+endpoint.APIKey)
    req.Header.Set("X-Routed-By", "agent-gateway")

    proxy.ServeHTTP(w, req)
}

func main() {
    router := NewRouter()

    http.HandleFunc("/v1/chat/completions", router.ProxyHandler)

    fmt.Println("🚀 Agent 网关启动在 http://localhost:8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

运行

bash
go run main.go

# 测试
curl -X POST http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"messages":[{"role":"user","content":"你好"}]}'

Day 1 完成。网关骨架+智能路由跑通了。