-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat(groq): Add TTS adapter #346
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
Open
dhamivibez
wants to merge
7
commits into
TanStack:main
Choose a base branch
from
dhamivibez:feat/groq-tts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86022ee
feat(groq): Add TTS adapter
dhamivibez 5c99251
chore: add changeset for @tanstack/ai-groq
dhamivibez 1946804
Add TTS model support to ResolveProviderOptions
dhamivibez 28ad865
fix(groq-tts): unify TTS format and voice casting for Groq SDK
dhamivibez 7ac7173
Merge branch 'main' into feat/groq-tts
dhamivibez 8108a70
Merge branch 'main' into feat/groq-tts
AlemTuzlak 8d0d792
fix(groq): handle empty object schemas for Groq structured output
dhamivibez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@tanstack/ai-groq': minor | ||
| --- | ||
|
|
||
| Add tree-shakeable Text-to-Speech (TTS) adapter for Groq API with English and Arabic voices, multiple output formats (default WAV), configurable speed and sample rate, new types, model metadata, and unit tests. |
This file contains hidden or 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,159 @@ | ||
| import { BaseTTSAdapter } from '@tanstack/ai/adapters' | ||
| import { createGroqClient, generateId, getGroqApiKeyFromEnv } from '../utils' | ||
| import { validateAudioInput } from '../audio/audio-provider-options' | ||
| import type { GroqTTSModel } from '../model-meta' | ||
| import type { | ||
| GroqTTSFormat, | ||
| GroqTTSProviderOptions, | ||
| GroqTTSVoice, | ||
| } from '../audio/tts-provider-options' | ||
| import type { TTSOptions, TTSResult } from '@tanstack/ai' | ||
| import type Groq_SDK from 'groq-sdk' | ||
| import type { GroqClientConfig } from '../utils' | ||
|
|
||
| /** | ||
| * Configuration for Groq TTS adapter | ||
| */ | ||
| export interface GroqTTSConfig extends GroqClientConfig {} | ||
|
|
||
| /** | ||
| * Groq Text-to-Speech Adapter | ||
| * | ||
| * Tree-shakeable adapter for Groq TTS functionality. | ||
| * Supports canopylabs/orpheus-v1-english and canopylabs/orpheus-arabic-saudi models. | ||
| * | ||
| * Features: | ||
| * - English voices: autumn(f), diana(f), hannah(f), austin(m), daniel(m), troy(m) | ||
| * - Arabic voices: fahad(m), sultan(m), lulwa(f), noura(f) | ||
| * - Output formats: flac, mp3, mulaw, ogg, wav (only wav currently supported) | ||
| * - Speed control | ||
| * - Configurable sample rate | ||
| * - Vocal direction support (English voices only) | ||
| */ | ||
| export class GroqTTSAdapter<TModel extends GroqTTSModel> extends BaseTTSAdapter< | ||
| TModel, | ||
| GroqTTSProviderOptions | ||
| > { | ||
| readonly name = 'groq' as const | ||
|
|
||
| private client: Groq_SDK | ||
|
|
||
| constructor(config: GroqTTSConfig, model: TModel) { | ||
| super(config, model) | ||
| this.client = createGroqClient(config) | ||
| } | ||
|
|
||
| async generateSpeech( | ||
| options: TTSOptions<GroqTTSProviderOptions>, | ||
| ): Promise<TTSResult> { | ||
| const { | ||
| model, | ||
| text, | ||
| voice = 'autumn', | ||
| format = 'wav', | ||
| speed, | ||
| modelOptions, | ||
| } = options | ||
|
|
||
| validateAudioInput({ input: text, model }) | ||
|
|
||
| const voiceFormat = format as GroqTTSFormat | ||
|
|
||
| const request: Groq_SDK.Audio.Speech.SpeechCreateParams = { | ||
| model, | ||
| input: text, | ||
| voice: voice as GroqTTSVoice, | ||
| response_format: voiceFormat, | ||
| speed, | ||
| ...modelOptions, | ||
| } | ||
|
|
||
| const response = await this.client.audio.speech.create(request) | ||
|
|
||
| const arrayBuffer = await response.arrayBuffer() | ||
| const base64 = Buffer.from(arrayBuffer).toString('base64') | ||
dhamivibez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const contentType = this.getContentType(voiceFormat) | ||
|
|
||
| return { | ||
| id: generateId(this.name), | ||
| model, | ||
| audio: base64, | ||
| format: voiceFormat, | ||
| contentType, | ||
| } | ||
| } | ||
|
|
||
| private getContentType(format: string): string { | ||
| const contentTypes: Record<string, string> = { | ||
| flac: 'audio/flac', | ||
| mp3: 'audio/mpeg', | ||
| mulaw: 'audio/basic', | ||
| ogg: 'audio/ogg', | ||
| wav: 'audio/wav', | ||
| } | ||
| return contentTypes[format] || 'audio/wav' | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Groq speech adapter with explicit API key. | ||
| * Type resolution happens here at the call site. | ||
| * | ||
| * @param model - The model name (e.g., 'canopylabs/orpheus-v1-english') | ||
| * @param apiKey - Your Groq API key | ||
| * @param config - Optional additional configuration | ||
| * @returns Configured Groq speech adapter instance with resolved types | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const adapter = createGroqSpeech('canopylabs/orpheus-v1-english', "gsk_..."); | ||
| * | ||
| * const result = await generateSpeech({ | ||
| * adapter, | ||
| * text: 'Hello, world!', | ||
| * voice: 'autumn' | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export function createGroqSpeech<TModel extends GroqTTSModel>( | ||
| model: TModel, | ||
| apiKey: string, | ||
| config?: Omit<GroqTTSConfig, 'apiKey'>, | ||
| ): GroqTTSAdapter<TModel> { | ||
| return new GroqTTSAdapter({ apiKey, ...config }, model) | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Groq speech adapter with automatic API key detection from environment variables. | ||
| * Type resolution happens here at the call site. | ||
| * | ||
| * Looks for `GROQ_API_KEY` in: | ||
| * - `process.env` (Node.js) | ||
| * - `window.env` (Browser with injected env) | ||
| * | ||
| * @param model - The model name (e.g., 'canopylabs/orpheus-v1-english') | ||
| * @param config - Optional configuration (excluding apiKey which is auto-detected) | ||
| * @returns Configured Groq speech adapter instance with resolved types | ||
| * @throws Error if GROQ_API_KEY is not found in environment | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // Automatically uses GROQ_API_KEY from environment | ||
| * const adapter = groqSpeech('canopylabs/orpheus-v1-english'); | ||
| * | ||
| * const result = await generateSpeech({ | ||
| * adapter, | ||
| * text: 'Welcome to TanStack AI!', | ||
| * voice: 'autumn', | ||
| * format: 'wav' | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export function groqSpeech<TModel extends GroqTTSModel>( | ||
| model: TModel, | ||
| config?: Omit<GroqTTSConfig, 'apiKey'>, | ||
| ): GroqTTSAdapter<TModel> { | ||
| const apiKey = getGroqApiKeyFromEnv() | ||
| return createGroqSpeech(model, apiKey, config) | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
packages/typescript/ai-groq/src/audio/audio-provider-options.ts
This file contains hidden or 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,25 @@ | ||
| /** | ||
| * Common audio provider options for Groq audio endpoints. | ||
| */ | ||
| export interface AudioProviderOptions { | ||
| /** | ||
| * The text to generate audio for. | ||
| * Maximum length is 200 characters. | ||
| * Use [directions] for vocal control (English voices only). | ||
| */ | ||
| input: string | ||
| /** | ||
| * The audio model to use for generation. | ||
| */ | ||
| model: string | ||
| } | ||
|
|
||
| /** | ||
| * Validates that the audio input text does not exceed the maximum length. | ||
| * @throws Error if input text exceeds 200 characters | ||
| */ | ||
| export const validateAudioInput = (options: AudioProviderOptions) => { | ||
| if (options.input.length > 200) { | ||
| throw new Error('Input text exceeds maximum length of 200 characters.') | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
packages/typescript/ai-groq/src/audio/tts-provider-options.ts
This file contains hidden or 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,49 @@ | ||
| /** | ||
| * Groq TTS voice options for English models | ||
| */ | ||
| export type GroqTTSEnglishVoice = | ||
| | 'autumn' | ||
| | 'diana' | ||
| | 'hannah' | ||
| | 'austin' | ||
| | 'daniel' | ||
| | 'troy' | ||
|
|
||
| /** | ||
| * Groq TTS voice options for Arabic models | ||
| */ | ||
| export type GroqTTSArabicVoice = 'fahad' | 'sultan' | 'lulwa' | 'noura' | ||
|
|
||
| /** | ||
| * Union of all Groq TTS voice options | ||
| */ | ||
| export type GroqTTSVoice = GroqTTSEnglishVoice | GroqTTSArabicVoice | ||
|
|
||
| /** | ||
| * Groq TTS output format options. | ||
| * Only wav is currently supported. | ||
| */ | ||
| export type GroqTTSFormat = 'flac' | 'mp3' | 'mulaw' | 'ogg' | 'wav' | ||
dhamivibez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Groq TTS sample rate options | ||
| */ | ||
| export type GroqTTSSampleRate = | ||
| | 8000 | ||
| | 16000 | ||
| | 22050 | ||
| | 24000 | ||
| | 32000 | ||
| | 44100 | ||
| | 48000 | ||
|
|
||
| /** | ||
| * Provider-specific options for Groq TTS. | ||
| * These options are passed via `modelOptions` when calling `generateSpeech`. | ||
| */ | ||
| export interface GroqTTSProviderOptions { | ||
| /** | ||
| * The sample rate of the generated audio in Hz. | ||
| */ | ||
| sample_rate?: GroqTTSSampleRate | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.