feat(stt): pass last assistant response as Whisper context to reduce mishearings
This commit is contained in:
@@ -40,16 +40,20 @@ class VoiceApi {
|
||||
}
|
||||
|
||||
/// POST WebM/Opus audio bytes and return the transcript string.
|
||||
/// [context] is optional recent conversation text passed as initial_prompt
|
||||
/// to Whisper, reducing mishearings of domain-specific words.
|
||||
/// Returns empty string on empty or error response.
|
||||
Future<String> transcribe(Uint8List audioBytes) async {
|
||||
Future<String> transcribe(Uint8List audioBytes, {String? context}) async {
|
||||
try {
|
||||
final formData = FormData.fromMap({
|
||||
final fields = <String, dynamic>{
|
||||
'audio': MultipartFile.fromBytes(
|
||||
audioBytes,
|
||||
filename: 'audio.m4a',
|
||||
contentType: DioMediaType('audio', 'mp4'),
|
||||
),
|
||||
});
|
||||
if (context != null && context.isNotEmpty) 'context': context,
|
||||
};
|
||||
final formData = FormData.fromMap(fields);
|
||||
final response = await _dio.post(
|
||||
'/api/voice/transcribe',
|
||||
data: formData,
|
||||
|
||||
@@ -7,6 +7,7 @@ class VoiceRepository {
|
||||
const VoiceRepository(this._api);
|
||||
|
||||
Future<VoiceStatus> checkStatus() => _api.checkStatus();
|
||||
Future<String> transcribe(Uint8List audioBytes) => _api.transcribe(audioBytes);
|
||||
Future<String> transcribe(Uint8List audioBytes, {String? context}) =>
|
||||
_api.transcribe(audioBytes, context: context);
|
||||
Future<Uint8List> synthesise(String text) => _api.synthesise(text);
|
||||
}
|
||||
|
||||
@@ -108,6 +108,10 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
||||
int _lastSeenLength = 0;
|
||||
bool _streamComplete = false;
|
||||
|
||||
// Last complete assistant response — passed to Whisper as initial_prompt
|
||||
// to reduce STT mishearings of domain-specific words.
|
||||
String _lastAssistantContent = '';
|
||||
|
||||
// TTS playback queue
|
||||
final _ttsQueue = Queue<Uint8List>();
|
||||
bool _ttsPlaying = false;
|
||||
@@ -205,6 +209,7 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
||||
_dispatchSentences(flush: isComplete);
|
||||
|
||||
if (isComplete) {
|
||||
_lastAssistantContent = fullContent;
|
||||
_streamComplete = true;
|
||||
_checkRestartListening();
|
||||
}
|
||||
@@ -278,8 +283,10 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
||||
|
||||
if (!state.voiceModeActive) return;
|
||||
|
||||
final transcript =
|
||||
await ref.read(voiceRepositoryProvider).transcribe(bytes);
|
||||
final transcript = await ref.read(voiceRepositoryProvider).transcribe(
|
||||
bytes,
|
||||
context: _lastAssistantContent.isNotEmpty ? _lastAssistantContent : null,
|
||||
);
|
||||
|
||||
if (!state.voiceModeActive) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user