transformers

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const Version = "1.0.5"

Version is bumped automatically by the release workflow on pushes to main.

Variables

View Source
var AutoConfig autoConfig
View Source
var AutoModelForCausalLM autoModelForCausalLM
View Source
var AutoTokenizer autoTokenizer

Functions

func EnsureONNXRuntimeSharedLib

func EnsureONNXRuntimeSharedLib() (string, error)

EnsureONNXRuntimeSharedLib downloads (if needed) and sets the path to the platform-appropriate ONNX Runtime shared library. It returns the absolute path configured via onnx.SetSharedLibraryPath.

func HFHubDownload

func HFHubDownload(repoID, filename string) (string, error)

HFHubDownload downloads a file from a Hugging Face repo into a local cache. Very simple v1: no auth, no revision. Cache dir can be overridden with CACHE_DIR env; default: ./models/huggingface.co/<repoID>/resolve/main/

func HFHubEnsureFiles

func HFHubEnsureFiles(repoID string, files []string) (map[string]string, error)

HFHubEnsureFiles checks (via HEAD) and downloads a set of files into the cache. Returns a map of filename -> local path.

func HFHubEnsureOptionalFiles

func HFHubEnsureOptionalFiles(repoID string, files []string) (map[string]string, error)

HFHubEnsureOptionalFiles is like HFHubEnsureFiles but skips files that return 404 on HEAD. It returns a map of filename -> local path for the files that were found/downloaded.

Types

type ChatMessage

type ChatMessage struct {
	Role       MessageRole `json:"role"`
	Content    string      `json:"content"`
	Name       string      `json:"name,omitempty"`
	ToolCallID string      `json:"tool_call_id,omitempty"`
}

type Config

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

Config holds model configuration loaded from config.json.

func (*Config) BOS_TOKEN_ID

func (c *Config) BOS_TOKEN_ID() int64

func (*Config) ConvLCache

func (c *Config) ConvLCache() int

func (*Config) EOS_TOKEN_ID

func (c *Config) EOS_TOKEN_ID() int64

func (*Config) HiddenSize

func (c *Config) HiddenSize() int

func (*Config) LayerTypes

func (c *Config) LayerTypes() []string

func (*Config) ModelType

func (c *Config) ModelType() string

accessors

func (*Config) NumAttentionHeads

func (c *Config) NumAttentionHeads() int

func (*Config) NumHiddenLayers

func (c *Config) NumHiddenLayers() int

func (*Config) NumKeyValueHeads

func (c *Config) NumKeyValueHeads() int

func (*Config) PAD_TOKEN_ID

func (c *Config) PAD_TOKEN_ID() int64

func (*Config) Raw

func (c *Config) Raw() map[string]any

func (*Config) StopStrings

func (c *Config) StopStrings() []string

func (*Config) VocabSize

func (c *Config) VocabSize() int

type GenerationOptions

type GenerationOptions struct {
	MaxNewTokens  int
	DoSample      bool
	Streamer      func(ev PipelineStreamEvent) bool // return false to stop early
	StopSequences []string
}

GenerationOptions describes generation parameters for a call.

type Generator

type Generator func(
	messages []ChatMessage,
	options map[string]any,
) ([]map[string]any, error)

Generator is what Pipeline(...) returns. It mirrors the JS/Python pattern: generator(messages, options) -> output.

func Pipeline

func Pipeline(
	task string,
	modelID string,
	options map[string]any,
) (Generator, error)

Pipeline is the exported HF-style entry point:

generator, err := Pipeline("text-generation", modelID, map[string]any{"dtype": "q4"})

Internally it delegates to the lowercase pipelineImpl, so you can define a small-p alias in your own code if you dot-import the package:

var pipeline = transformers.Pipeline

type IOPreset

type IOPreset int

IOPreset describes how we intend to wire inputs/outputs for a model.

const (
	// Automatic – fall back to GetInput/OutputInfo on the session.
	IOPresetAuto IOPreset = iota

	// Simple causal LM: [input_ids, attention_mask] -> [logits]
	IOPresetSimpleCausal

	// LFM2-style: input_ids, attention_mask, position_ids, past_* -> logits, present_*
	IOPresetLFM2
)

type MessageRole

type MessageRole string
const (
	RoleSystem    MessageRole = "system"
	RoleUser      MessageRole = "user"
	RoleAssistant MessageRole = "assistant"
	RoleTool      MessageRole = "tool"
)

type ModelForCausalLM

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

ModelForCausalLM is our ONNX-backed language model wrapper.

func (*ModelForCausalLM) Generate

func (m *ModelForCausalLM) Generate(
	tokenizer *Tokenizer,
	inputIDs [][]int64,
	attentionMask [][]int64,
	opts GenerationOptions,
) ([][]int64, error)

Generate runs a chat-style generation loop with optional streaming. It currently supports batch=1 only.

type PipelineStreamEvent

type PipelineStreamEvent struct {
	TokenID   int64
	DeltaText string
	FullText  string
	Step      int
	Done      bool
}

Streamer event exposed to user callbacks when using "streamer" option.

type Tokenizer

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

Tokenizer wraps sugarme/tokenizer with a HF-like interface.

func (*Tokenizer) BatchDecode

func (t *Tokenizer) BatchDecode(batch [][]int64) ([]string, error)

BatchDecode helper.

func (*Tokenizer) Decode

func (t *Tokenizer) Decode(ids []int64) (string, error)

Decode IDs into plain text.

func (*Tokenizer) Encode

func (t *Tokenizer) Encode(text string, addSpecialTokens bool) ([]int64, error)

Encode plain text into IDs.

func (*Tokenizer) EncodeChat

func (t *Tokenizer) EncodeChat(
	messages []ChatMessage,
) (inputIDs [][]int64, attentionMask [][]int64, promptLen int, rawText string, err error)

EncodeChat encodes the full chat into input IDs and attention mask.

func (*Tokenizer) Info

func (t *Tokenizer) Info() string

type ToolDefinition

type ToolDefinition struct {
	Name        string        `json:"name"`
	Description string        `json:"description,omitempty"`
	Parameters  ToolParameter `json:"parameters"`
}

type ToolParameter

type ToolParameter struct {
	Type        string                   `json:"type"`
	Description string                   `json:"description,omitempty"`
	Enum        []string                 `json:"enum,omitempty"`
	Properties  map[string]ToolParameter `json:"properties,omitempty"`
	Required    []string                 `json:"required,omitempty"`
}

Tool schema types – kept for future use; v1 doesn't yet embed tools into prompt.

Jump to

Keyboard shortcuts

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