Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(go): add modelgarden and anthropic support #1902

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
31c90e7
add model garden + claude models
hugoaguirre Feb 6, 2025
5b2a639
Merge branch 'main' into haguirre/addModelGarden
hugoaguirre Feb 7, 2025
09089e1
add client management and plugin arch
hugoaguirre Feb 8, 2025
745b038
add test cases and minor refactor
hugoaguirre Feb 10, 2025
d910186
add claude client and test cases (genkit wiring missing)
hugoaguirre Feb 11, 2025
966355e
wiring: system and user text messages + basic conf
hugoaguirre Feb 11, 2025
4e257f5
test: add model version test
hugoaguirre Feb 11, 2025
817bc06
tidy: added constants and docs
hugoaguirre Feb 11, 2025
1caa09c
docs: client.go docs
hugoaguirre Feb 11, 2025
78f759c
feat: add support to claude models
hugoaguirre Feb 12, 2025
4532142
use state.clients instead of standalone variable
hugoaguirre Feb 12, 2025
c27a307
docs: refine anthropic docs
hugoaguirre Feb 12, 2025
6458f88
use the least req params
hugoaguirre Feb 12, 2025
b425e02
fix: minor fixes and docs
hugoaguirre Feb 13, 2025
b95226e
draft: add tooling, media and system prompts features to anthropic
hugoaguirre Feb 13, 2025
946b360
feat: add system and user roles + media
hugoaguirre Feb 13, 2025
90ca4a6
feat: add media support and test cases
hugoaguirre Feb 13, 2025
c4590aa
Merge branch 'main' into haguirre/addModelGarden
hugoaguirre Feb 13, 2025
cf917e1
misc: tool flow complete, disabled tests -- prepare for refactor
hugoaguirre Feb 18, 2025
5a0758e
feat: add tools support (no response yet)
hugoaguirre Feb 19, 2025
e137900
docs: refined logs
hugoaguirre Feb 19, 2025
e77de85
feat: added streaming support
hugoaguirre Feb 19, 2025
6493ef0
test: add tools streaming test
hugoaguirre Feb 19, 2025
f59eb86
fix: refactor to independant packages
hugoaguirre Feb 19, 2025
28825e8
fix: update modelgarden sample
hugoaguirre Feb 19, 2025
04cfeaf
Merge branch 'main' into haguirre/addModelGarden
hugoaguirre Feb 19, 2025
aed1aea
add go.mod and go.sum
hugoaguirre Feb 19, 2025
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
56 changes: 56 additions & 0 deletions go/plugins/vertexai/modelgarden/anthropic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

package modelgarden

import (
"log"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/plugins/internal/gemini"
)

var AnthropicModels = map[string]ai.ModelInfo{
"claude-3-5-sonnet-v2": {
Label: "Vertex AI Model Garden - Claude 3.5 Sonnet",
Supports: &gemini.Multimodal,
Versions: []string{"claude-3-5-sonnet-v2@20241022"},
},
"claude-3-5-sonnet": {
Label: "Vertex AI Model Garden - Claude 3.5 Sonnet",
Supports: &gemini.Multimodal,
Versions: []string{"claude-3-5-sonnet@20240620"},
},
"claude-3-sonnet": {
Label: "Vertex AI Model Garden - Claude 3 Sonnet",
Supports: &gemini.Multimodal,
Versions: []string{"claude-3-sonnet@20240229"},
},
"claude-3-haiku": {
Label: "Vertex AI Model Garden - Claude 3 Haiku",
Supports: &gemini.Multimodal,
Versions: []string{"claude-3-haiku@20240307"},
},
"claude-3-opus": {
Label: "Vertex AI Model Garden - Claude 3 Opus",
Supports: &gemini.Multimodal,
Versions: []string{"claude-3-opus@20240229"},
},
}

type AnthropicCloudClient struct {
region string
project string
}

var AnthropicClient = func(region string, project string) (Client, error) {
return &AnthropicCloudClient{
region: region,
project: project,
}, nil
}

func (a *AnthropicCloudClient) DefineModel(name string, info *ai.ModelInfo) error {
log.Printf("created an anthropic model: %s, versions: %#v", name, info.Versions)
return nil
}
30 changes: 30 additions & 0 deletions go/plugins/vertexai/modelgarden/anthropic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

package modelgarden_test

import (
"testing"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/plugins/vertexai/modelgarden"
)

// keep track of all clients
var clients = modelgarden.NewClientFactory()

func TestAnthropicClient(t *testing.T) {
anthropicClient, err := clients.CreateClient(&modelgarden.ClientConfig{
Region: "us-west-1",
Provider: "anthropic",
Project: "project-123",
})
if err != nil {
t.Fatalf("unable to create anthropic client")
}

err = anthropicClient.DefineModel("foo_model", &ai.ModelInfo{})
if err != nil {
t.Fatalf("unable to define model: %v", err)
}
}
73 changes: 73 additions & 0 deletions go/plugins/vertexai/modelgarden/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

package modelgarden

import (
"errors"
"fmt"
"sync"

"github.com/firebase/genkit/go/ai"
)

// Generic Client interface for supported provider clients
type Client interface {
DefineModel(name string, info *ai.ModelInfo) error
}

type ClientFactory struct {
clients map[string]Client // cache for provider clients
mu sync.Mutex
}

func NewClientFactory() *ClientFactory {
return &ClientFactory{
clients: make(map[string]Client),
}
}

const (
Anthropic string = "anthropic"
)

// Function type for creating clients from supported providers
type ClientCreator func(region string, project string) (Client, error)

// Basic client configuration
type ClientConfig struct {
Creator ClientCreator
Provider string
Project string
Region string
}

func (f *ClientFactory) CreateClient(config *ClientConfig) (Client, error) {
if config == nil {
return nil, errors.New("empty client config")
}

f.mu.Lock()
defer f.mu.Unlock()

// every client will be identified by its provider-region combination
key := fmt.Sprintf("%s-%s", config.Provider, config.Region)
if client, ok := f.clients[key]; ok {
return client, nil // return from cache
}

var client Client
var err error

switch config.Provider {
case Anthropic:
client, err = AnthropicClient(config.Region, config.Project)
if err != nil {
return nil, err
}
}

f.clients[key] = client

return client, nil
}
42 changes: 42 additions & 0 deletions go/plugins/vertexai/modelgarden/modelgarden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0

package modelgarden

import (
"context"
"fmt"

"github.com/firebase/genkit/go/genkit"
)

type ModelGardenOptions struct {
ProjectID string
Region string
Models []string
}

// Init initializes the ModelGarden plugin
// After calling Init, you may call [DefineModel] to create and register
// any additional generative models
func Init(ctx context.Context, g *genkit.Genkit, cfg *ModelGardenOptions) error {
clients := NewClientFactory()
for _, m := range cfg.Models {
// ANTHROPIC
if info, ok := AnthropicModels[m]; ok {
anthropicClient, err := clients.CreateClient(&ClientConfig{
Provider: "anthropic",
Project: "anthropic-project",
Region: "us-west-1",
})
if err != nil {
return fmt.Errorf("unable to create client: %v", err)
}

anthropicClient.DefineModel(m, &info)
continue
}
}

return nil
}
Loading