Repository: sashabaranov/go-openai
Pull request #174 (2023-03-18 15:23:13)

Check for `Stream` parameter usage

codecov[bot]: ## [Codecov](https://codecov.io/gh/sashabaranov/go-openai/pull/174?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#174](https://codecov.io/gh/sashabaranov/go-openai/pull/174?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (434fa74) into [master](https://codecov.io/gh/sashabaranov/go-openai/commit/34f3a118df332077d3c72ce30d86bcfa6b80a953?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (34f3a11) will **increase** coverage by `0.38%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #174      +/-   ##
==========================================
+ Coverage   70.26%   70.65%   +0.38%     
==========================================
  Files          18       18              
  Lines         528      535       +7     
==========================================
+ Hits          371      378       +7     
  Misses        121      121              
  Partials       36       36              
```


| [Impacted Files](https://codecov.io/gh/sashabaranov/go-openai/pull/174?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [chat.go](https://codecov.io/gh/sashabaranov/go-openai/pull/174?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y2hhdC5nbw==) | `100.00% <100.00%> (ø)` | |
| [completion.go](https://codecov.io/gh/sashabaranov/go-openai/pull/174?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29tcGxldGlvbi5nbw==) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/chat.go b/chat.go
index 99edfe8..0f56216 100644
--- a/chat.go
+++ b/chat.go
@@ -14,7 +14,8 @@ const (
 )
 
 var (
-	ErrChatCompletionInvalidModel = errors.New("currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported")
+	ErrChatCompletionInvalidModel       = errors.New("currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported")                 //nolint:lll
+	ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll
 )
 
 type ChatCompletionMessage struct {
@@ -65,8 +66,12 @@ func (c *Client) CreateChatCompletion(
 	ctx context.Context,
 	request ChatCompletionRequest,
 ) (response ChatCompletionResponse, err error) {
-	model := request.Model
-	switch model {
+	if request.Stream {
+		err = ErrChatCompletionStreamNotSupported
+		return
+	}
+
+	switch request.Model {
 	case GPT3Dot5Turbo0301, GPT3Dot5Turbo, GPT4, GPT40314, GPT432K0314, GPT432K:
 	default:
 		err = ErrChatCompletionInvalidModel
diff --git a/completion.go b/completion.go
index 66b4866..22211d3 100644
--- a/completion.go
+++ b/completion.go
@@ -7,7 +7,8 @@ import (
 )
 
 var (
-	ErrCompletionUnsupportedModel = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll
+	ErrCompletionUnsupportedModel   = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll
+	ErrCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateCompletionStream")                      //nolint:lll
 )
 
 // GPT3 Defines the models provided by OpenAI to use when generating
@@ -99,6 +100,11 @@ func (c *Client) CreateCompletion(
 	ctx context.Context,
 	request CompletionRequest,
 ) (response CompletionResponse, err error) {
+	if request.Stream {
+		err = ErrCompletionStreamNotSupported
+		return
+	}
+
 	if request.Model == GPT3Dot5Turbo0301 || request.Model == GPT3Dot5Turbo {
 		err = ErrCompletionUnsupportedModel
 		return


Test patch:
diff --git a/chat_test.go b/chat_test.go
index 5c03ebf..8866ff2 100644
--- a/chat_test.go
+++ b/chat_test.go
@@ -38,6 +38,21 @@ func TestChatCompletionsWrongModel(t *testing.T) {
 	}
 }
 
+func TestChatCompletionsWithStream(t *testing.T) {
+	config := DefaultConfig("whatever")
+	config.BaseURL = "http://localhost/v1"
+	client := NewClientWithConfig(config)
+	ctx := context.Background()
+
+	req := ChatCompletionRequest{
+		Stream: true,
+	}
+	_, err := client.CreateChatCompletion(ctx, req)
+	if !errors.Is(err, ErrChatCompletionStreamNotSupported) {
+		t.Fatalf("CreateChatCompletion didn't return ErrChatCompletionStreamNotSupported error")
+	}
+}
+
 // TestCompletions Tests the completions endpoint of the API using the mocked server.
 func TestChatCompletions(t *testing.T) {
 	server := test.NewTestServer()
diff --git a/completion_test.go b/completion_test.go
index 9868eb2..daa02e3 100644
--- a/completion_test.go
+++ b/completion_test.go
@@ -33,6 +33,18 @@ func TestCompletionsWrongModel(t *testing.T) {
 	}
 }
 
+func TestCompletionWithStream(t *testing.T) {
+	config := DefaultConfig("whatever")
+	client := NewClientWithConfig(config)
+
+	ctx := context.Background()
+	req := CompletionRequest{Stream: true}
+	_, err := client.CreateCompletion(ctx, req)
+	if !errors.Is(err, ErrCompletionStreamNotSupported) {
+		t.Fatalf("CreateCompletion didn't return ErrCompletionStreamNotSupported")
+	}
+}
+
 // TestCompletions Tests the completions endpoint of the API using the mocked server.
 func TestCompletions(t *testing.T) {
 	server := test.NewTestServer()
diff --git a/models_test.go b/models_test.go
index c96ece8..972a5fe 100644
--- a/models_test.go
+++ b/models_test.go
@@ -33,7 +33,7 @@ func TestListModels(t *testing.T) {
 }
 
 // handleModelsEndpoint Handles the models endpoint by the test server.
-func handleModelsEndpoint(w http.ResponseWriter, r *http.Request) {
+func handleModelsEndpoint(w http.ResponseWriter, _ *http.Request) {
 	resBytes, _ := json.Marshal(ModelsList{})
 	fmt.Fprintln(w, string(resBytes))
 }
diff --git a/request_builder_test.go b/request_builder_test.go
index 533977a..f0f99ee 100644
--- a/request_builder_test.go
+++ b/request_builder_test.go
@@ -19,11 +19,11 @@ type (
 	failingMarshaller     struct{}
 )
 
-func (*failingMarshaller) marshal(value any) ([]byte, error) {
+func (*failingMarshaller) marshal(_ any) ([]byte, error) {
 	return []byte{}, errTestMarshallerFailed
 }
 
-func (*failingRequestBuilder) build(ctx context.Context, method, url string, requset any) (*http.Request, error) {
+func (*failingRequestBuilder) build(_ context.Context, _, _ string, _ any) (*http.Request, error) {
 	return nil, errTestRequestBuilderFailed
 }