### Install all-MiniLM-L6-v2 Go Library Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/README.md Install the Go library using the go get command. This is the first step to using the sentence transformer model in your Go projects. ```bash go get github.com/clems4ever/all-minilm-l6-v2-go ``` -------------------------------- ### Install ONNX Runtime on Ubuntu/Debian Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/README.md Steps to install the ONNX Runtime library on Ubuntu or Debian systems. This is a prerequisite for using the Go library. ```bash ORT_VERSION=1.23.0 wget https://github.com/microsoft/onnxruntime/releases/download/v${ORT_VERSION}/onnxruntime-linux-x64-${ORT_VERSION}.tgz tar -xzf onnxruntime-linux-x64-${ORT_VERSION}.tgz sudo cp -r onnxruntime-linux-x64-${ORT_VERSION}/include/* /usr/local/include/ sudo cp -r onnxruntime-linux-x64-${ORT_VERSION}/lib/* /usr/local/lib/ ``` -------------------------------- ### Go Library Usage for Sentence Embeddings Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/README.md Example of how to use the all-MiniLM-L6-v2 Go library to compute sentence embeddings. Ensure ONNX Runtime is correctly configured. ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { panic(err) } defer model.Close() // Base sentence to compare against baseSentence := "The dog is running in the park" // Three candidate sentences with varying degrees of similarity candidates := []string{ "A dog runs through the park", // Very similar "The cat is sleeping on the couch", // Somewhat similar "I love eating pizza for dinner", // Not similar } // Compute embeddings baseEmbedding, _ := model.Compute(baseSentence) candidateEmbeddings, _ := model.ComputeBatch(candidates) // computeCosineSimilarity.. // ... } ``` -------------------------------- ### Docker Quick Start for Sentence Embedding Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/README.md Use this command to quickly generate sentence embeddings using Docker without any local installation. It pipes a sentence to the Docker container and outputs the embedding in JSON format. ```bash echo "hello" | docker run -i ghcr.io/clems4ever/all-minilm-l6-v2-go:main . -o json {"embedding":[-0.06277175,0.054958846,0.052164856,...,0.051483486,0.0070921564],"sentence":"hello"} ``` -------------------------------- ### Initialize Sentence Embedding Model in Go Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Creates a new Model instance, loading the embedded ONNX model and tokenizer. Requires ONNX Runtime to be installed. Resources must be released by calling Close(). ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { // Initialize model with explicit runtime path model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model.Close() // Or rely on ONNXRUNTIME_LIB_PATH environment variable // export ONNXRUNTIME_LIB_PATH=libonnxruntime.so model2, err := all_minilm_l6_v2.NewModel() if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model2.Close() fmt.Println("Model initialized successfully") // Output: Model initialized successfully } ``` -------------------------------- ### CLI Tool for Command Line Embeddings Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Install and use the command-line interface to generate embeddings directly from your terminal. Supports various output formats including JSON and space-separated values, and batch processing for efficiency. ```bash # Install the CLI go install github.com/clems4ever/all-minilm-l6-v2-go/cmd/cli@latest # Generate embedding as JSON echo "hello world" | all-minilm-l6-v2-go --runtime-path libonnxruntime.so -o json # Output: {"embedding":[-0.0627,0.0549,...],"sentence":"hello world"} # Generate embedding with pretty JSON echo "semantic search is powerful" | all-minilm-l6-v2-go -o json-pretty # Output: # { # "embedding": [-0.0412, 0.0891, ...], # "sentence": "semantic search is powerful" # } # Process multiple sentences in batch mode (more efficient) cat < results[j].Similarity }) // Display top results fmt.Printf("Query: %s\n\n", query) fmt.Println("Top Results:") for i, r := range results[:3] { fmt.Printf("%d. (%.4f) %s\n", i+1, r.Similarity, r.Document) } // Output: // Query: What programming language is good for ML? // // Top Results: // 1. (0.6842) Python is widely used for machine learning and data science. // 2. (0.3521) Go is a statically typed programming language designed at Google. // 3. (0.2987) JavaScript runs in web browsers and Node.js servers. } ``` -------------------------------- ### Generate CPU Profile Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/PERFORMANCE.md Run a specific benchmark and generate a CPU profile for performance analysis. The profile will be saved to 'cpu.prof'. ```bash go test ./all_minilm_l6_v2 -bench=BenchmarkBatch8 -run=^$ -cpuprofile=cpu.prof ``` -------------------------------- ### Run Specific Benchmark Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/PERFORMANCE.md Execute a particular benchmark test, such as 'BenchmarkBatch8'. Use '-benchmem' to include memory allocation statistics. ```bash go test ./all_minilm_l6_v2 -bench=BenchmarkBatch8 -run=^$ -benchmem ``` -------------------------------- ### Specify ONNX Runtime Path in Go Code Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/README.md Configure the ONNX Runtime path directly within your Go code using the WithRuntimePath option when creating a new model instance. This provides a programmatic way to manage the library location. ```go model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) ``` -------------------------------- ### Standalone Tokenizer Access Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Create a tokenizer instance for direct access to text tokenization. This is useful for preprocessing text before embedding computation or for analyzing token IDs and attention masks. ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { tokenizer, err := all_minilm_l6_v2.NewTokenizer() if err != nil { log.Fatalf("Failed to create tokenizer: %v", err) } sentence := "Hello, world!" // Encode text to tokens encoding, err := tokenizer.Encode(sentence, false) if err != nil { log.Fatalf("Failed to encode: %v", err) } fmt.Printf("Token IDs: %v\n", encoding.Ids) fmt.Printf("Attention Mask: %v\n", encoding.AttentionMask) // Decode tokens back to text decoded := tokenizer.Decode(encoding.Ids, true) fmt.Printf("Decoded: %s\n", decoded) } ``` -------------------------------- ### Set ONNX Runtime Library Path Environment Variable Source: https://github.com/clems4ever/all-minilm-l6-v2-go/blob/main/README.md Use this command to set the ONNXRUNTIME_LIB_PATH environment variable before running your Go program. This is a quick fix for cases where the shared library is not found. ```bash export ONNXRUNTIME_LIB_PATH=libonnxruntime.so go run your_program.go ``` -------------------------------- ### Compute Batch Sentence Embeddings in Go Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Computes embeddings for multiple sentences efficiently in a single batch operation. Returns a [][]float32 slice, where each inner slice is a 384-dimensional embedding. ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model.Close() sentences := []string{ "The quick brown fox jumps over the lazy dog.", "Machine learning is transforming industries.", "I love programming in Go.", "Semantic search improves information retrieval.", } // Batch processing is ~30% faster than individual calls embeddings, err := model.ComputeBatch(sentences, false) if err != nil { log.Fatalf("Failed to compute batch embeddings: %v", err) } fmt.Printf("Processed %d sentences\n", len(embeddings)) for i, emb := range embeddings { fmt.Printf("Sentence %d: %d dimensions\n", i+1, len(emb)) } // Output: // Processed 4 sentences // Sentence 1: 384 dimensions // Sentence 2: 384 dimensions // Sentence 3: 384 dimensions // Sentence 4: 384 dimensions } ``` -------------------------------- ### ComputeBatch - Generate Embeddings for Multiple Sentences Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Computes embeddings for multiple sentences in a single batch operation. This is significantly more efficient than calling `Compute()` multiple times. Returns a `[][]float32` slice where each inner slice is a 384-dimensional embedding. ```APIDOC ## ComputeBatch - Generate Embeddings for Multiple Sentences ### Description Computes embeddings for multiple sentences in a single batch operation. This is significantly more efficient than calling `Compute()` multiple times. Returns a `[][]float32` slice where each inner slice is a 384-dimensional embedding. ### Method `ComputeBatch(sentences []string, addSpecialTokens bool)` ### Parameters #### Path Parameters - `sentences` ([]string) - Required - A slice of sentences to generate embeddings for. - `addSpecialTokens` (bool) - Required - Whether to add special tokens during tokenization. ### Request Example ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model.Close() sentences := []string{ "The quick brown fox jumps over the lazy dog.", "Machine learning is transforming industries.", "I love programming in Go.", "Semantic search improves information retrieval.", } // Batch processing is ~30% faster than individual calls embeddings, err := model.ComputeBatch(sentences, false) if err != nil { log.Fatalf("Failed to compute batch embeddings: %v", err) } fmt.Printf("Processed %d sentences\n", len(embeddings)) for i, emb := range embeddings { fmt.Printf("Sentence %d: %d dimensions\n", i+1, len(emb)) } } ``` ### Response #### Success Response (200) - `[][]float32`: A slice of slices, where each inner slice is a 384-dimensional embedding vector for a corresponding input sentence. #### Response Example ``` Processed 4 sentences Sentence 1: 384 dimensions Sentence 2: 384 dimensions Sentence 3: 384 dimensions Sentence 4: 384 dimensions ``` ``` -------------------------------- ### Compute Sentence Embedding in Go Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Computes a 384-dimensional embedding vector for a single sentence. Set addSpecialTokens to false for this model. Returns a []float32 slice. ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model.Close() sentence := "Hello, world! This is a test sentence." // Compute embedding (addSpecialTokens=false is typical for this model) embedding, err := model.Compute(sentence, false) if err != nil { log.Fatalf("Failed to compute embedding: %v", err) } fmt.Printf("Embedding dimension: %d\n", len(embedding)) fmt.Printf("First 5 values: %v\n", embedding[:5]) // Output: // Embedding dimension: 384 // First 5 values: [-0.0627 0.0549 0.0521 -0.0312 0.0847] } ``` -------------------------------- ### Calculate Cosine Similarity Between Embeddings Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Use this function to compare the semantic similarity of two embedding vectors. It returns a value between -1 and 1. Ensure the model is initialized and embeddings are computed before calling. ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model.Close() // Base sentence to compare against baseSentence := "The dog is running in the park" // Candidate sentences with varying similarity candidates := []string{ "A dog runs through the park", // Very similar "The cat is sleeping on the couch", // Somewhat similar "I love eating pizza for dinner", // Not similar } // Compute embeddings baseEmbedding, _ := model.Compute(baseSentence, false) candidateEmbeddings, _ := model.ComputeBatch(candidates, false) // Calculate and display similarities fmt.Printf("Base: %s\n\n", baseSentence) fmt.Println("Similarity | Sentence") fmt.Println("-----------|---------") for i, candidate := range candidates { similarity := all_minilm_l6_v2.CosineSimilarity(baseEmbedding, candidateEmbeddings[i]) fmt.Printf(" %.4f | %s\n", similarity, candidate) } } ``` -------------------------------- ### Compute - Generate Embedding for Single Sentence Source: https://context7.com/clems4ever/all-minilm-l6-v2-go/llms.txt Computes a 384-dimensional embedding vector for a single sentence. The `addSpecialTokens` parameter controls whether to add special tokens during tokenization (typically set to `false` for this model). Returns a `[]float32` slice representing the semantic vector of the input text. ```APIDOC ## Compute - Generate Embedding for Single Sentence ### Description Computes a 384-dimensional embedding vector for a single sentence. The `addSpecialTokens` parameter controls whether to add special tokens during tokenization (typically set to `false` for this model). Returns a `[]float32` slice representing the semantic vector of the input text. ### Method `Compute(sentence string, addSpecialTokens bool)` ### Parameters #### Path Parameters - `sentence` (string) - Required - The input sentence to generate an embedding for. - `addSpecialTokens` (bool) - Required - Whether to add special tokens during tokenization. ### Request Example ```go package main import ( "fmt" "log" "github.com/clems4ever/all-minilm-l6-v2-go/all_minilm_l6_v2" ) func main() { model, err := all_minilm_l6_v2.NewModel( all_minilm_l6_v2.WithRuntimePath("libonnxruntime.so")) if err != nil { log.Fatalf("Failed to create model: %v", err) } defer model.Close() sentence := "Hello, world! This is a test sentence." // Compute embedding (addSpecialTokens=false is typical for this model) embedding, err := model.Compute(sentence, false) if err != nil { log.Fatalf("Failed to compute embedding: %v", err) } fmt.Printf("Embedding dimension: %d\n", len(embedding)) fmt.Printf("First 5 values: %v\n", embedding[:5]) } ``` ### Response #### Success Response (200) - `[]float32`: A slice of float32 representing the 384-dimensional embedding vector. #### Response Example ``` Embedding dimension: 384 First 5 values: [-0.0627 0.0549 0.0521 -0.0312 0.0847] ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.