Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions components/model/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Options struct {
MaxTokens *int
// Stop is the stop words for the model, which controls the stopping condition of the model.
Stop []string
// ResponseFormat controls the structured output format of the model response.
ResponseFormat *schema.ResponseFormat

// Options only available for chat model.

Expand Down Expand Up @@ -173,6 +175,15 @@ func WithAgenticToolChoice(toolChoice *schema.AgenticToolChoice) Option {
}
}

// WithResponseFormat sets the response format for the model.
func WithResponseFormat(rf *schema.ResponseFormat) Option {
return Option{
apply: func(opts *Options) {
opts.ResponseFormat = rf
},
}
}

// WrapImplSpecificOptFn is the option to wrap the implementation specific option function.
// WrapImplSpecificOptFn wraps an implementation-specific option function into
// an [Option] so it can be passed alongside standard options.
Expand Down
43 changes: 43 additions & 0 deletions schema/response_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2026 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package schema

import "github.com/eino-contrib/jsonschema"

// ResponseFormatType specifies the format the model must output.
type ResponseFormatType string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要有个 text 类型吗,印象中似乎有的模型有这个枚举值?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有这个 enum,但传与不传的效果我没测试出来差异,所以暂时没加。


const (
// ResponseFormatTypeJSONObject forces the model to output valid JSON.
ResponseFormatTypeJSONObject ResponseFormatType = "json_object"
// ResponseFormatTypeJSONSchema forces the model to output JSON conforming to a given schema.
ResponseFormatTypeJSONSchema ResponseFormatType = "json_schema"
)

// ResponseFormat controls the structured output format of the model response.
type ResponseFormat struct {
// Type specifies the response format type.
Type ResponseFormatType
// JSONSchema specifies the schema when Type is ResponseFormatJSONSchema.
JSONSchema *ResponseFormatJSONSchema
}

// ResponseFormatJSONSchema defines the JSON Schema for structured output.
type ResponseFormatJSONSchema struct {
// Schema is the JSON Schema definition.
Schema *jsonschema.Schema
}
Loading