Skip to content

Commit

Permalink
Fix support for non json speech to text
Browse files Browse the repository at this point in the history
Fixes issue when using non audio formats:

Transcription error: json: cannot unmarshal number into Go value of type openai.audioTextResponse

Fixes: 8e165dc ("Feat Add headers to openai responses (sashabaranov#506)")
  • Loading branch information
mauriciovasquezbernal committed Dec 20, 2023
1 parent a09cb0c commit 687f7d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ type AudioResponse struct {
}

type audioTextResponse struct {
Text string `json:"text"`
Text string

httpHeader
}

func (r *audioTextResponse) SetText(text string) {
r.Text = text
}

func (r *audioTextResponse) ToAudioResponse() AudioResponse {
return AudioResponse{
Text: r.Text,
Expand Down
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type Response interface {
SetHeader(http.Header)
}

type TextResponse interface {
SetText(string)
}

type httpHeader http.Header

func (h *httpHeader) SetHeader(header http.Header) {
Expand Down Expand Up @@ -195,6 +199,13 @@ func decodeResponse(body io.Reader, v any) error {

if result, ok := v.(*string); ok {
return decodeString(body, result)
} else if result, ok := v.(TextResponse); ok {
var text string
if err := decodeString(body, &text); err != nil {
return err
}
result.SetText(text)
return nil
}
return json.NewDecoder(body).Decode(v)
}
Expand Down

0 comments on commit 687f7d5

Please sign in to comment.