Documentation
¶
Overview ¶
Package anthropic provides a Genkit plugin for Claude models through Anthropic's OpenAI-compatible endpoint. Anthropic positions that endpoint for testing and comparison; the plugins/anthropic package speaks the native Anthropic API and is the primary way to use Claude models with Genkit.
Index ¶
- func ModelRef(id string, config *ChatConfig) ai.ModelRef
- type Anthropic
- func (a *Anthropic) DefineModel(id string, opts ai.ModelOptions) ai.Modeldeprecated
- func (a *Anthropic) Init(ctx context.Context) []api.Action
- func (a *Anthropic) ListActions(ctx context.Context) []api.ActionDesc
- func (a *Anthropic) Model(g *genkit.Genkit, id string) ai.Modeldeprecated
- func (a *Anthropic) Name() string
- func (a *Anthropic) ResolveAction(atype api.ActionType, id string) api.Action
- type ChatConfig
- type ThinkingConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ModelRef ¶ added in v1.12.0
func ModelRef(id string, config *ChatConfig) ai.ModelRef
ModelRef names a Claude model and carries the config to generate with, so the config is typed at the call site instead of an any the model checks at runtime. A nil config leaves the request's config unset.
ai.WithModel(anthropic.ModelRef("claude-3-5-haiku-20241022", &anthropic.ChatConfig{
MaxOutputTokens: 1024,
}))
id is the model ID, with or without the provider prefix.
Types ¶
type Anthropic ¶
type Anthropic struct {
// APIKey is the key requests are authenticated with. When empty, the
// ANTHROPIC_API_KEY environment variable is used. A key set here or in
// the environment authenticates both surfaces the plugin speaks to: the
// chat endpoint as the bearer token and the native models list as
// x-api-key.
APIKey string
// Opts are additional options for the underlying client, such as
// [option.WithBaseURL] for a different endpoint; they are applied after
// what the plugin composes, so they win on overlap. The credential
// belongs in APIKey or the environment rather than here: a key inside
// Opts is opaque to the plugin, so model listing cannot be authenticated
// with it.
Opts []option.RequestOption
// Models overrides what the plugin knows about a Claude model, keyed by
// model ID, bare or provider-prefixed. Every Claude model already works
// without an entry: known IDs carry curated capabilities and the rest take
// the Claude defaults. Supply an entry only to correct or extend what the
// plugin resolves, most often for a model released after this version of
// the plugin.
//
// &anthropic.Anthropic{Models: map[string]ai.ModelOptions{
// "claude-sonnet-4-5-20250929": {Supports: &ai.ModelSupports{Multiturn: true, Tools: true}},
// }}
//
// Fields left at their zero value keep what the plugin resolves, so an
// entry can pin one capability without restating the label or the
// versions. Entries apply to the models Init registers as well as the
// ones [Anthropic.ListActions] advertises and [Anthropic.ResolveAction] builds,
// which is the way to describe a curated model differently: Init has
// already registered those and nothing can re-register them.
Models map[string]ai.ModelOptions
// contains filtered or unexported fields
}
func (*Anthropic) DefineModel
deprecated
DefineModel builds a Claude model and returns it, without registering it.
Deprecated: describe the model through Anthropic.Models instead. This method builds the model and registers nothing, so the result carries only the model's name: generation resolves a model from that name and serves the request with the capabilities the plugin resolves, not the ones passed here. An entry in Models reaches both paths.
func (*Anthropic) ListActions ¶ added in v1.12.0
func (a *Anthropic) ListActions(ctx context.Context) []api.ActionDesc
ListActions lists the Claude models the configured endpoint exposes, described by the plugin's config schema and capabilities.
func (*Anthropic) Model
deprecated
Model returns a previously registered model.
Deprecated: Generation resolves a model from its name, so looking one up first is rarely necessary: pass ai.WithModelName("anthropic/claude-3-5-haiku-20241022") or, to carry config with it, ModelRef. Use genkit.LookupModel when the action itself is what you need.
func (*Anthropic) ResolveAction ¶ added in v1.12.0
ResolveAction dynamically builds a Claude model, described by the plugin's config schema and capabilities.
type ChatConfig ¶ added in v1.12.0
type ChatConfig struct {
compat_oai.RequestConfig
// Temperature controls the degree of randomness, from 0 to 1; the
// endpoint caps greater values at 1, so the schema stops where the
// behavior does.
Temperature *float64 `` /* 220-byte string literal not displayed */
// MaxOutputTokens is the maximum number of tokens to generate, sent as
// the API's max_tokens.
MaxOutputTokens int `` /* 148-byte string literal not displayed */
// TopP is the nucleus sampling threshold. The endpoint documents no
// range for it, so the schema declares none.
TopP *float64 `` /* 144-byte string literal not displayed */
// StopSequences stop generation when produced by the model; whitespace
// stop sequences are not supported by the endpoint.
StopSequences []string `` /* 165-byte string literal not displayed */
// Thinking controls Claude's extended thinking.
Thinking *ThinkingConfig `` /* 154-byte string literal not displayed */
}
ChatConfig is the per-request config for Claude models served through Anthropic's OpenAI-compatible endpoint. It carries the fields that endpoint honors; OpenAI fields Anthropic documents as ignored (penalties, log probabilities, seed, response_format) are deliberately absent. See https://platform.claude.com/docs/en/api/openai-sdk.
func (ChatConfig) ApplyToChatCompletion ¶ added in v1.12.0
func (c ChatConfig) ApplyToChatCompletion(params *openai.ChatCompletionNewParams)
ApplyToChatCompletion implements compat_oai.ChatConfig: the endpoint's generation fields land on their chat completion counterparts and thinking rides as the endpoint's thinking extra field.
type ThinkingConfig ¶ added in v1.12.0
type ThinkingConfig struct {
// Type turns thinking "enabled" or "disabled". It is not an enum in the
// schema: the endpoint documents no closed set, and the native API's set
// has grown (Claude 5 added adaptive thinking), so a list here would
// reject a value Anthropic accepts.
Type string `json:"type,omitempty" jsonschema_description:"Turns thinking enabled or disabled."`
// BudgetTokens is the maximum number of tokens Claude may think with,
// sent as the API's budget_tokens. Anthropic rejects budgets under 1,024
// tokens, and the budget must stay below the request's max_tokens.
BudgetTokens int `` /* 208-byte string literal not displayed */
}
ThinkingConfig configures Claude's extended thinking through the OpenAI-compatible endpoint. The endpoint does not return the thinking content; the plugins/anthropic package does.