How to Use LangChain Alternatives in Go (LangChainGo)

Initialize LangChainGo in Go by importing the package, creating a Google AI client, and connecting to a Weaviate vector store for RAG applications.

Use the github.com/tmc/langchaingo package to abstract LLM and vector store interactions in Go. Initialize the Google AI client with your API key, create an embedder, and connect to Weaviate using the LangChainGo vector store wrapper.

package main

import (
	"context"
	"log"
	"net/http"
	"os"

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

func main() {
	ctx := context.Background()
	apiKey := os.Getenv("GEMINI_API_KEY")

	geminiClient, err := googleai.New(ctx,
		googleai.WithAPIKey(apiKey),
		googleai.WithDefaultEmbeddingModel("text-embedding-004"))
	if err != nil {
		log.Fatal(err)
	}

	emb, err := embeddings.NewEmbedder(geminiClient)
	if err != nil {
		log.Fatal(err)
	}

	wvStore, err := weaviate.New(
		weaviate.WithEmbedder(emb),
		weaviate.WithScheme("http"),
		weaviate.WithHost("localhost:9035"),
		weaviate.WithIndexName("Document"),
	)
	if err != nil {
		log.Fatal(err)
	}

	// Use wvStore.AddDocuments and wvStore.SimilaritySearch for RAG logic
	_ = wvStore
	_ = geminiClient
	log.Println("LangChainGo initialized")
}