Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audio): fix audioTextResponse decode #638

Merged
merged 3 commits into from
Jan 17, 2024

Conversation

WqyJh
Copy link
Contributor

@WqyJh WqyJh commented Jan 16, 2024

The audio api passes audioTextResponse to sendRequest,

	if request.HasJSONResponse() {
		err = c.sendRequest(req, &response)
	} else {
		var textResponse audioTextResponse
		err = c.sendRequest(req, &textResponse)
		response = textResponse.ToAudioResponse()
	}

but it cannot be asserted as *string type, lead to decoding failure.

func decodeResponse(body io.Reader, v any) error {
	if v == nil {
		return nil
	}

	if result, ok := v.(*string); ok {
		return decodeString(body, result)
	}
	return json.NewDecoder(body).Decode(v)
}

Fix this problem.

Copy link

codecov bot commented Jan 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4ce03a9) 98.43% compared to head (c4f4d18) 98.43%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #638   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files          24       24           
  Lines        1342     1345    +3     
=======================================
+ Hits         1321     1324    +3     
  Misses         15       15           
  Partials        6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sashabaranov
Copy link
Owner

@WqyJh thank you for the PR! Could you also please add a test for that?

@WqyJh
Copy link
Contributor Author

WqyJh commented Jan 17, 2024

Added test.

@sashabaranov
Copy link
Owner

@WqyJh thanks! There are some linter problems now

client_test.go Outdated
if err == nil {
t.Error("Unexpected nil error")
}
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can reduce the amount of else by using early return pattern:

if something {
// code
    return
}

// else block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! It would be better to use github.com/stretchr/testify/assert for testing, as it eliminates the need for if/else statements for assertions. However, I didn't use it because it doesn't have any external dependencies. Would you like to use it or keep it dependency-free?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WqyJh staying dependency free is important for the project, so let's not use the library. We have an internal lib of helpers though: https://github.com/sashabaranov/go-openai/blob/master/internal/test/checks/checks.go

Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sashabaranov sashabaranov merged commit eff8dc1 into sashabaranov:master Jan 17, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants