1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package anthropic
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "net/http"
10 "net/url"
11 "time"
12
13 "github.com/anthropics/anthropic-sdk-go/internal/apijson"
14 "github.com/anthropics/anthropic-sdk-go/internal/apiquery"
15 "github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
16 "github.com/anthropics/anthropic-sdk-go/option"
17 "github.com/anthropics/anthropic-sdk-go/packages/pagination"
18 "github.com/anthropics/anthropic-sdk-go/packages/param"
19 "github.com/anthropics/anthropic-sdk-go/packages/respjson"
20 "github.com/anthropics/anthropic-sdk-go/shared/constant"
21)
22
23// ModelService contains methods and other services that help with interacting with
24// the anthropic API.
25//
26// Note, unlike clients, this service does not read variables from the environment
27// automatically. You should not instantiate this service directly, and instead use
28// the [NewModelService] method instead.
29type ModelService struct {
30 Options []option.RequestOption
31}
32
33// NewModelService generates a new service that applies the given options to each
34// request. These options are applied after the parent client's options (if there
35// is one), and before any request-specific options.
36func NewModelService(opts ...option.RequestOption) (r ModelService) {
37 r = ModelService{}
38 r.Options = opts
39 return
40}
41
42// Get a specific model.
43//
44// The Models API response can be used to determine information about a specific
45// model or resolve a model alias to a model ID.
46func (r *ModelService) Get(ctx context.Context, modelID string, query ModelGetParams, opts ...option.RequestOption) (res *ModelInfo, err error) {
47 for _, v := range query.Betas {
48 opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%s", v)))
49 }
50 opts = append(r.Options[:], opts...)
51 if modelID == "" {
52 err = errors.New("missing required model_id parameter")
53 return
54 }
55 path := fmt.Sprintf("v1/models/%s", modelID)
56 err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
57 return
58}
59
60// List available models.
61//
62// The Models API response can be used to determine which models are available for
63// use in the API. More recently released models are listed first.
64func (r *ModelService) List(ctx context.Context, params ModelListParams, opts ...option.RequestOption) (res *pagination.Page[ModelInfo], err error) {
65 var raw *http.Response
66 for _, v := range params.Betas {
67 opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%s", v)))
68 }
69 opts = append(r.Options[:], opts...)
70 opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
71 path := "v1/models"
72 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
73 if err != nil {
74 return nil, err
75 }
76 err = cfg.Execute()
77 if err != nil {
78 return nil, err
79 }
80 res.SetPageConfig(cfg, raw)
81 return res, nil
82}
83
84// List available models.
85//
86// The Models API response can be used to determine which models are available for
87// use in the API. More recently released models are listed first.
88func (r *ModelService) ListAutoPaging(ctx context.Context, params ModelListParams, opts ...option.RequestOption) *pagination.PageAutoPager[ModelInfo] {
89 return pagination.NewPageAutoPager(r.List(ctx, params, opts...))
90}
91
92type ModelInfo struct {
93 // Unique model identifier.
94 ID string `json:"id,required"`
95 // RFC 3339 datetime string representing the time at which the model was released.
96 // May be set to an epoch value if the release date is unknown.
97 CreatedAt time.Time `json:"created_at,required" format:"date-time"`
98 // A human-readable name for the model.
99 DisplayName string `json:"display_name,required"`
100 // Object type.
101 //
102 // For Models, this is always `"model"`.
103 Type constant.Model `json:"type,required"`
104 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
105 JSON struct {
106 ID respjson.Field
107 CreatedAt respjson.Field
108 DisplayName respjson.Field
109 Type respjson.Field
110 ExtraFields map[string]respjson.Field
111 raw string
112 } `json:"-"`
113}
114
115// Returns the unmodified JSON received from the API
116func (r ModelInfo) RawJSON() string { return r.JSON.raw }
117func (r *ModelInfo) UnmarshalJSON(data []byte) error {
118 return apijson.UnmarshalRoot(data, r)
119}
120
121type ModelGetParams struct {
122 // Optional header to specify the beta version(s) you want to use.
123 Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
124 paramObj
125}
126
127type ModelListParams struct {
128 // ID of the object to use as a cursor for pagination. When provided, returns the
129 // page of results immediately after this object.
130 AfterID param.Opt[string] `query:"after_id,omitzero" json:"-"`
131 // ID of the object to use as a cursor for pagination. When provided, returns the
132 // page of results immediately before this object.
133 BeforeID param.Opt[string] `query:"before_id,omitzero" json:"-"`
134 // Number of items to return per page.
135 //
136 // Defaults to `20`. Ranges from `1` to `1000`.
137 Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
138 // Optional header to specify the beta version(s) you want to use.
139 Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
140 paramObj
141}
142
143// URLQuery serializes [ModelListParams]'s query parameters as `url.Values`.
144func (r ModelListParams) URLQuery() (v url.Values, err error) {
145 return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
146 ArrayFormat: apiquery.ArrayQueryFormatComma,
147 NestedFormat: apiquery.NestedQueryFormatBrackets,
148 })
149}