Skip to content

Commit

Permalink
Add testable request builder (sashabaranov#162)
Browse files Browse the repository at this point in the history
* Add testable request builder

* improve tests
  • Loading branch information
sashabaranov authored Mar 15, 2023
1 parent 53d195c commit c34bc77
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 152 deletions.
20 changes: 5 additions & 15 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -12,7 +11,7 @@ import (
type Client struct {
config ClientConfig

marshaller marshaller
requestBuilder requestBuilder
}

// NewClient creates new OpenAI API client.
Expand All @@ -24,8 +23,8 @@ func NewClient(authToken string) *Client {
// NewClientWithConfig creates new OpenAI API client for specified config.
func NewClientWithConfig(config ClientConfig) *Client {
return &Client{
config: config,
marshaller: &jsonMarshaller{},
config: config,
requestBuilder: newRequestBuilder(),
}
}

Expand Down Expand Up @@ -91,17 +90,8 @@ func (c *Client) newStreamRequest(
ctx context.Context,
method string,
urlSuffix string,
body interface{}) (*http.Request, error) {
var reqBody []byte
if body != nil {
var err error
reqBody, err = c.marshaller.marshal(body)
if err != nil {
return nil, err
}
}

req, err := http.NewRequestWithContext(ctx, method, c.fullURL(urlSuffix), bytes.NewBuffer(reqBody))
body any) (*http.Request, error) {
req, err := c.requestBuilder.build(ctx, method, c.fullURL(urlSuffix), body)
if err != nil {
return nil, err
}
Expand Down
9 changes: 1 addition & 8 deletions chat.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"errors"
"net/http"
Expand Down Expand Up @@ -72,14 +71,8 @@ func (c *Client) CreateChatCompletion(
return
}

var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

urlSuffix := "/chat/completions"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
if err != nil {
return
}
Expand Down
9 changes: 1 addition & 8 deletions completion.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"errors"
"net/http"
Expand Down Expand Up @@ -105,14 +104,8 @@ func (c *Client) CreateCompletion(
return
}

var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

urlSuffix := "/completions"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
if err != nil {
return
}
Expand Down
9 changes: 1 addition & 8 deletions edits.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"net/http"
)
Expand Down Expand Up @@ -32,13 +31,7 @@ type EditsResponse struct {

// Perform an API call to the Edits endpoint.
func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error) {
var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/edits"), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL("/edits"), request)
if err != nil {
return
}
Expand Down
10 changes: 1 addition & 9 deletions embeddings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"net/http"
)
Expand Down Expand Up @@ -133,14 +132,7 @@ type EmbeddingRequest struct {
// CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |request.Input|.
// https://beta.openai.com/docs/api-reference/embeddings/create
func (c *Client) CreateEmbeddings(ctx context.Context, request EmbeddingRequest) (resp EmbeddingResponse, err error) {
var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

urlSuffix := "/embeddings"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL("/embeddings"), request)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions engines.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type EnginesList struct {
// ListEngines Lists the currently available engines, and provides basic
// information about each option such as the owner and availability.
func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/engines"), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL("/engines"), nil)
if err != nil {
return
}
Expand All @@ -38,7 +38,7 @@ func (c *Client) GetEngine(
engineID string,
) (engine Engine, err error) {
urlSuffix := fmt.Sprintf("/engines/%s", engineID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File

// DeleteFile deletes an existing file.
func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.fullURL("/files/"+fileID), nil)
req, err := c.requestBuilder.build(ctx, http.MethodDelete, c.fullURL("/files/"+fileID), nil)
if err != nil {
return
}
Expand All @@ -124,7 +124,7 @@ func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
// ListFiles Lists the currently available files,
// and provides basic information about each file such as the file name and purpose.
func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/files"), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL("/files"), nil)
if err != nil {
return
}
Expand All @@ -137,7 +137,7 @@ func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
// such as the file name and purpose.
func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) {
urlSuffix := fmt.Sprintf("/files/%s", fileID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
if err != nil {
return
}
Expand Down
19 changes: 6 additions & 13 deletions fine_tunes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -68,14 +67,8 @@ type FineTuneDeleteResponse struct {
}

func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (response FineTune, err error) {
var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

urlSuffix := "/fine-tunes"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
if err != nil {
return
}
Expand All @@ -86,7 +79,7 @@ func (c *Client) CreateFineTune(ctx context.Context, request FineTuneRequest) (r

// CancelFineTune cancel a fine-tune job.
func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel"), nil)
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL("/fine-tunes/"+fineTuneID+"/cancel"), nil)
if err != nil {
return
}
Expand All @@ -96,7 +89,7 @@ func (c *Client) CancelFineTune(ctx context.Context, fineTuneID string) (respons
}

func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/fine-tunes"), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL("/fine-tunes"), nil)
if err != nil {
return
}
Expand All @@ -107,7 +100,7 @@ func (c *Client) ListFineTunes(ctx context.Context) (response FineTuneList, err

func (c *Client) GetFineTune(ctx context.Context, fineTuneID string) (response FineTune, err error) {
urlSuffix := fmt.Sprintf("/fine-tunes/%s", fineTuneID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
if err != nil {
return
}
Expand All @@ -117,7 +110,7 @@ func (c *Client) GetFineTune(ctx context.Context, fineTuneID string) (response F
}

func (c *Client) DeleteFineTune(ctx context.Context, fineTuneID string) (response FineTuneDeleteResponse, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.fullURL("/fine-tunes/"+fineTuneID), nil)
req, err := c.requestBuilder.build(ctx, http.MethodDelete, c.fullURL("/fine-tunes/"+fineTuneID), nil)
if err != nil {
return
}
Expand All @@ -127,7 +120,7 @@ func (c *Client) DeleteFineTune(ctx context.Context, fineTuneID string) (respons
}

func (c *Client) ListFineTuneEvents(ctx context.Context, fineTuneID string) (response FineTuneEventList, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/fine-tunes/"+fineTuneID+"/events"), nil)
req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL("/fine-tunes/"+fineTuneID+"/events"), nil)
if err != nil {
return
}
Expand Down
8 changes: 1 addition & 7 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,8 @@ type ImageResponseDataInner struct {

// CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.
func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (response ImageResponse, err error) {
var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

urlSuffix := "/images/generations"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
if err != nil {
return
}
Expand Down
71 changes: 0 additions & 71 deletions marshaller_test.go

This file was deleted.

9 changes: 1 addition & 8 deletions moderation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai

import (
"bytes"
"context"
"net/http"
)
Expand Down Expand Up @@ -51,13 +50,7 @@ type ModerationResponse struct {
// Moderations — perform a moderation api call over a string.
// Input can be an array or slice but a string will reduce the complexity.
func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error) {
var reqBytes []byte
reqBytes, err = c.marshaller.marshal(request)
if err != nil {
return
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/moderations"), bytes.NewBuffer(reqBytes))
req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL("/moderations"), request)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit c34bc77

Please sign in to comment.