Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions vertexai/genai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,19 @@ func float32pToInt32p(x *float32) *int32 {

// NewGenAIClient creates a new Google Vertex AI client and configures the the GenAI components.
func NewGenAIClient(ctx context.Context, cc *genai.ClientConfig) (*Client, error) {
if cc == nil {
cc = &genai.ClientConfig{Backend: genai.BackendVertexAI}
}
if cc.Backend == genai.BackendUnspecified {
cc.Backend = genai.BackendVertexAI
}
ac, err := genai.NewInternalAPIClient(ctx, cc)
if err != nil {
return nil, err
}
if ac.ClientConfig().Backend != genai.BackendVertexAI {
return nil, fmt.Errorf("only Vertex AI backend is supported")
}
return &Client{
AgentEngines: &clientAgentEngines{
AgentEngines: AgentEngines{apiClient: ac},
Expand Down
50 changes: 50 additions & 0 deletions vertexai/genai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"

"google.golang.org/api/iterator"
"google.golang.org/genai"
)

const defaultModel = "gemini-1.0-pro"
Expand Down Expand Up @@ -739,3 +740,52 @@ func printResponse(resp *GenerateContentResponse) {
}
fmt.Println("---")
}

func TestNewGenAIClient(t *testing.T) {
ctx := context.Background()
projectKey := "GOOGLE_CLOUD_PROJECT"
locationKey := "GOOGLE_CLOUD_LOCATION"
originalProjectValue, _ := os.LookupEnv(projectKey)
originalLocationValue, _ := os.LookupEnv(locationKey)
os.Setenv(projectKey, "test-gcp-project")
os.Setenv(locationKey, "us-central1")

t.Cleanup(func() {
os.Setenv(locationKey, originalLocationValue)
os.Setenv(projectKey, originalProjectValue)
})

for _, test := range []struct {
name string
cc *genai.ClientConfig
}{
{name: "nil config", cc: nil},
{name: "empty config", cc: &genai.ClientConfig{}},
} {
t.Run(test.name, func(t *testing.T) {
client, err := NewGenAIClient(ctx, test.cc)
if err != nil {
t.Fatalf("NewGenAIClient() failed unexpectedly, err: %v", err)
}
if client == nil {
t.Error("client must not be nil")
}
})
}
}

func TestNewGenAIClientErrors(t *testing.T) {
ctx := context.Background()
for _, test := range []struct {
name string
cc *genai.ClientConfig
}{
{name: "gemini backend", cc: &genai.ClientConfig{Backend: genai.BackendGeminiAPI}},
} {
t.Run(test.name, func(t *testing.T) {
if _, err := NewGenAIClient(ctx, test.cc); err == nil {
t.Error("wants error, but got nil")
}
})
}
}
Loading