func NewVertexAIClient()

in gollm/gemini.go [125:175]


func NewVertexAIClient(ctx context.Context, opt VertexAIClientOptions) (*GoogleAIClient, error) {
	log := klog.FromContext(ctx)

	cc := &genai.ClientConfig{
		// Project ID is loaded from the GOOGLE_CLOUD_PROJECT environment variable
		// Location/Region is loaded from either GOOGLE_CLOUD_LOCATION or GOOGLE_CLOUD_REGION environment variable
		Backend:  genai.BackendVertexAI,
		Project:  opt.Project,
		Location: opt.Location,
	}

	// ProjectID is required
	if cc.Project == "" {
		projectID, err := findDefaultGCPProject(ctx)
		if err != nil {
			return nil, fmt.Errorf("finding default GCP project ID: %w", err)
		}
		cc.Project = projectID
	}

	// Location is also required
	if cc.Location == "" {
		location := ""

		// Check well-known env vars
		for _, env := range []string{"GOOGLE_CLOUD_LOCATION", "GOOGLE_CLOUD_REGION"} {
			if v := os.Getenv(env); v != "" {
				location = v
				log.Info("got location for vertex client from env var", "location", location, "env", env)
				break
			}
		}

		// Fallback to us-central1
		if location == "" {
			location = "us-central1"
			log.Info("defaulted location for vertex client", "location", opt.Location)
		}

		cc.Location = location
	}

	client, err := genai.NewClient(ctx, cc)
	if err != nil {
		return nil, fmt.Errorf("building gemini client: %w", err)
	}

	return &GoogleAIClient{
		client: client,
	}, nil
}