topiccontrol

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package topiccontrol implements classify.TopicClassifier against NVIDIA's Llama-3.1-NemoGuard-8B TopicControl model, served by NIM over an OpenAI-compatible chat-completions endpoint.

The model takes a topical instruction as the system message and a conversation whose final entry is the user message to judge, and answers with exactly one of two labels: "on-topic" or "off-topic". Rendering a classify.TopicPolicy into that instruction lives here rather than in the eval handler, so a future non-chat backend can consume the same policy without the pack changing.

Index

Constants

View Source
const DefaultModel = "nvidia/llama-3.1-nemoguard-8b-topic-control"

DefaultModel is the TopicControl model id. NIM accepts it as the `model` field on the OpenAI-compatible endpoint it serves.

Variables

This section is empty.

Functions

func ParseLabel

func ParseLabel(raw string) classify.TopicDecision

ParseLabel maps the model's answer onto a decision. Anything that is not one of the two labels is unknown — never silently allow, because "the classifier said something unexpected" and "the message is in scope" are different facts and the policy decides what to do about each.

func RenderPolicy

func RenderPolicy(p classify.TopicPolicy) string

RenderPolicy turns a structured policy into the system instruction the TopicControl model expects.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements classify.TopicClassifier against a TopicControl NIM.

func New

func New(cfg Config) (*Client, error)

New builds a Client.

func (*Client) ClassifyTopic

func (c *Client) ClassifyTopic(
	ctx context.Context, req classify.TopicRequest,
) (classify.TopicResult, error)

ClassifyTopic renders the policy as the system instruction, replays history for reference resolution, and puts the message under judgment last.

func (*Client) HTTPTimeout

func (c *Client) HTTPTimeout() time.Duration

HTTPTimeout reports the per-call timeout this client will apply. Exported for tests: the timeout is only observable otherwise by waiting for it to expire.

type Config

type Config struct {
	// BaseURL is the OpenAI-compatible root of the NIM deployment, e.g.
	// http://topic-control:8000/v1. Required — there is no public default
	// endpoint for a self-hosted NIM, and guessing one would turn a
	// misconfiguration into a confusing timeout.
	BaseURL string
	// APIKey is optional: a locally deployed NIM usually needs none, while
	// NVIDIA-hosted endpoints do.
	APIKey string
	// Model overrides DefaultModel.
	Model string
	// Timeout bounds a single classification call. Zero means
	// defaultHTTPTimeout. Note this caps the call regardless of the deadline
	// on the context passed to ClassifyTopic: whichever is shorter wins, so a
	// caller cannot extend it by supplying a longer context.
	Timeout time.Duration
	// HTTPClient overrides the default client entirely, Timeout included.
	// Mainly for tests.
	HTTPClient *http.Client
}

Config configures a Client.

Jump to

Keyboard shortcuts

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