1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package openai
4
5import (
6 "context"
7 "net/http"
8 "os"
9
10 "github.com/openai/openai-go/internal/requestconfig"
11 "github.com/openai/openai-go/option"
12 "github.com/openai/openai-go/responses"
13)
14
15// Client creates a struct with services and top level methods that help with
16// interacting with the openai API. You should not instantiate this client
17// directly, and instead use the [NewClient] method instead.
18type Client struct {
19 Options []option.RequestOption
20 Completions CompletionService
21 Chat ChatService
22 Embeddings EmbeddingService
23 Files FileService
24 Images ImageService
25 Audio AudioService
26 Moderations ModerationService
27 Models ModelService
28 FineTuning FineTuningService
29 VectorStores VectorStoreService
30 Beta BetaService
31 Batches BatchService
32 Uploads UploadService
33 Responses responses.ResponseService
34}
35
36// DefaultClientOptions read from the environment (OPENAI_API_KEY, OPENAI_ORG_ID,
37// OPENAI_PROJECT_ID). This should be used to initialize new clients.
38func DefaultClientOptions() []option.RequestOption {
39 defaults := []option.RequestOption{option.WithEnvironmentProduction()}
40 if o, ok := os.LookupEnv("OPENAI_API_KEY"); ok {
41 defaults = append(defaults, option.WithAPIKey(o))
42 }
43 if o, ok := os.LookupEnv("OPENAI_ORG_ID"); ok {
44 defaults = append(defaults, option.WithOrganization(o))
45 }
46 if o, ok := os.LookupEnv("OPENAI_PROJECT_ID"); ok {
47 defaults = append(defaults, option.WithProject(o))
48 }
49 return defaults
50}
51
52// NewClient generates a new client with the default option read from the
53// environment (OPENAI_API_KEY, OPENAI_ORG_ID, OPENAI_PROJECT_ID). The option
54// passed in as arguments are applied after these default arguments, and all option
55// will be passed down to the services and requests that this client makes.
56func NewClient(opts ...option.RequestOption) (r Client) {
57 opts = append(DefaultClientOptions(), opts...)
58
59 r = Client{Options: opts}
60
61 r.Completions = NewCompletionService(opts...)
62 r.Chat = NewChatService(opts...)
63 r.Embeddings = NewEmbeddingService(opts...)
64 r.Files = NewFileService(opts...)
65 r.Images = NewImageService(opts...)
66 r.Audio = NewAudioService(opts...)
67 r.Moderations = NewModerationService(opts...)
68 r.Models = NewModelService(opts...)
69 r.FineTuning = NewFineTuningService(opts...)
70 r.VectorStores = NewVectorStoreService(opts...)
71 r.Beta = NewBetaService(opts...)
72 r.Batches = NewBatchService(opts...)
73 r.Uploads = NewUploadService(opts...)
74 r.Responses = responses.NewResponseService(opts...)
75
76 return
77}
78
79// Execute makes a request with the given context, method, URL, request params,
80// response, and request options. This is useful for hitting undocumented endpoints
81// while retaining the base URL, auth, retries, and other options from the client.
82//
83// If a byte slice or an [io.Reader] is supplied to params, it will be used as-is
84// for the request body.
85//
86// The params is by default serialized into the body using [encoding/json]. If your
87// type implements a MarshalJSON function, it will be used instead to serialize the
88// request. If a URLQuery method is implemented, the returned [url.Values] will be
89// used as query strings to the url.
90//
91// If your params struct uses [param.Field], you must provide either [MarshalJSON],
92// [URLQuery], and/or [MarshalForm] functions. It is undefined behavior to use a
93// struct uses [param.Field] without specifying how it is serialized.
94//
95// Any "β¦Params" object defined in this library can be used as the request
96// argument. Note that 'path' arguments will not be forwarded into the url.
97//
98// The response body will be deserialized into the res variable, depending on its
99// type:
100//
101// - A pointer to a [*http.Response] is populated by the raw response.
102// - A pointer to a byte array will be populated with the contents of the request
103// body.
104// - A pointer to any other type uses this library's default JSON decoding, which
105// respects UnmarshalJSON if it is defined on the type.
106// - A nil value will not read the response body.
107//
108// For even greater flexibility, see [option.WithResponseInto] and
109// [option.WithResponseBodyInto].
110func (r *Client) Execute(ctx context.Context, method string, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
111 opts = append(r.Options, opts...)
112 return requestconfig.ExecuteNewRequest(ctx, method, path, params, res, opts...)
113}
114
115// Get makes a GET request with the given URL, params, and optionally deserializes
116// to a response. See [Execute] documentation on the params and response.
117func (r *Client) Get(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
118 return r.Execute(ctx, http.MethodGet, path, params, res, opts...)
119}
120
121// Post makes a POST request with the given URL, params, and optionally
122// deserializes to a response. See [Execute] documentation on the params and
123// response.
124func (r *Client) Post(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
125 return r.Execute(ctx, http.MethodPost, path, params, res, opts...)
126}
127
128// Put makes a PUT request with the given URL, params, and optionally deserializes
129// to a response. See [Execute] documentation on the params and response.
130func (r *Client) Put(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
131 return r.Execute(ctx, http.MethodPut, path, params, res, opts...)
132}
133
134// Patch makes a PATCH request with the given URL, params, and optionally
135// deserializes to a response. See [Execute] documentation on the params and
136// response.
137func (r *Client) Patch(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
138 return r.Execute(ctx, http.MethodPatch, path, params, res, opts...)
139}
140
141// Delete makes a DELETE request with the given URL, params, and optionally
142// deserializes to a response. See [Execute] documentation on the params and
143// response.
144func (r *Client) Delete(ctx context.Context, path string, params interface{}, res interface{}, opts ...option.RequestOption) error {
145 return r.Execute(ctx, http.MethodDelete, path, params, res, opts...)
146}