Documentation
¶
Overview ¶
Package moderation defines the serializable content-moderation protocol and its single-method Model capability.
A Request can classify multiple texts. Each Output retains provider category names and their Verdict values, while Categories.Flagged provides the aggregate decision. Provider-only options use Options.Extensions.Set so Extensions remains JSON-safe; Request has no arbitrary parameter bag. Implementations and defaults live outside Core.
Example ¶
package main
import (
"fmt"
"github.com/Tangerg/scope/core/moderation"
)
func main() {
request, err := moderation.NewRequest([]string{"content to classify"})
if err != nil {
panic(err)
}
options := moderation.Options{Model: "moderation-model"}
err = options.Validate()
if err != nil {
panic(err)
}
request.Options = options
categories := moderation.Categories{
"violence": {Flagged: true, Score: 0.91},
}
fmt.Println(request.Options.Model, categories.Flagged())
}
Output: moderation-model true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Categories ¶
Categories is the provider-reported category set. Keys retain provider semantics instead of forcing every provider through one closed taxonomy; Flagged collapses the open set only when a caller needs a yes/no policy gate.
func (Categories) Flagged ¶
func (c Categories) Flagged() bool
func (Categories) MarshalJSON ¶
func (c Categories) MarshalJSON() ([]byte, error)
func (*Categories) UnmarshalJSON ¶
func (c *Categories) UnmarshalJSON(data []byte) error
type Model ¶
type Model interface {
// Call classifies one validated input batch without retaining or mutating the
// request. Outputs preserve input order, the returned response belongs to the
// caller, and context cancellation remains identifiable through errors.Is.
Call(ctx context.Context, request *Request) (*Response, error)
}
Model is the complete provider-neutral moderation SPI. Implementations reject explicit options they cannot represent and return responses that pass Validate; provider defaults and identity belong to provider construction.
type Options ¶
type Options struct {
Model string `json:"model"`
Extensions metadata.Extensions `json:"extensions,omitzero"`
}
Options holds per-request moderation configuration. Resolve overlays only explicitly supplied values, merges namespaced extensions, and never aliases mutable data from either input.
func (Options) MarshalJSON ¶
func (*Options) UnmarshalJSON ¶
type Output ¶
type Output struct {
Categories Categories `json:"categories,omitzero"`
Metadata metadata.Map `json:"metadata,omitzero"`
}
func NewOutput ¶
func NewOutput(categories Categories, outputMetadata metadata.Map) (*Output, error)
NewOutput validates and snapshots one provider result before it enters a Response.
func (Output) MarshalJSON ¶
func (*Output) UnmarshalJSON ¶
type Request ¶
type Request struct {
Texts []string `json:"texts,omitzero"`
Options Options `json:"options,omitzero"`
}
func NewRequest ¶
NewRequest snapshots the caller's input texts.
func (Request) MarshalJSON ¶
func (*Request) UnmarshalJSON ¶
type Response ¶
type Response struct {
// Outputs holds one entry per input, in the same order.
Outputs []*Output `json:"outputs,omitzero"`
Metadata *ResponseMetadata `json:"metadata,omitzero"`
}
func NewResponse ¶
func NewResponse(outputs []*Output, responseMetadata *ResponseMetadata) (*Response, error)
func (Response) MarshalJSON ¶
func (*Response) UnmarshalJSON ¶
func (*Response) ValidateFor ¶ added in v0.16.0
ValidateFor requires one output per input, in input order.
type ResponseMetadata ¶
type ResponseMetadata struct {
ID string `json:"id"`
Model string `json:"model"`
CreatedAt time.Time `json:"created_at,omitzero"`
Extra metadata.Map `json:"extra,omitzero"`
}
func (ResponseMetadata) MarshalJSON ¶
func (r ResponseMetadata) MarshalJSON() ([]byte, error)
func (*ResponseMetadata) UnmarshalJSON ¶
func (r *ResponseMetadata) UnmarshalJSON(data []byte) error
type Verdict ¶
Verdict is one moderation dimension's outcome — a flagged bit plus a confidence score in [0, 1].