NewStructuredOutputMiddleware 创建中间件实例
(cfg *StructuredOutputMiddlewareConfig)
| 32 | |
| 33 | // NewStructuredOutputMiddleware 创建中间件实例 |
| 34 | func NewStructuredOutputMiddleware(cfg *StructuredOutputMiddlewareConfig) (*StructuredOutputMiddleware, error) { |
| 35 | if cfg == nil { |
| 36 | return nil, errors.New("structured output config is nil") |
| 37 | } |
| 38 | |
| 39 | parser := cfg.Parser |
| 40 | if parser == nil { |
| 41 | parser = structured.NewJSONParser() |
| 42 | } |
| 43 | |
| 44 | priority := cfg.Priority |
| 45 | if priority == 0 { |
| 46 | priority = 60 |
| 47 | } |
| 48 | |
| 49 | return &StructuredOutputMiddleware{ |
| 50 | BaseMiddleware: NewBaseMiddleware("structured_output", priority), |
| 51 | spec: cfg.Spec, |
| 52 | parser: parser, |
| 53 | allowError: cfg.AllowError || cfg.Spec.AllowTextBackup, |
| 54 | }, nil |
| 55 | } |
| 56 | |
| 57 | // WrapModelCall 尝试解析结构化输出 |
| 58 | func (m *StructuredOutputMiddleware) WrapModelCall(ctx context.Context, req *ModelRequest, handler ModelCallHandler) (*ModelResponse, error) { |