-
Notifications
You must be signed in to change notification settings - Fork 27
Audio chat #292
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
Audio chat #292
Changes from 1 commit
3274c7f
b98e717
285b40d
0c0ed92
2d53275
3a706b8
ffe2bcf
7594d46
9a72e81
bb6124e
e609d1d
ba4ea60
4cecfca
cb6529a
6d85f2d
330296c
4eb342f
891bb75
003cdb3
5cc7c59
7afb3e1
77c2647
cf6648a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Julien Veyssier <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,12 @@ | |
| use OCA\Assistant\Db\ChattyLLM\Message; | ||
| use OCA\Assistant\Db\ChattyLLM\MessageMapper; | ||
| use OCA\Assistant\Db\ChattyLLM\SessionMapper; | ||
| use OCA\Assistant\Service\TaskProcessingService; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use OCP\TaskProcessing\Events\TaskSuccessfulEvent; | ||
| use OCP\TaskProcessing\Task; | ||
| use OCP\TaskProcessing\TaskTypes\TextToSpeech; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
|
|
@@ -26,6 +29,7 @@ class ChattyLLMTaskListener implements IEventListener { | |
| public function __construct( | ||
| private MessageMapper $messageMapper, | ||
| private SessionMapper $sessionMapper, | ||
| private TaskProcessingService $taskProcessingService, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
@@ -75,7 +79,7 @@ public function handle(Event $event): void { | |
| $message->setContent($outputTranscript); | ||
| // agency might not return any output but just ask for confirmation | ||
| if ($outputTranscript !== '') { | ||
| $message->setAttachments('[{"type":"Audio","fileId":' . $task->getOutput()['output'] . '}]'); | ||
| $message->setAttachments('[{"type":"Audio","file_id":' . $task->getOutput()['output'] . '}]'); | ||
| } | ||
| // now we have the transcription of the user audio input | ||
| if (preg_match('/^chatty-llm:\d+:(\d+)$/', $customId, $matches)) { | ||
|
|
@@ -85,7 +89,9 @@ public function handle(Event $event): void { | |
| $this->messageMapper->update($queryMessage); | ||
| } | ||
| } else { | ||
| $message->setContent(trim($task->getOutput()['output'] ?? '')); | ||
| $content = trim($task->getOutput()['output'] ?? ''); | ||
| $message->setContent($content); | ||
| $this->runTtsIfNeeded($sessionId, $message, $taskTypeId, $task->getUserId()); | ||
| } | ||
| try { | ||
| $this->messageMapper->insert($message); | ||
|
|
@@ -104,4 +110,48 @@ public function handle(Event $event): void { | |
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run TTS on the response of an agency confirmation message | ||
| * | ||
| * @param int $sessionId | ||
| * @param Message $message | ||
| * @param string $taskTypeId | ||
| * @param string|null $userId | ||
| * @return void | ||
| */ | ||
| private function runTtsIfNeeded(int $sessionId, Message $message, string $taskTypeId, ?string $userId): void { | ||
| if ($taskTypeId !== \OCP\TaskProcessing\TaskTypes\ContextAgentInteraction::ID) { | ||
| return; | ||
| } | ||
| // is the last non-empty user message an audio one? | ||
| $lastNonEmptyMessage = $this->messageMapper->getLastNonEmptyHumanMessage($sessionId); | ||
| $attachments = $lastNonEmptyMessage->jsonSerialize()['attachments'] ?? []; | ||
| foreach ($attachments as $attachment) { | ||
| if ($attachment['type'] === 'Audio') { | ||
| // we found an audio attachment | ||
| $this->runTtsTask($message, $userId); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param Message $message | ||
| * @param string|null $userId | ||
| * @return void | ||
| */ | ||
| private function runTtsTask(Message $message, ?string $userId): void { | ||
| $task = new Task( | ||
| TextToSpeech::ID, | ||
| ['input' => $message->getContent()], | ||
| Application::APP_ID . ':internal', | ||
| $userId, | ||
| ); | ||
| $ttsTaskOutput = $this->taskProcessingService->runTaskProcessingTask($task); | ||
|
||
| $speechFileId = $ttsTaskOutput['speech']; | ||
| // we need to set "ocp_task_id" here because the file is not an output of the task that produced the message | ||
| // and we need the task ID + the file ID to load the audio file in the frontend | ||
| $message->setAttachments('[{"type":"Audio","file_id":' . $speechFileId . ',"ocp_task_id":' . $task->getId() . '}]'); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.