github.com/anthropics/anthropic-sdk-go@v1.70.1/model.go (about)

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package anthropic
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"net/http"
    10  	"net/url"
    11  	"slices"
    12  	"time"
    13  
    14  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    15  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    16  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    17  	"github.com/anthropics/anthropic-sdk-go/option"
    18  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    19  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    20  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    21  	"github.com/anthropics/anthropic-sdk-go/shared/constant"
    22  )
    23  
    24  // ModelService contains methods and other services that help with interacting with
    25  // the anthropic API.
    26  //
    27  // Note, unlike clients, this service does not read variables from the environment
    28  // automatically. You should not instantiate this service directly, and instead use
    29  // the [NewModelService] method instead.
    30  type ModelService struct {
    31  	Options []option.RequestOption
    32  }
    33  
    34  // NewModelService generates a new service that applies the given options to each
    35  // request. These options are applied after the parent client's options (if there
    36  // is one), and before any request-specific options.
    37  func NewModelService(opts ...option.RequestOption) (r ModelService) {
    38  	r = ModelService{}
    39  	r.Options = opts
    40  	return
    41  }
    42  
    43  // Get a specific model.
    44  //
    45  // The Models API response can be used to determine information about a specific
    46  // model or resolve a model alias to a model ID.
    47  func (r *ModelService) Get(ctx context.Context, modelID string, query ModelGetParams, opts ...option.RequestOption) (res *ModelInfo, err error) {
    48  	for _, v := range query.Betas {
    49  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    50  	}
    51  	if !param.IsOmitted(query.WorkspaceID) {
    52  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", query.WorkspaceID.Value)))
    53  	}
    54  	opts = slices.Concat(r.Options, opts)
    55  	if modelID == "" {
    56  		err = errors.New("missing required model_id parameter")
    57  		return nil, err
    58  	}
    59  	path := fmt.Sprintf("v1/models/%s", modelID)
    60  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    61  	return res, err
    62  }
    63  
    64  // List available models.
    65  //
    66  // The Models API response can be used to determine which models are available for
    67  // use in the API. More recently released models are listed first.
    68  func (r *ModelService) List(ctx context.Context, params ModelListParams, opts ...option.RequestOption) (res *pagination.Page[ModelInfo], err error) {
    69  	var raw *http.Response
    70  	for _, v := range params.Betas {
    71  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    72  	}
    73  	if !param.IsOmitted(params.WorkspaceID) {
    74  		opts = append(opts, option.WithHeader("anthropic-workspace-id", fmt.Sprintf("%v", params.WorkspaceID.Value)))
    75  	}
    76  	opts = slices.Concat(r.Options, opts)
    77  	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
    78  	path := "v1/models"
    79  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	err = cfg.Execute()
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	res.SetPageConfig(cfg, raw)
    88  	return res, nil
    89  }
    90  
    91  // List available models.
    92  //
    93  // The Models API response can be used to determine which models are available for
    94  // use in the API. More recently released models are listed first.
    95  func (r *ModelService) ListAutoPaging(ctx context.Context, params ModelListParams, opts ...option.RequestOption) *pagination.PageAutoPager[ModelInfo] {
    96  	return pagination.NewPageAutoPager(r.List(ctx, params, opts...))
    97  }
    98  
    99  // Indicates whether a capability is supported.
   100  type CapabilitySupport struct {
   101  	// Whether this capability is supported by the model.
   102  	Supported bool `json:"supported" api:"required"`
   103  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   104  	JSON struct {
   105  		Supported   respjson.Field
   106  		ExtraFields map[string]respjson.Field
   107  		raw         string
   108  	} `json:"-"`
   109  }
   110  
   111  // Returns the unmodified JSON received from the API
   112  func (r CapabilitySupport) RawJSON() string { return r.JSON.raw }
   113  func (r *CapabilitySupport) UnmarshalJSON(data []byte) error {
   114  	return apijson.UnmarshalRoot(data, r)
   115  }
   116  
   117  // Context management capability details.
   118  type ContextManagementCapability struct {
   119  	// Indicates whether a capability is supported.
   120  	ClearThinking20251015 CapabilitySupport `json:"clear_thinking_20251015" api:"required"`
   121  	// Indicates whether a capability is supported.
   122  	ClearToolUses20250919 CapabilitySupport `json:"clear_tool_uses_20250919" api:"required"`
   123  	// Indicates whether a capability is supported.
   124  	Compact20260112 CapabilitySupport `json:"compact_20260112" api:"required"`
   125  	// Whether this capability is supported by the model.
   126  	Supported bool `json:"supported" api:"required"`
   127  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   128  	JSON struct {
   129  		ClearThinking20251015 respjson.Field
   130  		ClearToolUses20250919 respjson.Field
   131  		Compact20260112       respjson.Field
   132  		Supported             respjson.Field
   133  		ExtraFields           map[string]respjson.Field
   134  		raw                   string
   135  	} `json:"-"`
   136  }
   137  
   138  // Returns the unmodified JSON received from the API
   139  func (r ContextManagementCapability) RawJSON() string { return r.JSON.raw }
   140  func (r *ContextManagementCapability) UnmarshalJSON(data []byte) error {
   141  	return apijson.UnmarshalRoot(data, r)
   142  }
   143  
   144  // Effort (reasoning_effort) capability details.
   145  type EffortCapability struct {
   146  	// Whether the model supports high effort level.
   147  	High CapabilitySupport `json:"high" api:"required"`
   148  	// Whether the model supports low effort level.
   149  	Low CapabilitySupport `json:"low" api:"required"`
   150  	// Whether the model supports max effort level.
   151  	Max CapabilitySupport `json:"max" api:"required"`
   152  	// Whether the model supports medium effort level.
   153  	Medium CapabilitySupport `json:"medium" api:"required"`
   154  	// Whether this capability is supported by the model.
   155  	Supported bool `json:"supported" api:"required"`
   156  	// Indicates whether a capability is supported.
   157  	Xhigh CapabilitySupport `json:"xhigh" api:"required"`
   158  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   159  	JSON struct {
   160  		High        respjson.Field
   161  		Low         respjson.Field
   162  		Max         respjson.Field
   163  		Medium      respjson.Field
   164  		Supported   respjson.Field
   165  		Xhigh       respjson.Field
   166  		ExtraFields map[string]respjson.Field
   167  		raw         string
   168  	} `json:"-"`
   169  }
   170  
   171  // Returns the unmodified JSON received from the API
   172  func (r EffortCapability) RawJSON() string { return r.JSON.raw }
   173  func (r *EffortCapability) UnmarshalJSON(data []byte) error {
   174  	return apijson.UnmarshalRoot(data, r)
   175  }
   176  
   177  // Model capability information.
   178  type ModelCapabilities struct {
   179  	// Whether the model supports the Batch API.
   180  	Batch CapabilitySupport `json:"batch" api:"required"`
   181  	// Whether the model supports citation generation.
   182  	Citations CapabilitySupport `json:"citations" api:"required"`
   183  	// Whether the model supports code execution tools.
   184  	CodeExecution CapabilitySupport `json:"code_execution" api:"required"`
   185  	// Context management support and available strategies.
   186  	ContextManagement ContextManagementCapability `json:"context_management" api:"required"`
   187  	// Effort (reasoning_effort) support and available levels.
   188  	Effort EffortCapability `json:"effort" api:"required"`
   189  	// Whether the model accepts image content blocks.
   190  	ImageInput CapabilitySupport `json:"image_input" api:"required"`
   191  	// Whether the model accepts PDF content blocks.
   192  	PDFInput CapabilitySupport `json:"pdf_input" api:"required"`
   193  	// Whether the model supports structured output / JSON mode / strict tool schemas.
   194  	StructuredOutputs CapabilitySupport `json:"structured_outputs" api:"required"`
   195  	// Thinking capability and supported type configurations.
   196  	Thinking ThinkingCapability `json:"thinking" api:"required"`
   197  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   198  	JSON struct {
   199  		Batch             respjson.Field
   200  		Citations         respjson.Field
   201  		CodeExecution     respjson.Field
   202  		ContextManagement respjson.Field
   203  		Effort            respjson.Field
   204  		ImageInput        respjson.Field
   205  		PDFInput          respjson.Field
   206  		StructuredOutputs respjson.Field
   207  		Thinking          respjson.Field
   208  		ExtraFields       map[string]respjson.Field
   209  		raw               string
   210  	} `json:"-"`
   211  }
   212  
   213  // Returns the unmodified JSON received from the API
   214  func (r ModelCapabilities) RawJSON() string { return r.JSON.raw }
   215  func (r *ModelCapabilities) UnmarshalJSON(data []byte) error {
   216  	return apijson.UnmarshalRoot(data, r)
   217  }
   218  
   219  type ModelInfo struct {
   220  	// Unique model identifier.
   221  	ID string `json:"id" api:"required"`
   222  	// Model capability information.
   223  	Capabilities ModelCapabilities `json:"capabilities" api:"required"`
   224  	// RFC 3339 datetime string representing the time at which the model was released.
   225  	// May be set to an epoch value if the release date is unknown.
   226  	CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
   227  	// A human-readable name for the model.
   228  	DisplayName string `json:"display_name" api:"required"`
   229  	// Maximum input context window size in tokens for this model.
   230  	MaxInputTokens int64 `json:"max_input_tokens" api:"required"`
   231  	// Maximum value for the `max_tokens` parameter when using this model.
   232  	MaxTokens int64 `json:"max_tokens" api:"required"`
   233  	// Object type.
   234  	//
   235  	// For Models, this is always `"model"`.
   236  	Type constant.Model `json:"type" default:"model"`
   237  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   238  	JSON struct {
   239  		ID             respjson.Field
   240  		Capabilities   respjson.Field
   241  		CreatedAt      respjson.Field
   242  		DisplayName    respjson.Field
   243  		MaxInputTokens respjson.Field
   244  		MaxTokens      respjson.Field
   245  		Type           respjson.Field
   246  		ExtraFields    map[string]respjson.Field
   247  		raw            string
   248  	} `json:"-"`
   249  }
   250  
   251  // Returns the unmodified JSON received from the API
   252  func (r ModelInfo) RawJSON() string { return r.JSON.raw }
   253  func (r *ModelInfo) UnmarshalJSON(data []byte) error {
   254  	return apijson.UnmarshalRoot(data, r)
   255  }
   256  
   257  // Thinking capability details.
   258  type ThinkingCapability struct {
   259  	// Whether this capability is supported by the model.
   260  	Supported bool `json:"supported" api:"required"`
   261  	// Supported thinking type configurations.
   262  	Types ThinkingTypes `json:"types" api:"required"`
   263  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   264  	JSON struct {
   265  		Supported   respjson.Field
   266  		Types       respjson.Field
   267  		ExtraFields map[string]respjson.Field
   268  		raw         string
   269  	} `json:"-"`
   270  }
   271  
   272  // Returns the unmodified JSON received from the API
   273  func (r ThinkingCapability) RawJSON() string { return r.JSON.raw }
   274  func (r *ThinkingCapability) UnmarshalJSON(data []byte) error {
   275  	return apijson.UnmarshalRoot(data, r)
   276  }
   277  
   278  // Supported thinking type configurations.
   279  type ThinkingTypes struct {
   280  	// Whether the model supports thinking with type 'adaptive' (auto).
   281  	Adaptive CapabilitySupport `json:"adaptive" api:"required"`
   282  	// Whether the model supports thinking with type 'enabled'.
   283  	Enabled CapabilitySupport `json:"enabled" api:"required"`
   284  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   285  	JSON struct {
   286  		Adaptive    respjson.Field
   287  		Enabled     respjson.Field
   288  		ExtraFields map[string]respjson.Field
   289  		raw         string
   290  	} `json:"-"`
   291  }
   292  
   293  // Returns the unmodified JSON received from the API
   294  func (r ThinkingTypes) RawJSON() string { return r.JSON.raw }
   295  func (r *ThinkingTypes) UnmarshalJSON(data []byte) error {
   296  	return apijson.UnmarshalRoot(data, r)
   297  }
   298  
   299  type ModelGetParams struct {
   300  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   301  	// Optional header to specify the beta version(s) you want to use.
   302  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   303  	paramObj
   304  }
   305  
   306  type ModelListParams struct {
   307  	// ID of the object to use as a cursor for pagination. When provided, returns the
   308  	// page of results immediately after this object.
   309  	AfterID param.Opt[string] `query:"after_id,omitzero" json:"-"`
   310  	// ID of the object to use as a cursor for pagination. When provided, returns the
   311  	// page of results immediately before this object.
   312  	BeforeID param.Opt[string] `query:"before_id,omitzero" json:"-"`
   313  	// Number of items to return per page.
   314  	//
   315  	// Defaults to `20`. Ranges from `1` to `1000`.
   316  	Limit       param.Opt[int64]  `query:"limit,omitzero" json:"-"`
   317  	WorkspaceID param.Opt[string] `header:"anthropic-workspace-id,omitzero" json:"-"`
   318  	// Optional header to specify the beta version(s) you want to use.
   319  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   320  	paramObj
   321  }
   322  
   323  // URLQuery serializes [ModelListParams]'s query parameters as `url.Values`.
   324  func (r ModelListParams) URLQuery() (v url.Values, err error) {
   325  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   326  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   327  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   328  	})
   329  }