https://github.com/anush008/fastembed-go
Go implementation of @qdrant/fastembed.
https://github.com/anush008/fastembed-go
embeddings rag retrieval retrieval-augmented-generation vector-search
Last synced: 3 months ago
JSON representation
Go implementation of @qdrant/fastembed.
- Host: GitHub
- URL: https://github.com/anush008/fastembed-go
- Owner: Anush008
- License: mit
- Created: 2023-10-06T18:11:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-24T17:16:09.000Z (about 1 year ago)
- Last Synced: 2024-10-12T02:31:32.862Z (8 months ago)
- Topics: embeddings, rag, retrieval, retrieval-augmented-generation, vector-search
- Language: Go
- Homepage: https://pkg.go.dev/github.com/anush008/fastembed-go
- Size: 3.22 MB
- Stars: 45
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## đ Features
- Supports batch embeddings with parallelism using go-routines.
- Uses [@sugarme/tokenizer](https://github.com/sugarme/tokenizer) for fast tokenization.
- Optimized embedding models.The default embedding supports "query" and "passage" prefixes for the input text. The default model is Flag Embedding, which is top of the [MTEB](https://huggingface.co/spaces/mteb/leaderboard) leaderboard.
## đ Not looking for Go?
- Python đ: [fastembed](https://github.com/qdrant/fastembed)
- Rust đĻ: [fastembed-rs](https://github.com/Anush008/fastembed-rs)
- JavaScript đ: [fastembed-js](https://github.com/Anush008/fastembed-js)
## đ¤ Models- [**BAAI/bge-base-en**](https://huggingface.co/BAAI/bge-base-en)
- [**BAAI/bge-base-en-v1.5**](https://huggingface.co/BAAI/bge-base-en-v1.5)
- [**BAAI/bge-small-en**](https://huggingface.co/BAAI/bge-small-en)
- [**BAAI/bge-small-en-v1.5**](https://huggingface.co/BAAI/bge-small-en-v1.5) - Default
- [**BAAI/bge-base-zh-v1.5**](https://huggingface.co/BAAI/bge-base-zh-v1.5)
- [**sentence-transformers/all-MiniLM-L6-v2**](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)## đ Installation
Run the following Go CLI command in your project directory:
```bash
go get -u github.com/anush008/fastembed-go
```## âšī¸ Notice:
The Onnx runtime path is automatically loaded on most environments. However, if you encounter
```sh
panic: Platform-specific initialization failed: Error loading ONNX shared library
```
Set the `ONNX_PATH` env to your Onnx installation.
For eg, on MacOS:
```sh
export ONNX_PATH="/path/to/onnx/lib/libonnxruntime.dylib"
```
On Linux:
```sh
export ONNX_PATH="/path/to/onnx/lib/libonnxruntime.so"
```
You can find the Onnx runtime releases [here](https://github.com/microsoft/onnxruntime/releases).## đ Usage
```go
import "github.com/anush008/fastembed-go"// With default options
model, err := fastembed.NewFlagEmbedding(nil)
if err != nil {
panic(err)
}
defer model.Destroy()// With custom options
options := fastembed.InitOptions{
Model: fastembed.BGEBaseEN,
CacheDir: "model_cache",
MaxLength: 200,
}model, err = fastembed.NewFlagEmbedding(&options)
if err != nil {
panic(err)
}
defer model.Destroy()documents := []string{
"passage: Hello, World!",
"query: Hello, World!",
"passage: This is an example passage.",
// You can leave out the prefix but it's recommended
"fastembed-go is licensed under MIT",
}// Generate embeddings with a batch-size of 25, defaults to 256
embeddings, err := model.Embed(documents, 25) // -> Embeddings length: 4
if err != nil {
panic(err)
}
```### Supports passage and query embeddings for more accurate results
```go
// Generate embeddings for the passages
// The texts are prefixed with "passage" for better results
// The batch size is set to 1 for demonstration purposes
passages := []string{
"This is the first passage. It contains provides more context for retrieval.",
"Here's the second passage, which is longer than the first one. It includes additional information.",
"And this is the third passage, the longest of all. It contains several sentences and is meant for more extensive testing.",
}embeddings, err := model.PassageEmbed(passages, 1) // -> Embeddings length: 3
if err != nil {
panic(err)
}// Generate embeddings for the query
// The text is prefixed with "query" for better retrieval
query := "What is the answer to this generic question?";embeddings, err := model.QueryEmbed(query)
if err != nil {
panic(err)
}
```## đ Under the hood
### Why fast?
It's important we justify the "fast" in FastEmbed. FastEmbed is fast because:
1. Quantized model weights
2. ONNX Runtime which allows for inference on CPU, GPU, and other dedicated runtimes### Why light?
1. No hidden dependencies via Huggingface Transformers
### Why accurate?
1. Better than OpenAI Ada-002
2. Top of the Embedding leaderboards e.g. [MTEB](https://huggingface.co/spaces/mteb/leaderboard)## đ LICENSE
MIT Š [2023](https://github.com/Anush008/fastembed-go/blob/main/LICENSE)