Repository: sashabaranov/go-openai (go)

Issue:
Move some code into `internal` directory
The root directory of the project is pretty bloated, it would be nice if can move the following into the `internal` directory:

- [ ] `error_accumulator`
- [ ] `request_builder`
- [x] `form_builder`
- [x] `unmarshaler`
- [x] `marshaller`

Also, I don't think we need `Makefile` anymore given the CI setup.



Pull request:
move marshaller and unmarshaler into internal pkg (#304)
Fix #304 (marshaller and unmarshaler.)

Patch:
diff --git a/README.md b/README.md
index 8e76c1a..7562694 100644
--- a/README.md
+++ b/README.md
@@ -435,16 +435,15 @@ import (
 )
 
 func main() {
-
 	config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint")
-	//If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
-	//config.AzureModelMapperFunc = func(model string) string {
-        //    azureModelMapping = map[string]string{
-        //        "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name",
-        //    }
-        //    return azureModelMapping[model]
-        //}
-	
+	// If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
+	// config.AzureModelMapperFunc = func(model string) string {
+	// 	azureModelMapping = map[string]string{
+	// 		"gpt-3.5-turbo": "your gpt-3.5-turbo deployment name",
+	// 	}
+	// 	return azureModelMapping[model]
+	// }
+
 	client := openai.NewClientWithConfig(config)
 	resp, err := client.CreateChatCompletion(
 		context.Background(),
@@ -458,7 +457,6 @@ func main() {
 			},
 		},
 	)
-
 	if err != nil {
 		fmt.Printf("ChatCompletion error: %v\n", err)
 		return
@@ -466,6 +464,7 @@ func main() {
 
 	fmt.Println(resp.Choices[0].Message.Content)
 }
+
 ```
 </details>
 
diff --git a/chat_stream.go b/chat_stream.go
index f4fda88..842835e 100644
--- a/chat_stream.go
+++ b/chat_stream.go
@@ -4,6 +4,8 @@ import (
 	"bufio"
 	"context"
 	"net/http"
+
+	utils "github.com/sashabaranov/go-openai/internal"
 )
 
 type ChatCompletionStreamChoiceDelta struct {
@@ -65,7 +67,7 @@ func (c *Client) CreateChatCompletionStream(
 			reader:             bufio.NewReader(resp.Body),
 			response:           resp,
 			errAccumulator:     newErrorAccumulator(),
-			unmarshaler:        &jsonUnmarshaler{},
+			unmarshaler:        &utils.JSONUnmarshaler{},
 		},
 	}
 	return
diff --git a/error_accumulator.go b/error_accumulator.go
index ca6cec6..568afdb 100644
--- a/error_accumulator.go
+++ b/error_accumulator.go
@@ -4,6 +4,8 @@ import (
 	"bytes"
 	"fmt"
 	"io"
+
+	utils "github.com/sashabaranov/go-openai/internal"
 )
 
 type errorAccumulator interface {
@@ -19,13 +21,13 @@ type errorBuffer interface {
 
 type defaultErrorAccumulator struct {
 	buffer      errorBuffer
-	unmarshaler unmarshaler
+	unmarshaler utils.Unmarshaler
 }
 
 func newErrorAccumulator() errorAccumulator {
 	return &defaultErrorAccumulator{
 		buffer:      &bytes.Buffer{},
-		unmarshaler: &jsonUnmarshaler{},
+		unmarshaler: &utils.JSONUnmarshaler{},
 	}
 }
 
@@ -42,7 +44,7 @@ func (e *defaultErrorAccumulator) unmarshalError() (errResp *ErrorResponse) {
 		return
 	}
 
-	err := e.unmarshaler.unmarshal(e.buffer.Bytes(), &errResp)
+	err := e.unmarshaler.Unmarshal(e.buffer.Bytes(), &errResp)
 	if err != nil {
 		errResp = nil
 	}
diff --git a/internal/marshaller.go b/internal/marshaller.go
new file mode 100644
index 0000000..223a4dc
--- /dev/null
+++ b/internal/marshaller.go
@@ -0,0 +1,15 @@
+package openai
+
+import (
+	"encoding/json"
+)
+
+type Marshaller interface {
+	Marshal(value any) ([]byte, error)
+}
+
+type JSONMarshaller struct{}
+
+func (jm *JSONMarshaller) Marshal(value any) ([]byte, error) {
+	return json.Marshal(value)
+}
diff --git a/internal/unmarshaler.go b/internal/unmarshaler.go
new file mode 100644
index 0000000..8828760
--- /dev/null
+++ b/internal/unmarshaler.go
@@ -0,0 +1,15 @@
+package openai
+
+import (
+	"encoding/json"
+)
+
+type Unmarshaler interface {
+	Unmarshal(data []byte, v any) error
+}
+
+type JSONUnmarshaler struct{}
+
+func (jm *JSONUnmarshaler) Unmarshal(data []byte, v any) error {
+	return json.Unmarshal(data, v)
+}
diff --git a/marshaller.go b/marshaller.go
deleted file mode 100644
index 308ccd1..0000000
--- a/marshaller.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package openai
-
-import (
-	"encoding/json"
-)
-
-type marshaller interface {
-	marshal(value any) ([]byte, error)
-}
-
-type jsonMarshaller struct{}
-
-func (jm *jsonMarshaller) marshal(value any) ([]byte, error) {
-	return json.Marshal(value)
-}
diff --git a/request_builder.go b/request_builder.go
index f0cef10..b4db07c 100644
--- a/request_builder.go
+++ b/request_builder.go
@@ -4,6 +4,8 @@ import (
 	"bytes"
 	"context"
 	"net/http"
+
+	utils "github.com/sashabaranov/go-openai/internal"
 )
 
 type requestBuilder interface {
@@ -11,12 +13,12 @@ type requestBuilder interface {
 }
 
 type httpRequestBuilder struct {
-	marshaller marshaller
+	marshaller utils.Marshaller
 }
 
 func newRequestBuilder() *httpRequestBuilder {
 	return &httpRequestBuilder{
-		marshaller: &jsonMarshaller{},
+		marshaller: &utils.JSONMarshaller{},
 	}
 }
 
@@ -26,7 +28,7 @@ func (b *httpRequestBuilder) build(ctx context.Context, method, url string, requ
 	}
 
 	var reqBytes []byte
-	reqBytes, err := b.marshaller.marshal(request)
+	reqBytes, err := b.marshaller.Marshal(request)
 	if err != nil {
 		return nil, err
 	}
diff --git a/stream.go b/stream.go
index cd435fa..b9e784a 100644
--- a/stream.go
+++ b/stream.go
@@ -5,6 +5,8 @@ import (
 	"context"
 	"errors"
 	"net/http"
+
+	utils "github.com/sashabaranov/go-openai/internal"
 )
 
 var (
@@ -54,7 +56,7 @@ func (c *Client) CreateCompletionStream(
 			reader:             bufio.NewReader(resp.Body),
 			response:           resp,
 			errAccumulator:     newErrorAccumulator(),
-			unmarshaler:        &jsonUnmarshaler{},
+			unmarshaler:        &utils.JSONUnmarshaler{},
 		},
 	}
 	return
diff --git a/stream_reader.go b/stream_reader.go
index aa06f00..5eb6df7 100644
--- a/stream_reader.go
+++ b/stream_reader.go
@@ -6,6 +6,8 @@ import (
 	"fmt"
 	"io"
 	"net/http"
+
+	utils "github.com/sashabaranov/go-openai/internal"
 )
 
 type streamable interface {
@@ -19,7 +21,7 @@ type streamReader[T streamable] struct {
 	reader         *bufio.Reader
 	response       *http.Response
 	errAccumulator errorAccumulator
-	unmarshaler    unmarshaler
+	unmarshaler    utils.Unmarshaler
 }
 
 func (stream *streamReader[T]) Recv() (response T, err error) {
@@ -63,7 +65,7 @@ waitForData:
 		return
 	}
 
-	err = stream.unmarshaler.unmarshal(line, &response)
+	err = stream.unmarshaler.Unmarshal(line, &response)
 	return
 }
 
diff --git a/unmarshaler.go b/unmarshaler.go
deleted file mode 100644
index 05218f7..0000000
--- a/unmarshaler.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package openai
-
-import (
-	"encoding/json"
-)
-
-type unmarshaler interface {
-	unmarshal(data []byte, v any) error
-}
-
-type jsonUnmarshaler struct{}
-
-func (jm *jsonUnmarshaler) unmarshal(data []byte, v any) error {
-	return json.Unmarshal(data, v)
-}


Test patch:
diff --git a/error_accumulator_test.go b/error_accumulator_test.go
index ecf954d..821eb21 100644
--- a/error_accumulator_test.go
+++ b/error_accumulator_test.go
@@ -7,6 +7,7 @@ import (
 	"net/http"
 	"testing"
 
+	utils "github.com/sashabaranov/go-openai/internal"
 	"github.com/sashabaranov/go-openai/internal/test"
 	"github.com/sashabaranov/go-openai/internal/test/checks"
 )
@@ -33,7 +34,7 @@ func (b *failingErrorBuffer) Bytes() []byte {
 	return []byte{}
 }
 
-func (*failingUnMarshaller) unmarshal(_ []byte, _ any) error {
+func (*failingUnMarshaller) Unmarshal(_ []byte, _ any) error {
 	return errTestUnmarshalerFailed
 }
 
@@ -62,7 +63,7 @@ func TestErrorAccumulatorReturnsUnmarshalerErrors(t *testing.T) {
 func TestErrorByteWriteErrors(t *testing.T) {
 	accumulator := &defaultErrorAccumulator{
 		buffer:      &failingErrorBuffer{},
-		unmarshaler: &jsonUnmarshaler{},
+		unmarshaler: &utils.JSONUnmarshaler{},
 	}
 	err := accumulator.write([]byte("{"))
 	if !errors.Is(err, errTestErrorAccumulatorWriteFailed) {
@@ -91,7 +92,7 @@ func TestErrorAccumulatorWriteErrors(t *testing.T) {
 
 	stream.errAccumulator = &defaultErrorAccumulator{
 		buffer:      &failingErrorBuffer{},
-		unmarshaler: &jsonUnmarshaler{},
+		unmarshaler: &utils.JSONUnmarshaler{},
 	}
 
 	_, err = stream.Recv()
diff --git a/files_test.go b/files_test.go
index 56dbb41..ffdcfa7 100644
--- a/files_test.go
+++ b/files_test.go
@@ -1,7 +1,7 @@
 package openai //nolint:testpackage // testing private field
 
 import (
-	. "github.com/sashabaranov/go-openai/internal"
+	utils "github.com/sashabaranov/go-openai/internal"
 	"github.com/sashabaranov/go-openai/internal/test"
 	"github.com/sashabaranov/go-openai/internal/test/checks"
 
@@ -86,7 +86,7 @@ func TestFileUploadWithFailingFormBuilder(t *testing.T) {
 	config.BaseURL = ""
 	client := NewClientWithConfig(config)
 	mockBuilder := &mockFormBuilder{}
-	client.createFormBuilder = func(io.Writer) FormBuilder {
+	client.createFormBuilder = func(io.Writer) utils.FormBuilder {
 		return mockBuilder
 	}
 
diff --git a/request_builder_test.go b/request_builder_test.go
index b1adbf1..ed4b691 100644
--- a/request_builder_test.go
+++ b/request_builder_test.go
@@ -19,7 +19,7 @@ type (
 	failingMarshaller     struct{}
 )
 
-func (*failingMarshaller) marshal(_ any) ([]byte, error) {
+func (*failingMarshaller) Marshal(_ any) ([]byte, error) {
 	return []byte{}, errTestMarshallerFailed
 }