From 1277d0b14b9f089d298aef658c5688ef6c570a69 Mon Sep 17 00:00:00 2001 From: conneroisu Date: Fri, 6 Sep 2024 13:15:36 -0400 Subject: [PATCH] unexport unneeded fields --- client.go | 6 +- errors.go | 215 ++++++++++++++++++++++----------------------- moderation.go | 5 +- moderation_test.go | 8 +- stream.go | 4 +- stream_test.go | 4 +- 6 files changed, 122 insertions(+), 120 deletions(-) diff --git a/client.go b/client.go index 1601adf..d0cd8ba 100644 --- a/client.go +++ b/client.go @@ -217,7 +217,7 @@ func sendRequestStream[T streamer]( emptyMessagesLimit: client.EmptyMessagesLimit, reader: bufio.NewReader(resp.Body), response: resp, - errAccumulator: NewErrorAccumulator(), + errAccumulator: newErrorAccumulator(), Header: resp.Header, }, nil } @@ -273,10 +273,10 @@ func withModel(model Model) fullURLOption { } func (c *Client) handleErrorResp(resp *http.Response) error { - var errRes ErrorResponse + var errRes errorResponse err := json.NewDecoder(resp.Body).Decode(&errRes) if err != nil || errRes.Error == nil { - reqErr := &RequestError{ + reqErr := &requestError{ HTTPStatusCode: resp.StatusCode, Err: err, } diff --git a/errors.go b/errors.go index faa91df..32de196 100644 --- a/errors.go +++ b/errors.go @@ -8,14 +8,96 @@ import ( "strings" ) -// Helper is an interface for error helpers -type Helper interface { - error - Advice() string +// DefaultErrorAccumulator is a default implementation of ErrorAccumulator +type DefaultErrorAccumulator struct { + Buffer errorBuffer +} + +// APIError provides error information returned by the Groq API. +type APIError struct { + Code any `json:"code,omitempty"` // Code is the code of the error. + Message string `json:"message"` // Message is the message of the error. + Param *string `json:"param,omitempty"` // Param is the param of the error. + Type string `json:"type"` // Type is the type of the error. + HTTPStatusCode int `json:"-"` // HTTPStatusCode is the status code of the error. } -// ErrorAccumulator is an interface for accumulating errors -type ErrorAccumulator interface { +// ErrChatCompletionInvalidModel is an error that occurs when the model is not supported with the CreateChatCompletion method. +type ErrChatCompletionInvalidModel struct { + Model Model + Endpoint Endpoint +} + +// Error implements the error interface. +func (e ErrChatCompletionInvalidModel) Error() string { + return fmt.Errorf( + "this model (%s) is not supported with this method of interaction over %s, please use CreateCompletion client method instead", + e.Endpoint, + e.Model, + ). + Error() +} + +// ErrChatCompletionStreamNotSupported is an error that occurs when streaming is not supported with the CreateChatCompletionStream method. +type ErrChatCompletionStreamNotSupported struct { + model Model +} + +// Error implements the error interface. +func (e ErrChatCompletionStreamNotSupported) Error() string { + return fmt.Errorf("streaming is not supported with this method, please use CreateChatCompletionStream client method instead"). + Error() +} + +// ErrContentFieldsMisused is an error that occurs when both Content and MultiContent properties are set. +type ErrContentFieldsMisused struct { + field string +} + +// Error implements the error interface. +func (e ErrContentFieldsMisused) Error() string { + return fmt.Errorf("can't use both Content and MultiContent properties simultaneously"). + Error() +} + +// ErrCompletionUnsupportedModel is an error that occurs when the model is not +// supported with the CreateCompletion method. +type ErrCompletionUnsupportedModel struct{ Model Model } + +// Error implements the error interface. +func (e ErrCompletionUnsupportedModel) Error() string { + return fmt.Errorf("this model (%s) is not supported with this method, please use CreateCompletion client method instead", e.Model). + Error() +} + +// ErrCompletionStreamNotSupported is an error that occurs when streaming is not supported with the CreateCompletionStream method. +type ErrCompletionStreamNotSupported struct{} + +// Error implements the error interface. +func (e ErrCompletionStreamNotSupported) Error() string { + return fmt.Errorf("streaming is not supported with this method, please use CreateCompletionStream client method instead"). + Error() +} + +// ErrCompletionRequestPromptTypeNotSupported is an error that occurs when the type of CompletionRequest.Prompt only supports string and []string. +type ErrCompletionRequestPromptTypeNotSupported struct{} + +// Error implements the error interface. +func (e ErrCompletionRequestPromptTypeNotSupported) Error() string { + return fmt.Errorf("the type of CompletionRequest.Prompt only supports string and []string"). + Error() +} + +// ErrTooManyEmptyStreamMessages is returned when the stream has sent too many empty messages. +type ErrTooManyEmptyStreamMessages struct{} + +// Error returns the error message. +func (e ErrTooManyEmptyStreamMessages) Error() string { + return "stream has sent too many empty messages" +} + +// errorAccumulator is an interface for accumulating errors +type errorAccumulator interface { // Write writes bytes to the error accumulator // // It implements the io.Writer interface. @@ -30,19 +112,26 @@ type errorBuffer interface { Bytes() []byte } -// DefaultErrorAccumulator is a default implementation of ErrorAccumulator -type DefaultErrorAccumulator struct { - Buffer errorBuffer +// requestError provides information about generic request errors. +type requestError struct { + HTTPStatusCode int + Err error +} + +// errorResponse is a response from the error endpoint. +type errorResponse struct { + Error *APIError `json:"error,omitempty"` } -// NewErrorAccumulator creates a new error accumulator -func NewErrorAccumulator() ErrorAccumulator { +// Error implements the error interface. +// newErrorAccumulator creates a new error accumulator +func newErrorAccumulator() errorAccumulator { return &DefaultErrorAccumulator{ Buffer: &bytes.Buffer{}, } } -// Write writes bytes to the error accumulator +// Write writes bytes to the error accumulator. func (e *DefaultErrorAccumulator) Write(p []byte) error { _, err := e.Buffer.Write(p) if err != nil { @@ -51,7 +140,7 @@ func (e *DefaultErrorAccumulator) Write(p []byte) error { return nil } -// Bytes returns the bytes of the error accumulator +// Bytes returns the bytes of the error accumulator. func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) { if e.Buffer.Len() == 0 { return @@ -60,26 +149,6 @@ func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) { return } -// APIError provides error information returned by the Groq API. -type APIError struct { - Code any `json:"code,omitempty"` // Code is the code of the error. - Message string `json:"message"` // Message is the message of the error. - Param *string `json:"param,omitempty"` // Param is the param of the error. - Type string `json:"type"` // Type is the type of the error. - HTTPStatusCode int `json:"-"` // HTTPStatusCode is the status code of the error. -} - -// RequestError provides information about generic request errors. -type RequestError struct { - HTTPStatusCode int - Err error -} - -// ErrorResponse is a response from the error endpoint. -type ErrorResponse struct { - Error *APIError `json:"error,omitempty"` -} - // Error implements the error interface. func (e *APIError) Error() string { if e.HTTPStatusCode > 0 { @@ -129,8 +198,7 @@ func (e *APIError) UnmarshalJSON(data []byte) (err error) { return json.Unmarshal(rawMap["code"], &e.Code) } -// Error implements the error interface. -func (e *RequestError) Error() string { +func (e *requestError) Error() string { return fmt.Sprintf( "error, status code: %d, message: %s", e.HTTPStatusCode, @@ -138,81 +206,6 @@ func (e *RequestError) Error() string { ) } -// Unwrap returns the underlying error -func (e *RequestError) Unwrap() error { +func (e *requestError) Unwrap() error { return e.Err } - -// ErrChatCompletionInvalidModel is an error that occurs when the model is not supported with the CreateChatCompletion method. -type ErrChatCompletionInvalidModel struct { - Model Model - Endpoint Endpoint -} - -// Error implements the error interface. -func (e ErrChatCompletionInvalidModel) Error() string { - return fmt.Errorf( - "this model (%s) is not supported with this method of interaction over %s, please use CreateCompletion client method instead", - e.Endpoint, - e.Model, - ). - Error() -} - -// ErrChatCompletionStreamNotSupported is an error that occurs when streaming is not supported with the CreateChatCompletionStream method. -type ErrChatCompletionStreamNotSupported struct { - model Model -} - -// Error implements the error interface. -func (e ErrChatCompletionStreamNotSupported) Error() string { - return fmt.Errorf("streaming is not supported with this method, please use CreateChatCompletionStream client method instead"). - Error() -} - -// ErrContentFieldsMisused is an error that occurs when both Content and MultiContent properties are set. -type ErrContentFieldsMisused struct { - field string -} - -// Error implements the error interface. -func (e ErrContentFieldsMisused) Error() string { - return fmt.Errorf("can't use both Content and MultiContent properties simultaneously"). - Error() -} - -// ErrCompletionUnsupportedModel is an error that occurs when the model is not -// supported with the CreateCompletion method. -type ErrCompletionUnsupportedModel struct{ Model Model } - -// Error implements the error interface. -func (e ErrCompletionUnsupportedModel) Error() string { - return fmt.Errorf("this model (%s) is not supported with this method, please use CreateCompletion client method instead", e.Model). - Error() -} - -// ErrCompletionStreamNotSupported is an error that occurs when streaming is not supported with the CreateCompletionStream method. -type ErrCompletionStreamNotSupported struct{} - -// Error implements the error interface. -func (e ErrCompletionStreamNotSupported) Error() string { - return fmt.Errorf("streaming is not supported with this method, please use CreateCompletionStream client method instead"). - Error() -} - -// ErrCompletionRequestPromptTypeNotSupported is an error that occurs when the type of CompletionRequest.Prompt only supports string and []string. -type ErrCompletionRequestPromptTypeNotSupported struct{} - -// Error implements the error interface. -func (e ErrCompletionRequestPromptTypeNotSupported) Error() string { - return fmt.Errorf("the type of CompletionRequest.Prompt only supports string and []string"). - Error() -} - -// ErrTooManyEmptyStreamMessages is returned when the stream has sent too many empty messages. -type ErrTooManyEmptyStreamMessages struct{} - -// Error returns the error message. -func (e ErrTooManyEmptyStreamMessages) Error() string { - return "stream has sent too many empty messages" -} diff --git a/moderation.go b/moderation.go index b675db5..efa8a11 100644 --- a/moderation.go +++ b/moderation.go @@ -163,7 +163,10 @@ func (c *Client) Moderate( response.Flagged = true split := strings.Split(strings.Split(content, "\n")[1], ",") for _, s := range split { - response.Categories = append(response.Categories, SectionMap[strings.TrimSpace(s)]) + response.Categories = append( + response.Categories, + SectionMap[strings.TrimSpace(s)], + ) } } return diff --git a/moderation_test.go b/moderation_test.go index dcdc8f8..2b86d58 100644 --- a/moderation_test.go +++ b/moderation_test.go @@ -22,7 +22,13 @@ func TestModerate(t *testing.T) { a := assert.New(t) a.NoError(err, "Moderation error") a.Equal(true, mod.Flagged) - a.Equal(mod.Categories, []groq.HarmfulCategory{groq.CategoryViolentCrimes, groq.CategoryNonviolentCrimes}) + a.Equal( + mod.Categories, + []groq.HarmfulCategory{ + groq.CategoryViolentCrimes, + groq.CategoryNonviolentCrimes, + }, + ) } func handleModerationEndpoint(w http.ResponseWriter, r *http.Request) { diff --git a/stream.go b/stream.go index bd597a2..4eb2721 100644 --- a/stream.go +++ b/stream.go @@ -62,7 +62,7 @@ type streamReader[T streamer] struct { reader *bufio.Reader response *http.Response - errAccumulator ErrorAccumulator + errAccumulator errorAccumulator Header http.Header // Header is the header of the response. } @@ -127,7 +127,7 @@ func (stream *streamReader[T]) processLines() (T, error) { } } -func (stream *streamReader[T]) unmarshalError() (errResp *ErrorResponse) { +func (stream *streamReader[T]) unmarshalError() (errResp *errorResponse) { errBytes := stream.errAccumulator.Bytes() if len(errBytes) == 0 { return diff --git a/stream_test.go b/stream_test.go index 2b7611e..04f279f 100644 --- a/stream_test.go +++ b/stream_test.go @@ -12,7 +12,7 @@ import ( // TestStreamReaderReturnsUnmarshalerErrors tests the stream reader returns an unmarshaler error. func TestStreamReaderReturnsUnmarshalerErrors(t *testing.T) { stream := &streamReader[ChatCompletionStreamResponse]{ - errAccumulator: NewErrorAccumulator(), + errAccumulator: newErrorAccumulator(), } respErr := stream.unmarshalError() @@ -39,7 +39,7 @@ func TestStreamReaderReturnsErrTooManyEmptyStreamMessages(t *testing.T) { reader: bufio.NewReader( bytes.NewReader([]byte("\n\n\n\n")), ), - errAccumulator: NewErrorAccumulator(), + errAccumulator: newErrorAccumulator(), } _, err := stream.Recv() a.ErrorIs(