func BatchEmbedContents()

in go/embed.go [46:78]


func BatchEmbedContents() error {
	// [START batch_embed_contents]
	ctx := context.Background()
	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		APIKey:  os.Getenv("GEMINI_API_KEY"),
		Backend: genai.BackendGeminiAPI,
	})
	if err != nil {
		log.Fatal(err)
	}

	contents := []*genai.Content{
		genai.NewContentFromText("What is the meaning of life?", genai.RoleUser),
		genai.NewContentFromText("How much wood would a woodchuck chuck?", genai.RoleUser),
		genai.NewContentFromText("How does the brain work?", genai.RoleUser),
	}

	outputDim := int32(10)
	result, err := client.Models.EmbedContent(ctx, "text-embedding-004", contents, &genai.EmbedContentConfig{
		OutputDimensionality: &outputDim,
	})
	if err != nil {
		log.Fatal(err)
	}
	
	embeddings, err := json.MarshalIndent(result.Embeddings, "", "  ")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(embeddings))
	// [END batch_embed_contents]
	return err
}