O cache-hit do DeepSeek-V4-Flash custa $0.0028/M contra $0.018/M na Deep Infra (~6,4x mais barato), e a Sollinha tem prefixo estável (system prompt + tools + historico), entao o custo efetivo cai. Mesmo modelo (deepseek-v4-flash), mesmas tools. @ai-sdk/deepseek pinado em 3.0.19 para ficar na ferroviaria provider 4.0.4 / provider-utils 5.0.18 do ai@7.0.47 (o ^ flutuaria a 3.0.26 e duplicaria 4.0.7). STT (B173) permanece na Deep Infra: whisper-large-v3 a $0.00045/min de audio e o mais barato verificado (Groq $0.0019, OpenAI $0.006). .env.example documenta as duas chaves: DEEPSEEK_API_KEY (LLM) e DEEPINFRA_API_KEY (STT).
73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import 'server-only'
|
|
|
|
/**
|
|
* Transcribes a recorded voice clip to text via Deep Infra's OpenAI-compatible
|
|
* transcription endpoint (B173). Deep Infra serves Whisper large-v3 at
|
|
* $0.00045/audio minute — the cheapest speech-to-text around — so it stays
|
|
* here even though the Sollinha LLM talks to api.deepseek.com directly
|
|
* (`DEEPSEEK_API_KEY`); this path uses its own `DEEPINFRA_API_KEY`.
|
|
*
|
|
* The audio is forwarded as a multipart `file` and never persisted anywhere —
|
|
* it lives only in this function's memory for the duration of the provider
|
|
* call (product requirement: B173 processes and discards the recording).
|
|
*/
|
|
|
|
export const DEEPINFRA_TRANSCRIBE_URL = 'https://api.deepinfra.com/v1/openai/audio/transcriptions'
|
|
const DEEPINFRA_WHISPER_MODEL = 'openai/whisper-large-v3'
|
|
const DEEPINFRA_LANGUAGE = 'pt'
|
|
|
|
export type TranscribeAudioResult =
|
|
| { ok: true; text: string }
|
|
| { ok: false; error: string; status: number }
|
|
|
|
/**
|
|
* Sends the clip to Deep Infra Whisper large-v3 and returns the transcript.
|
|
* Never throws: every failure becomes `{ ok: false }` with a pt-BR message the
|
|
* route can return verbatim, so a provider outage or a missing key degrades to
|
|
* "sem voz" instead of breaking the text chat.
|
|
*/
|
|
export const deepInfraTranscribe = async (file: Blob): Promise<TranscribeAudioResult> => {
|
|
const apiKey = process.env.DEEPINFRA_API_KEY
|
|
if (!apiKey) {
|
|
return { ok: false, error: 'Transcrição de voz está indisponível no momento.', status: 503 }
|
|
}
|
|
|
|
const form = new FormData()
|
|
form.append('file', file, 'voice.webm')
|
|
form.append('model', DEEPINFRA_WHISPER_MODEL)
|
|
form.append('language', DEEPINFRA_LANGUAGE)
|
|
|
|
try {
|
|
const response = await fetch(DEEPINFRA_TRANSCRIBE_URL, {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${apiKey}` },
|
|
body: form,
|
|
})
|
|
|
|
if (!response.ok) {
|
|
return {
|
|
ok: false,
|
|
error: 'Não foi possível transcrever o áudio. Tente novamente.',
|
|
status: 502,
|
|
}
|
|
}
|
|
|
|
const data = (await response.json()) as { text?: unknown }
|
|
const text = typeof data.text === 'string' ? data.text.trim() : ''
|
|
if (!text) {
|
|
return {
|
|
ok: false,
|
|
error: 'Não foi possível transcrever o áudio. Tente novamente.',
|
|
status: 502,
|
|
}
|
|
}
|
|
|
|
return { ok: true, text }
|
|
} catch {
|
|
return {
|
|
ok: false,
|
|
error: 'Não foi possível transcrever o áudio. Tente novamente.',
|
|
status: 502,
|
|
}
|
|
}
|
|
}
|