feat(gateway): 实现网关核心模块
实现内容: - internal/adapter: Provider Adapter抽象层和OpenAI实现 - internal/router: 多Provider路由(支持latency/weighted/availability策略) - internal/handler: OpenAI兼容API端点(/v1/chat/completions, /v1/completions) - internal/ratelimit: Token Bucket和Sliding Window限流器 - internal/alert: 告警系统(支持邮件/钉钉/飞书) - internal/config: 配置管理 - pkg/error: 完整错误码体系 - pkg/model: API请求/响应模型 PRD对齐: - P0-1: 统一API接入 ✅ (OpenAI兼容) - P0-2: 基础路由与稳定性 ✅ (多Provider路由+Fallback) - P0-4: 预算与限流 ✅ (Token Bucket限流) 注意:需要供应链模块支持后再完善成本归因和账单导出
This commit is contained in:
144
gateway/internal/adapter/adapter.go
Normal file
144
gateway/internal/adapter/adapter.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package adapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
// CompletionOptions 完成选项
|
||||
type CompletionOptions struct {
|
||||
Temperature float64
|
||||
MaxTokens int
|
||||
TopP float64
|
||||
Stream bool
|
||||
Stop []string
|
||||
}
|
||||
|
||||
// CompletionResponse 完成响应
|
||||
type CompletionResponse struct {
|
||||
ID string
|
||||
Object string
|
||||
Created int64
|
||||
Model string
|
||||
Choices []Choice
|
||||
Usage Usage
|
||||
}
|
||||
|
||||
// Choice 选择
|
||||
type Choice struct {
|
||||
Index int
|
||||
Message *Message
|
||||
FinishReason string
|
||||
}
|
||||
|
||||
// Message 消息
|
||||
type Message struct {
|
||||
Role string
|
||||
Content string
|
||||
Name string
|
||||
}
|
||||
|
||||
// Usage 使用量
|
||||
type Usage struct {
|
||||
PromptTokens int
|
||||
CompletionTokens int
|
||||
TotalTokens int
|
||||
}
|
||||
|
||||
// StreamChunk 流式响应块
|
||||
type StreamChunk struct {
|
||||
ID string
|
||||
Object string
|
||||
Created int64
|
||||
Model string
|
||||
Choices []StreamChoice
|
||||
}
|
||||
|
||||
// StreamChoice 流式选择
|
||||
type StreamChoice struct {
|
||||
Index int
|
||||
Delta *Delta
|
||||
FinishReason string
|
||||
}
|
||||
|
||||
// Delta 增量
|
||||
type Delta struct {
|
||||
Role string
|
||||
Content string
|
||||
}
|
||||
|
||||
// ProviderAdapter 供应商适配器抽象基类
|
||||
type ProviderAdapter interface {
|
||||
// ChatCompletion 发送聊天完成请求
|
||||
ChatCompletion(ctx context.Context, model string, messages []Message, options CompletionOptions) (*CompletionResponse, error)
|
||||
|
||||
// ChatCompletionStream 流式聊天完成请求
|
||||
ChatCompletionStream(ctx context.Context, model string, messages []Message, options CompletionOptions) (<-chan *StreamChunk, error)
|
||||
|
||||
// GetUsage 获取使用量
|
||||
GetUsage(response *CompletionResponse) Usage
|
||||
|
||||
// MapError 错误码映射
|
||||
MapError(err error) ProviderError
|
||||
|
||||
// HealthCheck 健康检查
|
||||
HealthCheck(ctx context.Context) bool
|
||||
|
||||
// ProviderName 供应商名称
|
||||
ProviderName() string
|
||||
|
||||
// SupportedModels 支持的模型列表
|
||||
SupportedModels() []string
|
||||
}
|
||||
|
||||
// ProviderError 供应商错误
|
||||
type ProviderError struct {
|
||||
Code string
|
||||
Message string
|
||||
HTTPStatus int
|
||||
Retryable bool
|
||||
}
|
||||
|
||||
// Error 实现error接口
|
||||
func (e ProviderError) Error() string {
|
||||
return e.Code + ": " + e.Message
|
||||
}
|
||||
|
||||
// IsRetryable 是否可重试
|
||||
func (e ProviderError) IsRetryable() bool {
|
||||
return e.Retryable
|
||||
}
|
||||
|
||||
// Router 路由器接口
|
||||
type Router interface {
|
||||
// SelectProvider 选择最佳Provider
|
||||
SelectProvider(ctx context.Context, model string) (ProviderAdapter, error)
|
||||
|
||||
// GetFallbackProviders 获取Fallback Providers
|
||||
GetFallbackProviders(ctx context.Context, model string) ([]ProviderAdapter, error)
|
||||
|
||||
// RecordResult 记录调用结果用于负载均衡
|
||||
RecordResult(ctx context.Context, provider string, success bool, latencyMs int64)
|
||||
}
|
||||
|
||||
// HealthChecker 健康检查器
|
||||
type HealthChecker interface {
|
||||
// Check 检查服务健康状态
|
||||
Check(ctx context.Context) error
|
||||
|
||||
// IsHealthy 是否健康
|
||||
IsHealthy() bool
|
||||
}
|
||||
|
||||
// ReadCloser 带错误回调的io.ReadCloser
|
||||
type ReadCloser struct {
|
||||
io.Reader
|
||||
OnClose func() error
|
||||
}
|
||||
|
||||
func (r *ReadCloser) Close() error {
|
||||
if r.OnClose != nil {
|
||||
return r.OnClose()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user