|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package anthropic |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "log/slog" |
| 23 | + "os" |
| 24 | + "reflect" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "github.com/anthropics/anthropic-sdk-go" |
| 28 | + "github.com/anthropics/anthropic-sdk-go/option" |
| 29 | + "github.com/firebase/genkit/go/ai" |
| 30 | + "github.com/firebase/genkit/go/core/api" |
| 31 | + "github.com/firebase/genkit/go/genkit" |
| 32 | + "github.com/firebase/genkit/go/internal/base" |
| 33 | + ant "github.com/firebase/genkit/go/plugins/internal/anthropic" |
| 34 | + "github.com/invopop/jsonschema" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + provider = "anthropic" |
| 39 | + anthropicLabelPrefix = "Anthropic" |
| 40 | +) |
| 41 | + |
| 42 | +// Anthropic is a Genkit plugin for interacting with the Anthropic services |
| 43 | +type Anthropic struct { |
| 44 | + APIKey string // If not provided, defaults to ANTHROPIC_API_KEY |
| 45 | + BaseURL string // Optional. If not provided, defaults to ANTHROPIC_BASE_URL |
| 46 | + |
| 47 | + aclient anthropic.Client // Anthropic client |
| 48 | + mu sync.Mutex // Mutex to control access |
| 49 | + initted bool // Whether the plugin has been initialized |
| 50 | +} |
| 51 | + |
| 52 | +// Name returns the name of the plugin |
| 53 | +func (a *Anthropic) Name() string { |
| 54 | + return provider |
| 55 | +} |
| 56 | + |
| 57 | +// Init initializes the Anthropic plugin and all known models |
| 58 | +func (a *Anthropic) Init(ctx context.Context) []api.Action { |
| 59 | + if a == nil { |
| 60 | + a = &Anthropic{} |
| 61 | + } |
| 62 | + |
| 63 | + a.mu.Lock() |
| 64 | + defer a.mu.Unlock() |
| 65 | + if a.initted { |
| 66 | + panic("plugin already initialized") |
| 67 | + } |
| 68 | + |
| 69 | + apiKey := a.APIKey |
| 70 | + if apiKey == "" { |
| 71 | + apiKey = os.Getenv("ANTHROPIC_API_KEY") |
| 72 | + } |
| 73 | + if apiKey == "" { |
| 74 | + panic("Anthropic requires setting ANTHROPIC_API_KEY in the environment") |
| 75 | + } |
| 76 | + |
| 77 | + opts := []option.RequestOption{option.WithAPIKey(apiKey)} |
| 78 | + |
| 79 | + baseURL := a.BaseURL |
| 80 | + if baseURL == "" { |
| 81 | + baseURL = os.Getenv("ANTHROPIC_BASE_URL") |
| 82 | + } |
| 83 | + if baseURL != "" { |
| 84 | + opts = append(opts, option.WithBaseURL(baseURL)) |
| 85 | + } |
| 86 | + |
| 87 | + ac := anthropic.NewClient(opts...) |
| 88 | + a.aclient = ac |
| 89 | + a.initted = true |
| 90 | + |
| 91 | + return []api.Action{} |
| 92 | +} |
| 93 | + |
| 94 | +// DefineModel defines an unknown model with the given name. |
| 95 | +// The second argument describes the capability of the model. |
| 96 | +// Use [IsDefinedModel] to determine if a model is already defined. |
| 97 | +// After [Init] is called, only the known models are defined. |
| 98 | +func (a *Anthropic) DefineModel(g *genkit.Genkit, name string, opts *ai.ModelOptions) (ai.Model, error) { |
| 99 | + return ant.DefineModel(a.aclient, provider, name, *opts), nil |
| 100 | +} |
| 101 | + |
| 102 | +// ListActions lists all the actions supported by the Anthropic plugin |
| 103 | +func (a *Anthropic) ListActions(ctx context.Context) []api.ActionDesc { |
| 104 | + actions := []api.ActionDesc{} |
| 105 | + |
| 106 | + models, err := listModels(ctx, &a.aclient) |
| 107 | + if err != nil { |
| 108 | + slog.Error("unable to list anthropic models from Anthropic API", "error", err) |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + for _, name := range models { |
| 113 | + model := newModel(a.aclient, name, defaultClaudeOpts) |
| 114 | + if actionDef, ok := model.(api.Action); ok { |
| 115 | + actions = append(actions, actionDef.Desc()) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return actions |
| 120 | +} |
| 121 | + |
| 122 | +// Model returns a previously registered model |
| 123 | +func Model(g *genkit.Genkit, name string) ai.Model { |
| 124 | + return genkit.LookupModel(g, api.NewName(provider, name)) |
| 125 | +} |
| 126 | + |
| 127 | +// IsDefinedModel returns whether a model is already defined |
| 128 | +func IsDefinedModel(g *genkit.Genkit, name string) bool { |
| 129 | + return genkit.LookupModel(g, api.NewName(provider, name)) != nil |
| 130 | +} |
| 131 | + |
| 132 | +// ResolveAction resolves an action with the given name |
| 133 | +func (a *Anthropic) ResolveAction(atype api.ActionType, id string) api.Action { |
| 134 | + switch atype { |
| 135 | + case api.ActionTypeModel: |
| 136 | + return newModel(a.aclient, id, ai.ModelOptions{ |
| 137 | + Label: fmt.Sprintf("%s - %s", anthropicLabelPrefix, id), |
| 138 | + Stage: ai.ModelStageStable, |
| 139 | + Versions: []string{}, |
| 140 | + Supports: defaultClaudeOpts.Supports, |
| 141 | + }).(api.Action) |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// newModel creates a model wihout registering it |
| 147 | +func newModel(client anthropic.Client, name string, opts ai.ModelOptions) ai.Model { |
| 148 | + config := &anthropic.MessageNewParams{} |
| 149 | + |
| 150 | + meta := &ai.ModelOptions{ |
| 151 | + Label: opts.Label, |
| 152 | + Supports: opts.Supports, |
| 153 | + Versions: opts.Versions, |
| 154 | + ConfigSchema: configToMap(config), |
| 155 | + Stage: opts.Stage, |
| 156 | + } |
| 157 | + |
| 158 | + fn := func( |
| 159 | + ctx context.Context, |
| 160 | + input *ai.ModelRequest, |
| 161 | + cb func(context.Context, *ai.ModelResponseChunk) error, |
| 162 | + ) (*ai.ModelResponse, error) { |
| 163 | + return ant.Generate(ctx, client, name, input, cb) |
| 164 | + } |
| 165 | + |
| 166 | + return ai.NewModel(api.NewName(provider, name), meta, fn) |
| 167 | +} |
| 168 | + |
| 169 | +// configToMap converts a config struct to a map[string]any. |
| 170 | +func configToMap(config any) map[string]any { |
| 171 | + r := jsonschema.Reflector{ |
| 172 | + DoNotReference: false, // Prevent $ref usage |
| 173 | + AllowAdditionalProperties: false, |
| 174 | + RequiredFromJSONSchemaTags: true, |
| 175 | + } |
| 176 | + // The anthropic SDK uses a number of wrapper types for float, int, etc. |
| 177 | + // By default, jsonschema will treat these as objects, but we want to |
| 178 | + // treat them as their underlying primitive types. |
| 179 | + r.Mapper = func(r reflect.Type) *jsonschema.Schema { |
| 180 | + if r.Name() == "Opt[float64]" { |
| 181 | + return &jsonschema.Schema{ |
| 182 | + Type: "number", |
| 183 | + } |
| 184 | + } |
| 185 | + if r.Name() == "Opt[int64]" { |
| 186 | + return &jsonschema.Schema{ |
| 187 | + Type: "integer", |
| 188 | + } |
| 189 | + } |
| 190 | + if r.Name() == "Opt[string]" { |
| 191 | + return &jsonschema.Schema{ |
| 192 | + Type: "string", |
| 193 | + } |
| 194 | + } |
| 195 | + if r.Name() == "Opt[bool]" { |
| 196 | + return &jsonschema.Schema{ |
| 197 | + Type: "boolean", |
| 198 | + } |
| 199 | + } |
| 200 | + return nil |
| 201 | + } |
| 202 | + schema := r.Reflect(config) |
| 203 | + result := base.SchemaAsMap(schema) |
| 204 | + return result |
| 205 | +} |
0 commit comments