Skip to content

Commit

Permalink
Merge pull request #9 from ak9024/add-error-handler
Browse files Browse the repository at this point in the history
feat(chat): add error handling
  • Loading branch information
ak9024 committed Jul 23, 2023
2 parents e634e1b + 19af2c2 commit 6a0eafd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
18 changes: 14 additions & 4 deletions chat.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package gochatgptsdk

import "fmt"
import (
"fmt"
"net/http"
)

func (c *chatgpt) ChatCompletions(b ModelChat) (*ModelChatResponse, error) {
func (c *chatgpt) ChatCompletions(b ModelChat) (*ModelChatResponse, *ErrorResponse) {
// POST https://api.openai.com/v1/chat/completions
endpointChatCompletions := fmt.Sprintf("%s/chat/completions", ChatGPTAPIV1)

result := ModelChatResponse{}
errResponse := ErrorResponse{}

_, err := DoRequest(c.OpenAIKey).
resp, err := DoRequest(c.OpenAIKey).
SetBody(b).
SetResult(&result).
SetError(&errResponse).
Post(endpointChatCompletions)
if err != nil {
return nil, err
return nil, &errResponse
}

// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errResponse
}

return &result, nil
Expand Down
59 changes: 45 additions & 14 deletions images.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,69 @@
package gochatgptsdk

import "fmt"
import (
"fmt"
"net/http"
)

func (c *chatgpt) ImagesGenerations(b ModelImages) (*ModelImagesResponse[DataURL], error) {
func (c *chatgpt) ImagesGenerations(b ModelImages) (*ModelImagesResponse[DataURL], *ErrorResponse) {
// POST https://api.openai.com/v1/images/generations
endpointImagesGeneratios := fmt.Sprintf("%s/images/generations", ChatGPTAPIV1)

result := ModelImagesResponse[DataURL]{}
errorResponse := ErrorResponse{}

_, err := DoRequest(c.OpenAIKey).
resp, err := DoRequest(c.OpenAIKey).
SetBody(b).
SetResult(&result).
SetError(&errorResponse).
Post(endpointImagesGeneratios)
if err != nil {
return nil, err
return nil, &errorResponse
}

// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errorResponse
}

return &result, nil
}

func (c *chatgpt) ImagesGenerationsB64JSON(b ModelImages) (*ModelImagesResponse[DataB64JSON], error) {
func (c *chatgpt) ImagesGenerationsB64JSON(b ModelImages) (*ModelImagesResponse[DataB64JSON], *ErrorResponse) {
// POST https/api.openai.com/v1/images/generations
endpointImagesGeneratios := fmt.Sprintf("%s/images/generations", ChatGPTAPIV1)

result := ModelImagesResponse[DataB64JSON]{}
errorResponse := ErrorResponse{}

_, err := DoRequest(c.OpenAIKey).
resp, err := DoRequest(c.OpenAIKey).
SetBody(b).
SetResult(&result).
SetError(&errorResponse).
Post(endpointImagesGeneratios)
if err != nil {
return nil, err
return nil, &errorResponse
}

// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errorResponse
}

return &result, nil
}

func (c *chatgpt) ImagesVariations(b ModelImagesVariations) (*ModelImagesResponse[DataURL], error) {
func (c *chatgpt) ImagesVariations(b ModelImagesVariations) (*ModelImagesResponse[DataURL], *ErrorResponse) {
// POST https/api.openai.com/v1/images/variations
endpointImagesVariations := fmt.Sprintf("%s/images/variations", ChatGPTAPIV1)

result := ModelImagesResponse[DataURL]{}
errResponse := ErrorResponse{}

// set default images variations
d := defaultImagesVariations(b, ResponseFormatURL)

_, err := DoRequest(c.OpenAIKey).
resp, err := DoRequest(c.OpenAIKey).
SetFile("image", *&d.Image).
SetFormData(map[string]string{
"n": d.N,
Expand All @@ -54,35 +72,48 @@ func (c *chatgpt) ImagesVariations(b ModelImagesVariations) (*ModelImagesRespons
"user": d.User,
}).
SetResult(&result).
SetError(&errResponse).
Post(endpointImagesVariations)
if err != nil {
return nil, err
return nil, &errResponse
}

// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errResponse
}

return &result, nil
}

func (c *chatgpt) ImagesVariationsB64JSON(b ModelImagesVariations) (*ModelImagesResponse[DataB64JSON], error) {
func (c *chatgpt) ImagesVariationsB64JSON(b ModelImagesVariations) (*ModelImagesResponse[DataB64JSON], *ErrorResponse) {
// POST https/api.openai.com/v1/images/variations
endpointImagesVariations := fmt.Sprintf("%s/images/variations", ChatGPTAPIV1)

result := ModelImagesResponse[DataB64JSON]{}
errResponse := ErrorResponse{}

// set default images variations
d := defaultImagesVariations(b, ResponseFormatB64JSON)

_, err := DoRequest(c.OpenAIKey).
SetFile("image", *&b.Image).
resp, err := DoRequest(c.OpenAIKey).
SetFile("image", *&d.Image).
SetFormData(map[string]string{
"n": d.N,
"size": d.Size,
"response_format": d.ResponseFormat,
"user": d.User,
}).
SetResult(&result).
SetError(&errResponse).
Post(endpointImagesVariations)
if err != nil {
return nil, err
return nil, &errResponse
}

// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errResponse
}

return &result, nil
Expand Down
11 changes: 11 additions & 0 deletions struct_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ type ModelImagesResponse[T DataURL | DataB64JSON] struct {
Created int64 `json:"created"`
Data []T `json:"data"`
}

type Error struct {
Code interface{} `json:"code"`
Message string `json:"message"`
Param interface{} `json:"param"`
Type string `json:"type"`
}

type ErrorResponse struct {
Error Error `json:"error"`
}
18 changes: 14 additions & 4 deletions text.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package gochatgptsdk

import "fmt"
import (
"fmt"
"net/http"
)

func (c *chatgpt) Completions(b ModelText) (*ModelTextResponse, error) {
func (c *chatgpt) Completions(b ModelText) (*ModelTextResponse, *ErrorResponse) {
// POST https://api.openai.com/v1/completions
endpointCompletions := fmt.Sprintf("%s/completions", ChatGPTAPIV1)

result := ModelTextResponse{}
errResponse := ErrorResponse{}

_, err := DoRequest(c.OpenAIKey).
resp, err := DoRequest(c.OpenAIKey).
SetBody(b).
SetResult(&result).
SetError(&errResponse).
Post(endpointCompletions)
if err != nil {
return nil, err
return nil, &errResponse
}

// if status code not 200 ok, please send error message
if resp.StatusCode() != http.StatusOK {
return nil, &errResponse
}

return &result, nil
Expand Down

0 comments on commit 6a0eafd

Please sign in to comment.