moderation

package
v0.58.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
var (
	ErrInvalidOptions  = errors.New("moderation: invalid options")
	ErrInvalidRequest  = errors.New("moderation: invalid request")
	ErrInvalidResponse = errors.New("moderation: invalid response")
)

Functions

This section is empty.

Types

type Categories

type Categories map[string]Verdict

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 ModelFunc

type ModelFunc func(context.Context, *Request) (*Response, error)

func (ModelFunc) Call

func (m ModelFunc) Call(ctx context.Context, request *Request) (*Response, error)

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) Clone

func (o Options) Clone() Options

func (Options) MarshalJSON

func (o Options) MarshalJSON() ([]byte, error)

func (Options) Resolve

func (o Options) Resolve(override Options) (Options, error)

func (*Options) UnmarshalJSON

func (o *Options) UnmarshalJSON(data []byte) error

func (Options) Validate

func (o Options) Validate() error

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 (o Output) MarshalJSON() ([]byte, error)

func (*Output) UnmarshalJSON

func (o *Output) UnmarshalJSON(data []byte) error

func (*Output) Validate

func (o *Output) Validate() error

type Request

type Request struct {
	Texts []string `json:"texts,omitzero"`

	Options Options `json:"options,omitzero"`
}

func NewRequest

func NewRequest(texts []string) (*Request, error)

NewRequest snapshots the caller's input texts.

func (Request) MarshalJSON

func (r Request) MarshalJSON() ([]byte, error)

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(data []byte) error

func (*Request) Validate

func (r *Request) Validate() error

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) First

func (r *Response) First() *Output

func (Response) MarshalJSON

func (r Response) MarshalJSON() ([]byte, error)

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(data []byte) error

func (*Response) Validate

func (r *Response) Validate() error

func (*Response) ValidateFor added in v0.16.0

func (r *Response) ValidateFor(request *Request) error

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

type Verdict struct {
	Flagged bool `json:"flagged"`

	Score float64 `json:"score"`
}

Verdict is one moderation dimension's outcome — a flagged bit plus a confidence score in [0, 1]. JSON requires both fields, including explicit false and zero values, so an absent result cannot become an unflagged verdict.

func (Verdict) MarshalJSON

func (v Verdict) MarshalJSON() ([]byte, error)

func (*Verdict) UnmarshalJSON

func (v *Verdict) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL