How to Implement Embeddings and Similarity Search in Go

Implement embeddings and similarity search in Go by initializing an embedding model, connecting to a vector database, and using the store's SimilaritySearch method to retrieve relevant documents.

Implement embeddings and similarity search in Go by initializing an embedding model, connecting to a vector database, and using the store's SimilaritySearch method to retrieve relevant documents.

import (
	"context"
	"github.com/tmc/langchaingo/embeddings"
	"github.com/tmc/langchaingo/llms/googleai"
	"github.com/tmc/langchaingo/vectorstores/weaviate"
)

func setupRAG(ctx context.Context, apiKey string) (weaviate.Store, error) {
	geminiClient, err := googleai.New(ctx, googleai.WithAPIKey(apiKey))
	if err != nil {
		return nil, err
	}
	emb, err := embeddings.NewEmbedder(geminiClient)
	if err != nil {
		return nil, err
	}
	return weaviate.New(
		weaviate.WithEmbedder(emb),
		weaviate.WithScheme("http"),
		weaviate.WithHost("localhost:9035"),
		weaviate.WithIndexName("Document"),
	)
}
  1. Initialize the LLM client with your API key: geminiClient, err := googleai.New(ctx, googleai.WithAPIKey(apiKey))
  2. Create an embedder from the client: emb, err := embeddings.NewEmbedder(geminiClient)
  3. Connect to the vector store using the embedder: store, err := weaviate.New(weaviate.WithEmbedder(emb), weaviate.WithHost("localhost:9035"))
  4. Add documents to the store: _, err = store.AddDocuments(ctx, docs)
  5. Perform similarity search: results, err := store.SimilaritySearch(ctx, "query text", 3)