From e4e654f0b09e7762694b2b4f2a295e9e28adf586 Mon Sep 17 00:00:00 2001 From: zhangxm Date: Tue, 25 Apr 2023 18:46:18 +0800 Subject: [PATCH 01/10] Fix OpenAI or Azure returns an undefined error message return --- client.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client.go b/client.go index 368947b23..500b3d56e 100644 --- a/client.go +++ b/client.go @@ -148,6 +148,9 @@ func (c *Client) handleErrorResp(resp *http.Response) error { HTTPStatusCode: resp.StatusCode, Err: err, } + if errRes.Error != nil { + reqErr.Err = errRes.Error + } return fmt.Errorf("error, %w", &reqErr) } errRes.Error.HTTPStatusCode = resp.StatusCode From 31841da43e5a3739f48515d3496e8b801450d8dd Mon Sep 17 00:00:00 2001 From: zhangxm Date: Wed, 26 Apr 2023 10:18:24 +0800 Subject: [PATCH 02/10] Fix: OpenAI or Azure returns an undefined error message return #280 --- client_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/client_test.go b/client_test.go index 7bea6dd87..30d056125 100644 --- a/client_test.go +++ b/client_test.go @@ -3,6 +3,7 @@ package openai //nolint:testpackage // testing private field import ( "bytes" "io" + "net/http" "testing" ) @@ -57,3 +58,41 @@ func TestDecodeResponse(t *testing.T) { }) } } + +func TestHandleErrorResp(t *testing.T) { + const mockToken = "mock token" + client := NewClient(mockToken) + + testCases := []struct { + name string + httpCode int + body io.Reader + expected string + }{ + { + name: "401 Invalid Authentication", + httpCode: http.StatusUnauthorized, + body: bytes.NewReader([]byte(`{"error":{"message":"You didn't provide an API key. ....","type":"invalid_request_error","param":null,"code":null}}`)), + expected: "error, status code: 401, message: You didn't provide an API key. ....", + }, + { + name: "401 Invalid Authentication", + httpCode: http.StatusUnauthorized, + body: bytes.NewReader([]byte(`{"error":{"code":"AccessDenied","message":"Access denied due to Virtual Network/Firewall rules."}}`)), + expected: "error, Access denied due to Virtual Network/Firewall rules.", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + testCase := &http.Response{} + testCase.StatusCode = tc.httpCode + testCase.Body = io.NopCloser(tc.body) + err := client.handleErrorResp(testCase) + if err.Error() != tc.expected { + t.Errorf("Unexpected error: %v , expected: %s", err, tc.expected) + t.Fail() + } + }) + } +} From c9d1faf8b260d85c6668e7175183d7b7e5eb3620 Mon Sep 17 00:00:00 2001 From: zhangxm Date: Wed, 26 Apr 2023 10:51:55 +0800 Subject: [PATCH 03/10] Fix: OpenAI or Azure returns an undefined error message return #280 --- client_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 30d056125..84be07c43 100644 --- a/client_test.go +++ b/client_test.go @@ -76,11 +76,17 @@ func TestHandleErrorResp(t *testing.T) { expected: "error, status code: 401, message: You didn't provide an API key. ....", }, { - name: "401 Invalid Authentication", + name: "401 Azure Access Denied", httpCode: http.StatusUnauthorized, body: bytes.NewReader([]byte(`{"error":{"code":"AccessDenied","message":"Access denied due to Virtual Network/Firewall rules."}}`)), expected: "error, Access denied due to Virtual Network/Firewall rules.", }, + { + name: "503 Model Overloaded", + httpCode: http.StatusServiceUnavailable, + body: bytes.NewReader([]byte(`{"error":{"message":"That model is currently overloaded with other requests. You can retry your request...","type":"server_error","param":null,"code":null}}`)), + expected: "error, status code: 503, message: That model is currently overloaded with other requests. You can retry your request...", + }, } for _, tc := range testCases { From b2453656c4ab53b5d78e04db9c9afbe6427c030d Mon Sep 17 00:00:00 2001 From: zhangxm Date: Wed, 26 Apr 2023 11:04:22 +0800 Subject: [PATCH 04/10] Fix: OpenAI or Azure returns an undefined error message return #280 Handle long line --- client_test.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/client_test.go b/client_test.go index 84be07c43..0042f41f7 100644 --- a/client_test.go +++ b/client_test.go @@ -72,20 +72,44 @@ func TestHandleErrorResp(t *testing.T) { { name: "401 Invalid Authentication", httpCode: http.StatusUnauthorized, - body: bytes.NewReader([]byte(`{"error":{"message":"You didn't provide an API key. ....","type":"invalid_request_error","param":null,"code":null}}`)), + body: bytes.NewReader([]byte( + `{ + "error":{ + "message":"You didn't provide an API key. ....", + "type":"invalid_request_error", + "param":null, + "code":null + } + }`, + )), expected: "error, status code: 401, message: You didn't provide an API key. ....", }, { name: "401 Azure Access Denied", httpCode: http.StatusUnauthorized, - body: bytes.NewReader([]byte(`{"error":{"code":"AccessDenied","message":"Access denied due to Virtual Network/Firewall rules."}}`)), + body: bytes.NewReader([]byte( + `{ + "error":{ + "code":"AccessDenied", + "message":"Access denied due to Virtual Network/Firewall rules." + } + }`, + )), expected: "error, Access denied due to Virtual Network/Firewall rules.", }, { name: "503 Model Overloaded", httpCode: http.StatusServiceUnavailable, - body: bytes.NewReader([]byte(`{"error":{"message":"That model is currently overloaded with other requests. You can retry your request...","type":"server_error","param":null,"code":null}}`)), - expected: "error, status code: 503, message: That model is currently overloaded with other requests. You can retry your request...", + body: bytes.NewReader([]byte(` + { + "error":{ + "message":"That model...", + "type":"server_error", + "param":null, + "code":null + } + }`)), + expected: "error, status code: 503, message: That model...", }, } From 289c15cbc8e434e9a832670736b931a726d1d014 Mon Sep 17 00:00:00 2001 From: zhangxm Date: Wed, 26 Apr 2023 17:49:43 +0800 Subject: [PATCH 05/10] Fix: OpenAI or Azure returns an undefined error message return #280 Add unit test ErrorResponse nil pointer check --- client_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/client_test.go b/client_test.go index 0042f41f7..3f0d67860 100644 --- a/client_test.go +++ b/client_test.go @@ -2,6 +2,7 @@ package openai //nolint:testpackage // testing private field import ( "bytes" + "fmt" "io" "net/http" "testing" @@ -60,6 +61,16 @@ func TestDecodeResponse(t *testing.T) { } func TestHandleErrorResp(t *testing.T) { + //var errRes *ErrorResponse + var errRes ErrorResponse + var reqErr RequestError + t.Log(errRes, errRes.Error) + if errRes.Error != nil { + reqErr.Err = errRes.Error + } + t.Log(fmt.Errorf("error, %w", &reqErr)) + t.Log(errRes.Error, "nil pointer check Pass") + const mockToken = "mock token" client := NewClient(mockToken) From f23fdfda6977f571a89e48fd4a1d8b926e6db8fe Mon Sep 17 00:00:00 2001 From: zhangxm Date: Wed, 26 Apr 2023 17:53:37 +0800 Subject: [PATCH 06/10] Fix: OpenAI or Azure returns an undefined error message return #280 Add unit test ErrorResponse nil pointer check --- client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 3f0d67860..ca5145cc6 100644 --- a/client_test.go +++ b/client_test.go @@ -61,7 +61,7 @@ func TestDecodeResponse(t *testing.T) { } func TestHandleErrorResp(t *testing.T) { - //var errRes *ErrorResponse + // var errRes *ErrorResponse var errRes ErrorResponse var reqErr RequestError t.Log(errRes, errRes.Error) From 59f29122f2814855fc4d71d092f9a5aabff9cdc3 Mon Sep 17 00:00:00 2001 From: zhangxm Date: Thu, 11 May 2023 18:57:01 +0800 Subject: [PATCH 07/10] Upgrade: According to the modification of #306, adjust the azure model deployment name call corresponding to README #308 --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 272d853c6..3093f1621 100644 --- a/README.md +++ b/README.md @@ -436,7 +436,13 @@ import ( func main() { - config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint ", "your Model deployment name") + config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint") + config.AzureModelMapperFunc = func(model string) string { + azureModelMapping = []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(), From 8e5c036c49ece48609ec70de332f9bc51b3b8e2a Mon Sep 17 00:00:00 2001 From: zhangxm Date: Fri, 12 May 2023 09:13:12 +0800 Subject: [PATCH 08/10] Upgrade: According to the modification of #306, adjust the azure model deployment name call corresponding to README #308 Add AzureModelMapperFunc custom function instructions --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3093f1621..0efef44eb 100644 --- a/README.md +++ b/README.md @@ -437,12 +437,14 @@ import ( func main() { config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint") - config.AzureModelMapperFunc = func(model string) string { - azureModelMapping = []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 = []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(), From 56de2391dea250159f488362bdfffb97ed2172e4 Mon Sep 17 00:00:00 2001 From: zhangxm Date: Fri, 12 May 2023 09:41:33 +0800 Subject: [PATCH 09/10] Upgrade: According to the modification of #306, adjust the azure model deployment name call corresponding to README #308 Add AzureModelMapperFunc custom function instructions --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0efef44eb..01d965651 100644 --- a/README.md +++ b/README.md @@ -439,11 +439,11 @@ 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 = []string{ - // "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name", - // } - // return azureModelMapping[model] - //} + // azureModelMapping = []string{ + // "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name", + // } + // return azureModelMapping[model] + //} client := openai.NewClientWithConfig(config) resp, err := client.CreateChatCompletion( From 12ddb93c5c6c40bf2b3367baa87a204ec3d0c5a4 Mon Sep 17 00:00:00 2001 From: "xuanming.zhang" Date: Sat, 13 May 2023 18:06:01 +0800 Subject: [PATCH 10/10] Update README.md Modify variable type definition error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 01d965651..994f26dd1 100644 --- a/README.md +++ b/README.md @@ -439,7 +439,7 @@ 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 = []string{ + // azureModelMapping = map[string]string{ // "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name", // } // return azureModelMapping[model]