|
1 | 1 | package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + |
| 7 | + // Packages |
| 8 | + client "github.com/mutablelogic/go-client" |
| 9 | + llm "github.com/mutablelogic/go-llm" |
| 10 | +) |
| 11 | + |
| 12 | +/////////////////////////////////////////////////////////////////////////////// |
| 13 | +// PRIVATE METHODS |
| 14 | + |
| 15 | +type reqAudioCompletion struct { |
| 16 | + Model string `json:"model"` |
| 17 | + Input string `json:"input"` |
| 18 | + Voice string `json:"voice"` |
| 19 | + Speed float64 `json:"speed,omitempty"` |
| 20 | + ResponseFormat string `json:"response_format,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type responseAudio struct { |
| 24 | + audio *llm.Attachment |
| 25 | +} |
| 26 | + |
| 27 | +// Send a completion request with text for text-to-speech |
| 28 | +func (model *model) audioCompletion(ctx context.Context, input string, opt *llm.Opts) (llm.Completion, error) { |
| 29 | + // Request |
| 30 | + req, err := client.NewJSONRequest(reqAudioCompletion{ |
| 31 | + Model: model.Name(), |
| 32 | + Input: input, |
| 33 | + Voice: optVoice(opt), |
| 34 | + Speed: optSpeed(opt), |
| 35 | + ResponseFormat: optAudioFormat(opt), |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + // Response |
| 42 | + var response responseAudio |
| 43 | + if err := model.DoWithContext(ctx, req, &response, client.OptPath("audio", "speech")); err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return &response, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (resp *responseAudio) Unmarshal(mimetype string, r io.Reader) error { |
| 51 | + // Unmarshal the response |
| 52 | + attachment, err := llm.ReadAttachment(r, mimetype) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } else { |
| 56 | + resp.audio = attachment |
| 57 | + } |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +/////////////////////////////////////////////////////////////////////////////// |
| 62 | +// COMPLETION |
| 63 | + |
| 64 | +// Return the number of completions |
| 65 | +func (r *responseAudio) Num() int { |
| 66 | + return 1 |
| 67 | +} |
| 68 | + |
| 69 | +// Return message for a specific completion |
| 70 | +func (r *responseAudio) Choice(index int) llm.Completion { |
| 71 | + if index != 0 { |
| 72 | + return nil |
| 73 | + } |
| 74 | + return r |
| 75 | +} |
| 76 | + |
| 77 | +// Return the role of the completion |
| 78 | +func (r *responseAudio) Role() string { |
| 79 | + return "assistant" |
| 80 | +} |
| 81 | + |
| 82 | +// Unsupported |
| 83 | +func (r *responseAudio) Text(index int) string { |
| 84 | + return "" |
| 85 | +} |
| 86 | + |
| 87 | +// Return media content for a specific completion |
| 88 | +func (r *responseAudio) Attachment(index int) *llm.Attachment { |
| 89 | + if index != 0 { |
| 90 | + return nil |
| 91 | + } else { |
| 92 | + return r.audio |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Unsupported |
| 97 | +func (r *responseAudio) ToolCalls(index int) []llm.ToolCall { |
| 98 | + return nil |
| 99 | +} |
0 commit comments