1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package openai
  4
  5import (
  6	"context"
  7	"errors"
  8	"fmt"
  9	"net/http"
 10
 11	"github.com/openai/openai-go/internal/apijson"
 12	"github.com/openai/openai-go/internal/requestconfig"
 13	"github.com/openai/openai-go/option"
 14	"github.com/openai/openai-go/packages/pagination"
 15	"github.com/openai/openai-go/packages/respjson"
 16	"github.com/openai/openai-go/shared/constant"
 17)
 18
 19// ModelService contains methods and other services that help with interacting with
 20// the openai API.
 21//
 22// Note, unlike clients, this service does not read variables from the environment
 23// automatically. You should not instantiate this service directly, and instead use
 24// the [NewModelService] method instead.
 25type ModelService struct {
 26	Options []option.RequestOption
 27}
 28
 29// NewModelService generates a new service that applies the given options to each
 30// request. These options are applied after the parent client's options (if there
 31// is one), and before any request-specific options.
 32func NewModelService(opts ...option.RequestOption) (r ModelService) {
 33	r = ModelService{}
 34	r.Options = opts
 35	return
 36}
 37
 38// Retrieves a model instance, providing basic information about the model such as
 39// the owner and permissioning.
 40func (r *ModelService) Get(ctx context.Context, model string, opts ...option.RequestOption) (res *Model, err error) {
 41	opts = append(r.Options[:], opts...)
 42	if model == "" {
 43		err = errors.New("missing required model parameter")
 44		return
 45	}
 46	path := fmt.Sprintf("models/%s", model)
 47	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
 48	return
 49}
 50
 51// Lists the currently available models, and provides basic information about each
 52// one such as the owner and availability.
 53func (r *ModelService) List(ctx context.Context, opts ...option.RequestOption) (res *pagination.Page[Model], err error) {
 54	var raw *http.Response
 55	opts = append(r.Options[:], opts...)
 56	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
 57	path := "models"
 58	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, nil, &res, opts...)
 59	if err != nil {
 60		return nil, err
 61	}
 62	err = cfg.Execute()
 63	if err != nil {
 64		return nil, err
 65	}
 66	res.SetPageConfig(cfg, raw)
 67	return res, nil
 68}
 69
 70// Lists the currently available models, and provides basic information about each
 71// one such as the owner and availability.
 72func (r *ModelService) ListAutoPaging(ctx context.Context, opts ...option.RequestOption) *pagination.PageAutoPager[Model] {
 73	return pagination.NewPageAutoPager(r.List(ctx, opts...))
 74}
 75
 76// Delete a fine-tuned model. You must have the Owner role in your organization to
 77// delete a model.
 78func (r *ModelService) Delete(ctx context.Context, model string, opts ...option.RequestOption) (res *ModelDeleted, err error) {
 79	opts = append(r.Options[:], opts...)
 80	if model == "" {
 81		err = errors.New("missing required model parameter")
 82		return
 83	}
 84	path := fmt.Sprintf("models/%s", model)
 85	err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
 86	return
 87}
 88
 89// Describes an OpenAI model offering that can be used with the API.
 90type Model struct {
 91	// The model identifier, which can be referenced in the API endpoints.
 92	ID string `json:"id,required"`
 93	// The Unix timestamp (in seconds) when the model was created.
 94	Created int64 `json:"created,required"`
 95	// The object type, which is always "model".
 96	Object constant.Model `json:"object,required"`
 97	// The organization that owns the model.
 98	OwnedBy string `json:"owned_by,required"`
 99	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
100	JSON struct {
101		ID          respjson.Field
102		Created     respjson.Field
103		Object      respjson.Field
104		OwnedBy     respjson.Field
105		ExtraFields map[string]respjson.Field
106		raw         string
107	} `json:"-"`
108}
109
110// Returns the unmodified JSON received from the API
111func (r Model) RawJSON() string { return r.JSON.raw }
112func (r *Model) UnmarshalJSON(data []byte) error {
113	return apijson.UnmarshalRoot(data, r)
114}
115
116type ModelDeleted struct {
117	ID      string `json:"id,required"`
118	Deleted bool   `json:"deleted,required"`
119	Object  string `json:"object,required"`
120	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
121	JSON struct {
122		ID          respjson.Field
123		Deleted     respjson.Field
124		Object      respjson.Field
125		ExtraFields map[string]respjson.Field
126		raw         string
127	} `json:"-"`
128}
129
130// Returns the unmodified JSON received from the API
131func (r ModelDeleted) RawJSON() string { return r.JSON.raw }
132func (r *ModelDeleted) UnmarshalJSON(data []byte) error {
133	return apijson.UnmarshalRoot(data, r)
134}