From 55bb4fe31eb9e59ec6b15aff5abc9d288864bf52 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Fri, 7 Jun 2024 14:47:31 +0200 Subject: [PATCH 1/2] feat: Allow answering in the same langauge as the input Signed-off-by: Marcel Klehr --- lib/TextProcessing/HeadlineProvider.php | 2 +- lib/TextProcessing/ReformulateProvider.php | 2 +- lib/TextProcessing/SummaryProvider.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/TextProcessing/HeadlineProvider.php b/lib/TextProcessing/HeadlineProvider.php index 4798e3fc..da382cdf 100644 --- a/lib/TextProcessing/HeadlineProvider.php +++ b/lib/TextProcessing/HeadlineProvider.php @@ -34,7 +34,7 @@ public function getName(): string { 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:' . "\n\n" . $prompt; + $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()) { diff --git a/lib/TextProcessing/ReformulateProvider.php b/lib/TextProcessing/ReformulateProvider.php index e85c6e18..578ea8b2 100644 --- a/lib/TextProcessing/ReformulateProvider.php +++ b/lib/TextProcessing/ReformulateProvider.php @@ -33,7 +33,7 @@ public function getName(): string { 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 = 'Reformulate the following text:' . "\n\n" . $prompt; + $prompt = 'Reformulate the following text. Detect the language of the text. Use the same language as the original text. Output only the reformulation. Here is the text:' . "\n\n" . $prompt . "\n\n" . 'Here is your reformulation in the same language:'; // 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()) { diff --git a/lib/TextProcessing/SummaryProvider.php b/lib/TextProcessing/SummaryProvider.php index 5263ab89..7950772f 100644 --- a/lib/TextProcessing/SummaryProvider.php +++ b/lib/TextProcessing/SummaryProvider.php @@ -36,7 +36,7 @@ public function process(string $prompt): string { // 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:' . "\n\n" . $prompt; + $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()) { From 7c17ffeeebada912abd1ee3b324fa0e55767c309 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 24 Jun 2024 11:27:50 +0200 Subject: [PATCH 2/2] fix tests Signed-off-by: Julien Veyssier --- tests/unit/Providers/OpenAiProviderTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/Providers/OpenAiProviderTest.php b/tests/unit/Providers/OpenAiProviderTest.php index 201be314..464ec1fd 100644 --- a/tests/unit/Providers/OpenAiProviderTest.php +++ b/tests/unit/Providers/OpenAiProviderTest.php @@ -182,7 +182,7 @@ public function testHeadlineProvider(): void { $url = self::OPENAI_API_BASE . 'chat/completions'; $options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']]; - $options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => 'Give me the headline of the following text:' . "\n\n" . $prompt]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]); + $options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => 'Give me the headline of the following text in its original language. Output only the headline.' . "\n\n" . $prompt]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]); $iResponse = $this->createMock(\OCP\Http\Client\IResponse::class); $iResponse->method('getBody')->willReturn($response); @@ -236,7 +236,7 @@ public function testReformulateProvider(): void { $url = self::OPENAI_API_BASE . 'chat/completions'; $options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']]; - $options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => 'Reformulate the following text:' . "\n\n" . $prompt]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]); + $options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => 'Reformulate the following text. Detect the language of the text. Use the same language as the original text. Output only the reformulation. Here is the text:' . "\n\n" . $prompt . "\n\n" . 'Here is your reformulation in the same language:']], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]); $iResponse = $this->createMock(\OCP\Http\Client\IResponse::class); $iResponse->method('getBody')->willReturn($response); @@ -290,7 +290,7 @@ public function testSummaryProvider(): void { $url = self::OPENAI_API_BASE . 'chat/completions'; $options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']]; - $options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => 'Summarize the following text:' . "\n\n" . $prompt]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]); + $options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => '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' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]); $iResponse = $this->createMock(\OCP\Http\Client\IResponse::class); $iResponse->method('getBody')->willReturn($response);