Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make engine public #2

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type CompletionResponse struct {
//
// The default number of tokens to complete is 1024.
// Docs: https://beta.openai.com/docs/api-reference/completions
func (e *engine) Completion(ctx context.Context, opts *CompletionOptions) (*CompletionResponse, error) {
func (e *Engine) Completion(ctx context.Context, opts *CompletionOptions) (*CompletionResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type EditResponse struct {
// Edit given a prompt and an instruction, the model will return an edited version of the prompt.
//
// Docs: https://beta.openai.com/docs/api-reference/edits
func (e *engine) Edit(ctx context.Context, opts *EditOptions) (*EditResponse, error) {
func (e *Engine) Edit(ctx context.Context, opts *EditOptions) (*EditResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions images.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ImageCreateResponse struct {
// ImageCreate given a prompt and/or an input image, the model will generate a new image.
//
// Docs: https://beta.openai.com/docs/api-reference/images/create
func (e *engine) ImageCreate(ctx context.Context, opts *ImageCreateOptions) (*ImageCreateResponse, error) {
func (e *Engine) ImageCreate(ctx context.Context, opts *ImageCreateOptions) (*ImageCreateResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ type ImageEditResponse struct {
// ImageEdit creates an edited or extended image given an original image and a prompt.
//
// Docs: https://beta.openai.com/docs/api-reference/images/create-edit
func (e *engine) ImageEdit(ctx context.Context, opts *ImageEditOptions) (*ImageEditResponse, error) {
func (e *Engine) ImageEdit(ctx context.Context, opts *ImageEditOptions) (*ImageEditResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
Expand Down Expand Up @@ -178,7 +178,7 @@ type ImageVariationResponse struct {
// ImageVariation creates a variation of a given image.
//
// Docs: https://beta.openai.com/docs/api-reference/images/create-variation
func (e *engine) ImageVariation(ctx context.Context, opts *ImageVariationOptions) (*ImageCreateResponse, error) {
func (e *Engine) ImageVariation(ctx context.Context, opts *ImageVariationOptions) (*ImageCreateResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ListModelsResponse struct {
// each one such as the owner and availability.
//
// Docs: https://beta.openai.com/docs/api-reference/models/list
func (e *engine) ListModels(ctx context.Context) (*ListModelsResponse, error) {
func (e *Engine) ListModels(ctx context.Context) (*ListModelsResponse, error) {
url := e.apiBaseURL + "/models"
req, err := e.newReq(ctx, http.MethodGet, url, "", nil)
if err != nil {
Expand Down Expand Up @@ -71,7 +71,7 @@ type RetrieveModelResponse struct {
// about the model such as the owner and permissioning.
//
// Docs: https://beta.openai.com/docs/api-reference/models/retrieve
func (e *engine) RetrieveModel(ctx context.Context, opts *RetrieveModelOptions) (*RetrieveModelResponse, error) {
func (e *Engine) RetrieveModel(ctx context.Context, opts *RetrieveModelOptions) (*RetrieveModelResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/go-playground/validator/v10"
)

type engine struct {
type Engine struct {
apiKey string
apiBaseURL string
organizationId string
Expand All @@ -28,8 +28,8 @@ const (
)

// New is used to initialize engine.
func New(apiKey string) *engine {
e := &engine{
func New(apiKey string) *Engine {
e := &Engine{
apiKey: apiKey,
apiBaseURL: "https://api.openai.com/v1",
client: &http.Client{},
Expand All @@ -42,16 +42,16 @@ func New(apiKey string) *engine {
}

// SetApiKey is used to set API key to access OpenAI API.
func (e *engine) SetApiKey(apiKey string) {
func (e *Engine) SetApiKey(apiKey string) {
e.apiKey = apiKey
}

// SetOrganizationId is used to set organization ID if user belongs to multiple organizations.
func (e *engine) SetOrganizationId(organizationId string) {
func (e *Engine) SetOrganizationId(organizationId string) {
e.organizationId = organizationId
}

func (e *engine) newReq(ctx context.Context, method string, uri string, postType string, body io.Reader) (*http.Request, error) {
func (e *Engine) newReq(ctx context.Context, method string, uri string, postType string, body io.Reader) (*http.Request, error) {
if ctx == nil {
ctx = context.Background() // prevent nil context error
}
Expand All @@ -76,7 +76,7 @@ func (e *engine) newReq(ctx context.Context, method string, uri string, postType
return req, err
}

func (e *engine) doReq(req *http.Request) (*http.Response, error) {
func (e *Engine) doReq(req *http.Request) (*http.Response, error) {
e.n++ // increment number of requests
resp, err := e.client.Do(req)
if err != nil {
Expand Down