Documentation
¶
Overview ¶
Package goformer provides pure Go BERT-family transformer inference.
It loads model weights directly from HuggingFace safetensors format and runs inference to produce embeddings. No CGO, no ONNX, no native dependencies.
Quick Start ¶
Point it at a HuggingFace model directory containing config.json, tokenizer.json, and model.safetensors:
model, err := goformer.Load("./bge-small-en-v1.5")
if err != nil {
log.Fatal(err)
}
embedding, err := model.Embed("DMA channel configuration")
// embedding is a []float32 of length model.Dims()
Supported Models ¶
Any BERT-family encoder model published in safetensors format on HuggingFace should work. The reference model is BGE-small-en-v1.5 (384-dim, 6 layers, 33M params). Both F32 and F16 safetensors weights are supported (F16 is converted to F32 at load time).
Embeddings ¶
Embed and EmbedBatch produce L2-normalised embeddings using mean pooling over non-padding tokens. The output vectors can be compared directly using dot product (equivalent to cosine similarity for unit vectors).
Example ¶
package main
import (
"fmt"
"log"
"github.com/MichaelAyles/goformer"
)
func main() {
model, err := goformer.Load("./bge-small-en-v1.5")
if err != nil {
log.Fatal(err)
}
embedding, err := model.Embed("DMA channel configuration")
if err != nil {
log.Fatal(err)
}
fmt.Printf("dims: %d\n", len(embedding))
}
Example (Batch) ¶
package main
import (
"fmt"
"log"
"github.com/MichaelAyles/goformer"
)
func main() {
model, err := goformer.Load("./bge-small-en-v1.5")
if err != nil {
log.Fatal(err)
}
embeddings, err := model.EmbedBatch([]string{
"What is a DMA controller?",
"How does SPI communication work?",
"Configure the UART baud rate",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("batch size: %d, dims: %d\n", len(embeddings), len(embeddings[0]))
}
Example (Similarity) ¶
package main
import (
"fmt"
"log"
"github.com/MichaelAyles/goformer"
)
func main() {
model, err := goformer.Load("./bge-small-en-v1.5")
if err != nil {
log.Fatal(err)
}
a, _ := model.Embed("What is a DMA controller?")
b, _ := model.Embed("Direct memory access configuration")
// Dot product of L2-normalised vectors equals cosine similarity.
var similarity float32
for i := range a {
similarity += a[i] * b[i]
}
fmt.Printf("similarity: %.4f\n", similarity)
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model holds a loaded BERT-family transformer model ready for inference. A Model is safe for concurrent use by multiple goroutines.
func Load ¶
Load reads model weights, config, and tokeniser from a HuggingFace model directory. The directory must contain config.json, tokenizer.json, and a .safetensors weight file. Both F32 and F16 safetensors are supported.
func (*Model) Embed ¶
Embed produces a normalised embedding vector for the input text. The returned slice has length Model.Dims. Texts longer than Model.MaxSeqLen tokens are truncated.
func (*Model) EmbedBatch ¶
EmbedBatch produces embeddings for multiple texts. All inputs are padded to the longest sequence in the batch and processed together. Each returned slice has length Model.Dims.