-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openaiprovider): implement text-to-speech provider
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package openaiprovider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
baseprovider "github.com/nullswan/nomi/internal/providers/base" | ||
"github.com/sashabaranov/go-openai" | ||
) | ||
|
||
const ( | ||
OpenAITextToSpeechDefaultModel = openai.TTSModel1 | ||
) | ||
|
||
type TextToSpeechProvider struct { | ||
client *openai.Client | ||
} | ||
|
||
func NewTextToSpeechProvider( | ||
config oaiProviderConfig, | ||
) (baseprovider.TextToSpeechProvider, error) { | ||
p := &TextToSpeechProvider{ | ||
client: openai.NewClient(config.apiKey), | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
func (p TextToSpeechProvider) Close() error { | ||
return nil | ||
} | ||
|
||
func (p TextToSpeechProvider) GenerateSpeech( | ||
ctx context.Context, | ||
message string, | ||
) ([]byte, error) { | ||
resp, err := p.client.CreateSpeech(ctx, openai.CreateSpeechRequest{ | ||
Model: OpenAITextToSpeechDefaultModel, | ||
Voice: openai.VoiceAlloy, | ||
Input: message, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating speech: %w", err) | ||
} | ||
|
||
defer resp.Close() | ||
|
||
buf, err := io.ReadAll(resp) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading speech response: %w", err) | ||
} | ||
|
||
return buf, nil | ||
} | ||
|
||
// For now, we are always using the default model | ||
func (p TextToSpeechProvider) GetModel() string { | ||
return string(OpenAITextToSpeechDefaultModel) | ||
} |