model.go

  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/resp"
 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	// Metadata for the response, check the presence of optional fields with the
100	// [resp.Field.IsPresent] method.
101	JSON struct {
102		ID          resp.Field
103		Created     resp.Field
104		Object      resp.Field
105		OwnedBy     resp.Field
106		ExtraFields map[string]resp.Field
107		raw         string
108	} `json:"-"`
109}
110
111// Returns the unmodified JSON received from the API
112func (r Model) RawJSON() string { return r.JSON.raw }
113func (r *Model) UnmarshalJSON(data []byte) error {
114	return apijson.UnmarshalRoot(data, r)
115}
116
117type ModelDeleted struct {
118	ID      string `json:"id,required"`
119	Deleted bool   `json:"deleted,required"`
120	Object  string `json:"object,required"`
121	// Metadata for the response, check the presence of optional fields with the
122	// [resp.Field.IsPresent] method.
123	JSON struct {
124		ID          resp.Field
125		Deleted     resp.Field
126		Object      resp.Field
127		ExtraFields map[string]resp.Field
128		raw         string
129	} `json:"-"`
130}
131
132// Returns the unmodified JSON received from the API
133func (r ModelDeleted) RawJSON() string { return r.JSON.raw }
134func (r *ModelDeleted) UnmarshalJSON(data []byte) error {
135	return apijson.UnmarshalRoot(data, r)
136}