Skip to content

feat(go/plugin/anthropic): add Anthropic plugin #3109

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
133 changes: 133 additions & 0 deletions go/plugins/anthropic/anthropic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package anthropic

import (
"context"
"errors"
"fmt"
"os"
"sync"

"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core"
"github.com/firebase/genkit/go/genkit"
aint "github.com/firebase/genkit/go/plugins/internal/anthropic"
)

const (
provider = "anthropic"
anthropicLabelPrefix = "Anthropic"
)

type Anthropic struct {
APIKey string // If not provided, defaults to ANTHROPIC_API_KEY
aclient anthropic.Client // Anthropic client
mu sync.Mutex // Mutex to control access
initted bool // Whether the plugin has been initialized
}

func (a *Anthropic) Name() string {
return provider
}

func (a *Anthropic) Init(ctx context.Context, g *genkit.Genkit) (err error) {
if a == nil {
a = &Anthropic{}
}

a.mu.Lock()
defer a.mu.Unlock()
if a.initted {
return errors.New("plugin already initialized")
}
defer func() {
if err != nil {
err = fmt.Errorf("Anthropic.Init: %w", err)
}
}()

apiKey := a.APIKey
if apiKey == "" {
apiKey = os.Getenv("ANTHROPIC_API_KEY")
}
if apiKey == "" {
return fmt.Errorf("Anthropic requires setting ANTHROPIC_API_KEY in the environment")
}

ac := anthropic.NewClient(
option.WithAPIKey(apiKey),
)
a.aclient = ac
a.initted = true

return nil
}

func (a *Anthropic) DefineModel(g *genkit.Genkit, name string, info *ai.ModelInfo) ai.Model {
return aint.DefineModel(g, a.aclient, provider, name, *info)
}

func (a *Anthropic) IsDefinedModel(g *genkit.Genkit, name string) bool {
return genkit.LookupModel(g, provider, name) != nil
}

func (a *Anthropic) ListActions(ctx context.Context) []core.ActionDesc {
actions := []core.ActionDesc{}

models, err := listModels(ctx, &a.aclient)
if err != nil {
return nil
}

for _, name := range models {
metadata := map[string]any{
"model": map[string]any{
"supports": map[string]any{
"media": true,
"multiturn": true,
"systemRole": true,
"tools": true,
"toolChoice": true,
"constrained": true,
},
},
"versions": []string{},
"stage": string(ai.ModelStageStable),
}
metadata["label"] = fmt.Sprintf("%s - %s", anthropicLabelPrefix, name)

actions = append(actions, core.ActionDesc{
Type: core.ActionTypeModel,
Name: fmt.Sprintf("%s/%s", provider, name),
Key: fmt.Sprintf("/%s/%s/%s", core.ActionTypeModel, provider, name),
Metadata: metadata,
})

}
return actions
}

func (a *Anthropic) ResolveAction(g *genkit.Genkit, atype core.ActionType, name string) error {
switch atype {
case core.ActionTypeModel:
// TODO: anthropic.defineModel()
}
return nil
}
40 changes: 40 additions & 0 deletions go/plugins/anthropic/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package anthropic

import (
"context"

"github.com/anthropics/anthropic-sdk-go"
)

// listModels returns a list of model names supported by the Anthropic client
func listModels(ctx context.Context, client *anthropic.Client) ([]string, error) {
iter := client.Models.ListAutoPaging(ctx, anthropic.ModelListParams{})
models := []string{}

for iter.Next() {
m := iter.Current()
models = append(models, m.DisplayName)
}

if err := iter.Err(); err != nil {
panic(err.Error())
}

return models, nil
}
Loading
Loading