Skip to content
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

Bring back textProcessing, translation, STT, and image generation providers #120

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
namespace OCA\OpenAi\AppInfo;

use OCA\OpenAi\Capabilities;
use OCA\OpenAi\OldProcessing\SpeechToText\STTProvider as OldSTTProvider;
use OCA\OpenAi\OldProcessing\TextProcessing\FreePromptProvider as OldFreePromptProvider;
use OCA\OpenAi\OldProcessing\TextProcessing\HeadlineProvider as OldHeadlineProvider;
use OCA\OpenAi\OldProcessing\TextProcessing\SummaryProvider as OldSummaryProvider;
use OCA\OpenAi\OldProcessing\TextToImage\TextToImageProvider as OldTextToImageProvider;
use OCA\OpenAi\OldProcessing\Translation\TranslationProvider as OldTranslationProvider;
use OCA\OpenAi\TaskProcessing\AudioToTextProvider;
use OCA\OpenAi\TaskProcessing\ContextWriteProvider;
use OCA\OpenAi\TaskProcessing\HeadlineProvider;
use OCA\OpenAi\TaskProcessing\ReformulateProvider;
use OCA\OpenAi\TaskProcessing\STTProvider;
use OCA\OpenAi\TaskProcessing\SummaryProvider;
use OCA\OpenAi\TaskProcessing\TextToImageProvider;
use OCA\OpenAi\TaskProcessing\TextToTextChatProvider;
Expand Down Expand Up @@ -72,11 +78,29 @@ public function __construct(array $urlParams = []) {
}

public function register(IRegistrationContext $context): void {
// deprecated APIs
if ($this->config->getAppValue(Application::APP_ID, 'translation_provider_enabled', '1') === '1') {
$context->registerTranslationProvider(OldTranslationProvider::class);
}
if ($this->config->getAppValue(Application::APP_ID, 'stt_provider_enabled', '1') === '1') {
$context->registerSpeechToTextProvider(OldSTTProvider::class);
}

if ($this->config->getAppValue(Application::APP_ID, 'llm_provider_enabled', '1') === '1') {
$context->registerTextProcessingProvider(OldFreePromptProvider::class);
$context->registerTextProcessingProvider(OldSummaryProvider::class);
$context->registerTextProcessingProvider(OldHeadlineProvider::class);
}
if ($this->config->getAppValue(Application::APP_ID, 't2i_provider_enabled', '1') === '1') {
$context->registerTextToImageProvider(OldTextToImageProvider::class);
}

// Task processing
if ($this->config->getAppValue(Application::APP_ID, 'translation_provider_enabled', '1') === '1') {
$context->registerTaskProcessingProvider(TranslateProvider::class);
}
if ($this->config->getAppValue(Application::APP_ID, 'stt_provider_enabled', '1') === '1') {
$context->registerTaskProcessingProvider(STTProvider::class);
$context->registerTaskProcessingProvider(AudioToTextProvider::class);
}

if ($this->config->getAppValue(Application::APP_ID, 'llm_provider_enabled', '1') === '1') {
Expand Down
44 changes: 44 additions & 0 deletions lib/OldProcessing/SpeechToText/STTProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
// SPDX-FileCopyrightText: Julien Veyssier <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\OpenAi\OldProcessing\SpeechToText;

use OCA\OpenAi\Service\OpenAiAPIService;
use OCP\Files\File;
use OCP\IL10N;
use OCP\SpeechToText\ISpeechToTextProvider;
use Psr\Log\LoggerInterface;

class STTProvider implements ISpeechToTextProvider {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private LoggerInterface $logger,
private IL10N $l,
private ?string $userId,
) {
}

/**
* @inheritDoc
*/
public function getName(): string {
return $this->openAiAPIService->isUsingOpenAi()
? $this->l->t('OpenAI\'s Whisper Speech-To-Text')
: $this->l->t('LocalAI\'s Whisper Speech-To-Text');
}

/**
* @inheritDoc
*/
public function transcribeFile(File $file): string {
try {
return $this->openAiAPIService->transcribeFile($this->userId, $file);
} catch(\Exception $e) {
$this->logger->warning('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new \RuntimeException('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage());
}
}
}
68 changes: 68 additions & 0 deletions lib/OldProcessing/TextProcessing/FreePromptProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace OCA\OpenAi\OldProcessing\TextProcessing;

use Exception;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCP\IConfig;
use OCP\TextProcessing\FreePromptTaskType;
use OCP\TextProcessing\IProviderWithExpectedRuntime;
use OCP\TextProcessing\IProviderWithUserId;
use RuntimeException;

/**
* @template-implements IProviderWithExpectedRuntime<FreePromptTaskType>
* @template-implements IProviderWithUserId<FreePromptTaskType>
*/
class FreePromptProvider implements IProviderWithExpectedRuntime, IProviderWithUserId {

public function __construct(
private OpenAiAPIService $openAiAPIService,
private IConfig $config,
private ?string $userId,
private OpenAiSettingsService $openAiSettingsService,
) {
}

public function getName(): string {
return $this->openAiAPIService->getServiceName();
}

public function process(string $prompt): string {
$startTime = time();
$adminModel = $this->config->getAppValue(Application::APP_ID, 'default_completion_model_id', Application::DEFAULT_COMPLETION_MODEL_ID) ?: Application::DEFAULT_COMPLETION_MODEL_ID;
// Max tokens are limited later to max tokens specified in the admin settings so here we just request PHP_INT_MAX
try {
if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) {
$completion = $this->openAiAPIService->createChatCompletion($this->userId, $adminModel, $prompt, null, null, 1, PHP_INT_MAX);
} else {
$completion = $this->openAiAPIService->createCompletion($this->userId, $prompt, 1, $adminModel, PHP_INT_MAX);
}
} catch (Exception $e) {
throw new RuntimeException('OpenAI/LocalAI request failed: ' . $e->getMessage());
}
if (count($completion) > 0) {
$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
return array_pop($completion);
}

throw new RuntimeException('No result in OpenAI/LocalAI response.');
}

public function getTaskType(): string {
return FreePromptTaskType::class;
}

public function getExpectedRuntime(): int {
return $this->openAiAPIService->getExpTextProcessingTime();
}

public function setUserId(?string $userId): void {
$this->userId = $userId;
}
}
68 changes: 68 additions & 0 deletions lib/OldProcessing/TextProcessing/HeadlineProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace OCA\OpenAi\OldProcessing\TextProcessing;

use Exception;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCP\IConfig;
use OCP\TextProcessing\HeadlineTaskType;
use OCP\TextProcessing\IProviderWithExpectedRuntime;
use OCP\TextProcessing\IProviderWithUserId;
use RuntimeException;

/**
* @template-implements IProviderWithExpectedRuntime<HeadlineTaskType>
* @template-implements IProviderWithUserId<HeadlineTaskType>
*/
class HeadlineProvider implements IProviderWithExpectedRuntime, IProviderWithUserId {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private IConfig $config,
private ?string $userId,
private OpenAiSettingsService $openAiSettingsService,
) {
}

public function getName(): string {
return $this->openAiAPIService->getServiceName();
}

public function process(string $prompt): string {
$startTime = time();
$adminModel = $this->config->getAppValue(Application::APP_ID, 'default_completion_model_id', Application::DEFAULT_COMPLETION_MODEL_ID) ?: Application::DEFAULT_COMPLETION_MODEL_ID;
$prompt = 'Give me the headline of the following text in its original language. Output only the headline.' . "\n\n" . $prompt;
// Max tokens are limited later to max tokens specified in the admin settings so here we just request PHP_INT_MAX
try {
if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) {
$completion = $this->openAiAPIService->createChatCompletion($this->userId, $adminModel, $prompt, null, null, 1, PHP_INT_MAX);
} else {
$completion = $this->openAiAPIService->createCompletion($this->userId, $prompt, 1, $adminModel, PHP_INT_MAX);
}
} catch (Exception $e) {
throw new RunTimeException('OpenAI/LocalAI request failed: ' . $e->getMessage());
}
if (count($completion) > 0) {
$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
return array_pop($completion);
}

throw new RunTimeException('No result in OpenAI/LocalAI response. ');
}

public function getTaskType(): string {
return HeadlineTaskType::class;
}

public function getExpectedRuntime(): int {
return $this->openAiAPIService->getExpTextProcessingTime();
}

public function setUserId(?string $userId): void {
$this->userId = $userId;
}
}
71 changes: 71 additions & 0 deletions lib/OldProcessing/TextProcessing/SummaryProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace OCA\OpenAi\OldProcessing\TextProcessing;

use Exception;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\OpenAiSettingsService;
use OCP\IConfig;
use OCP\TextProcessing\IProviderWithExpectedRuntime;
use OCP\TextProcessing\IProviderWithUserId;
use OCP\TextProcessing\SummaryTaskType;
use RuntimeException;

/**
* @template-implements IProviderWithExpectedRuntime<SummaryTaskType>
* @template-implements IProviderWithUserId<SummaryTaskType>
*/
class SummaryProvider implements IProviderWithExpectedRuntime, IProviderWithUserId {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private IConfig $config,
private ?string $userId,
private OpenAiSettingsService $openAiSettingsService,
) {
}

public function getName(): string {
return $this->openAiAPIService->getServiceName();
}

public function process(string $prompt): string {
$startTime = time();
// to try it out:
// curl -H "content-type: application/json" -H "ocs-apirequest: true" -u user:pass http://localhost/dev/server/ocs/v2.php/textprocessing/schedule -d '{"input":"this is a short sentence to talk about food and weather and sport","type":"OCP\\TextProcessing\\SummaryTaskType","appId":"plopapp","identifier":"superidentifier"}' -X POST
$adminModel = $this->config->getAppValue(Application::APP_ID, 'default_completion_model_id', Application::DEFAULT_COMPLETION_MODEL_ID) ?: Application::DEFAULT_COMPLETION_MODEL_ID;
$prompt = 'Summarize the following text. Detect the language of the text. Use the same language as the text. Output only the summary. Here is the text:' . "\n\n" . $prompt . "\n\n" . 'Here is your summary in the same language as the text:';
// Max tokens are limited later to max tokens specified in the admin settings so here we just request PHP_INT_MAX
try {
if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) {
$completion = $this->openAiAPIService->createChatCompletion($this->userId, $adminModel, $prompt, null, null, 1, PHP_INT_MAX);
} else {
$completion = $this->openAiAPIService->createCompletion($this->userId, $prompt, 1, $adminModel, PHP_INT_MAX);
}
} catch (Exception $e) {
throw new RunTimeException('OpenAI/LocalAI request failed: ' . $e->getMessage());
}
if (count($completion) > 0) {
$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
return array_pop($completion);
}

throw new RunTimeException('No result in OpenAI/LocalAI response. ');

}

public function getTaskType(): string {
return SummaryTaskType::class;
}

public function getExpectedRuntime(): int {
return $this->openAiAPIService->getExpTextProcessingTime();
}

public function setUserId(?string $userId): void {
$this->userId = $userId;
}
}
83 changes: 83 additions & 0 deletions lib/OldProcessing/TextToImage/TextToImageProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);
// SPDX-FileCopyrightText: Julien Veyssier <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\OpenAi\OldProcessing\TextToImage;

use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCP\Http\Client\IClientService;
use OCP\IL10N;
use OCP\TextToImage\IProvider;
use Psr\Log\LoggerInterface;

class TextToImageProvider implements IProvider {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private LoggerInterface $logger,
private IL10N $l,
private IClientService $clientService,
private ?string $userId,
) {
}

public function getId(): string {
return Application::APP_ID . '_image_generation';
}

/**
* @inheritDoc
*/
public function getName(): string {
return $this->openAiAPIService->isUsingOpenAi()
? $this->l->t('OpenAI\'s DALL-E 2 Text-To-Image')
: $this->openAiAPIService->getServiceName();
}

/**
* @inheritDoc
*/
public function generate(string $prompt, array $resources): void {
$startTime = time();
try {
$apiResponse = $this->openAiAPIService->requestImageCreation($this->userId, $prompt, count($resources));
$urls = array_map(static function (array $result) {
return $result['url'] ?? null;
}, $apiResponse['data']);
$urls = array_filter($urls, static function (?string $url) {
return $url !== null;
});
$urls = array_values($urls);
if (empty($urls)) {
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed: no image returned');
throw new \RuntimeException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
}
$client = $this->clientService->newClient();
$requestOptions = $this->openAiAPIService->getImageRequestOptions($this->userId);
// just in case $resources is not 0-based indexed, we know $urls is
$i = 0;
foreach ($resources as $resource) {
if (isset($urls[$i])) {
$url = $urls[$i];
$imageResponse = $client->get($url, $requestOptions);
fwrite($resource, $imageResponse->getBody());
}
$i++;
}
$endTime = time();
$this->openAiAPIService->updateExpImgProcessingTime($endTime - $startTime);
} catch(\Exception $e) {
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new \RuntimeException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
}
}

/**
* @inheritDoc
*/
public function getExpectedRuntime(): int {
return $this->openAiAPIService->getExpImgProcessingTime();
}
}
Loading
Loading